Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service plan visibilities functionality (#107)
- Loading branch information
Showing
3 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cfclient | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ServicePlanVisibilitiesResponse struct { | ||
Count int `json:"total_results"` | ||
Pages int `json:"total_pages"` | ||
NextUrl string `json:"next_url"` | ||
Resources []ServicePlanVisibilityResource `json:"resources"` | ||
} | ||
|
||
type ServicePlanVisibilityResource struct { | ||
Meta Meta `json:"metadata"` | ||
Entity ServicePlanVisibility `json:"entity"` | ||
} | ||
|
||
type ServicePlanVisibility struct { | ||
Guid string `json:"guid"` | ||
ServicePlanGuid string `json:"service_plan_guid"` | ||
OrganizationGuid string `json:"organization_guid"` | ||
ServicePlanUrl string `json:"service_plan_url"` | ||
OrganizationUrl string `json:"organization_url"` | ||
c *Client | ||
} | ||
|
||
func (c *Client) ListServicePlanVisibilitiesByQuery(query url.Values) ([]ServicePlanVisibility, error) { | ||
var servicePlanVisibilities []ServicePlanVisibility | ||
requestUrl := "/v2/service_plan_visibilities?" + query.Encode() | ||
for { | ||
var servicePlanVisibilitiesResp ServicePlanVisibilitiesResponse | ||
r := c.NewRequest("GET", requestUrl) | ||
resp, err := c.DoRequest(r) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error requesting service plan visibilities") | ||
} | ||
resBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error reading service plan visibilities request:") | ||
} | ||
|
||
err = json.Unmarshal(resBody, &servicePlanVisibilitiesResp) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error unmarshaling service plan visibilities") | ||
} | ||
for _, servicePlanVisibility := range servicePlanVisibilitiesResp.Resources { | ||
servicePlanVisibility.Entity.Guid = servicePlanVisibility.Meta.Guid | ||
servicePlanVisibility.Entity.c = c | ||
servicePlanVisibilities = append(servicePlanVisibilities, servicePlanVisibility.Entity) | ||
} | ||
requestUrl = servicePlanVisibilitiesResp.NextUrl | ||
if requestUrl == "" { | ||
break | ||
} | ||
} | ||
return servicePlanVisibilities, nil | ||
} | ||
|
||
func (c *Client) ListServicePlanVisibilities() ([]ServicePlanVisibility, error) { | ||
return c.ListServicePlanVisibilitiesByQuery(nil) | ||
} | ||
|
||
func (c *Client) CreateServicePlanVisibility(servicePlanGuid string, organizationGuid string) (*ServicePlanVisibility, error) { | ||
req := c.NewRequest("POST", "/v2/service_plan_visibilities") | ||
req.obj = map[string]interface{}{ | ||
"service_plan_guid": servicePlanGuid, | ||
"organization_guid": organizationGuid, | ||
} | ||
resp, err := c.DoRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != http.StatusCreated { | ||
return nil, errors.Wrapf(err, "Error creating service plan visibility, response code: %d", resp.StatusCode) | ||
} | ||
return respBodyToServicePlanVisibility(resp.Body, c) | ||
} | ||
|
||
func respBodyToServicePlanVisibility(body io.ReadCloser, c *Client) (*ServicePlanVisibility, error) { | ||
bodyRaw, err := ioutil.ReadAll(body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
servicePlanVisibilityRes := ServicePlanVisibilityResource{} | ||
err = json.Unmarshal([]byte(bodyRaw), &servicePlanVisibilityRes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
servicePlanVisibility := servicePlanVisibilityRes.Entity | ||
servicePlanVisibility.Guid = servicePlanVisibilityRes.Meta.Guid | ||
servicePlanVisibility.c = c | ||
return &servicePlanVisibility, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cfclient | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestListServicePlanVisibilities(t *testing.T) { | ||
Convey("List service plan visibilities", t, func() { | ||
setup(MockRoute{"GET", "/v2/service_plan_visibilities", listServicePlanVisibilitiesPayload, "", 200, "", nil}, t) | ||
defer teardown() | ||
c := &Config{ | ||
ApiAddress: server.URL, | ||
Token: "foobar", | ||
} | ||
client, err := NewClient(c) | ||
So(err, ShouldBeNil) | ||
|
||
servicePlanVisibilities, err := client.ListServicePlanVisibilities() | ||
So(err, ShouldBeNil) | ||
|
||
So(len(servicePlanVisibilities), ShouldEqual, 2) | ||
So(servicePlanVisibilities[0].Guid, ShouldEqual, "d1b5ea55-f354-4f43-b52e-53045747adb9") | ||
So(servicePlanVisibilities[0].ServicePlanGuid, ShouldEqual, "62cb572c-e9ca-4c9f-b822-8292db1d9a96") | ||
So(servicePlanVisibilities[0].OrganizationGuid, ShouldEqual, "81df84f3-8ce0-4c92-990a-3760b6ff66bd") | ||
So(servicePlanVisibilities[0].ServicePlanUrl, ShouldEqual, "/v2/service_plans/62cb572c-e9ca-4c9f-b822-8292db1d9a96") | ||
So(servicePlanVisibilities[0].OrganizationUrl, ShouldEqual, "/v2/organizations/81df84f3-8ce0-4c92-990a-3760b6ff66bd") | ||
}) | ||
} | ||
|
||
func TestCreateServicePlanVisibility(t *testing.T) { | ||
Convey("Create service plan visibility", t, func() { | ||
setup(MockRoute{"POST", "/v2/service_plan_visibilities", postServicePlanVisibilityPayload, "", 201, "", nil}, t) | ||
defer teardown() | ||
c := &Config{ | ||
ApiAddress: server.URL, | ||
Token: "foobar", | ||
} | ||
client, err := NewClient(c) | ||
So(err, ShouldBeNil) | ||
|
||
servicePlanVisibility, err := client.CreateServicePlanVisibility("ab5780a9-ac8e-4412-9496-4512e865011a", "55d0ff39-dac9-431f-ba6d-83f37381f1c3") | ||
So(err, ShouldBeNil) | ||
|
||
So(servicePlanVisibility.Guid, ShouldEqual, "f740b01a-4afe-4435-aedd-0a8308a7e7d6") | ||
So(servicePlanVisibility.ServicePlanGuid, ShouldEqual, "ab5780a9-ac8e-4412-9496-4512e865011a") | ||
So(servicePlanVisibility.OrganizationGuid, ShouldEqual, "55d0ff39-dac9-431f-ba6d-83f37381f1c3") | ||
So(servicePlanVisibility.ServicePlanUrl, ShouldEqual, "/v2/service_plans/ab5780a9-ac8e-4412-9496-4512e865011a") | ||
So(servicePlanVisibility.OrganizationUrl, ShouldEqual, "/v2/organizations/55d0ff39-dac9-431f-ba6d-83f37381f1c3") | ||
}) | ||
} |