/
azure_dns.go
77 lines (63 loc) · 1.99 KB
/
azure_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
//go:build azure || !onlyprovider
package azure
import (
"context"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/Azure/go-autorest/autorest/to"
"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/types"
)
// FindOrCreateZoneIDByName searches for a DNS zone with the name passed by argument and if it doesn't exist it creates one
func (a *Azure) FindOrCreateZoneIDByName(config *types.Config, dnsName string) (string, error) {
service := dns.NewZonesClient(a.subID)
authr, _ := a.GetResourceManagementAuthorizer()
service.Authorizer = authr
ctx := context.TODO()
zonesListResponse, err := service.List(ctx, nil)
if err != nil {
return "", err
}
zones := zonesListResponse.Values()
if len(zones) == 0 {
location := "global"
zone := dns.Zone{
Name: &dnsName,
Location: &location,
}
_, err := service.CreateOrUpdate(ctx, a.groupName, dnsName, zone, "", "")
if err != nil {
return "", err
}
}
return dnsName, nil
}
// DeleteZoneRecordIfExists deletes a record from a DNS zone if it exists
func (a *Azure) DeleteZoneRecordIfExists(config *types.Config, zoneID string, recordName string) error {
return nil
}
// CreateZoneRecord creates a record in a DNS zone
func (a *Azure) CreateZoneRecord(config *types.Config, zoneID string, record *lepton.DNSRecord) error {
service := dns.NewRecordSetsClient(a.subID)
authr, _ := a.GetResourceManagementAuthorizer()
service.Authorizer = authr
// remove trailing dot if it exists
if record.Name[len(record.Name)-1] == '.' {
record.Name = record.Name[:len(record.Name)-1]
}
ctx := context.TODO()
dnsRecord := dns.RecordSet{
Name: &record.Name,
Type: &record.Type,
RecordSetProperties: &dns.RecordSetProperties{
ARecords: &[]dns.ARecord{
{Ipv4Address: &record.IP},
},
TTL: to.Int64Ptr(int64(record.TTL)),
},
}
_, err := service.CreateOrUpdate(ctx, a.groupName, zoneID, record.Name, dns.RecordType(record.Type), dnsRecord, "", "")
if err != nil {
return err
}
return nil
}