forked from huaweicloud/huaweicloud-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
166 lines (134 loc) · 4.12 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
package cloudservers
import (
"strings"
"github.com/YoungPioneers/huaweicloud-sdk-go"
"fmt"
)
func ResetPassword(client *gophercloud.ServiceClient, serverID, newPassword string) (r ActionResult) {
if serverID == "" {
message := fmt.Sprintf(gophercloud.CE_MissingInputMessage, "serverID")
err := gophercloud.NewSystemCommonError(gophercloud.CE_MissingInputCode, message)
r.Err = err
return
}
if newPassword == "" {
message := fmt.Sprintf(gophercloud.CE_MissingInputMessage, "newPassword")
err := gophercloud.NewSystemCommonError(gophercloud.CE_MissingInputCode, message)
r.Err = err
return
}
body := map[string]interface{}{
"reset-password": map[string]string{
"new_password": newPassword,
},
}
//v2替换成v2.1
newUrl := strings.Replace(resetPwdURL(client, serverID), "/v2/", "/v2.1/", 1)
_, r.Err = client.Put(newUrl, body, nil, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
type ChangeOptsBuilder interface {
// Returns value that can be passed to json.Marshal
ToServerChangeMap() (map[string]interface{}, error)
}
type ChangeOpts struct {
AdminPass string `json:"adminpass,omitempty"`
KeyName string `json:"keyname,omitempty"`
UserID string `json:"userid,omitempty"`
ImageID string `json:"imageid" required:"true"`
MetaData *MetaData `json:"metadata,omitempty"`
}
type MetaData struct {
UserData string `json:"user_data,omitempty"`
}
func (opts ChangeOpts) ToServerChangeMap() (map[string]interface{}, error) {
body, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return map[string]interface{}{"os-change": body}, nil
}
func ChangeOS(client *gophercloud.ServiceClient, serverID string, opts ChangeOptsBuilder) (r ChangeResult) {
if serverID == "" {
message := fmt.Sprintf(gophercloud.CE_MissingInputMessage, "serverID")
err := gophercloud.NewSystemCommonError(gophercloud.CE_MissingInputCode, message)
r.Err = err
return
}
body, err := opts.ToServerChangeMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(changeURL(client, serverID), body, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
type ReinstallOptsBuilder interface {
ToServerReinstallMap() (map[string]interface{}, error)
}
type ReinstallOpts struct {
AdminPass string `json:"adminpass,omitempty"`
KeyName string `json:"keyname,omitempty"`
UserID string `json:"userid,omitempty"`
MetaData *MetaData `json:"metadata,omitempty"`
}
func (opts ReinstallOpts) ToServerReinstallMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "os-reinstall")
return b, err
}
func ReinstallOS(client *gophercloud.ServiceClient, serverID string, opts ReinstallOptsBuilder) (r ChangeResult) {
if serverID == "" {
message := fmt.Sprintf(gophercloud.CE_MissingInputMessage, "serverID")
err := gophercloud.NewSystemCommonError(gophercloud.CE_MissingInputCode, message)
r.Err = err
return
}
body, err := opts.ToServerReinstallMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(reinstallOSURL(client, serverID), body, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
type ResizeOptsBuilder interface {
ToResizeQuery() (string, error)
}
type ResizeOpts struct {
// Limit limits the number of Images to return.
Limit int `q:"limit"`
// Mark is an Image UUID at which to set a marker.
Marker string `q:"marker"`
InstanceUUID string `q:"instance_uuid"`
SourceFlavorID string `q:"source_flavor_id"`
SourceFlavorName string `q:"source_flavor_name"`
SortKey string `q:"sort_key"`
SortDir string `q:"sort_dir"`
}
func (opts ResizeOpts) ToResizeQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
//func ResizeFlavor(client *gophercloud.ServiceClient, opts ResizeOptsBuilder) (r FlavorResult) {
//
// url, err := opts.ToResizeQuery()
// if err != nil {
// r.Err = err
// return
// }
// url = resizeFlavorURL(client) + url
//
// //v2替换成v2.1
// newUrl := strings.Replace(url, "/v2/", "/v2.1/", 1)
//
// _, r.Err = client.Get(newUrl, &r.Body, &gophercloud.RequestOpts{
// OkCodes: []int{200},
// })
// return
//}