forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plans.go
201 lines (162 loc) · 5.25 KB
/
plans.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
package packngo
import (
"encoding/json"
"path"
)
const planBasePath = "/plans"
// PlanService interface defines available plan methods
type PlanService interface {
List(*ListOptions) ([]Plan, *Response, error)
ProjectList(string, *ListOptions) ([]Plan, *Response, error)
OrganizationList(string, *ListOptions) ([]Plan, *Response, error)
}
type planRoot struct {
Plans []Plan `json:"plans"`
}
// Plan represents an Equinix Metal service plan
type Plan struct {
ID string `json:"id"`
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Line string `json:"line,omitempty"`
Legacy bool `json:"legacy,omitempty"`
Specs *Specs `json:"specs,omitempty"`
Pricing *Pricing `json:"pricing,omitempty"`
DeploymentTypes []string `json:"deployment_types"`
Class string `json:"class"`
AvailableIn []Facility `json:"available_in"`
AvailableInMetros []Metro `json:"available_in_metros"`
ReservationPricing *ReservationPricing `json:"reservation_pricing,omitempty"`
Href string `json:"href,omitempty"`
}
func (p Plan) String() string {
return Stringify(p)
}
type MetroPricing map[string]AnnualReservationPricing
// ReservationPricing - The reserved pricing for a plan
type ReservationPricing struct {
AnnualReservationPricing
Metros MetroPricing
}
func (r ReservationPricing) String() string {
return Stringify(r)
}
// UnmarshalJSON - Custom unmarshal function to set up the ReservationPricing object
func (r *ReservationPricing) UnmarshalJSON(data []byte) error {
var a AnnualReservationPricing
var m MetroPricing
// Leverage the built in unmarshalling to sort out all the fields
err := json.Unmarshal(data, &a)
if err != nil {
return err
}
err = json.Unmarshal(data, &m)
if err != nil {
return err
}
// Remove three_year and one_year from the metropricing object
delete(m, "three_year")
delete(m, "one_year")
// Pass the objects to the parent
r.Metros = m
r.AnnualReservationPricing = a
return nil
}
type AnnualReservationPricing struct {
OneYear *Pricing `json:"one_year"`
ThreeYear *Pricing `json:"three_year"`
}
func (m AnnualReservationPricing) String() string {
return Stringify(m)
}
// Specs - the server specs for a plan
type Specs struct {
Cpus []*Cpus `json:"cpus,omitempty"`
Memory *Memory `json:"memory,omitempty"`
Drives []*Drives `json:"drives,omitempty"`
Nics []*Nics `json:"nics,omitempty"`
Features *Features `json:"features,omitempty"`
}
func (s Specs) String() string {
return Stringify(s)
}
// Cpus - the CPU config details for specs on a plan
type Cpus struct {
Count int `json:"count,omitempty"`
Type string `json:"type,omitempty"`
}
func (c Cpus) String() string {
return Stringify(c)
}
// Memory - the RAM config details for specs on a plan
type Memory struct {
Total string `json:"total,omitempty"`
}
func (m Memory) String() string {
return Stringify(m)
}
// Drives - the storage config details for specs on a plan
type Drives struct {
Count int `json:"count,omitempty"`
Size string `json:"size,omitempty"`
Type string `json:"type,omitempty"`
}
func (d Drives) String() string {
return Stringify(d)
}
// Nics - the network hardware details for specs on a plan
type Nics struct {
Count int `json:"count,omitempty"`
Type string `json:"type,omitempty"`
}
func (n Nics) String() string {
return Stringify(n)
}
// Features - other features in the specs for a plan
type Features struct {
Raid bool `json:"raid,omitempty"`
Txt bool `json:"txt,omitempty"`
}
func (f Features) String() string {
return Stringify(f)
}
// Pricing - the pricing options on a plan
type Pricing struct {
Hour float32 `json:"hour,omitempty"`
Month float32 `json:"month,omitempty"`
}
func (p Pricing) String() string {
return Stringify(p)
}
// PlanServiceOp implements PlanService
type PlanServiceOp struct {
client *Client
}
func planList(c *Client, apiPath string, opts *ListOptions) ([]Plan, *Response, error) {
root := new(planRoot)
apiPathQuery := opts.WithQuery(apiPath)
resp, err := c.DoRequest("GET", apiPathQuery, nil, root)
if err != nil {
return nil, resp, err
}
return root.Plans, resp, err
}
// List method returns all available plans
func (s *PlanServiceOp) List(opts *ListOptions) ([]Plan, *Response, error) {
return planList(s.client, planBasePath, opts)
}
// ProjectList method returns plans available in a project
func (s *PlanServiceOp) ProjectList(projectID string, opts *ListOptions) ([]Plan, *Response, error) {
if validateErr := ValidateUUID(projectID); validateErr != nil {
return nil, nil, validateErr
}
return planList(s.client, path.Join(projectBasePath, projectID, planBasePath), opts)
}
// OrganizationList method returns plans available in an organization
func (s *PlanServiceOp) OrganizationList(organizationID string, opts *ListOptions) ([]Plan, *Response, error) {
if validateErr := ValidateUUID(organizationID); validateErr != nil {
return nil, nil, validateErr
}
return planList(s.client, path.Join(organizationBasePath, organizationID, planBasePath), opts)
}