-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
223 lines (198 loc) · 7.06 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package acls
import (
"fmt"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
// CreateOpts is the structure that used to create a new ACL policy.
type CreateOpts struct {
// The ACL name.
Name string `json:"acl_name" required:"true"`
// The ACL type. The valid values are as follows:
// + PERMIT
// + DENY
Type string `json:"acl_type" required:"true"`
// The value of the ACL policy.
// One or more values are supported, separated by commas (,).
Value string `json:"acl_value" required:"true"`
// The entity type. The valid values are as follows:
// + IP
// + DOMAIN
// + DOMAIN_ID
EntityType string `json:"entity_type" required:"true"`
}
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// Create is a method used to create a private DNAT rule using given parameters.
func Create(c *golangsdk.ServiceClient, instanceId string, opts CreateOpts) (*Policy, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var r Policy
_, err = c.Post(rootURL(c, instanceId), b, &r, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return &r, err
}
// Get is a method used to obtain the ACL policy detail by its ID.
func Get(c *golangsdk.ServiceClient, instanceId, policyId string) (*Policy, error) {
var r Policy
_, err := c.Get(resourceURL(c, instanceId, policyId), &r, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return &r, err
}
// UpdateOpts is the structure that used to modify an existing ACL policy configuration.
type UpdateOpts struct {
// The ACL name.
Name string `json:"acl_name" required:"true"`
// The ACL type. The valid values are as follows:
// + PERMIT
// + DENY
Type string `json:"acl_type" required:"true"`
// The value of the ACL policy.
// One or more values are supported, separated by commas (,).
Value string `json:"acl_value" required:"true"`
// The entity type. The valid values are as follows:
// + IP
// + DOMAIN
// + DOMAIN_ID
// The entity type does not support update.
EntityType string `json:"entity_type" required:"true"`
}
// ListBindOpts is the structure used to querying published API list that ACL policy associated.
type ListOpts struct {
// The instnace ID to which the API belongs.
InstanceId string `json:"-" required:"true"`
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
Offset int `q:"offset"`
// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
Limit int `q:"limit"`
// The ACL policy ID.
PolicyId string `q:"id"`
// The ACL policy name.
Name string `q:"name"`
// The ACL type.
Type string `q:"acl_type"`
// The object type.
EntityType string `q:"entity_type"`
// Parameter name (name) for exact matching.
PreciseSearch string `q:"precise_search"`
}
// List is a method to obtain all ACL policies under a specified instance.
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Policy, error) {
url := rootURL(c, opts.InstanceId)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}
url += query.String()
pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := PolicyPage{pagination.OffsetPageBase{PageResult: r}}
return p
}).AllPages()
if err != nil {
return nil, err
}
return ExtractPolicies(pages)
}
// Update is a method used to modify the ACL policy configuration using given parameters.
func Update(c *golangsdk.ServiceClient, instanceId, policyId string, opts UpdateOpts) (*Policy, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var r Policy
_, err = c.Put(resourceURL(c, instanceId, policyId), b, &r, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return &r, err
}
// Delete is a method to remove the specified ACL policy using its ID and related dedicated instance ID.
func Delete(c *golangsdk.ServiceClient, instanceId, policyId string) error {
_, err := c.Delete(resourceURL(c, instanceId, policyId), &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
return err
}
// BindOpts is the structure that used to bind a policy to the published APIs.
type BindOpts struct {
// Instnace ID to which the API belongs.
InstanceId string `json:"-" required:"true"`
// ACL policy ID.
PolicyId string `json:"acl_id,omitempty"`
// The IDs of the API publish record.
PublishIds []string `json:"publish_ids,omitempty"`
}
// Bind is a method to bind the policy to one or more APIs.
func Bind(c *golangsdk.ServiceClient, opts BindOpts) ([]BindResp, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var r struct {
BindList []BindResp `json:"acl_bindings"`
}
_, err = c.Post(bindURL(c, opts.InstanceId), b, &r, nil)
return r.BindList, err
}
// ListBindOpts is the structure used to querying published API list that ACL policy associated.
type ListBindOpts struct {
// The instnace ID to which the API belongs.
InstanceId string `json:"-" required:"true"`
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
Offset int `q:"offset"`
// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
Limit int `q:"limit"`
// The ACL policy ID.
PolicyId string `q:"acl_id"`
// The API ID.
ApiId string `q:"api_id"`
// The API name.
ApiName string `q:"api_name"`
// The environment ID where the API is published.
EnvId string `q:"env_id"`
// The group ID where the API is located.
GroupId string `q:"group_id"`
}
// ListBind is a method to obtain all API to which the ACL policy bound.
func ListBind(c *golangsdk.ServiceClient, opts ListBindOpts) ([]AclBindApiInfo, error) {
url := listBindURL(c, opts.InstanceId)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}
url += query.String()
pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
p := BindPage{pagination.OffsetPageBase{PageResult: r}}
return p
}).AllPages()
if err != nil {
return nil, err
}
return ExtractBindInfos(pages)
}
// BatchUnbindOpts is the structure that used to unbinding policy from the published APIs.
type BatchUnbindOpts struct {
// Instance ID.
InstanceId string `json:"-" required:"true"`
// List of API and ACL policy binding relationship IDs that need to be unbound.
AclBindings []string `json:"acl_bindings,omitempty"`
}
// BatchUnbind is an API to unbind all ACL policies associated with the list.
func BatchUnbind(c *golangsdk.ServiceClient, unbindOpt BatchUnbindOpts, action string) (*BatchUnbindResp, error) {
b, err := golangsdk.BuildRequestBody(unbindOpt, "")
if err != nil {
return nil, err
}
var (
url = fmt.Sprintf("%v?action=%v", bindURL(c, unbindOpt.InstanceId), action)
r BatchUnbindResp
)
_, err = c.Put(url, b, &r, nil)
return &r, err
}