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
190 lines (165 loc) · 6.46 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
package softwaredeployment
import (
"reflect"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// ListOpts allows the filtering and sorting of paginated collections through
// the API.
type ListOpts struct {
//Specifies the ID of the instance deployed by the software configuration.
ServerId string `q:"server_id"`
//Specifies the ID of this deployment resource.
Id string
//Specifies the ID of the software configuration resource running on an instance.
ConfigId string
//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
Status string
// Specifies the stack action that triggers this deployment resource.
Action string
}
// List returns collection of
// Software Deployment. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
//
// Default policy settings return only those Software Deployment that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Deployment, error) {
q, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return nil, err
}
u := rootURL(client) + q.String()
pages, err := pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
return DeploymentPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()
allConfigs, err := ExtractDeployments(pages)
if err != nil {
return nil, err
}
return FilterDeployments(allConfigs, opts)
}
func FilterDeployments(deployments []Deployment, opts ListOpts) ([]Deployment, error) {
var refinedDeployments []Deployment
var matched bool
m := map[string]interface{}{}
if opts.Id != "" {
m["Id"] = opts.Id
}
if opts.ServerId != "" {
m["ServerId"] = opts.ServerId
}
if opts.ConfigId != "" {
m["ConfigId"] = opts.ConfigId
}
if opts.Status != "" {
m["Status"] = opts.Status
}
if opts.Action != "" {
m["Action"] = opts.Action
}
if len(m) > 0 && len(deployments) > 0 {
for _, deployment := range deployments {
matched = true
for key, value := range m {
if sVal := getStructField(&deployment, key); !(sVal == value) {
matched = false
}
}
if matched {
refinedDeployments = append(refinedDeployments, deployment)
}
}
} else {
refinedDeployments = deployments
}
return refinedDeployments, nil
}
func getStructField(v *Deployment, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToSoftwareDeploymentCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new Software Deployment. There are
// no required values.
type CreateOpts struct {
// Specifies the stack action that triggers this deployment resource.
Action string `json:"action,omitempty"`
//Specifies the ID of the software configuration resource running on an instance.
ConfigId string `json:"config_id" required:"true"`
//Specifies input data stored in the form of a key-value pair.
InputValues map[string]interface{} `json:"input_values,omitempty"`
//Specifies the ID of the instance deployed by the software configuration.
ServerId string `json:"server_id" required:"true"`
//Specifies the ID of the authenticated tenant who can perform operations on the deployment resources.
TenantId string `json:"stack_user_project_id,omitempty"`
//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
Status string `json:"status,omitempty"`
//Specifies the cause of the current deployment resource status.
StatusReason string `json:"status_reason,omitempty"`
}
// ToSoftwareDeploymentCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToSoftwareDeploymentCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create accepts a CreateOpts struct and uses the values to create a new Software Deployment
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToSoftwareDeploymentCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}
// Get retrieves a particular software Deployment based on its unique ID.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
return
}
//UpdateOptsBuilder is an interface by which can be able to build the request body of software deployment.
type UpdateOptsBuilder interface {
ToSoftwareDeploymentUpdateMap() (map[string]interface{}, error)
}
//UpdateOpts is a struct which represents the request body of update method.
type UpdateOpts struct {
// Specifies the stack action that triggers this deployment resource.
Action string `json:"action,omitempty"`
//Specifies the ID of the software configuration resource running on an instance.
ConfigId string `json:"config_id" required:"true"`
//Specifies input data stored in the form of a key-value pair.
InputValues map[string]interface{} `json:"input_values,omitempty"`
//Specifies output data stored in the form of a key-value pair.
OutputValues map[string]interface{} `json:"output_values" required:"true"`
//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
Status string `json:"status,omitempty"`
//Specifies the cause of the current deployment resource status.
StatusReason string `json:"status_reason,omitempty"`
}
//ToSoftwareDeploymentUpdateMap builds a update request body from UpdateOpts.
func (opts UpdateOpts) ToSoftwareDeploymentUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
//Update is a method which can be able to update the name of software deployment.
func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToSoftwareDeploymentUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete will permanently delete a particular Software Deployment based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, id), nil)
return
}