@@ -18,61 +18,53 @@ package deploy
1818
1919import (
2020 "fmt"
21- "strings"
2221
22+ awsprovider "github.com/pulumi/pulumi-aws/sdk/v5/go/aws"
23+
24+ "github.com/nitrictech/nitric/cloud/aws/common/resources"
2325 "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/acm"
24- "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/apigatewayv2"
2526 "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/route53"
2627 "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
2728)
2829
29- type domainNameArgs struct {
30- domainName string
31- api * apigatewayv2.Api
32- stage * apigatewayv2.Stage
33- }
34-
35- type domainName struct {
30+ type Domain struct {
3631 pulumi.ResourceState
3732
38- Name string
33+ Name string
34+ ZoneLookup * resources.ZoneLookup
35+ CertificateValidation * acm.CertificateValidation
3936}
4037
41- func newDomainName (ctx * pulumi.Context , name string , args domainNameArgs ) (* domainName , error ) {
42- domainParts := strings .Split (args .domainName , "." )
38+ func (a * NitricAwsPulumiProvider ) newPulumiDomainName (ctx * pulumi.Context , domainName string ) (* Domain , error ) {
39+ var err error
40+ res := & Domain {Name : domainName }
4341
44- res := & domainName {Name : name }
42+ res .ZoneLookup , err = resources .GetZoneID (domainName )
43+ if err != nil {
44+ return nil , err
45+ }
4546
46- err : = ctx .RegisterComponentResource ("nitric:api:DomainName" , fmt .Sprintf ("%s-%s" , name , args . domainName ), res )
47+ err = ctx .RegisterComponentResource ("nitric:api:DomainName" , fmt .Sprintf ("%s-%s" , domainName , a . StackId ), res )
4748 if err != nil {
4849 return nil , err
4950 }
5051
5152 defaultOptions := []pulumi.ResourceOption {pulumi .Parent (res )}
5253
53- // Treat this domain as root by default
54- baseName := ""
55- // attempt to find hosted zone as the root domain name
56- hostedZone , err := route53 .LookupZone (ctx , & route53.LookupZoneArgs {
57- // The name is the base name for the domain
58- Name : & args .domainName ,
59- })
60- if err != nil {
61- // try by parent domain instead
62- parentDomain := strings .Join (domainParts [1 :], "." )
63- hostedZone , err = route53 .LookupZone (ctx , & route53.LookupZoneArgs {
64- // The name is the base name for the domain
65- Name : & parentDomain ,
54+ // Create an AWS provider for the us-east-1 region as the acm certificates require being deployed in us-east-1 region
55+ if a .Region != "us-east-1" {
56+ useast1 , err := awsprovider .NewProvider (ctx , "us-east-1" , & awsprovider.ProviderArgs {
57+ Region : pulumi .String ("us-east-1" ),
6658 })
6759 if err != nil {
68- return nil , fmt . Errorf ( "unable to find Route53 hosted zone to create records in: %w" , err )
60+ return nil , err
6961 }
7062
71- baseName = domainParts [ 0 ]
63+ defaultOptions = append ( defaultOptions , pulumi . Provider ( useast1 ))
7264 }
7365
74- cert , err := acm .NewCertificate (ctx , fmt .Sprintf ("%s -%s-cert " , name , args . domainName ), & acm.CertificateArgs {
75- DomainName : pulumi .String (args . domainName ),
66+ cert , err := acm .NewCertificate (ctx , fmt .Sprintf ("cert -%s" , a . StackId ), & acm.CertificateArgs {
67+ DomainName : pulumi .String (domainName ),
7668 ValidationMethod : pulumi .String ("DNS" ),
7769 }, defaultOptions ... )
7870 if err != nil {
@@ -83,7 +75,7 @@ func newDomainName(ctx *pulumi.Context, name string, args domainNameArgs) (*doma
8375 return options [0 ]
8476 })
8577
86- certValidationDns , err := route53 .NewRecord (ctx , fmt .Sprintf ("%s-%s-certvalidationdns " , name , args . domainName ), & route53.RecordArgs {
78+ cdnRecord , err := route53 .NewRecord (ctx , fmt .Sprintf ("cdn-record-%s " , a . StackId ), & route53.RecordArgs {
8779 Name : domainValidationOption .ApplyT (func (option interface {}) string {
8880 return * option .(acm.CertificateDomainValidationOption ).ResourceRecordName
8981 }).(pulumi.StringOutput ),
@@ -96,58 +88,16 @@ func newDomainName(ctx *pulumi.Context, name string, args domainNameArgs) (*doma
9688 }).(pulumi.StringOutput ),
9789 },
9890 Ttl : pulumi .Int (10 * 60 ),
99- ZoneId : pulumi .String (hostedZone . ZoneId ),
100- }, defaultOptions ... )
91+ ZoneId : pulumi .String (res . ZoneLookup . ZoneID ),
92+ }, []pulumi. ResourceOption { pulumi . Parent ( res )} ... )
10193 if err != nil {
10294 return nil , err
10395 }
10496
105- certValidation , err : = acm .NewCertificateValidation (ctx , fmt .Sprintf ("%s-%s-certvalidation " , name , args . domainName ), & acm.CertificateValidationArgs {
97+ res . CertificateValidation , err = acm .NewCertificateValidation (ctx , fmt .Sprintf ("cert-validation-%s " , a . StackId ), & acm.CertificateValidationArgs {
10698 CertificateArn : cert .Arn ,
10799 ValidationRecordFqdns : pulumi.StringArray {
108- certValidationDns .Fqdn ,
109- },
110- }, defaultOptions ... )
111- if err != nil {
112- return nil , err
113- }
114-
115- // Create a domain name if one has been requested
116- apiDomainName , err := apigatewayv2 .NewDomainName (ctx , fmt .Sprintf ("%s-%s" , name , args .domainName ), & apigatewayv2.DomainNameArgs {
117- DomainName : pulumi .String (args .domainName ),
118- DomainNameConfiguration : & apigatewayv2.DomainNameDomainNameConfigurationArgs {
119- EndpointType : pulumi .String ("REGIONAL" ),
120- SecurityPolicy : pulumi .String ("TLS_1_2" ),
121- CertificateArn : certValidation .CertificateArn ,
122- },
123- }, defaultOptions ... )
124- if err != nil {
125- return nil , err
126- }
127-
128- // Create an API mapping for the new domain name
129- _ , err = apigatewayv2 .NewApiMapping (ctx , fmt .Sprintf ("%s-%s" , name , args .domainName ), & apigatewayv2.ApiMappingArgs {
130- ApiId : args .api .ID (),
131- DomainName : apiDomainName .DomainName ,
132- Stage : args .stage .Name ,
133- }, append (defaultOptions , pulumi .DependsOn ([]pulumi.Resource {args .stage }))... )
134- if err != nil {
135- return nil , err
136- }
137-
138- // Create a DNS record for the domain name that maps to the APIs
139- // regional endpoint
140- _ , err = route53 .NewRecord (ctx , fmt .Sprintf ("%s-%s-dnsrecord" , name , args .domainName ), & route53.RecordArgs {
141- ZoneId : pulumi .String (hostedZone .ZoneId ),
142- Type : pulumi .String ("A" ),
143- Name : pulumi .String (baseName ),
144- Aliases : & route53.RecordAliasArray {
145- & route53.RecordAliasArgs {
146- // The target of the A record
147- Name : apiDomainName .DomainNameConfiguration .TargetDomainName ().Elem (),
148- ZoneId : apiDomainName .DomainNameConfiguration .HostedZoneId ().Elem (),
149- EvaluateTargetHealth : pulumi .Bool (false ),
150- },
100+ cdnRecord .Fqdn ,
151101 },
152102 }, defaultOptions ... )
153103 if err != nil {
0 commit comments