-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
103 lines (90 loc) · 3.23 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
package configurations
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
)
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 {
ToConfigCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new configuration.
type CreateOpts struct {
//Configuration Name
Name string `json:"name" required:"true"`
//Configuration Description
Description string `json:"description,omitempty"`
//Configuration Values
Values map[string]string `json:"values,omitempty"`
//Database Object
DataStore DataStore `json:"datastore" required:"true"`
}
type DataStore struct {
//DB Engine
Type string `json:"type" required:"true"`
//DB version
Version string `json:"version" required:"true"`
}
// ToConfigCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToConfigCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create will create a new Config based on the values in CreateOpts.
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToConfigCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToConfigUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains all the values needed to update a Configuration.
type UpdateOpts struct {
//Configuration Name
Name string `json:"name,omitempty"`
//Configuration Description
Description string `json:"description,omitempty"`
//Configuration Values
Values map[string]string `json:"values,omitempty"`
}
// ToConfigUpdateMap builds a update request body from UpdateOpts.
func (opts UpdateOpts) ToConfigUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Update accepts a UpdateOpts struct and uses the values to update a Configuration.The response code from api is 200
func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToConfigUpdateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders}
_, r.Err = c.Put(resourceURL(c, id), b, nil, reqOpt)
return
}
// Get retrieves a particular Configuration based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}
// Delete will permanently delete a particular Configuration based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders}
_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
return
}