-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprice_sets.go
134 lines (120 loc) · 4.18 KB
/
price_sets.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
package morpheus
import (
"fmt"
)
var (
// PriceSetsPath is the API endpoint for priceSets
PriceSetsPath = "/api/price-sets"
)
// PriceSets structures for use in request and response payloads
type PriceSet struct {
ID int64 `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Active bool `json:"active"`
PriceUnit string `json:"priceUnit"`
Type string `json:"type"`
RegionCode string `json:"regionCode"`
SystemCreated bool `json:"systemCreated"`
Zone map[string]interface{} `json:"zone"`
ZonePool map[string]interface{} `json:"zonePool"`
Prices []Price `json:"prices"`
RestartUsage bool `json:"restartUsage"`
}
// ListPriceSetsResult structure parses the list priceSets response payload
type ListPriceSetsResult struct {
PriceSets *[]PriceSet `json:"priceSets"`
Meta *MetaResult `json:"meta"`
}
// GetPriceSetResult structure parses the get priceSet response payload
type GetPriceSetResult struct {
PriceSet *PriceSet `json:"priceSet"`
}
// CreatePriceSetResult structure parses the create priceSet response payload
type CreatePriceSetResult struct {
Success bool `json:"success"`
Message string `json:"msg"`
Errors map[string]string `json:"errors"`
ID int64 `json:"id"`
}
// UpdatePriceSetResult structure parses the update priceSet response payload
type UpdatePriceSetResult struct {
CreatePriceSetResult
}
// DeletePriceSetResult structure parses the delete priceSet response payload
type DeletePriceSetResult struct {
DeleteResult
}
// ListPriceSetSets lists all priceSets
// https://apidocs.morpheusdata.com/#get-all-priceSets
func (client *Client) ListPriceSets(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: PriceSetsPath,
QueryParams: req.QueryParams,
Result: &ListPriceSetsResult{},
})
}
// GetPriceSetSet gets an existing priceSet
// https://apidocs.morpheusdata.com/#get-a-specific-priceSet
func (client *Client) GetPriceSet(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: fmt.Sprintf("%s/%d", PriceSetsPath, id),
QueryParams: req.QueryParams,
Result: &GetPriceSetResult{},
})
}
// CreatePriceSetSet creates a new priceSetSet
// https://apidocs.morpheusdata.com/#create-a-priceSet
func (client *Client) CreatePriceSet(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: PriceSetsPath,
QueryParams: req.QueryParams,
Body: req.Body,
Result: &CreatePriceSetResult{},
})
}
// UpdatePriceSet updates an existing priceSet
// https://apidocs.morpheusdata.com/#update-a-priceSet
func (client *Client) UpdatePriceSet(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "PUT",
Path: fmt.Sprintf("%s/%d", PriceSetsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &UpdatePriceSetResult{},
})
}
// DeletePriceSet deletes an existing priceSet
// https://apidocs.morpheusdata.com/#deactivate-a-priceSet
func (client *Client) DeletePriceSet(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "PUT",
Path: fmt.Sprintf("%s/%d/deactivate", PriceSetsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &DeletePriceSetResult{},
})
}
// FindPriceSetByName gets an existing priceSet by name
func (client *Client) FindPriceSetByName(name string) (*Response, error) {
// Find by name, then get by ID
resp, err := client.ListPriceSets(&Request{
QueryParams: map[string]string{
"name": name,
},
})
if err != nil {
return resp, err
}
listResult := resp.Result.(*ListPriceSetsResult)
priceSetCount := len(*listResult.PriceSets)
if priceSetCount != 1 {
return resp, fmt.Errorf("found %d PriceSets for %v", priceSetCount, name)
}
firstRecord := (*listResult.PriceSets)[0]
priceSetID := firstRecord.ID
return client.GetPriceSet(priceSetID, &Request{})
}