/
requests.go
175 lines (149 loc) · 5.15 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
package policies
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToPolicyListQuery() (string, error)
}
// ListOpts represents options used to list policies.
type ListOpts struct {
// Limit limits the number of Policies to return.
Limit int `q:"limit"`
// Marker and Limit control paging. Marker instructs List where to start
// listing from.
Marker string `q:"marker"`
// Sorts the response by one or more attribute and optional sort direction
// combinations.
Sort string `q:"sort"`
// GlobalProject indicates whether to include resources for all projects or
// resources for the current project.
GlobalProject *bool `q:"global_project"`
// Name to filter the response by the specified name property of the object.
Name string `q:"name"`
// Filter the response by the specified type property of the object.
Type string `q:"type"`
}
// ToPolicyListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToPolicyListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List instructs OpenStack to retrieve a list of policies.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := policyListURL(client)
if opts != nil {
query, err := opts.ToPolicyListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
p := PolicyPage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
})
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToPolicyCreateMap() (map[string]interface{}, error)
}
// CreateOpts represents options used to create a policy.
type CreateOpts struct {
Name string `json:"name"`
Spec Spec `json:"spec"`
}
// ToPolicyCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return map[string]interface{}{"policy": b}, nil
}
// Create makes a request against the API to create a policy
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToPolicyCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(policyCreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete makes a request against the API to delete a policy.
func Delete(client *gophercloud.ServiceClient, policyID string) (r DeleteResult) {
resp, err := client.Delete(policyDeleteURL(client, policyID), &gophercloud.RequestOpts{
OkCodes: []int{204},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToPolicyUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts represents options to update a policy.
type UpdateOpts struct {
Name string `json:"name,omitempty"`
}
// ToPolicyUpdateMap constructs a request body from UpdateOpts.
func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "policy")
}
// Update updates a specified policy.
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToPolicyUpdateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Patch(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// ValidateOptsBuilder allows extensions to add additional parameters to the
// Validate request.
type ValidateOptsBuilder interface {
ToPolicyValidateMap() (map[string]interface{}, error)
}
// ValidateOpts represents options used to validate a policy.
type ValidateOpts struct {
Spec Spec `json:"spec"`
}
// ToPolicyValidateMap formats a CreateOpts into a body map.
func (opts ValidateOpts) ToPolicyValidateMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "policy")
}
// Validate policy will validate a specified policy.
func Validate(client *gophercloud.ServiceClient, opts ValidateOptsBuilder) (r ValidateResult) {
b, err := opts.ToPolicyValidateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(validateURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Get makes a request against the API to get details for a policy.
func Get(client *gophercloud.ServiceClient, policyTypeName string) (r GetResult) {
url := policyGetURL(client, policyTypeName)
resp, err := client.Get(url, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}