-
Notifications
You must be signed in to change notification settings - Fork 11
/
resource_pool_groups.go
125 lines (111 loc) · 3.68 KB
/
resource_pool_groups.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
package morpheus
import (
"fmt"
)
var (
// ResourcePoolGroupsPath is the API endpoint for resource pool groups
ResourcePoolGroupsPath = "/api/resource-pools/groups"
)
// ResourcePoolGroup structures for use in request and response payloads
type ResourcePoolGroup struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Visibility string `json:"visibility"`
Mode string `json:"mode"`
Pools []int64 `json:"pools"`
Tenants []struct {
ID int64 `json:"id"`
Name string `json:"name"`
} `json:"tenants"`
ResourcePermission struct {
All bool `json:"all"`
Sites []struct {
ID int64 `json:"id"`
Name string `json:"name"`
Default bool `json:"default"`
} `json:"sites"`
} `json:"resourcePermission"`
}
type ListResourcePoolGroupsResult struct {
ResourcePoolGroups *[]ResourcePoolGroup `json:"resourcePoolGroups"`
Meta *MetaResult `json:"meta"`
}
type GetResourcePoolGroupResult struct {
ResourcePoolGroup *ResourcePoolGroup `json:"resourcePoolGroup"`
}
type CreateResourcePoolGroupResult struct {
Success bool `json:"success"`
Message string `json:"msg"`
Errors map[string]string `json:"errors"`
ResourcePoolGroup *ResourcePoolGroup `json:"resourcePoolGroup"`
}
type UpdateResourcePoolGroupResult struct {
CreateResourcePoolGroupResult
}
type DeleteResourcePoolGroupResult struct {
DeleteResult
}
func (client *Client) ListResourcePoolGroups(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: ResourcePoolGroupsPath,
QueryParams: req.QueryParams,
Result: &ListResourcePoolGroupsResult{},
})
}
func (client *Client) GetResourcePoolGroup(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: fmt.Sprintf("%s/%d", ResourcePoolGroupsPath, id),
QueryParams: req.QueryParams,
Result: &GetResourcePoolGroupResult{},
})
}
func (client *Client) CreateResourcePoolGroup(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: ResourcePoolGroupsPath,
QueryParams: req.QueryParams,
Body: req.Body,
Result: &CreateResourcePoolGroupResult{},
})
}
func (client *Client) UpdateResourcePoolGroup(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "PUT",
Path: fmt.Sprintf("%s/%d", ResourcePoolGroupsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &UpdateResourcePoolGroupResult{},
})
}
func (client *Client) DeleteResourcePoolGroup(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "DELETE",
Path: fmt.Sprintf("%s/%d", ResourcePoolGroupsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &DeleteResourcePoolGroupResult{},
})
}
// FindResourcePoolGroupByName gets an existing resource pool group by name
func (client *Client) FindResourcePoolGroupByName(name string) (*Response, error) {
// Find by name, then get by ID
resp, err := client.ListResourcePoolGroups(&Request{
QueryParams: map[string]string{
"name": name,
},
})
if err != nil {
return resp, err
}
listResult := resp.Result.(*ListResourcePoolGroupsResult)
resourcePoolGroupCount := len(*listResult.ResourcePoolGroups)
if resourcePoolGroupCount != 1 {
return resp, fmt.Errorf("found %d resource pool groups for %v", resourcePoolGroupCount, name)
}
firstRecord := (*listResult.ResourcePoolGroups)[0]
resourcePoolGroupID := firstRecord.ID
return client.GetResourcePoolGroup(resourcePoolGroupID, &Request{})
}