-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaremetal.go
275 lines (230 loc) · 9.85 KB
/
baremetal.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package edgecloud
import (
"context"
"fmt"
"net/http"
"github.com/go-playground/validator/v10"
)
const (
bmInstancesBasePathV1 = "/v1/bminstances"
bmCapacityBasePathV1 = "/v1/bmcapacity"
bmCheckLimitsSupPath = "check_limits"
bmRebuildSubPath = "rebuild"
bmAvailableFlavorsSubPath = "available_flavors"
)
// BareMetalService is an interface for creating and managing bare metal Instances with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/instances
type BareMetalService interface {
BareMetalListInstances(context.Context, *BareMetalInstancesListOpts) ([]Instance, *Response, error)
BareMetalCreateInstance(context.Context, *BareMetalServerCreateRequest) (*TaskResponse, *Response, error)
BareMetalRebuildInstance(context.Context, string, *BareMetalRebuildRequest) (*TaskResponse, *Response, error)
BareMetalListFlavors(context.Context, *BareMetalFlavorsOpts, *BareMetalFlavorsRequest) ([]BareMetalFlavor, *Response, error)
BareMetalGetCountAvailableNodes(context.Context) (*BareMetalCapacity, *Response, error)
BareMetalCheckQuotasForInstanceCreation(context.Context, *BareMetalQuotaCheckRequest) (Quota, *Response, error)
}
// BareMetalInstancesListOpts allows the filtering and sorting of paginated collections through the API.
type BareMetalInstancesListOpts struct {
Name string `url:"name,omitempty" validate:"omitempty"`
ProfileName string `url:"profile_name,omitempty" validate:"omitempty"`
OnlyWithFixedExternalIP bool `url:"only_with_fixed_external_ip,omitempty" validate:"omitempty"`
Limit int `url:"limit,omitempty" validate:"omitempty"`
Offset int `url:"offset,omitempty" validate:"omitempty"`
FlavorID string `url:"flavor_id,omitempty" validate:"omitempty"`
Status string `url:"status,omitempty" validate:"omitempty"`
ChangesBefore string `url:"changes-before,omitempty" validate:"omitempty"`
IP string `url:"ip,omitempty" validate:"omitempty"`
UUID string `url:"uuid,omitempty" validate:"omitempty"`
MetadataKV string `url:"metadata_kv,omitempty" validate:"omitempty"`
MetadataK string `url:"metadata_k,omitempty" validate:"omitempty"`
OrderBy string `url:"order_by,omitempty" validate:"omitempty"`
}
// BareMetalRebuildOpts allows the filtering and sorting of paginated collections through the API.
type BareMetalRebuildOpts struct {
InstanceID string `json:"instance_id" required:"true" validate:"uuid4"`
}
type BareMetalInterfaceOpts struct {
Type InterfaceType `json:"type" validate:"omitempty,enum"`
NetworkID string `json:"network_id,omitempty" validate:"rfe=Type:subnet,omitempty,uuid4"`
SubnetID string `json:"subnet_id,omitempty" validate:"rfe=Type:subnet,omitempty,uuid4"`
PortID string `json:"port_id,omitempty" validate:"rfe=Type:reserved_fixed_ip,allowed_without_all=NetworkID SubnetID,omitempty,uuid4"`
FloatingIP *InterfaceFloatingIP `json:"floating_ip,omitempty" validate:"omitempty,dive"`
}
// BareMetalServerCreateRequest represents a request to create an bare metal server.
type BareMetalServerCreateRequest struct {
KeypairName string `json:"keypair_name,omitempty"`
AppTemplateID string `json:"apptemplate_id,omitempty"`
Flavor string `json:"flavor" required:"true"`
Metadata Metadata `json:"metadata,omitempty" validate:"omitempty,dive"`
NameTemplates []string `json:"name_templates,omitempty" validate:"required_without=Names"`
Username string `json:"username,omitempty" validate:"omitempty,required_with=Password"`
Password string `json:"password,omitempty" validate:"omitempty,required_with=Username"`
Names []string `json:"names,omitempty" validate:"required_without=NameTemplates"`
Interfaces []BareMetalInterfaceOpts `json:"interfaces" required:"true" validate:"required,dive"`
ImageID string `json:"image_id" validate:"omitempty"`
UserData string `json:"user_data,omitempty" validate:"omitempty,base64"`
AppConfig map[string]interface{} `json:"app_config,omitempty" validate:"omitempty,dive"`
}
type BareMetalRebuildRequest struct {
ImageID string `json:"image_id" validate:"omitempty"`
}
type BareMetalFlavorsOpts struct {
IncludePrices bool `url:"include_prices,omitempty"`
}
type BareMetalFlavorsRequest struct {
ImageID string `json:"image_id" url:"image_id,omitempty"`
}
type bareMetalFlavorsRoot struct {
Count int `json:"count"`
Flavors []BareMetalFlavor `json:"results"`
}
// BareMetalFlavor represents an EdgecenterCloud BareMetalFlavor.
type BareMetalFlavor struct {
FlavorName string `json:"flavor_name"`
PricePerMonth int `json:"price_per_month"`
Disabled bool `json:"disabled"`
HardwareDescription HardwareDescription `json:"hardware_description,omitempty"`
CurrencyCode string `json:"currency_code"`
PriceStatus string `json:"price_status"`
FlavorID string `json:"flavor_id"`
PricePerHour int `json:"price_per_hour"`
ResourceClass string `json:"resource_class"`
}
type BareMetalCapacity struct {
Capacity map[string]int `json:"capacity"`
}
type BareMetalQuotaCheckRequest struct {
Flavor string `json:"flavor" required:"true"`
Interfaces []BareMetalInterfaceOpts `json:"interfaces" required:"true" validate:"required,dive"`
}
func (s *InstancesServiceOp) BareMetalCheckQuotasForInstanceCreation(ctx context.Context, reqBody *BareMetalQuotaCheckRequest) (Quota, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
path := s.client.addProjectRegionPath(bmInstancesBasePathV1)
path = fmt.Sprintf("%s/%s", path, bmCheckLimitsSupPath)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
quotas := new(Quota)
resp, err := s.client.Do(ctx, req, quotas)
if err != nil {
return nil, resp, err
}
return *quotas, resp, err
}
func (s *InstancesServiceOp) BareMetalGetCountAvailableNodes(ctx context.Context) (*BareMetalCapacity, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(bmCapacityBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
capacity := new(BareMetalCapacity)
resp, err := s.client.Do(ctx, req, capacity)
if err != nil {
return nil, resp, err
}
return capacity, resp, err
}
func (s *InstancesServiceOp) BareMetalListFlavors(ctx context.Context, opts *BareMetalFlavorsOpts, reqBody *BareMetalFlavorsRequest) ([]BareMetalFlavor, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
var err error
path := s.client.addProjectRegionPath(bmInstancesBasePathV1)
path = fmt.Sprintf("%s/%s", path, bmAvailableFlavorsSubPath)
if opts != nil {
path, err = addOptions(path, opts)
if err != nil {
return nil, nil, err
}
}
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
root := new(bareMetalFlavorsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Flavors, resp, err
}
func (s *InstancesServiceOp) BareMetalRebuildInstance(ctx context.Context, instanceID string, reqBody *BareMetalRebuildRequest) (*TaskResponse, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := isValidUUID(instanceID, "instanceID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(bmInstancesBasePathV1)
path = fmt.Sprintf("%s/%s/%s", path, instanceID, bmRebuildSubPath)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
func (s *InstancesServiceOp) BareMetalCreateInstance(ctx context.Context, reqBody *BareMetalServerCreateRequest) (*TaskResponse, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(bmInstancesBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
func (s *InstancesServiceOp) BareMetalListInstances(ctx context.Context, opts *BareMetalInstancesListOpts) ([]Instance, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
validate := validator.New()
err := validate.Struct(opts)
if err != nil {
return nil, nil, err
}
path := s.client.addProjectRegionPath(bmInstancesBasePathV1)
if opts != nil {
path, err = addOptions(path, opts)
if err != nil {
return nil, nil, err
}
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(instancesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Instances, resp, err
}