Skip to content

Commit

Permalink
Add SubscriptionIdsForCluster() method. (#42)
Browse files Browse the repository at this point in the history
* Add SubscriptionIdsForCluster().

* fix missing EOL at end of file

* Remove test focus
  • Loading branch information
steven-collins-omega authored Aug 9, 2021
1 parent a71274e commit 9d4f411
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 68 deletions.
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

0 comments on commit 9d4f411

Please sign in to comment.