-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_groups.go
161 lines (146 loc) · 4.41 KB
/
resource_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
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
package kel
import (
"fmt"
"net/http"
"github.com/brosner/go-json-spec-handler"
)
const (
resourceGroupResourceType string = "resource-groups"
resourceGroupAPIPath string = "/resource-groups"
)
// ResourceGroupService represents the link between the ResourceGroup and the
// Client.
type ResourceGroupService struct {
client *Client
}
// Create sends an HTTP request to create the Kel resource group.
func (srv *ResourceGroupService) Create(resourceGroup *ResourceGroup) CreateRequest {
return &createRequest{
client: srv.client,
path: resourceGroupAPIPath,
buildDoc: buildResourceGroupDoc(resourceGroup),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(resourceGroupResourceType, resourceGroup); objErr != nil {
return objErr
}
resourceGroup.srv = srv
return nil
},
}
}
// CreateWithToken sends an HTTP request to create the Kel resource group
// providing the given token.
func (srv *ResourceGroupService) CreateWithToken(resourceGroup *ResourceGroup, token string) CreateRequest {
req := &createRequest{
client: srv.client,
path: resourceGroupAPIPath,
hdr: make(http.Header),
buildDoc: buildResourceGroupDoc(resourceGroup),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(resourceGroupResourceType, resourceGroup); objErr != nil {
return objErr
}
resourceGroup.srv = srv
return nil
},
}
req.hdr.Set("X-Kel-Token", token)
return req
}
// List returns all resource groups reachable by the API
func (srv *ResourceGroupService) List(resourceGroups *[]*ResourceGroup) ListRequest {
return &listRequest{
client: srv.client,
path: resourceGroupAPIPath,
handler: func(document *jsh.Document) error {
for i := range document.Data {
obj := document.Data[i]
resourceGroup := &ResourceGroup{srv: srv}
if objErr := obj.Unmarshal(resourceGroup.GetResourceType(), resourceGroup); objErr != nil {
return objErr
}
*resourceGroups = append(*resourceGroups, resourceGroup)
}
return nil
},
}
}
// Get returns the resource group with the given name reachable by the API
func (srv *ResourceGroupService) Get(name string, resourceGroup *ResourceGroup) GetRequest {
resourceGroup.Name = name // ID for lookup
return &getRequest{
client: srv.client,
path: resourceGroup.getDetailPath(),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(resourceGroupResourceType, resourceGroup); objErr != nil {
return objErr
}
resourceGroup.srv = srv
return nil
},
}
}
func (resourceGroup *ResourceGroup) getDetailPath() string {
return fmt.Sprintf(
"%s/%s",
resourceGroupAPIPath,
resourceGroup.GetID(),
)
}
// GetResourceType ...
func (resourceGroup *ResourceGroup) GetResourceType() string {
return resourceGroupResourceType
}
// GetID ...
func (resourceGroup *ResourceGroup) GetID() string {
return resourceGroup.Name
}
// Reload will get an updated resource group and point to it as its own.
func (resourceGroup *ResourceGroup) Reload() error {
reloaded := &ResourceGroup{}
if err := resourceGroup.srv.Get(resourceGroup.GetID(), reloaded).Do(); err != nil {
return err
}
*resourceGroup = *reloaded
return nil
}
// Save will persistent local data with the API.
func (resourceGroup *ResourceGroup) Save() error {
req := &updateRequest{
client: resourceGroup.srv.client,
path: resourceGroup.getDetailPath(),
buildDoc: buildResourceGroupDoc(resourceGroup),
handler: func(document *jsh.Document) error {
obj := document.Data[0]
if objErr := obj.Unmarshal(resourceGroupResourceType, resourceGroup); objErr != nil {
return objErr
}
return nil
},
}
return req.Do()
}
// Delete will destroy the resource group.
func (resourceGroup *ResourceGroup) Delete() error {
req := &deleteRequest{
client: resourceGroup.srv.client,
path: resourceGroup.getDetailPath(),
}
return req.Do()
}
func buildResourceGroupDoc(resourceGroup *ResourceGroup) func(outreq *http.Request) (*jsh.Document, error) {
return func(outreq *http.Request) (*jsh.Document, error) {
obj, objErr := jsh.NewObject(resourceGroup.GetID(), resourceGroupResourceType, resourceGroup)
if objErr != nil {
return nil, objErr
}
if err := obj.Validate(outreq, false); err != nil {
return nil, fmt.Errorf("error preparing object: %s", err.Error())
}
doc := jsh.Build(obj)
return doc, nil
}
}