forked from pragkent/alidns-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
88 lines (70 loc) · 2.03 KB
/
client.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
package alidns
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/jetstack/cert-manager/pkg/issuer/acme/dns/util"
)
type Client struct {
dnsc *alidns.Client
}
func newClient(region string, cred auth.Credential) (*Client, error) {
cfg := sdk.NewConfig()
client, err := alidns.NewClientWithOptions(region, cfg, cred)
if err != nil {
return nil, err
}
return &Client{dnsc: client}, nil
}
func (c *Client) getHostedZone(zone string) (string, error) {
request := alidns.CreateDescribeDomainsRequest()
request.KeyWord = util.UnFqdn(zone)
request.SearchMode = "EXACT"
response, err := c.dnsc.DescribeDomains(request)
if err != nil {
return "", err
}
zones := response.Domains.Domain
if len(zones) == 0 {
return "", fmt.Errorf("zone %s does not exist", zone)
}
return zones[0].DomainName, nil
}
func (c *Client) addTxtRecord(zone, rr, value string) error {
record := c.newTxtRecord(zone, rr, value)
_, err := c.dnsc.AddDomainRecord(record)
return err
}
const recordTypeTxt = "TXT"
func (c *Client) newTxtRecord(zone, rr, value string) *alidns.AddDomainRecordRequest {
request := alidns.CreateAddDomainRecordRequest()
request.Type = recordTypeTxt
request.DomainName = zone
request.RR = rr
request.Value = value
return request
}
func (c *Client) getTxtRecord(zone, rr string) (*alidns.Record, error) {
request := alidns.CreateDescribeDomainRecordsRequest()
request.Type = recordTypeTxt
request.DomainName = zone
request.RRKeyWord = rr
response, err := c.dnsc.DescribeDomainRecords(request)
if err != nil {
return nil, err
}
records := response.DomainRecords.Record
for _, r := range records {
if r.RR == rr {
return &r, nil
}
}
return nil, fmt.Errorf("txt record does not exist: %v.%v", rr, zone)
}
func (c *Client) deleteDomainRecord(id string) error {
request := alidns.CreateDeleteDomainRecordRequest()
request.RecordId = id
_, err := c.dnsc.DeleteDomainRecord(request)
return err
}