Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SubscriptionIdsForCluster() method. #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/actions/subscriptions/add_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var _ = Describe("AddSubscription", func() {
})
})

Describe("AddSubcription", func() {
Describe("AddSubscription", func() {

var (
addSubscriptionResponse AddSubscriptionResponse
Expand Down
1 change: 1 addition & 0 deletions client/actions/subscriptions/subscription_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SubscriptionService interface {
SetSubscription(orgID string, subscriptionUuid string, versionUuid string) (*SetSubscriptionResponseDataDetails, error)
RemoveSubscription(orgID, uuid string) (*RemoveSubscriptionResponseDataDetails, error)
Subscriptions(orgID string) (types.SubscriptionList, error)
SubscriptionIdsForCluster(orgID string, clusterID string) ([]string, error)
}

// Client is an implementation of a satcon client.
Expand Down
73 changes: 73 additions & 0 deletions client/actions/subscriptions/subscription_ids_for_cluster.go
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 client/actions/subscriptions/subscription_ids_for_cluster_test.go
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())
})
})

})

})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,7 @@ type User struct {
Email string `json:"email,omitempty"`
Role string `json:"role,omitempty"`
}

type UuidOnly struct {
UUID string `json:"uuid,omitempty"`
}
Loading