forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizations.go
212 lines (175 loc) · 6.97 KB
/
organizations.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
package packngo
import (
"path"
)
// API documentation https://metal.equinix.com/developers/api/organizations/
const organizationBasePath = "/organizations"
// OrganizationService interface defines available organization methods
type OrganizationService interface {
List(*ListOptions) ([]Organization, *Response, error)
Get(string, *GetOptions) (*Organization, *Response, error)
Create(*OrganizationCreateRequest) (*Organization, *Response, error)
Update(string, *OrganizationUpdateRequest) (*Organization, *Response, error)
Delete(string) (*Response, error)
ListPaymentMethods(string) ([]PaymentMethod, *Response, error)
ListEvents(string, *ListOptions) ([]Event, *Response, error)
ListHardwareReservations(string, *ListOptions) ([]HardwareReservation, *Response, error)
}
type organizationsRoot struct {
Organizations []Organization `json:"organizations"`
Meta meta `json:"meta"`
}
// Organization represents an Equinix Metal organization
type Organization struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Website string `json:"website,omitempty"`
Twitter string `json:"twitter,omitempty"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Address Address `json:"address,omitempty"`
TaxID string `json:"tax_id,omitempty"`
MainPhone string `json:"main_phone,omitempty"`
BillingPhone string `json:"billing_phone,omitempty"`
CreditAmount float64 `json:"credit_amount,omitempty"`
Logo string `json:"logo,omitempty"`
LogoThumb string `json:"logo_thumb,omitempty"`
Projects []Project `json:"projects,omitempty"`
URL string `json:"href,omitempty"`
Members []Member `json:"members,omitempty"`
Owners []User `json:"owners,omitempty"`
PrimaryOwner User `json:"primary_owner,omitempty"`
}
func (o Organization) String() string {
return Stringify(o)
}
// OrganizationCreateRequest type used to create an Equinix Metal organization
type OrganizationCreateRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Website string `json:"website,omitempty"`
Twitter string `json:"twitter,omitempty"`
Logo string `json:"logo,omitempty"`
Address Address `json:"address,omitempty"`
}
func (o OrganizationCreateRequest) String() string {
return Stringify(o)
}
// OrganizationUpdateRequest type used to update an Equinix Metal organization
type OrganizationUpdateRequest struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Website *string `json:"website,omitempty"`
Twitter *string `json:"twitter,omitempty"`
Logo *string `json:"logo,omitempty"`
Address *Address `json:"address,omitempty"`
}
func (o OrganizationUpdateRequest) String() string {
return Stringify(o)
}
// OrganizationServiceOp implements OrganizationService
type OrganizationServiceOp struct {
client *Client
}
// List returns the user's organizations
func (s *OrganizationServiceOp) List(opts *ListOptions) (orgs []Organization, resp *Response, err error) {
subset := new(organizationsRoot)
apiPathQuery := opts.WithQuery(organizationBasePath)
for {
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
orgs = append(orgs, subset.Organizations...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Get returns a organization by id
func (s *OrganizationServiceOp) Get(organizationID string, opts *GetOptions) (*Organization, *Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(organizationBasePath, organizationID)
apiPathQuery := opts.WithQuery(endpointPath)
organization := new(Organization)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Create creates a new organization
func (s *OrganizationServiceOp) Create(createRequest *OrganizationCreateRequest) (*Organization, *Response, error) {
organization := new(Organization)
resp, err := s.client.DoRequest("POST", organizationBasePath, createRequest, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Update updates an organization
func (s *OrganizationServiceOp) Update(id string, updateRequest *OrganizationUpdateRequest) (*Organization, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(organizationBasePath, id)
organization := new(Organization)
resp, err := s.client.DoRequest("PATCH", apiPath, updateRequest, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Delete deletes an organizationID
func (s *OrganizationServiceOp) Delete(organizationID string) (*Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(organizationBasePath, organizationID)
return s.client.DoRequest("DELETE", apiPath, nil, nil)
}
// ListPaymentMethods returns PaymentMethods for an organization
func (s *OrganizationServiceOp) ListPaymentMethods(organizationID string) ([]PaymentMethod, *Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(organizationBasePath, organizationID, paymentMethodBasePath)
root := new(paymentMethodsRoot)
resp, err := s.client.DoRequest("GET", apiPath, nil, root)
if err != nil {
return nil, resp, err
}
return root.PaymentMethods, resp, err
}
// ListEvents returns list of organization events
func (s *OrganizationServiceOp) ListEvents(organizationID string, listOpt *ListOptions) ([]Event, *Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(organizationBasePath, organizationID, eventBasePath)
return listEvents(s.client, apiPath, listOpt)
}
// ListHardwareReservations returns list of hardware reservations
func (s *OrganizationServiceOp) ListHardwareReservations(organizationID string, opts *ListOptions) (reservations []HardwareReservation, resp *Response, err error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(organizationBasePath, organizationID, hardwareReservationBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(hardwareReservationRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
reservations = append(reservations, subset.HardwareReservations...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}