forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batches.go
97 lines (78 loc) · 2.88 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
package packngo
import (
"fmt"
)
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"`
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"`
}
// BatchServiceOp implements BatchService
type BatchServiceOp struct {
client *Client
}
// Get returns batch details
func (s *BatchServiceOp) Get(batchID string, getOpt *GetOptions) (*Batch, *Response, error) {
params := createGetOptionsURL(getOpt)
path := fmt.Sprintf("%s/%s?%s", batchBasePath, batchID, params)
batch := new(Batch)
resp, err := s.client.DoRequest("GET", path, 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, listOpt *ListOptions) (batches []Batch, resp *Response, err error) {
params := createListOptionsURL(listOpt)
path := fmt.Sprintf("%s/%s%s?%s", projectBasePath, projectID, batchBasePath, params)
subset := new(batchesList)
resp, err = s.client.DoRequest("GET", path, 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) {
path := fmt.Sprintf("%s/%s/devices/batch", projectBasePath, projectID)
batches := new(batchesList)
resp, err := s.client.DoRequest("POST", path, 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) {
path := fmt.Sprintf("%s/%s?remove_associated_instances=%t", batchBasePath, id, removeDevices)
return s.client.DoRequest("DELETE", path, nil, nil)
}