From 9380ee1d436a72827ba483a654c0e1a1454234b3 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Fri, 19 Jul 2024 14:08:29 -0400 Subject: [PATCH] Add support for setting project in `plural cd clusters bootstrap` Can be useful for testing/experimentation on a multi-project structure --- cmd/plural/cd_clusters.go | 13 +++++++++++++ go.mod | 2 +- go.sum | 2 ++ pkg/console/clusters.go | 9 +++++++++ pkg/console/console.go | 1 + pkg/test/mocks/ConsoleClient.go | 30 ++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 1 deletion(-) diff --git a/cmd/plural/cd_clusters.go b/cmd/plural/cd_clusters.go index d4b7710d..750f5aeb 100644 --- a/cmd/plural/cd_clusters.go +++ b/cmd/plural/cd_clusters.go @@ -99,6 +99,7 @@ func (p *Plural) cdClusterCommands() []cli.Command { cli.StringFlag{Name: "name", Usage: "The name you'll give the cluster", Required: true}, cli.StringFlag{Name: "handle", Usage: "optional handle for the cluster"}, cli.StringFlag{Name: "values", Usage: "values file to use for the deployment agent helm chart", Required: false}, + cli.StringFlag{Name: "project", Usage: "the project this cluster will belong to", Required: false}, cli.StringSliceFlag{ Name: "tag", Usage: "a cluster tag to add, useful for targeting with global services", @@ -421,6 +422,18 @@ func (p *Plural) handleClusterBootstrap(c *cli.Context) error { attrs.Handle = lo.ToPtr(c.String("handle")) } + if c.String("project") != "" { + project, err := p.ConsoleClient.GetProject(c.String("project")) + if err != nil { + return nil + } + if project == nil { + return fmt.Errorf("Could not find project %s", c.String("project")) + } + + attrs.ProjectID = lo.ToPtr(project.ID) + } + if c.IsSet("tag") { attrs.Tags = lo.Map(c.StringSlice("tag"), func(tag string, index int) *gqlclient.TagAttributes { tags := strings.Split(tag, "=") diff --git a/go.mod b/go.mod index 19f8b20a..c3a69558 100644 --- a/go.mod +++ b/go.mod @@ -56,7 +56,7 @@ require ( github.com/packethost/packngo v0.29.0 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/pluralsh/cluster-api-migration v0.2.16 - github.com/pluralsh/console-client-go v0.9.1 + github.com/pluralsh/console-client-go v0.12.3 github.com/pluralsh/gqlclient v1.11.0 github.com/pluralsh/plural-operator v0.5.5 github.com/pluralsh/polly v0.1.8 diff --git a/go.sum b/go.sum index 35f0677c..24cff5ce 100644 --- a/go.sum +++ b/go.sum @@ -1887,6 +1887,8 @@ github.com/pluralsh/console-client-go v0.9.0 h1:2rCPLVeooSV6WKfIKxqssloE1e6AkdP6 github.com/pluralsh/console-client-go v0.9.0/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo= github.com/pluralsh/console-client-go v0.9.1 h1:o2khvyRu/d3ndehbX02CjnMbU6i0smhulhe0OYnp8yE= github.com/pluralsh/console-client-go v0.9.1/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo= +github.com/pluralsh/console-client-go v0.12.3 h1:cx/a9eAPgohzcDyNs/5BQXYA9WRWcXCyNzNzLwTd3T8= +github.com/pluralsh/console-client-go v0.12.3/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo= github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E= github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s= github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA= diff --git a/pkg/console/clusters.go b/pkg/console/clusters.go index 419acee2..71d496c8 100644 --- a/pkg/console/clusters.go +++ b/pkg/console/clusters.go @@ -8,6 +8,15 @@ import ( "github.com/samber/lo" ) +func (c *consoleClient) GetProject(name string) (*consoleclient.ProjectFragment, error) { + result, err := c.client.GetProject(c.ctx, nil, lo.ToPtr(name)) + if err != nil { + return nil, api.GetErrorResponse(err, "GetProject") + } + + return result.Project, nil +} + func (c *consoleClient) ListClusters() (*consoleclient.ListClusters, error) { result, err := c.client.ListClusters(c.ctx, nil, nil, nil) diff --git a/pkg/console/console.go b/pkg/console/console.go index 2827eb7c..073019e4 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -24,6 +24,7 @@ type ConsoleClient interface { Token() string AgentUrl(id string) (string, error) ListClusters() (*consoleclient.ListClusters, error) + GetProject(name string) (*consoleclient.ProjectFragment, error) GetCluster(clusterId, clusterName *string) (*consoleclient.ClusterFragment, error) GetDeployToken(clusterId, clusterName *string) (string, error) UpdateCluster(id string, attr consoleclient.ClusterUpdateAttributes) (*consoleclient.UpdateCluster, error) diff --git a/pkg/test/mocks/ConsoleClient.go b/pkg/test/mocks/ConsoleClient.go index bcc56623..158d9a2d 100644 --- a/pkg/test/mocks/ConsoleClient.go +++ b/pkg/test/mocks/ConsoleClient.go @@ -482,6 +482,36 @@ func (_m *ConsoleClient) GetGlobalSettings() (*gqlclient.DeploymentSettingsFragm return r0, r1 } +// GetProject provides a mock function with given fields: name +func (_m *ConsoleClient) GetProject(name string) (*gqlclient.ProjectFragment, error) { + ret := _m.Called(name) + + if len(ret) == 0 { + panic("no return value specified for GetProject") + } + + var r0 *gqlclient.ProjectFragment + var r1 error + if rf, ok := ret.Get(0).(func(string) (*gqlclient.ProjectFragment, error)); ok { + return rf(name) + } + if rf, ok := ret.Get(0).(func(string) *gqlclient.ProjectFragment); ok { + r0 = rf(name) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*gqlclient.ProjectFragment) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(name) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetServiceContext provides a mock function with given fields: name func (_m *ConsoleClient) GetServiceContext(name string) (*gqlclient.ServiceContextFragment, error) { ret := _m.Called(name)