Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Payload for test service keys * Service keys * Corrected indentation * Payload for additional service key tests * Additonal test cases for service keys
- Loading branch information
Showing
3 changed files
with
265 additions
and
0 deletions.
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,87 @@ | ||
package cfclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/url" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ServiceKeysResponse struct { | ||
Count int `json:"total_results"` | ||
Pages int `json:"total_pages"` | ||
Resources []ServiceKeyResource `json:"resources"` | ||
} | ||
|
||
type ServiceKeyResource struct { | ||
Meta Meta `json:"metadata"` | ||
Entity ServiceKey `json:"entity"` | ||
} | ||
|
||
type ServiceKey struct { | ||
Name string `json:"name"` | ||
Guid string `json:"guid"` | ||
ServiceInstanceGuid string `json:"service_instance_guid"` | ||
Credentials interface{} `json:"credentials"` | ||
ServiceInstanceUrl string `json:"service_instance_url"` | ||
c *Client | ||
} | ||
|
||
func (c *Client) ListServiceKeysByQuery(query url.Values) ([]ServiceKey, error) { | ||
var serviceKeys []ServiceKey | ||
var serviceKeysResp ServiceKeysResponse | ||
r := c.NewRequest("GET", "/v2/service_keys?"+query.Encode()) | ||
resp, err := c.DoRequest(r) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error requesting service keys") | ||
} | ||
resBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error reading service keys request:") | ||
} | ||
|
||
err = json.Unmarshal(resBody, &serviceKeysResp) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error unmarshaling service keys") | ||
} | ||
for _, serviceKey := range serviceKeysResp.Resources { | ||
serviceKey.Entity.Guid = serviceKey.Meta.Guid | ||
serviceKey.Entity.c = c | ||
serviceKeys = append(serviceKeys, serviceKey.Entity) | ||
} | ||
return serviceKeys, nil | ||
} | ||
|
||
func (c *Client) ListServiceKeys() ([]ServiceKey, error) { | ||
return c.ListServiceKeysByQuery(nil) | ||
} | ||
|
||
func (c *Client) GetServiceKeyByName(name string) (ServiceKey, error) { | ||
var serviceKey ServiceKey | ||
q := url.Values{} | ||
q.Set("q", "name:"+name) | ||
serviceKeys, err := c.ListServiceKeysByQuery(q) | ||
if err != nil { | ||
return serviceKey, err | ||
} | ||
if len(serviceKeys) == 0 { | ||
return serviceKey, fmt.Errorf("Unable to find service key %s", name) | ||
} | ||
return serviceKeys[0], nil | ||
} | ||
|
||
func (c *Client) GetServiceKeyByInstanceGuid(guid string) (ServiceKey, error) { | ||
var serviceKey ServiceKey | ||
q := url.Values{} | ||
q.Set("q", "service_instance_guid:"+guid) | ||
serviceKeys, err := c.ListServiceKeysByQuery(q) | ||
if err != nil { | ||
return serviceKey, err | ||
} | ||
if len(serviceKeys) == 0 { | ||
return serviceKey, fmt.Errorf("Unable to find service key for guid %s", guid) | ||
} | ||
return serviceKeys[0], 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,81 @@ | ||
package cfclient | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestListServiceKeys(t *testing.T) { | ||
Convey("List Service Keys", t, func() { | ||
setup(MockRoute{"GET", "/v2/service_keys", listServiceKeysPayload, "", 200, "", nil}, t) | ||
defer teardown() | ||
c := &Config{ | ||
ApiAddress: server.URL, | ||
Token: "foobar", | ||
} | ||
client, err := NewClient(c) | ||
So(err, ShouldBeNil) | ||
|
||
serviceKeys, err := client.ListServiceKeys() | ||
So(err, ShouldBeNil) | ||
|
||
So(len(serviceKeys), ShouldEqual, 2) | ||
So(serviceKeys[0].Guid, ShouldEqual, "3b933598-64ed-4613-a0f5-b7e8c0379368") | ||
So(serviceKeys[0].Name, ShouldEqual, "RedisMonitoringKey") | ||
So(serviceKeys[0].ServiceInstanceGuid, ShouldEqual, "ad98f310-a3a0-47aa-9116-f8295d41a9b2") | ||
So(serviceKeys[0].Credentials, ShouldNotEqual, nil) | ||
So(serviceKeys[0].ServiceInstanceUrl, ShouldEqual, "/v2/service_instances/ad98f310-a3a0-47aa-9116-f8295d41a9b2") | ||
So(serviceKeys[1].Guid, ShouldEqual, "8be3911b-c621-4467-8866-f8b924aaee57") | ||
So(serviceKeys[1].Name, ShouldEqual, "test01_key") | ||
So(serviceKeys[1].ServiceInstanceGuid, ShouldEqual, "ecf26687-e176-4784-b181-b3c942fecb62") | ||
So(serviceKeys[1].Credentials, ShouldNotEqual, nil) | ||
m := serviceKeys[1].Credentials.(map[string]interface{}) | ||
So(m["uri"], ShouldEqual, "nhp://100.100.100.100:9008") | ||
So(serviceKeys[1].ServiceInstanceUrl, ShouldEqual, "/v2/service_instances/fcf26687-e176-4784-b181-b3c942fecb62") | ||
}) | ||
} | ||
|
||
func TestGetServiceKeyByName(t *testing.T) { | ||
Convey("Get service key by name", t, func() { | ||
setup(MockRoute{"GET", "/v2/service_keys", getServiceKeyPayload, "", 200, "q=name:test01_key", nil}, t) | ||
defer teardown() | ||
c := &Config{ | ||
ApiAddress: server.URL, | ||
Token: "foobar", | ||
} | ||
client, err := NewClient(c) | ||
So(err, ShouldBeNil) | ||
|
||
serviceKey, err := client.GetServiceKeyByName("test01_key") | ||
So(err, ShouldBeNil) | ||
|
||
So(serviceKey, ShouldNotBeNil) | ||
So(serviceKey.Name, ShouldEqual, "test01_key") | ||
So(serviceKey.ServiceInstanceGuid, ShouldEqual, "ecf26687-e176-4784-b181-b3c942fecb62") | ||
So(serviceKey.Credentials, ShouldNotEqual, nil) | ||
So(serviceKey.ServiceInstanceUrl, ShouldEqual, "/v2/service_instances/fcf26687-e176-4784-b181-b3c942fecb62") | ||
}) | ||
} | ||
|
||
func TestGetServiceKeyByGuid(t *testing.T) { | ||
Convey("Get service key by guid", t, func() { | ||
setup(MockRoute{"GET", "/v2/service_keys", getServiceKeyPayload, "", 200, "q=service_instance_guid:ecf26687-e176-4784-b181-b3c942fecb62", nil}, t) | ||
defer teardown() | ||
c := &Config{ | ||
ApiAddress: server.URL, | ||
Token: "foobar", | ||
} | ||
client, err := NewClient(c) | ||
So(err, ShouldBeNil) | ||
|
||
serviceKey, err := client.GetServiceKeyByInstanceGuid("ecf26687-e176-4784-b181-b3c942fecb62") | ||
So(err, ShouldBeNil) | ||
|
||
So(serviceKey, ShouldNotBeNil) | ||
So(serviceKey.Name, ShouldEqual, "test01_key") | ||
So(serviceKey.ServiceInstanceGuid, ShouldEqual, "ecf26687-e176-4784-b181-b3c942fecb62") | ||
So(serviceKey.Credentials, ShouldNotEqual, nil) | ||
So(serviceKey.ServiceInstanceUrl, ShouldEqual, "/v2/service_instances/fcf26687-e176-4784-b181-b3c942fecb62") | ||
}) | ||
} |