This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
recordset.go
177 lines (156 loc) · 4.53 KB
/
recordset.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
package main
import (
"encoding/json"
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/auth/token"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/pagination"
)
func main() {
opts := token.TokenOptions{
IdentityEndpoint: "https://iam.xxx.yyy.com/v3",
Username: "your username",
Password: "your password",
DomainID: "your domainId",
ProjectID: "your projectID",
}
provider, err := openstack.AuthenticatedClient(opts)
if err != nil {
panic(err)
}
dnsClient, err2 := openstack.NewDNSV2(provider, gophercloud.EndpointOpts{})
if err2 != nil {
panic(err2)
}
//Create RecordSets
createRecordset(dnsClient)
//Get Recordset by ID
getRecordSetsByID(dnsClient)
//List RecordSets
listRecordsets(dnsClient)
//List RecordSets by Zone
listRecordsetsByZone(dnsClient)
//update Recordset
updateRecordset(dnsClient)
//Delete Recordsets
deleteRecordset(dnsClient)
}
func listRecordsets(dnsClient *gophercloud.ServiceClient) {
listOpts := recordsets.ListOpts{
Type: "A",
Limit: 20,
}
recordsets.List(dnsClient, listOpts).EachPage(func(page pagination.Page) (bool, error) {
allRRs, err := recordsets.ExtractRecordSets(page)
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return false, err
} else {
for _, rr := range allRRs.Recordsets {
rrjson, _ := json.MarshalIndent(rr, "", " ")
fmt.Println(string(rrjson))
}
}
return false, err
})
}
func listRecordsetsByZone(dnsClient *gophercloud.ServiceClient) {
listOpts := recordsets.ListByZoneOpts{
Limit: 10,
}
zoneID := "your zone id"
recordsets.ListByZone(dnsClient, zoneID, listOpts).EachPage(func(page pagination.Page) (bool, error) {
allRRs, err := recordsets.ExtractRecordSets(page)
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return false, err
} else {
for _, rr := range allRRs.Recordsets {
rrjson, _ := json.MarshalIndent(rr, "", " ")
fmt.Println(string(rrjson))
}
}
return false, err
})
}
func createRecordset(dnsClient *gophercloud.ServiceClient) {
createOpts := recordsets.CreateOpts{
Name: "your recordset name",
Type: "A",
TTL: 3600,
Description: "your description",
Records: []string{"10.1.0.2"},
}
zoneID := "your zone id"
rr, err := recordsets.Create(dnsClient, zoneID, createOpts).Extract()
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println(rr.ID, rr.Name, rr.Records)
rrjson, _ := json.MarshalIndent(rr, "", " ")
fmt.Println(string(rrjson))
}
func getRecordSetsByID(sc *gophercloud.ServiceClient) {
zoneId := "your zone id"
rrId := "your recordset id"
rset, err := recordsets.Get(sc, zoneId, rrId).Extract()
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println(rset.ID, rset.Name, rset.Records)
fmt.Println(rset.Links)
rsetjson, _ := json.MarshalIndent(rset, "", " ")
fmt.Println(string(rsetjson))
}
func deleteRecordset(dnsClient *gophercloud.ServiceClient) {
zoneID := "your zone id"
recordsetID := "your recordset id"
rr, err := recordsets.Delete(dnsClient, zoneID, recordsetID).Extract()
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
rrjson, _ := json.MarshalIndent(rr, "", " ")
fmt.Println(string(rrjson))
fmt.Printf("%+v", rr.Links)
fmt.Println(rr.Links, rr.Status)
}
func updateRecordset(dnsClient *gophercloud.ServiceClient) {
updataOpts := recordsets.UpdateOpts{
Description: "your description",
TTL: 600,
Records: []string{"1.3.1.4", "3.3.4.4"}, //your ip address
}
zoneID := "your zone id"
recordsetID := "your recordser id"
rr, err := recordsets.Update(dnsClient, zoneID, recordsetID, updataOpts).Extract()
if err != nil {
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println(rr.ID, rr.Name, rr.Records)
rrjson, _ := json.MarshalIndent(rr, "", " ")
fmt.Println(string(rrjson))
}