This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
89 lines (76 loc) · 2.92 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
/*
Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
*/
package whiteblackip_rules
import (
"github.com/huaweicloud/golangsdk"
)
var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToWhiteBlackIPCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new whiteblackip rule.
type CreateOpts struct {
Addr string `json:"addr" required:"true"`
White int `json:"white,omitempty"`
}
// ToWhiteBlackIPCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToWhiteBlackIPCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create will create a new whiteblackip rule based on the values in CreateOpts.
func Create(c *golangsdk.ServiceClient, policyID string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToWhiteBlackIPCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(rootURL(c, policyID), b, &r.Body, reqOpt)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToWhiteBlackIPUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains all the values needed to update a whiteblackip rule.
type UpdateOpts struct {
Addr string `json:"addr" required:"true"`
White *int `json:"white" required:"true"`
}
// ToWhiteBlackIPUpdateMap builds a update request body from UpdateOpts.
func (opts UpdateOpts) ToWhiteBlackIPUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update accepts a UpdateOpts struct and uses the values to update a rule.The response code from api is 200
func Update(c *golangsdk.ServiceClient, policyID, ruleID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToWhiteBlackIPUpdateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Put(resourceURL(c, policyID, ruleID), b, nil, reqOpt)
return
}
// Get retrieves a particular whiteblackip rule based on its unique ID.
func Get(c *golangsdk.ServiceClient, policyID, ruleID string) (r GetResult) {
reqOpt := &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders,
}
_, r.Err = c.Get(resourceURL(c, policyID, ruleID), &r.Body, reqOpt)
return
}
// Delete will permanently delete a particular whiteblackip rule based on its unique ID.
func Delete(c *golangsdk.ServiceClient, policyID, ruleID string) (r DeleteResult) {
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders}
_, r.Err = c.Delete(resourceURL(c, policyID, ruleID), reqOpt)
return
}