-
Notifications
You must be signed in to change notification settings - Fork 15
/
requests.go
143 lines (126 loc) · 4.36 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
package zones
import (
"github.com/gophercloud/gophercloud"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/util"
)
type CreateOptsBuilder interface {
Map() (map[string]interface{}, error)
}
// CreateOpts specifies the attributes used to create a zone.
type CreateOpts struct {
Zone string `json:"zone" required:"true"`
SOAPrimaryDNS string `json:"soa_primary_dns,omitempty"`
SOAAdminEmail string `json:"soa_admin_email,omitempty"`
SOARefresh int `json:"soa_refresh,omitempty"`
SOARetry int `json:"soa_retry,omitempty"`
SOAExpire int `json:"soa_expire,omitempty"`
SOATTL int `json:"soa_ttl,omitempty"`
}
// Map formats a CreateOpts structure into a request body.
func (opts CreateOpts) Map() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
return b, err
}
// Create implements a zone create request.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.Map()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(zonesURL(client), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Get returns information about a zone, given its ID.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
resp, err := client.Get(zoneURL(client, id), &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ListOptsBuilder allows extensions to add additional parameters to the
// list request.
type ListOptsBuilder interface {
ToListQuery() (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.
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"`
ID string `q:"uuid"`
Zone string `q:"zone"`
SOAPrimaryDNS string `q:"soa_primary_dns"`
SOAAdminEmail string `q:"soa_admin_email"`
SOASerial int `q:"soa_serial"`
SOARefresh int `q:"soa_refresh"`
SOARetry int `q:"soa_retry"`
SOAExpire int `q:"soa_expire"`
SOATTL int `q:"soa_ttl"`
Status string `q:"status"`
}
// ToListQuery formats a listOpts structure into a query string.
func (opts ListOpts) ToListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List implements a zone list request.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) (r ListResult) {
url := zonesURL(client)
if opts != nil {
query, err := opts.ToListQuery()
if err != nil {
r.Err = err
return
}
url += query
}
resp, err := client.Get(url, &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
r.Err = util.ErrorWithRequestID(r.Err, r.Header.Get(util.RequestIDHeader))
return
}
type UpdateOptsBuilder interface {
Map() (map[string]interface{}, error)
}
// UpdateOpts specifies the attributes to update a zone.
type UpdateOpts struct {
SOAPrimaryDNS string `json:"soa_primary_dns,omitempty"`
SOAAdminEmail string `json:"soa_admin_email,omitempty"`
SOARefresh int `json:"soa_refresh,omitempty"`
SOARetry int `json:"soa_retry,omitempty"`
SOAExpire int `json:"soa_expire,omitempty"`
SOATTL int `json:"soa_ttl,omitempty"`
}
// Map formats a zoneUpdateOpts structure into a request body.
func (opts UpdateOpts) Map() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
return b, err
}
// Update implements a zone update request.
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.Map()
if err != nil {
r.Err = err
return
}
resp, err := client.Put(zoneURL(client, id), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete implements a zone delete request.
func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
resp, err := client.Delete(zoneURL(client, id), &gophercloud.RequestOpts{
OkCodes: []int{204},
JSONResponse: &r.Body,
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}