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 clusterByName query #20

Merged
merged 3 commits into from
Jan 6, 2021
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: 2 additions & 0 deletions client/actions/clusters/cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type ClusterService interface {
RegisterCluster(string, types.Registration) (*RegisterClusterResponseDataDetails, error)
// ClustersByOrgID lists the clusters registered under the specified organization.
ClustersByOrgID(string) (types.ClusterList, error)
// ClusterByName returns the cluster registered under the specified organization and name.
ClusterByName(string, string) (*types.Cluster, error)
// DeleteClusterByClusterID deletes the specified cluster from the specified org,
// including all resources under that cluster.
DeleteClusterByClusterID(string, string) (*DeleteClustersResponseDataDetails, error)
Expand Down
66 changes: 66 additions & 0 deletions client/actions/clusters/clusters_by_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package clusters

import (
"github.com/IBM/satcon-client-go/client/actions"
"github.com/IBM/satcon-client-go/client/types"
)

const (
QueryClusterByName = "clusterByName"
ClusterByNameVarTemplate = `{{define "vars"}}"orgId":"{{js .OrgID}}","clusterName":"{{js .ClusterName}}"{{end}}`
)

type ClusterByNameVariables struct {
actions.GraphQLQuery
OrgID string
ClusterName string
}

func NewClusterByNameVariables(orgID string, clusterName string) ClusterByNameVariables {
vars := ClusterByNameVariables{
OrgID: orgID,
ClusterName: clusterName,
}

vars.Type = actions.QueryTypeQuery
vars.QueryName = QueryClusterByName
vars.Args = map[string]string{
"orgId": "String!",
"clusterName": "String!",
}
vars.Returns = []string{
"id",
"orgId",
"clusterId",
"name",
"metadata",
}

return vars
}

type ClusterByNameResponse struct {
Data *ClusterByNameResponseData `json:"data,omitempty"`
}

type ClusterByNameResponseData struct {
Cluster *types.Cluster `json:"clusterByName,omitempty"`
}

func (c *Client) ClusterByName(orgID string, clusterName string) (*types.Cluster, error) {
var response ClusterByNameResponse

vars := NewClusterByNameVariables(orgID, clusterName)

err := c.DoQuery(ClusterByNameVarTemplate, vars, nil, &response)

if err != nil {
return nil, err
}

if response.Data != nil {
return response.Data.Cluster, nil
}

return nil, err
}
122 changes: 122 additions & 0 deletions client/actions/clusters/clusters_by_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package clusters_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/clusters"
"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("ClusterByName", func() {
var (
orgID string
clusterName string
fakeAuthClient authfakes.FakeAuthClient
)

BeforeEach(func() {
orgID = "someorg"
clusterName = "somename"
})

Describe("NewClustersByNameVariables", func() {
It("Returns a correctly populated instance of ClustersByNameVariables", func() {
vars := NewClusterByNameVariables(orgID, clusterName)
Expect(vars.Type).To(Equal(actions.QueryTypeQuery))
Expect(vars.QueryName).To(Equal(QueryClusterByName))
Expect(vars.OrgID).To(Equal(orgID))
Expect(vars.ClusterName).To(Equal(clusterName))
Expect(vars.Args).To(Equal(map[string]string{
"orgId": "String!",
"clusterName": "String!",
}))
Expect(vars.Returns).To(ConsistOf(
"id",
"orgId",
"clusterId",
"name",
"metadata",
))
})
})

Describe("ClustersByName", func() {
var (
c ClusterService
httpClient *webfakes.FakeHTTPClient
response *http.Response
clusterResponse ClusterByNameResponse
)

BeforeEach(func() {
clusterResponse = ClusterByNameResponse{
Data: &ClusterByNameResponseData{
&types.Cluster{
ID: "asdf",
OrgID: orgID,
ClusterID: "cluster1",
Name: "cluster1",
},
},
}

respBodyBytes, err := json.Marshal(clusterResponse)
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.ClusterByName(orgID, clusterName)
Expect(err).NotTo(HaveOccurred())
Expect(httpClient.DoCallCount()).To(Equal(1))
})

It("Returns a cluster by name", func() {
clusters, _ := c.ClusterByName(orgID, clusterName)
expected := clusterResponse.Data.Cluster
Expect(clusters).To(Equal(expected))
})

Context("When query execution errors", func() {
BeforeEach(func() {
httpClient.DoReturns(response, errors.New("None whatsoever, Frank."))
})

It("Bubbles up the error", func() {
_, err := c.ClusterByName(orgID, clusterName)
Expect(err).To(MatchError(MatchRegexp("None whatsoever, Frank.")))
})
})

Context("When the response is empty for some reason", func() {
BeforeEach(func() {
respBodyBytes, _ := json.Marshal(ClustersByOrgIDResponse{})
response.Body = ioutil.NopCloser(bytes.NewReader(respBodyBytes))
})

It("Returns nil", func() {
clusters, err := c.ClusterByName(orgID, clusterName)
Expect(err).NotTo(HaveOccurred())
Expect(clusters).To(BeNil())
})
})
})
})
81 changes: 81 additions & 0 deletions client/actions/clusters/clustersfakes/fake_cluster_service.go

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

6 changes: 6 additions & 0 deletions test/integration/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ var _ = Describe("Clusters", func() {
}
Expect(found).To(BeTrue())

cluster, err := c.Clusters.ClusterByName(testConfig.OrgID, clusterName)
Expect(err).NotTo(HaveOccurred())
Expect(cluster).NotTo(BeNil())
Expect(cluster.ClusterID).To(Equal(details.ClusterID))
Expect(cluster.Name).To(Equal(clusterName))

delDetails, err := c.Clusters.DeleteClusterByClusterID(testConfig.OrgID, details.ClusterID)
Expect(err).NotTo(HaveOccurred())
Expect(delDetails.DeletedClusterCount).To(Equal(1))
Expand Down