Skip to content

Commit 8a9f2f2

Browse files
committed
chore(controller-sdk-go): add resource service api
1 parent dbf56da commit 8a9f2f2

3 files changed

Lines changed: 162 additions & 9 deletions

File tree

api/resource.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
package api
22

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"`
3+
// ResourcePlan is the structure of an app's resource plan.
4+
type ResourcePlan struct {
5+
// ID is a unique string for resource plan.
6+
ID string `json:"id,omitempty"`
7+
// Name is a unique string for resource plan.
8+
Name string `json:"name,omitempty"`
9+
// Description is a detailed description of the resource plan
10+
Description string `json:"description,omitempty"`
11+
}
12+
13+
type ResourcePlans []ResourcePlan
14+
15+
// ResourceService is the structure of an app's resource service.
16+
type ResourceService struct {
17+
// ID is a unique string for resource service.
18+
ID string `json:"id,omitempty"`
19+
// Name is a unique string for resource service.
20+
Name string `json:"name,omitempty"`
21+
// Updatable is the plans of the current resource can be upgraded
22+
Updateable bool `json:"updateable,omitempty"`
623
}
724

25+
type ResourceServices []ResourceService
26+
827
// Resource is the structure of an app's resource.
928
type Resource struct {
1029
// Owner is the app owner.
@@ -33,3 +52,8 @@ type Resource struct {
3352
}
3453

3554
type Resources []Resource
55+
56+
// Binding is the definition of PATCH /v2/apps/<app_id>/resources/<name>/binding/.
57+
type ResourceBinding struct {
58+
BindAction string `json:"bind_action,omitempty"`
59+
}

resources/resources.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,39 @@ package resources
44
import (
55
"encoding/json"
66
"fmt"
7+
78
drycc "github.com/drycc/controller-sdk-go"
89
"github.com/drycc/controller-sdk-go/api"
910
)
1011

12+
// Services is list all available resource services
13+
func Services(c *drycc.Client, results int) (api.ResourceServices, int, error) {
14+
u := fmt.Sprintf("/v2/resources/services/")
15+
body, count, reqErr := c.LimitedRequest(u, results)
16+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
17+
return []api.ResourceService{}, -1, reqErr
18+
}
19+
var services []api.ResourceService
20+
if err := json.Unmarshal([]byte(body), &services); err != nil {
21+
return []api.ResourceService{}, -1, err
22+
}
23+
return services, count, reqErr
24+
}
25+
26+
// Plans is list all available resource services
27+
func Plans(c *drycc.Client, serviceName string, results int) (api.ResourcePlans, int, error) {
28+
u := fmt.Sprintf("/v2/resources/services/%s/plans/", serviceName)
29+
body, count, reqErr := c.LimitedRequest(u, results)
30+
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
31+
return []api.ResourcePlan{}, -1, reqErr
32+
}
33+
var plans []api.ResourcePlan
34+
if err := json.Unmarshal([]byte(body), &plans); err != nil {
35+
return []api.ResourcePlan{}, -1, err
36+
}
37+
return plans, count, reqErr
38+
}
39+
1140
// List list an app's resources.
1241
func List(c *drycc.Client, appID string, results int) (api.Resources, int, error) {
1342
u := fmt.Sprintf("/v2/apps/%s/resources/", appID)
@@ -86,7 +115,7 @@ func Put(c *drycc.Client, appID string, name string, resource api.Resource) (api
86115
}
87116

88117
// Binding servicebinding binding with a serviceinstance
89-
func Binding(c *drycc.Client, appID string, name string, resource api.Binding) (api.Resource, error) {
118+
func Binding(c *drycc.Client, appID string, name string, resource api.ResourceBinding) (api.Resource, error) {
90119
body, err := json.Marshal(resource)
91120
if err != nil {
92121
return api.Resource{}, err

resources/resources_test.go

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,42 @@ package resources
22

33
import (
44
"fmt"
5-
drycc "github.com/drycc/controller-sdk-go"
6-
"github.com/drycc/controller-sdk-go/api"
75
"io/ioutil"
86
"net/http"
97
"net/http/httptest"
108
"reflect"
119
"testing"
10+
11+
drycc "github.com/drycc/controller-sdk-go"
12+
"github.com/drycc/controller-sdk-go/api"
1213
)
1314

15+
const resourceServices string = `
16+
{
17+
"results": [
18+
{
19+
"id": "332588e0-6c2c-4f56-a6af-a56fd01ec4b4",
20+
"name": "mysql",
21+
"updateable": true
22+
}
23+
],
24+
"count": 1
25+
}
26+
`
27+
28+
const resourceServicePlans string = `
29+
{
30+
"results": [
31+
{
32+
"id": "4d1dbd33-201b-45bc-9abb-757584ef7ab8",
33+
"name": "standard-1600",
34+
"description": "mysql standard-1600 plan which limit persistence size 1600Gi."
35+
}
36+
],
37+
"count": 1
38+
}
39+
`
40+
1441
const resourceCreateExpected string = `{"name":"mysql","plan":"mysql:5.6"}`
1542

1643
const resourcePutExpected string = `{"plan":"mysql:5.7"}`
@@ -126,7 +153,18 @@ type fakeHTTPServer struct{}
126153

127154
func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
128155
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
129-
156+
// Services
157+
if req.URL.Path == "/v2/resources/services/" && req.Method == "GET" {
158+
res.WriteHeader(http.StatusCreated)
159+
res.Write([]byte(resourceServices))
160+
return
161+
}
162+
// Plans
163+
if req.URL.Path == "/v2/resources/services/mysql/plans/" && req.Method == "GET" {
164+
res.WriteHeader(http.StatusCreated)
165+
res.Write([]byte(resourceServicePlans))
166+
return
167+
}
130168
// Create
131169
if req.URL.Path == "/v2/apps/example-go/resources/" && req.Method == "POST" {
132170
body, err := ioutil.ReadAll(req.Body)
@@ -267,6 +305,68 @@ func TestResourcesCreate(t *testing.T) {
267305
}
268306
}
269307

308+
func TestServices(t *testing.T) {
309+
t.Parallel()
310+
311+
expected := api.ResourceServices{
312+
{
313+
ID: "332588e0-6c2c-4f56-a6af-a56fd01ec4b4",
314+
Name: "mysql",
315+
Updateable: true,
316+
},
317+
}
318+
319+
handler := fakeHTTPServer{}
320+
server := httptest.NewServer(&handler)
321+
defer server.Close()
322+
323+
drycc, err := drycc.New(false, server.URL, "abc")
324+
if err != nil {
325+
t.Fatal(err)
326+
}
327+
328+
actual, _, err := Services(drycc, 100)
329+
330+
if err != nil {
331+
t.Fatal(err)
332+
}
333+
334+
if !reflect.DeepEqual(expected, actual) {
335+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
336+
}
337+
}
338+
339+
func TestServicePlans(t *testing.T) {
340+
t.Parallel()
341+
342+
expected := api.ResourcePlans{
343+
{
344+
ID: "4d1dbd33-201b-45bc-9abb-757584ef7ab8",
345+
Name: "standard-1600",
346+
Description: "mysql standard-1600 plan which limit persistence size 1600Gi.",
347+
},
348+
}
349+
350+
handler := fakeHTTPServer{}
351+
server := httptest.NewServer(&handler)
352+
defer server.Close()
353+
354+
drycc, err := drycc.New(false, server.URL, "abc")
355+
if err != nil {
356+
t.Fatal(err)
357+
}
358+
359+
actual, _, err := Plans(drycc, "mysql", 100)
360+
361+
if err != nil {
362+
t.Fatal(err)
363+
}
364+
365+
if !reflect.DeepEqual(expected, actual) {
366+
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
367+
}
368+
}
369+
270370
//
271371
func TestResourcesList(t *testing.T) {
272372
t.Parallel()
@@ -426,7 +526,7 @@ func TestResourceBind(t *testing.T) {
426526
Updated: "2020-09-08T00:00:00UTC",
427527
}
428528

429-
resourceVars := api.Binding{
529+
resourceVars := api.ResourceBinding{
430530
BindAction: "bind",
431531
}
432532
actual, err := Binding(drycc, "example-bind", "mysql", resourceVars)
@@ -466,7 +566,7 @@ func TestResourceUnbind(t *testing.T) {
466566
Updated: "2020-09-08T00:00:00UTC",
467567
}
468568

469-
resourceVars := api.Binding{
569+
resourceVars := api.ResourceBinding{
470570
BindAction: "unbind",
471571
}
472572
actual, err := Binding(drycc, "example-unbind", "mysql", resourceVars)

0 commit comments

Comments
 (0)