-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENG-22395] - Add Resource Handler Client (#7)
* [ENG-22395] - Add Resource Handler Client * [ENG-22395] - Added Logic to handle 404 for get by ID
- Loading branch information
Showing
3 changed files
with
202 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cbclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type CloudBoltResourceHandlerResult struct { | ||
CloudBoltResult | ||
Embedded struct { | ||
ResourceHandlers []CloudBoltReferenceFields `json:"resourceHandlers"` | ||
} `json:"_embedded"` | ||
} | ||
|
||
// GetResourceHandler accepts the name of a Resource Handler | ||
// | ||
func (c *CloudBoltClient) GetResourceHandler(name string) (*CloudBoltReferenceFields, error) { | ||
apiurl := c.baseURL | ||
apiurl.Path = c.apiEndpoint("resourceHandlers") | ||
apiurl.RawQuery = fmt.Sprintf(filterByName, url.QueryEscape(name)) | ||
|
||
resp, err := c.makeRequest("GET", apiurl.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// TODO: HTTP Response handling | ||
|
||
// We Decode the data because we already have an io.Reader on hand | ||
var res CloudBoltResourceHandlerResult | ||
json.NewDecoder(resp.Body).Decode(&res) | ||
|
||
// TODO: Sanity check the decoded object | ||
if len(res.Embedded.ResourceHandlers) == 0 { | ||
return nil, fmt.Errorf( | ||
"Could not find resource handler with name %s. Does the user have permission to view this?", | ||
name, | ||
) | ||
} | ||
return &res.Embedded.ResourceHandlers[0], nil | ||
} | ||
|
||
func (c *CloudBoltClient) GetResourceHandlerById(id string) (*CloudBoltReferenceFields, error) { | ||
apiurl := c.baseURL | ||
apiurl.Path = c.apiEndpoint( | ||
"resourceHandlers", | ||
id, | ||
) | ||
|
||
resp, err := c.makeRequest("GET", apiurl.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// TODO: HTTP Response handling | ||
if resp.StatusCode == 404 { | ||
return nil, fmt.Errorf( | ||
"Could not find resource handler with ID %s. Does the user have permission to view this?", | ||
id, | ||
) | ||
} | ||
|
||
// We Decode the data because we already have an io.Reader on hand | ||
var res CloudBoltReferenceFields | ||
json.NewDecoder(resp.Body).Decode(&res) | ||
|
||
return &res, 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,75 @@ | ||
package cbclient | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGetResourceHandler(t *testing.T) { | ||
// Register the test with gomega | ||
RegisterTestingT(t) | ||
|
||
// Setup mock server with scripted responses | ||
// Setup requests buffer | ||
server, requests := mockServer(responsesForResourceHandler) | ||
Expect(server).NotTo(BeNil()) | ||
Expect(requests).NotTo(BeNil()) | ||
|
||
// Setup CloudBolt Client | ||
client := getClient(server) | ||
Expect(client).NotTo(BeNil()) | ||
|
||
rhName := "My Test Resource Handler" | ||
rh, err := client.GetResourceHandler(rhName) | ||
Expect(rh).NotTo(BeNil()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// This should have made three requests: | ||
// 1+2. Fail to get RH, get a token | ||
// 3. Successfully getting the order | ||
Expect(len(*requests)).To(Equal(3)) | ||
|
||
// The last request is the one we care about | ||
Expect((*requests)[2].URL.Path).To(Equal("/api/v3/cmp/resourceHandlers/")) | ||
|
||
// The CloudBolt Order object should be parsed correctly | ||
Expect(rh.Links.Self.Href).To(Equal("/api/v3/cmp/resourceHandlers/RH-nza16uyn/")) | ||
Expect(rh.Links.Self.Title).To(Equal("My Test Resource Handler")) | ||
Expect(rh.Name).To(Equal("My Test Resource Handler")) | ||
Expect(rh.ID).To(Equal("RH-nza16uyn")) | ||
} | ||
|
||
func TestGetResourceHandlerById(t *testing.T) { | ||
// Register the test with gomega | ||
RegisterTestingT(t) | ||
|
||
// Setup mock server with scripted responses | ||
// Setup requests buffer | ||
server, requests := mockServer(responsesForResourceHandlerById) | ||
Expect(server).NotTo(BeNil()) | ||
Expect(requests).NotTo(BeNil()) | ||
|
||
// Setup CloudBolt Client | ||
client := getClient(server) | ||
Expect(client).NotTo(BeNil()) | ||
|
||
rhId := "RH-nza16uyn" | ||
rh, err := client.GetResourceHandlerById(rhId) | ||
Expect(rh).NotTo(BeNil()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// This should have made three requests: | ||
// 1+2. Fail to get order, get a token | ||
// 3. Successfully getting the order | ||
Expect(len(*requests)).To(Equal(3)) | ||
|
||
// The last request is the one we care about | ||
Expect((*requests)[2].URL.Path).To(Equal("/api/v3/cmp/resourceHandlers/RH-nza16uyn/")) | ||
|
||
// The CloudBolt Order object should be parsed correctly | ||
Expect(rh.Links.Self.Href).To(Equal("/api/v3/cmp/resourceHandlers/RH-nza16uyn/")) | ||
Expect(rh.Links.Self.Title).To(Equal("My Test Resource Handler")) | ||
Expect(rh.Name).To(Equal("My Test Resource Handler")) | ||
Expect(rh.ID).To(Equal("RH-nza16uyn")) | ||
} |
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,60 @@ | ||
package cbclient | ||
|
||
const aResourceHandlerList string = `{ | ||
"_links": { | ||
"self": { | ||
"href": "/api/v3/cmp/resourceHandlers/?page=1&filter=name%3AMy+Test+Resource+HAndler", | ||
"title": "List of Resource Handlers - Page 1 of 1" | ||
} | ||
}, | ||
"total": 1, | ||
"count": 1, | ||
"_embedded": { | ||
"resourceHandlers": [ | ||
{ | ||
"_links": { | ||
"self": { | ||
"href": "/api/v3/cmp/resourceHandlers/RH-nza16uyn/", | ||
"title": "My Test Resource Handler" | ||
} | ||
}, | ||
"name": "My Test Resource Handler", | ||
"id": "RH-nza16uyn", | ||
"type": "AWS resource handler" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
const aResourceHandler = `{ | ||
"_links": { | ||
"self": { | ||
"href": "/api/v3/cmp/resourceHandlers/RH-nza16uyn/", | ||
"title": "My Test Resource Handler" | ||
} | ||
}, | ||
"name": "My Test Resource Handler", | ||
"id": "RH-nza16uyn", | ||
"type": "AWS resource handler" | ||
}` | ||
|
||
func responsesForResourceHandler(i int) (string, int) { | ||
return bodyForGetResourceHandler(i), missingTokenStatusPattern(i) | ||
} | ||
|
||
func bodyForGetResourceHandler(i int) string { | ||
return missingTokenBodyPattern( | ||
aResourceHandlerList, | ||
)[i] | ||
} | ||
|
||
func responsesForResourceHandlerById(i int) (string, int) { | ||
return bodyForGetResourceHandlerById(i), missingTokenStatusPattern(i) | ||
} | ||
|
||
func bodyForGetResourceHandlerById(i int) string { | ||
return missingTokenBodyPattern( | ||
aResourceHandler, | ||
)[i] | ||
} |