-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresources.go
More file actions
134 lines (124 loc) · 4.07 KB
/
resources.go
File metadata and controls
134 lines (124 loc) · 4.07 KB
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 config provides methods for managing configuration of apps.
package resources
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// Services is list all available resource services
func Services(c *drycc.Client, results int) (api.ResourceServices, int, error) {
u := "/v2/resources/services/"
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.ResourceService{}, -1, reqErr
}
var services []api.ResourceService
if err := json.Unmarshal([]byte(body), &services); err != nil {
return []api.ResourceService{}, -1, err
}
return services, count, reqErr
}
// Plans is list all available resource services
func Plans(c *drycc.Client, serviceName string, results int) (api.ResourcePlans, int, error) {
u := fmt.Sprintf("/v2/resources/services/%s/plans/", serviceName)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.ResourcePlan{}, -1, reqErr
}
var plans []api.ResourcePlan
if err := json.Unmarshal([]byte(body), &plans); err != nil {
return []api.ResourcePlan{}, -1, err
}
return plans, count, reqErr
}
// List list an app's resources.
func List(c *drycc.Client, appID string, results int) (api.Resources, int, error) {
u := fmt.Sprintf("/v2/apps/%s/resources/", appID)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.Resource{}, -1, reqErr
}
var resources []api.Resource
if err := json.Unmarshal([]byte(body), &resources); err != nil {
return []api.Resource{}, -1, err
}
return resources, count, reqErr
}
// Create create an app's resource.
func Create(c *drycc.Client, appID string, resource api.Resource) (api.Resource, error) {
body, err := json.Marshal(resource)
if err != nil {
return api.Resource{}, err
}
u := fmt.Sprintf("/v2/apps/%s/resources/", appID)
res, reqErr := c.Request("POST", u, body)
if reqErr != nil {
return api.Resource{}, reqErr
}
defer res.Body.Close()
newResource := api.Resource{}
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
return api.Resource{}, err
}
return newResource, reqErr
}
// Get retrieves information about a resource
func Get(c *drycc.Client, appID string, name string) (api.Resource, error) {
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil {
return api.Resource{}, reqErr
}
defer res.Body.Close()
resResource := api.Resource{}
if err := json.NewDecoder(res.Body).Decode(&resResource); err != nil {
return api.Resource{}, err
}
return resResource, reqErr
}
// Delete delete an app's resource.
func Delete(c *drycc.Client, appID string, name string) error {
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}
// Put update resource
func Put(c *drycc.Client, appID string, name string, resource api.Resource) (api.Resource, error) {
body, err := json.Marshal(resource)
if err != nil {
return api.Resource{}, err
}
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
res, reqErr := c.Request("PUT", u, body)
if reqErr != nil {
return api.Resource{}, reqErr
}
defer res.Body.Close()
newResource := api.Resource{}
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
return api.Resource{}, err
}
return newResource, reqErr
}
// Binding servicebinding binding with a serviceinstance
func Binding(c *drycc.Client, appID string, name string, resource api.ResourceBinding) (api.Resource, error) {
body, err := json.Marshal(resource)
if err != nil {
return api.Resource{}, err
}
u := fmt.Sprintf("/v2/apps/%s/resources/%s/binding/", appID, name)
res, reqErr := c.Request("PATCH", u, body)
if reqErr != nil {
return api.Resource{}, reqErr
}
defer res.Body.Close()
newResource := api.Resource{}
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
return api.Resource{}, err
}
return newResource, reqErr
}