-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SubscriptionIdsForCluster() method. (#42)
* Add SubscriptionIdsForCluster(). * fix missing EOL at end of file * Remove test focus
- Loading branch information
1 parent
a71274e
commit 9d4f411
Showing
6 changed files
with
442 additions
and
68 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
73 changes: 73 additions & 0 deletions
73
client/actions/subscriptions/subscription_ids_for_cluster.go
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,73 @@ | ||
package subscriptions | ||
|
||
import ( | ||
"github.com/IBM/satcon-client-go/client/actions" | ||
"github.com/IBM/satcon-client-go/client/types" | ||
) | ||
|
||
const ( | ||
//QuerySubscriptionIdsForCluster specifies the query | ||
QuerySubscriptionIdsForCluster = "subscriptionsForCluster" | ||
//SubscriptionIdsForClusterVarTemplate is the template used to create the graphql query | ||
SubscriptionIdsForClusterVarTemplate = `{{define "vars"}}"orgId":{{json .OrgID}},"clusterId":{{json .ClusterID}}{{end}}` | ||
) | ||
|
||
//SubscriptionIdsForClusterVariables are the variables used for the subscription query | ||
type SubscriptionIdsForClusterVariables struct { | ||
actions.GraphQLQuery | ||
OrgID string | ||
ClusterID string | ||
} | ||
|
||
//NewSubscriptionIdsForClusterVariables generates variables used for query | ||
func NewSubscriptionIdsForClusterVariables(orgID string, clusterID string) SubscriptionIdsForClusterVariables { | ||
vars := SubscriptionIdsForClusterVariables{ | ||
OrgID: orgID, | ||
ClusterID: clusterID, | ||
} | ||
|
||
vars.Type = actions.QueryTypeQuery | ||
vars.QueryName = QuerySubscriptionIdsForCluster | ||
vars.Args = map[string]string{ | ||
"orgId": "String!", | ||
"clusterId": "String!", | ||
} | ||
vars.Returns = []string{ | ||
"uuid", | ||
} | ||
|
||
return vars | ||
} | ||
|
||
//SubscriptionIdsForClusterResponse response from query | ||
type SubscriptionIdsForClusterResponse struct { | ||
Data *SubscriptionIdsForClusterResponseData `json:"data,omitempty"` | ||
} | ||
|
||
// SubscriptionIdsForClusterResponseData data response from query | ||
type SubscriptionIdsForClusterResponseData struct { | ||
SubscriptionIdsForCluster []types.UuidOnly `json:"subscriptionsForCluster,omitempty"` | ||
} | ||
|
||
func (c *Client) SubscriptionIdsForCluster(orgID string, clusterID string) ([]string, error) { | ||
var response SubscriptionIdsForClusterResponse | ||
|
||
vars := NewSubscriptionIdsForClusterVariables(orgID, clusterID) | ||
|
||
err := c.DoQuery(SubscriptionIdsForClusterVarTemplate, vars, nil, &response) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if response.Data != nil { | ||
uuidResponses := response.Data.SubscriptionIdsForCluster | ||
uuids := make([]string, len(uuidResponses)) | ||
for i := 0; i < len(uuidResponses); i++ { | ||
uuids[i] = uuidResponses[i].UUID | ||
} | ||
return uuids, nil | ||
} | ||
|
||
return nil, err | ||
} |
126 changes: 126 additions & 0 deletions
126
client/actions/subscriptions/subscription_ids_for_cluster_test.go
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,126 @@ | ||
package subscriptions_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/IBM/satcon-client-go/client/actions" | ||
. "github.com/IBM/satcon-client-go/client/actions/subscriptions" | ||
"github.com/IBM/satcon-client-go/client/auth/authfakes" | ||
"github.com/IBM/satcon-client-go/client/types" | ||
"github.com/IBM/satcon-client-go/client/web/webfakes" | ||
) | ||
|
||
var _ = Describe("SubscriptionIdsForCluster", func() { | ||
|
||
var ( | ||
orgID, clusterID string | ||
fakeAuthClient authfakes.FakeAuthClient | ||
) | ||
|
||
BeforeEach(func() { | ||
orgID = "project-jupiter" | ||
clusterID = "some-cluster" | ||
}) | ||
|
||
Describe("NewSubscriptionIdsForClusterVariables", func() { | ||
It("Returns a correctly populated instance of SubscriptionIdsForClusterVariables", func() { | ||
vars := NewSubscriptionIdsForClusterVariables(orgID, clusterID) | ||
Expect(vars.Type).To(Equal(actions.QueryTypeQuery)) | ||
Expect(vars.QueryName).To(Equal(QuerySubscriptionIdsForCluster)) | ||
Expect(vars.OrgID).To(Equal(orgID)) | ||
Expect(vars.Args).To(Equal(map[string]string{ | ||
"orgId": "String!", | ||
"clusterId": "String!", | ||
})) | ||
Expect(vars.Returns).To(ConsistOf( | ||
"uuid", | ||
)) | ||
}) | ||
}) | ||
|
||
Describe("SubscriptionIdsForCluster", func() { | ||
|
||
var ( | ||
subscriptionsResponse SubscriptionIdsForClusterResponse | ||
subscriptionIds []string | ||
c SubscriptionService | ||
httpClient *webfakes.FakeHTTPClient | ||
response *http.Response | ||
) | ||
|
||
BeforeEach(func() { | ||
|
||
subscriptionIds = []string{ | ||
"hal", | ||
"9000", | ||
"2001", | ||
} | ||
responseDataField := make([]types.UuidOnly, len(subscriptionIds)) | ||
for i := 0; i < len(subscriptionIds); i++ { | ||
responseDataField[i] = types.UuidOnly{UUID: subscriptionIds[i]} | ||
} | ||
subscriptionsResponse = SubscriptionIdsForClusterResponse{ | ||
Data: &SubscriptionIdsForClusterResponseData{ | ||
SubscriptionIdsForCluster: responseDataField, | ||
}, | ||
} | ||
|
||
respBodyBytes, err := json.Marshal(subscriptionsResponse) | ||
Expect(err).NotTo(HaveOccurred()) | ||
response = &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewReader(respBodyBytes)), | ||
} | ||
|
||
httpClient = &webfakes.FakeHTTPClient{} | ||
Expect(httpClient.DoCallCount()).To(Equal(0)) | ||
httpClient.DoReturns(response, nil) | ||
|
||
c, _ = NewClient("https://foo.bar", httpClient, &fakeAuthClient) | ||
|
||
}) | ||
|
||
It("Makes a valid http request", func() { | ||
_, err := c.SubscriptionIdsForCluster(orgID, clusterID) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(httpClient.DoCallCount()).To(Equal(1)) | ||
}) | ||
|
||
It("Returns the list of subscriptions", func() { | ||
subscriptions, _ := c.SubscriptionIdsForCluster(orgID, clusterID) | ||
Expect(subscriptions).To(Equal(subscriptionIds)) | ||
}) | ||
|
||
Context("When query execution errors", func() { | ||
BeforeEach(func() { | ||
httpClient.DoReturns(response, errors.New("None whatsoever, Frank.")) | ||
}) | ||
|
||
It("Bubbles up the error", func() { | ||
_, err := c.SubscriptionIdsForCluster(orgID, clusterID) | ||
Expect(err).To(MatchError(MatchRegexp("None whatsoever, Frank."))) | ||
}) | ||
}) | ||
|
||
Context("When the response is empty for some reason", func() { | ||
BeforeEach(func() { | ||
respBodyBytes, _ := json.Marshal(SubscriptionIdsForClusterResponse{}) | ||
response.Body = ioutil.NopCloser(bytes.NewReader(respBodyBytes)) | ||
}) | ||
|
||
It("Returns nil", func() { | ||
subscriptions, err := c.SubscriptionIdsForCluster(orgID, clusterID) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(subscriptions).To(BeNil()) | ||
}) | ||
}) | ||
|
||
}) | ||
|
||
}) |
81 changes: 81 additions & 0 deletions
81
client/actions/subscriptions/subscriptionsfakes/fake_subscription_service.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.