-
Notifications
You must be signed in to change notification settings - Fork 4
/
dns.go
187 lines (141 loc) · 3.14 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"os"
"time"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
waitForDNSDefaultTimeout = 15 * time.Minute
defaultRetryInterval = 5 * time.Second
)
const (
CNAME = "CNAME"
ALIAS = "ALIAS"
)
var (
TTL = uint32(360)
)
func WaitForDNSReady(client *scw.Client, DNSZone string) (*domain.DNSZone, error) {
fmt.Println("waiting for dns zone to be ready")
api := domain.NewAPI(client)
dns, err := api.WaitForDNSZone(&domain.WaitForDNSZoneRequest{
DNSZone: DNSZone,
})
if err != nil {
return dns, err
}
return dns, nil
}
func DeleteDNSRecord(
client *scw.Client,
Container *container.Container,
DNSZone string,
) (*domain.UpdateDNSZoneRecordsResponse, error) {
fmt.Println("Update Zone DNS - Delete")
// ENV
Prefix := os.Getenv(EnvDNSPrefix)
RootZone := os.Getenv(EnvRootZone)
api := domain.NewAPI(client)
Data := Container.DomainName + "."
var Name string = Container.Name
var Type domain.RecordType = CNAME
// Handle Prefix DNS
if Prefix != "" {
Name = Prefix
fmt.Println("Update With Prefix Zone DNS - Delete", Prefix)
}
// Handle Root Zone Alias
// Some DNS doesn't handle correctly CNAME on Root Zone.
// We should use an Alias
if RootZone == "true" {
Name = ""
Type = ALIAS
fmt.Println("Update Root Zone DNS - Delete")
}
IDFields := &domain.RecordIdentifier{
Name: Name,
Data: &Data,
Type: Type,
TTL: &TTL,
}
Changes := []*domain.RecordChange{
{
Delete: &domain.RecordChangeDelete{
IDFields: IDFields,
},
},
}
records, err := api.UpdateDNSZoneRecords(
&domain.UpdateDNSZoneRecordsRequest{
DNSZone: DNSZone,
Changes: Changes,
})
if err != nil {
return nil, err
}
return records, nil
}
func SetDNSRecord(
client *scw.Client,
Container *container.Container,
DNSZone string,
) (string, error) {
// ENV
Prefix := os.Getenv(EnvDNSPrefix)
RootZone := os.Getenv(EnvRootZone)
fmt.Println("Update Zone DNS - Add")
api := domain.NewAPI(client)
var Name string = Container.Name
var Type domain.RecordType = CNAME
// Handle Prefix DNS
if Prefix != "" {
Name = Prefix
fmt.Println("Update With Prefix Zone DNS - Add", Prefix)
}
var Hostname string = Name + "." + DNSZone
// Handle Root Zone Alias
// Some DNS doesn't handle correctly CNAME on Root Zone.
// We should use an Alias
if RootZone == "true" {
Name = ""
Type = ALIAS
Hostname = DNSZone
fmt.Println("Update Root Zone DNS - Add")
}
Records := []*domain.Record{
{
Name: Name,
Type: Type,
TTL: TTL,
Data: Container.DomainName + ".",
},
}
Data := Container.DomainName + "."
IDFields := &domain.RecordIdentifier{
Name: Name,
Type: Type,
TTL: &TTL,
Data: &Data,
}
Changes := []*domain.RecordChange{
{
Set: &domain.RecordChangeSet{
IDFields: IDFields,
Records: Records,
},
},
}
_, err := api.UpdateDNSZoneRecords(
&domain.UpdateDNSZoneRecordsRequest{
DNSZone: DNSZone,
Changes: Changes,
})
if err != nil {
return "", err
}
fmt.Println("Hostname", Hostname)
return Hostname,nil
}