-
Notifications
You must be signed in to change notification settings - Fork 32
/
requests.go
164 lines (138 loc) · 4.75 KB
/
requests.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
package recordsets
import (
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToRecordSetListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned. Marker and Limit are used
// for pagination.
// https://developer.openstack.org/api-ref/dns/
type ListOpts struct {
// Integer value for the limit of values to return.
Limit int `q:"limit"`
// UUID of the recordset at which you want to set a marker.
Marker string `q:"marker"`
Data string `q:"data"`
Description string `q:"description"`
Name string `q:"name"`
SortDir string `q:"sort_dir"`
SortKey string `q:"sort_key"`
Status string `q:"status"`
TTL int `q:"ttl"`
Type string `q:"type"`
ZoneID string `q:"zone_id"`
}
// ToRecordSetListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToRecordSetListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
// ListByZone implements the recordset list request.
func ListByZone(client *golangsdk.ServiceClient, zoneID string, opts ListOptsBuilder) pagination.Pager {
url := baseURL(client, zoneID)
if opts != nil {
query, err := opts.ToRecordSetListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RecordSetPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get implements the recordset Get request.
func Get(client *golangsdk.ServiceClient, zoneID string, rrsetID string) (r GetResult) {
_, r.Err = client.Get(rrsetURL(client, zoneID, rrsetID), &r.Body, nil)
return
}
// CreateOptsBuilder allows extensions to add additional attributes to the
// Create request.
type CreateOptsBuilder interface {
ToRecordSetCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies the base attributes that may be used to create a
// RecordSet.
type CreateOpts struct {
// Name is the name of the RecordSet.
Name string `json:"name" required:"true"`
// Description is a description of the RecordSet.
Description string `json:"description,omitempty"`
// Records are the DNS records of the RecordSet.
Records []string `json:"records,omitempty"`
// TTL is the time to live of the RecordSet.
TTL int `json:"ttl,omitempty"`
// Type is the RRTYPE of the RecordSet.
Type string `json:"type,omitempty"`
}
// ToRecordSetCreateMap formats an CreateOpts structure into a request body.
func (opts CreateOpts) ToRecordSetCreateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Create creates a recordset in a given zone.
func Create(client *golangsdk.ServiceClient, zoneID string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToRecordSetCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(baseURL(client, zoneID), &b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{201, 202},
})
return
}
// UpdateOptsBuilder allows extensions to add additional attributes to the
// Update request.
type UpdateOptsBuilder interface {
ToRecordSetUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts specifies the base attributes that may be updated on an existing
// RecordSet.
type UpdateOpts struct {
// Description is a description of the RecordSet.
Description string `json:"description,omitempty"`
// TTL is the time to live of the RecordSet.
TTL int `json:"ttl,omitempty"`
// Records are the DNS records of the RecordSet.
Records []string `json:"records,omitempty"`
}
// ToRecordSetUpdateMap formats an UpdateOpts structure into a request body.
func (opts UpdateOpts) ToRecordSetUpdateMap() (map[string]interface{}, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.TTL > 0 {
b["ttl"] = opts.TTL
}
return b, nil
}
// Update updates a recordset in a given zone
func Update(client *golangsdk.ServiceClient, zoneID string, rrsetID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToRecordSetUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(rrsetURL(client, zoneID, rrsetID), &b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
// Delete removes an existing RecordSet.
func Delete(client *golangsdk.ServiceClient, zoneID string, rrsetID string) (r DeleteResult) {
_, r.Err = client.Delete(rrsetURL(client, zoneID, rrsetID), &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}