Skip to content

Commit

Permalink
Cr 9843 add CreateArgoRollouts mutation (#419)
Browse files Browse the repository at this point in the history
* renamed RemoveCluster to Delete
* added cluster_v2 with `List`
* updated version to 0.42.0
  • Loading branch information
ATGardner authored Apr 19, 2022
1 parent ae43f72 commit 4462ab5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.41.2
0.42.0
41 changes: 35 additions & 6 deletions pkg/codefresh/ap_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package codefresh
import (
"context"
"fmt"

// model "github.com/codefresh-io/go-sdk/pkg/codefresh/model/app-proxy"
)

type (
IAppProxyClustersAPI interface {
RemoveCluster(ctx context.Context, server string, runtime string) error
CreateArgoRollouts(ctx context.Context, server string, namespace string) error
Delete(ctx context.Context, server string, runtime string) error
}

appProxyClusters struct {
codefresh *codefresh
}

graphqlClusterRemoveResponse struct {
graphqlClusterResponse struct {
Errors []graphqlError
}
)
Expand All @@ -24,7 +24,36 @@ func newAppProxyClustersAPI(c *codefresh) IAppProxyClustersAPI {
return &appProxyClusters{codefresh: c}
}

func (c *appProxyClusters) RemoveCluster(ctx context.Context, server string, runtime string) error {
func (c *appProxyClusters)CreateArgoRollouts(ctx context.Context, server string, namespace string) error {
jsonData := map[string]interface{}{
"query": `
mutation createArgoRollouts($args: CreateArgoRolloutsInput!) {
createArgoRollouts(args: $args)
}
`,
"variables": map[string]interface{}{
"args": map[string]interface{}{
"destServer": server,
"destNamespace": namespace,
},
},
}

res := &graphqlClusterResponse{}
err := c.codefresh.graphqlAPI(ctx, jsonData, res)

if err != nil {
return fmt.Errorf("failed making a graphql API call to add rollouts: %w", err)
}

if len(res.Errors) > 0 {
return graphqlErrorResponse{errors: res.Errors}
}

return nil
}

func (c *appProxyClusters)Delete(ctx context.Context, server string, runtime string) error {
jsonData := map[string]interface{}{
"query": `
mutation RemoveCluster($server: String!, $runtime: String!) {
Expand All @@ -37,7 +66,7 @@ func (c *appProxyClusters) RemoveCluster(ctx context.Context, server string, run
},
}

res := &graphqlClusterRemoveResponse{}
res := &graphqlClusterResponse{}
err := c.codefresh.graphqlAPI(ctx, jsonData, res)

if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/codefresh/argo_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ func (r *argoRuntime) Create(ctx context.Context, opts *model.RuntimeInstallatio
func (r *argoRuntime) Get(ctx context.Context, name string) (*model.Runtime, error) {
jsonData := map[string]interface{}{
"query": `
query GetRuntime(
$name: String!
) {
query GetRuntime($name: String!) {
runtime(name: $name) {
metadata {
name
Expand Down
106 changes: 106 additions & 0 deletions pkg/codefresh/cluster_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package codefresh

import (
"context"
"fmt"

"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
)

type (
IClusterV2API interface {
List(ctx context.Context, runtime string) ([]model.Cluster, error)
}

clusterV2 struct {
codefresh *codefresh
}

graphqlClusterListResponse struct {
Data struct {
Clusters model.ClusterSlice
}
Errors []graphqlError
}
)

func newClusterV2API(codefresh *codefresh) IClusterV2API {
return &clusterV2{codefresh: codefresh}
}

func (c *clusterV2)List(ctx context.Context, runtime string) ([]model.Cluster, error) {
after := ""
clusters := make([]model.Cluster, 0)
for {
clusterSlice, err := c.getClusterSlice(ctx, runtime, after)
if err != nil {
return nil, err
}

for i := range clusterSlice.Edges {
clusters = append(clusters, *clusterSlice.Edges[i].Node)
}

if clusterSlice.PageInfo.HasNextPage {
after = *clusterSlice.PageInfo.EndCursor
} else {
break
}
}

return clusters, nil
}


func (c *clusterV2)getClusterSlice(ctx context.Context, runtime string, after string) (*model.ClusterSlice, error) {
jsonData := map[string]interface{}{
"query": `
query clusters($runtime: String, $pagination: SlicePaginationArgs) {
clusters(runtime: $runtime, pagination: $pagination) {
edges {
node {
metadata {
name
runtime
}
server
info {
connectionState {
status
message
}
serverVersion
cacheInfo {
resourcesCount
apisCount
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
`,
"variables": map[string]interface{}{
"runtime": runtime,
"pagination": map[string]interface{}{
"after": after,
},
},
}

res := &graphqlClusterListResponse{}
err := c.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return nil, fmt.Errorf("failed getting cluster list: %w", err)
}

if len(res.Errors) > 0 {
return nil, graphqlErrorResponse{errors: res.Errors}
}

return &res.Data.Clusters, nil
}
5 changes: 5 additions & 0 deletions pkg/codefresh/codefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (

V2API interface {
Runtime() IRuntimeAPI
Cluster() IClusterV2API
GitSource() IGitSourceAPI
Component() IComponentAPI
Workflow() IWorkflowV2API
Expand Down Expand Up @@ -124,6 +125,10 @@ func (c *codefresh) CliReleases() ICliReleasesAPI {
return newCliReleasesAPI(c)
}

func (c *codefresh) Cluster() IClusterV2API {
return newClusterV2API(c)
}

func (c *codefresh) AppProxy(ctx context.Context, runtime string, insecure bool) (AppProxyAPI, error) {
rt, err := c.V2().Runtime().Get(ctx, runtime)
if err != nil {
Expand Down

0 comments on commit 4462ab5

Please sign in to comment.