/
requests.go
178 lines (148 loc) · 5.01 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package zones
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add parameters to the List request.
type ListOptsBuilder interface {
ToZoneListQuery() (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 zone at which you want to set a marker.
Marker string `q:"marker"`
Description string `q:"description"`
Email string `q:"email"`
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"`
}
// ToZoneListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToZoneListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List implements a zone List request.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := baseURL(client)
if opts != nil {
query, err := opts.ToZoneListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ZonePage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get returns information about a zone, given its ID.
func Get(client *gophercloud.ServiceClient, zoneID string) (r GetResult) {
resp, err := client.Get(zoneURL(client, zoneID), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// CreateOptsBuilder allows extensions to add additional attributes to the
// Create request.
type CreateOptsBuilder interface {
ToZoneCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies the attributes used to create a zone.
type CreateOpts struct {
// Attributes are settings that supply hints and filters for the zone.
Attributes map[string]string `json:"attributes,omitempty"`
// Email contact of the zone.
Email string `json:"email,omitempty"`
// Description of the zone.
Description string `json:"description,omitempty"`
// Name of the zone.
Name string `json:"name" required:"true"`
// Masters specifies zone masters if this is a secondary zone.
Masters []string `json:"masters,omitempty"`
// TTL is the time to live of the zone.
TTL int `json:"-"`
// Type specifies if this is a primary or secondary zone.
Type string `json:"type,omitempty"`
}
// ToZoneCreateMap formats an CreateOpts structure into a request body.
func (opts CreateOpts) ToZoneCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.TTL > 0 {
b["ttl"] = opts.TTL
}
return b, nil
}
// Create implements a zone create request.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToZoneCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201, 202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// UpdateOptsBuilder allows extensions to add additional attributes to the
// Update request.
type UpdateOptsBuilder interface {
ToZoneUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts specifies the attributes to update a zone.
type UpdateOpts struct {
// Email contact of the zone.
Email string `json:"email,omitempty"`
// TTL is the time to live of the zone.
TTL int `json:"-"`
// Masters specifies zone masters if this is a secondary zone.
Masters []string `json:"masters,omitempty"`
// Description of the zone.
Description *string `json:"description,omitempty"`
}
// ToZoneUpdateMap formats an UpdateOpts structure into a request body.
func (opts UpdateOpts) ToZoneUpdateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.TTL > 0 {
b["ttl"] = opts.TTL
}
return b, nil
}
// Update implements a zone update request.
func Update(client *gophercloud.ServiceClient, zoneID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToZoneUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Patch(zoneURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 202},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete implements a zone delete request.
func Delete(client *gophercloud.ServiceClient, zoneID string) (r DeleteResult) {
resp, err := client.Delete(zoneURL(client, zoneID), &gophercloud.RequestOpts{
OkCodes: []int{202},
JSONResponse: &r.Body,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}