Skip to content

Commit 77b90b3

Browse files
author
lijianguo
committed
feat(controller):add drycc resource cmd
1 parent 471d3aa commit 77b90b3

5 files changed

Lines changed: 625 additions & 4 deletions

File tree

api/apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type AppUpdateRequest struct {
2828

2929
// AppRunRequest is the definition of POST /v2/apps/<app id>/run.
3030
type AppRunRequest struct {
31-
Command string `json:"command"`
31+
Command string `json:"command"`
3232
Volumes map[string]interface{} `json:"volumes,omitempty"`
3333
}
3434

api/resource.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package api
2+
3+
// Binding is the definition of PATCH /v2/apps/<app_id>/resources/<name>/binding/.
4+
type Binding struct {
5+
BindAction string `json:"bind_action,omitempty"`
6+
}
7+
8+
// Resource is the structure of an app's resource.
9+
type Resource struct {
10+
// Owner is the app owner.
11+
Owner string `json:"owner,omitempty,-"`
12+
// App is the app the tls settings apply to and cannot be updated.
13+
App string `json:"app,omitempty,-"`
14+
// Created is the time that the resource was created and cannot be updated.
15+
Created string `json:"created,omitempty,-"`
16+
// Updated is the last time the TLS settings was changed and cannot be updated.
17+
Updated string `json:"updated,omitempty,-"`
18+
// UUID is a unique string reflecting the resource in its current state.
19+
// It changes every time the resource is changed and cannot be updated.
20+
UUID string `json:"uuid,omitempty,-"`
21+
// Resource's name
22+
Name string `json:"name,omitempty"`
23+
// Resource's Plan
24+
Plan string `json:"plan,omitempty"`
25+
// Resource connet info
26+
Data map[string]interface{} `json:"data,omitempty"`
27+
// Resource's status
28+
Status string `json:"status,omitempty"`
29+
// Resource's binding status
30+
Binding string `json:"binding,omitempty"`
31+
// Resource Options
32+
Options map[string]interface{} `json:"options,omitempty"`
33+
}
34+
35+
type Resources []Resource

resources/resources.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Package config provides methods for managing configuration of apps.
2+
package resources
3+
4+
import (
5+
"encoding/json"
6+
"fmt"
7+
drycc "github.com/drycc/controller-sdk-go"
8+
"github.com/drycc/controller-sdk-go/api"
9+
)
10+
11+
// List list an app's resources.
12+
func List(c *drycc.Client, appID string, results int) (api.Resources, int, error) {
13+
u := fmt.Sprintf("/v2/apps/%s/resources/", appID)
14+
body, count, reqErr := c.LimitedRequest(u, results)
15+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
16+
return []api.Resource{}, -1, reqErr
17+
}
18+
var resources []api.Resource
19+
if err := json.Unmarshal([]byte(body), &resources); err != nil {
20+
return []api.Resource{}, -1, err
21+
}
22+
return resources, count, reqErr
23+
}
24+
25+
// Create create an app's resource.
26+
func Create(c *drycc.Client, appID string, resource api.Resource) (api.Resource, error) {
27+
body, err := json.Marshal(resource)
28+
if err != nil {
29+
return api.Resource{}, err
30+
}
31+
u := fmt.Sprintf("/v2/apps/%s/resources/", appID)
32+
res, reqErr := c.Request("POST", u, body)
33+
if reqErr != nil {
34+
return api.Resource{}, reqErr
35+
}
36+
defer res.Body.Close()
37+
newResource := api.Resource{}
38+
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
39+
return api.Resource{}, err
40+
}
41+
return newResource, reqErr
42+
}
43+
44+
// Get retrieves information about a resource
45+
func Get(c *drycc.Client, appID string, name string) (api.Resource, error) {
46+
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
47+
res, reqErr := c.Request("GET", u, nil)
48+
if reqErr != nil {
49+
return api.Resource{}, reqErr
50+
}
51+
defer res.Body.Close()
52+
resResource := api.Resource{}
53+
if err := json.NewDecoder(res.Body).Decode(&resResource); err != nil {
54+
return api.Resource{}, err
55+
}
56+
return resResource, reqErr
57+
}
58+
59+
// Delete delete an app's resource.
60+
func Delete(c *drycc.Client, appID string, name string) error {
61+
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
62+
res, err := c.Request("DELETE", u, nil)
63+
if err == nil {
64+
res.Body.Close()
65+
}
66+
return err
67+
}
68+
69+
// Put update resource
70+
func Put(c *drycc.Client, appID string, name string, resource api.Resource) (api.Resource, error) {
71+
body, err := json.Marshal(resource)
72+
if err != nil {
73+
return api.Resource{}, err
74+
}
75+
u := fmt.Sprintf("/v2/apps/%s/resources/%s/", appID, name)
76+
res, reqErr := c.Request("PUT", u, body)
77+
if reqErr != nil {
78+
return api.Resource{}, reqErr
79+
}
80+
defer res.Body.Close()
81+
newResource := api.Resource{}
82+
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
83+
return api.Resource{}, err
84+
}
85+
return newResource, reqErr
86+
}
87+
88+
// Binding servicebinding binding with a serviceinstance
89+
func Binding(c *drycc.Client, appID string, name string, resource api.Binding) (api.Resource, error) {
90+
body, err := json.Marshal(resource)
91+
if err != nil {
92+
return api.Resource{}, err
93+
}
94+
u := fmt.Sprintf("/v2/apps/%s/resources/%s/binding/", appID, name)
95+
res, reqErr := c.Request("PATCH", u, body)
96+
if reqErr != nil {
97+
return api.Resource{}, reqErr
98+
}
99+
defer res.Body.Close()
100+
newResource := api.Resource{}
101+
if err = json.NewDecoder(res.Body).Decode(&newResource); err != nil {
102+
return api.Resource{}, err
103+
}
104+
return newResource, reqErr
105+
}

0 commit comments

Comments
 (0)