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
146 lines (129 loc) · 4.71 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
package domains
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 {
ToDomainCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new backup.
type CreateOpts struct {
// Domain name
HostName string `json:"hostname" required:"true"`
// The original server information
Servers []ServerOpts `json:"server" required:"true"`
// Whether proxy is configured
Proxy *bool `json:"proxy" required:"true"`
// Certificate ID
CertificateId string `json:"certificate_id,omitempty"`
// The type of the source IP header
SipHeaderName string `json:"sip_header_name,omitempty"`
// The HTTP request header for identifying the real source IP.
SipHeaderList []string `json:"sip_header_list,omitempty"`
}
// ServerOpts contains the origin server information.
type ServerOpts struct {
// Protocol type of the client
ClientProtocol string `json:"client_protocol" required:"true"`
// Protocol used by WAF to forward client requests to the server
ServerProtocol string `json:"server_protocol" required:"true"`
// IP address or domain name of the web server that the client accesses.
Address string `json:"address" required:"true"`
// Port number used by the web server, the value ranges from 0 to 65535.
Port string `json:"port" required:"true"`
}
// ToDomainCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToDomainCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create will create a new Domain based on the values in CreateOpts.
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToDomainCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToDomainUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains all the values needed to update a Domain.
type UpdateOpts struct {
// Certificate ID
CertificateId string `json:"certificate_id,omitempty"`
// The original server information
Servers []ServerOpts `json:"server,omitempty"`
// Whether proxy is configured
Proxy *bool `json:"proxy,omitempty"`
// The type of the source IP header
SipHeaderName string `json:"sip_header_name,omitempty"`
// The HTTP request header for identifying the real source IP.
SipHeaderList []string `json:"sip_header_list,omitempty"`
}
// ToDomainUpdateMap builds a update request body from UpdateOpts.
func (opts UpdateOpts) ToDomainUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update accepts a UpdateOpts struct and uses the values to update a Domain.The response code from api is 200
func Update(c *golangsdk.ServiceClient, domainID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToDomainUpdateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Put(resourceURL(c, domainID), b, nil, reqOpt)
return
}
// Get retrieves a particular Domain based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
reqOpt := &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders,
}
_, r.Err = c.Get(resourceURL(c, id), &r.Body, reqOpt)
return
}
// DeleteOptsBuilder allows extensions to add additional parameters to the
// delete request.
type DeleteOptsBuilder interface {
ToDeleteQuery() (string, error)
}
// DeleteOpts contains all the values needed to delete a domain.
type DeleteOpts struct {
// KeepPolicy specifies whether to retain the policy when deleting a domain name
// the default value is false
KeepPolicy bool `q:"keepPolicy"`
}
// ToDeleteQuery builds a delete request body from DeleteOpts.
func (opts DeleteOpts) ToDeleteQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
// Delete will permanently delete a particular Domain based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
url := resourceURL(c, id)
if opts != nil {
var query string
query, r.Err = opts.ToDeleteQuery()
if r.Err != nil {
return
}
url += query
}
reqOpt := &golangsdk.RequestOpts{
OkCodes: []int{204},
MoreHeaders: RequestOpts.MoreHeaders,
}
_, r.Err = c.Delete(url, reqOpt)
return
}