-
Notifications
You must be signed in to change notification settings - Fork 8
/
compute_resource_client.go
113 lines (96 loc) · 2.77 KB
/
compute_resource_client.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
package compute
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/mitchellh/mapstructure"
)
// ResourceClient is an AuthenticatedClient with some additional information about the resources to be addressed.
type ResourceClient struct {
*ComputeClient
ResourceDescription string
ContainerPath string
ResourceRootPath string
}
func (c *ResourceClient) createResource(requestBody interface{}, responseBody interface{}) error {
resp, err := c.executeRequest("POST", c.ContainerPath, requestBody)
if err != nil {
return err
}
return c.unmarshalResponseBody(resp, responseBody)
}
func (c *ResourceClient) updateResource(name string, requestBody interface{}, responseBody interface{}) error {
resp, err := c.executeRequest("PUT", c.getObjectPath(c.ResourceRootPath, name), requestBody)
if err != nil {
return err
}
return c.unmarshalResponseBody(resp, responseBody)
}
func (c *ResourceClient) getResource(name string, responseBody interface{}) error {
var objectPath string
if name != "" {
objectPath = c.getObjectPath(c.ResourceRootPath, name)
} else {
objectPath = c.ResourceRootPath
}
resp, err := c.executeRequest("GET", objectPath, nil)
if err != nil {
return err
}
return c.unmarshalResponseBody(resp, responseBody)
}
func (c *ResourceClient) deleteResource(name string) error {
var objectPath string
if name != "" {
objectPath = c.getObjectPath(c.ResourceRootPath, name)
} else {
objectPath = c.ResourceRootPath
}
_, err := c.executeRequest("DELETE", objectPath, nil)
if err != nil {
return err
}
// No errors and no response body to write
return nil
}
func (c *ResourceClient) deleteOrchestration(name string) error {
var objectPath string
if name != "" {
objectPath = c.getObjectPath(c.ResourceRootPath, name)
} else {
objectPath = c.ResourceRootPath
}
// Set terminate to true as we always want to delete an orchestration
objectPath = fmt.Sprintf("%s?terminate=True", objectPath)
_, err := c.executeRequest("DELETE", objectPath, nil)
if err != nil {
return err
}
// No errors and no response body to write
return nil
}
func (c *ResourceClient) unmarshalResponseBody(resp *http.Response, iface interface{}) error {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
c.client.DebugLogString(fmt.Sprintf("HTTP Resp (%d): %s", resp.StatusCode, buf.String()))
// JSON decode response into interface
var tmp interface{}
dcd := json.NewDecoder(buf)
if err := dcd.Decode(&tmp); err != nil {
return err
}
// Use mapstructure to weakly decode into the resulting interface
msdcd, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: iface,
TagName: "json",
})
if err != nil {
return err
}
if err := msdcd.Decode(tmp); err != nil {
return err
}
return nil
}