This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
batches.go
118 lines (98 loc) · 3.67 KB
/
batches.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
package packngo
import (
"fmt"
"path"
)
const batchBasePath = "/batches"
// BatchService interface defines available batch methods
type BatchService interface {
Get(batchID string, getOpt *GetOptions) (*Batch, *Response, error)
List(ProjectID string, listOpt *ListOptions) ([]Batch, *Response, error)
Create(projectID string, batches *BatchCreateRequest) ([]Batch, *Response, error)
Delete(string, bool) (*Response, error)
}
// Batch type
type Batch struct {
ID string `json:"id"`
ErrorMessages []string `json:"error_messages,omitempty"`
// State may be 'failed' or 'completed'
State string `json:"state,omitempty"`
Quantity int32 `json:"quantity,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
Href string `json:"href,omitempty"`
Project Href `json:"project,omitempty"`
Devices []Device `json:"devices,omitempty"`
}
//BatchesList represents collection of batches
type batchesList struct {
Batches []Batch `json:"batches,omitempty"`
}
// BatchCreateRequest type used to create batch of device instances
type BatchCreateRequest struct {
Batches []BatchCreateDevice `json:"batches"`
}
// BatchCreateDevice type used to describe batch instances
type BatchCreateDevice struct {
DeviceCreateRequest
Quantity int32 `json:"quantity"`
FacilityDiversityLevel int32 `json:"facility_diversity_level,omitempty"`
SpotInstance bool `json:"spot_instance,omitempty"`
SpotPriceMax float64 `json:"spot_price_max,omitempty"`
}
// BatchServiceOp implements BatchService
type BatchServiceOp struct {
client *Client
}
// Get returns batch details
func (s *BatchServiceOp) Get(batchID string, opts *GetOptions) (*Batch, *Response, error) {
if validateErr := ValidateUUID(batchID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(batchBasePath, batchID)
apiPathQuery := opts.WithQuery(endpointPath)
batch := new(Batch)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, batch)
if err != nil {
return nil, resp, err
}
return batch, resp, err
}
// List returns batches on a project
func (s *BatchServiceOp) List(projectID string, opts *ListOptions) (batches []Batch, resp *Response, err error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(projectBasePath, projectID, batchBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
subset := new(batchesList)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
batches = append(batches, subset.Batches...)
return batches, resp, err
}
// Create function to create batch of device instances
func (s *BatchServiceOp) Create(projectID string, request *BatchCreateRequest) ([]Batch, *Response, error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(projectBasePath, projectID, "devices", "batch")
batches := new(batchesList)
resp, err := s.client.DoRequest("POST", apiPath, request, batches)
if err != nil {
return nil, resp, err
}
return batches.Batches, resp, err
}
// Delete function to remove an instance batch
func (s *BatchServiceOp) Delete(id string, removeDevices bool) (*Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, validateErr
}
// API doc days the remove_associated_instances params shout be in the body
// https://metal.equinix.com/developers/api/batches/#delete-the-batch
// .. does this even work?
apiPath := fmt.Sprintf("%s/%s?remove_associated_instances=%t", batchBasePath, id, removeDevices)
return s.client.DoRequest("DELETE", apiPath, nil, nil)
}