-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
89 lines (74 loc) · 1.92 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
89
package huaweicloud
import (
"context"
"fmt"
"strings"
"sync"
"github.com/libdns/huaweicloud/sdk/core/auth/basic"
dns "github.com/libdns/huaweicloud/sdk/services/dns/v2"
"github.com/libdns/huaweicloud/sdk/services/dns/v2/model"
regions "github.com/libdns/huaweicloud/sdk/services/dns/v2/region"
)
func (p *Provider) getClient() (*dns.DnsClient, error) {
client := sync.OnceValues(func() (*dns.DnsClient, error) {
auth, err := basic.NewCredentialsBuilder().
WithAk(p.AccessKeyId).
WithSk(p.SecretAccessKey).
SafeBuild()
if err != nil {
return nil, err
}
if p.RegionId == "" {
p.RegionId = "cn-south-1"
}
region, err := regions.SafeValueOf(p.RegionId)
if err != nil {
return nil, err
}
builder, err := dns.DnsClientBuilder().
WithRegion(region).
WithCredential(auth).
SafeBuild()
if err != nil {
return nil, err
}
return dns.NewDnsClient(builder), nil
})
return client()
}
func (p *Provider) getZoneIdByName(name string) (string, error) {
client, err := p.getClient()
if err != nil {
return "", err
}
name = strings.TrimSuffix(name, ".")
searchMode := "equal"
request := &model.ListPublicZonesRequest{
Name: &name,
SearchMode: &searchMode,
}
response, err := client.ListPublicZones(request)
if err != nil {
return "", err
}
zones := *response.Zones
if len(zones) == 0 {
return "", fmt.Errorf("zone %q not found", name)
}
if len(zones) != 1 {
return "", fmt.Errorf("returned more than one zone for %q, expected one, actual %d", name, len(*response.Zones))
}
return *zones[0].Id, nil
}
func (p *Provider) getRecordIdByNameAndType(ctx context.Context, zone, recName, recType string) (string, error) {
records, err := p.GetRecords(ctx, zone)
if err != nil {
return "", err
}
for _, record := range records {
if recName == record.Name && recType == record.Type {
return record.ID, nil
}
}
return "", fmt.Errorf("record %q not found", recName)
}