-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
116 lines (109 loc) · 3.8 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"strconv"
"strings"
"github.com/pulumi/pulumi-azure/sdk/v5/go/azure/dns"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func (pr *projectResources) lookupDnsZone() (err error) {
dnsLookupZoneArgs := dns.LookupZoneArgs{
Name: pr.cfgKeys.dnsLookupZone,
ResourceGroupName: &pr.cfgKeys.dnsResourceGrp,
}
pr.webDnsZone, err = dns.LookupZone(pr.pulumiCtx, &dnsLookupZoneArgs)
if err != nil {
fmt.Printf("ERROR: looking up dnsZone in RG %s failed\n", pr.cfgKeys.dnsResourceGrp)
return err
}
return
}
func (pr *projectResources) createDnsRecordByEnv() (err error) {
fqdnErr := fmt.Errorf("passed FQDN string didn't include trailing '.' did the Azure API change?")
switch pr.cfgKeys.envKey {
case PROD: // apex domain for prod eg tld.com uses A record referencing Azure resource
// create A record pointing at CDN Endpoint resource ID
err = pr.createApexRecordPointingAtCdnResourceID()
if err != nil {
return err
}
// create CNAME 'cdnverify.tld.com' record
cdnVerify := "cdnverify"
cdnVerifyHostname := pr.webCdnEp.HostName.ApplyT(func(h string) (r string) {
r = cdnVerify + "." + h
return
}).(pulumi.StringOutput)
err = pr.createCNAMERecordPointingAtCdnEndpoint(cdnVerifyHostname, pr.cfgKeys.siteKey+cdnVerify)
if err != nil {
return err
}
// strip out trailing '.' from A record returned FQDN string within Azure DNS API
pr.webFqdn = pr.dnsRecords.a.Fqdn.ApplyT(func(fqdn string) (string, error) {
h, found := strings.CutSuffix(fqdn, ".")
if !found {
return h, fqdnErr
}
return h, nil
}).(pulumi.StringOutput)
default: // everything that's not prod and has a sub-domain eg dev.tld.com
// create CNAME DNS record to point at CDN endpoint
err = pr.createCNAMERecordPointingAtCdnEndpoint(pr.webCdnEp.HostName, pr.cfgKeys.envKey)
if err != nil {
return err
}
// strip out trailing '.' from CNAME's returned FQDN string within Azure DNS API
pr.webFqdn = pr.dnsRecords.cname.Fqdn.ApplyT(func(fqdn string) (string, error) {
h, found := strings.CutSuffix(fqdn, ".")
if !found {
return h, fqdnErr
}
return h, nil
}).(pulumi.StringOutput)
}
return
}
func (pr *projectResources) createCNAMERecordPointingAtCdnEndpoint(ep pulumi.StringOutput, name string) (err error) {
ttl, err := strconv.Atoi(pr.cfgKeys.dnsRecordTTL)
if err != nil {
fmt.Printf("ERROR: dnsRecordTTL provided cannot be converted from string to int\n")
return err
}
// create new CNAME record in zone for non-prod env that will be used by CDN endpoint
dnsRecordArgs := dns.CNameRecordArgs{
ZoneName: pulumi.String(pr.webDnsZone.Name),
ResourceGroupName: pulumi.String(pr.cfgKeys.dnsResourceGrp),
Ttl: pulumi.Int(ttl),
Name: pulumi.String(name),
Record: ep,
}
pr.dnsRecords.cname, err = dns.NewCNameRecord(pr.pulumiCtx, name, &dnsRecordArgs)
if err != nil {
fmt.Printf("ERROR: creating CNAME record in RG %s failed\n",
pr.cfgKeys.dnsResourceGrp)
return err
}
return
}
func (pr *projectResources) createApexRecordPointingAtCdnResourceID() (err error) {
ttl, err := strconv.Atoi(pr.cfgKeys.dnsRecordTTL)
if err != nil {
fmt.Printf("ERROR: dnsRecordTTL provided cannot be converted from string to int\n")
return err
}
rootRecordName := "@"
dnsRecordArgs := dns.ARecordArgs{
Name: pulumi.String(rootRecordName),
ZoneName: pulumi.String(pr.webDnsZone.Name),
ResourceGroupName: pulumi.String(pr.cfgKeys.dnsResourceGrp),
Ttl: pulumi.Int(ttl),
TargetResourceId: pulumi.StringOutput(pr.webCdnEp.ID()),
}
name := pr.cfgKeys.siteKey + pr.cfgKeys.envKey
pr.dnsRecords.a, err = dns.NewARecord(pr.pulumiCtx, name, &dnsRecordArgs)
if err != nil {
fmt.Printf("ERROR: creating A record in RG %s failed\n",
pr.cfgKeys.dnsResourceGrp)
return err
}
return
}