From 3194a03a06e1e66f94e48639655d38f2065701fb Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Mon, 7 Oct 2024 15:18:57 +0200 Subject: [PATCH] better streamline plural up --cloud --- cmd/command/cd/cd_clusters.go | 25 ++++++++++++++++--------- cmd/command/up/up.go | 32 ++++++++++++++++++++++++++++++-- pkg/manifest/manifest.go | 10 ++++++---- pkg/provider/aws.go | 23 +++++++++++++---------- pkg/provider/azure.go | 2 +- pkg/provider/equinix.go | 2 +- pkg/provider/gcp.go | 2 +- pkg/provider/kind.go | 2 +- pkg/provider/linode.go | 2 +- 9 files changed, 70 insertions(+), 30 deletions(-) diff --git a/cmd/command/cd/cd_clusters.go b/cmd/command/cd/cd_clusters.go index b5afad5b..3d5bd9e4 100644 --- a/cmd/command/cd/cd_clusters.go +++ b/cmd/command/cd/cd_clusters.go @@ -118,19 +118,12 @@ func (p *Plural) cdClusterCommands() []cli.Command { } func (p *Plural) handleListClusters(_ *cli.Context) error { - if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { - return err - } - - clusters, err := p.ConsoleClient.ListClusters() + clusters, err := p.ListClusters() if err != nil { return err } - if clusters == nil { - return fmt.Errorf("returned objects list [ListClusters] is nil") - } headers := []string{"Id", "Name", "Handle", "Version", "Provider"} - return utils.PrintTable(clusters.Clusters.Edges, headers, func(cl *gqlclient.ClusterEdgeFragment) ([]string, error) { + return utils.PrintTable(clusters, headers, func(cl *gqlclient.ClusterEdgeFragment) ([]string, error) { provider := "" if cl.Node.Provider != nil { provider = cl.Node.Provider.Name @@ -147,6 +140,20 @@ func (p *Plural) handleListClusters(_ *cli.Context) error { }) } +func (p *Plural) ListClusters() ([]*gqlclient.ClusterEdgeFragment, error) { + if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { + return nil, err + } + clusters, err := p.ConsoleClient.ListClusters() + if err != nil { + return nil, err + } + if clusters == nil { + return nil, fmt.Errorf("returned objects list [ListClusters] is nil") + } + return clusters.Clusters.Edges, nil +} + func (p *Plural) GetClusterId(handle string) (string, string, error) { if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { return "", "", err diff --git a/cmd/command/up/up.go b/cmd/command/up/up.go index 4795650a..fd1405ac 100644 --- a/cmd/command/up/up.go +++ b/cmd/command/up/up.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/AlecAivazis/survey/v2" "github.com/pluralsh/plural-cli/cmd/command/cd" "github.com/pluralsh/plural-cli/pkg/client" "github.com/pluralsh/plural-cli/pkg/common" @@ -81,11 +82,10 @@ func (p *Plural) handleUp(c *cli.Context) error { } if c.Bool("cloud") { - id, name, err := cd.GetClusterId("mgmt") + id, name, err := getCluster(cd) if err != nil { return err } - ctx.ImportCluster = lo.ToPtr(id) ctx.CloudCluster = name } @@ -119,3 +119,31 @@ func (p *Plural) handleUp(c *cli.Context) error { utils.Highlight("Feel free to use terraform as you normally would, and leverage the gitops setup we've generated in the apps/ subfolder\n") return nil } + +func getCluster(cd *cd.Plural) (id string, name string, err error) { + if cd == nil { + return "", "", fmt.Errorf("please provide a plural client") + } + clusters, err := cd.ListClusters() + if err != nil { + return "", "", err + } + + clusterNames := []string{} + clusterMap := map[string]string{} + + for _, cluster := range clusters { + clusterNames = append(clusterNames, cluster.Node.Name) + clusterMap[cluster.Node.Name] = cluster.Node.ID + } + + prompt := &survey.Select{ + Message: "Select one of the following clusters:", + Options: clusterNames, + } + if err = survey.AskOne(prompt, &name, survey.WithValidator(survey.Required)); err != nil { + return + } + id = clusterMap[name] + return +} diff --git a/pkg/manifest/manifest.go b/pkg/manifest/manifest.go index d720fa96..7b4c0882 100644 --- a/pkg/manifest/manifest.go +++ b/pkg/manifest/manifest.go @@ -118,14 +118,16 @@ func Read(path string) (man *Manifest, err error) { return } -func (pMan *ProjectManifest) Configure(cloud bool) Writer { +func (pMan *ProjectManifest) Configure(cloud bool, cluster string) Writer { utils.Highlight("\nLet's get some final information about your workspace set up\n\n") - res, _ := utils.ReadAlphaNum("Give us a unique, memorable string to use for bucket naming, eg an abbreviation for your company: ") - pMan.BucketPrefix = res - pMan.Bucket = fmt.Sprintf("%s-tf-state", res) + pMan.BucketPrefix = cluster + pMan.Bucket = fmt.Sprintf("plrl-cloud-%s", cluster) if !cloud { + res, _ := utils.ReadAlphaNum("Give us a unique, memorable string to use for bucket naming, eg an abbreviation for your company: ") + pMan.BucketPrefix = res + pMan.Bucket = fmt.Sprintf("%s-tf-state", res) if err := pMan.ConfigureNetwork(); err != nil { return nil } diff --git a/pkg/provider/aws.go b/pkg/provider/aws.go index dbdac54c..5e428da8 100644 --- a/pkg/provider/aws.go +++ b/pkg/provider/aws.go @@ -109,7 +109,7 @@ func mkAWS(conf config.Config) (provider *AWSProvider, err error) { provider.project = account provider.storageClient = client - azones, err := getAvailabilityZones(ctx, provider.Region()) + azones, err := getAvailabilityZones(ctx, provider.Region(), cloudFlag) if err != nil { return } @@ -123,7 +123,7 @@ func mkAWS(conf config.Config) (provider *AWSProvider, err error) { Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - provider.writer = projectManifest.Configure(cloudFlag) + provider.writer = projectManifest.Configure(cloudFlag, provider.Cluster()) provider.bucket = projectManifest.Bucket return } @@ -159,17 +159,20 @@ func getEC2Client(ctx context.Context, region string) (*ec2.Client, error) { } // TODO: during Plural init we should ask the user to choose which AZs they want to use (first 3, random, manual, look at how CAPA does that). There should be a minimum limit of 3. -func getAvailabilityZones(ctx context.Context, region string) ([]string, error) { +func getAvailabilityZones(ctx context.Context, region string, cloudFlag bool) ([]string, error) { first3 := "first three" random := "random" manual := "manual" - choice := "" - prompt := &survey.Select{ - Message: "Which availability zones you would like to use:", - Options: []string{first3, random, manual}, - } - if err := survey.AskOne(prompt, &choice); err != nil { - return nil, err + choice := first3 + if !cloudFlag { + choice = "" + prompt := &survey.Select{ + Message: "Which availability zones you would like to use:", + Options: []string{first3, random, manual}, + } + if err := survey.AskOne(prompt, &choice); err != nil { + return nil, err + } } switch choice { diff --git a/pkg/provider/azure.go b/pkg/provider/azure.go index 41f65beb..fc83c024 100644 --- a/pkg/provider/azure.go +++ b/pkg/provider/azure.go @@ -183,7 +183,7 @@ func mkAzure(conf config.Config) (prov *AzureProvider, err error) { Context: prov.Context(), Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - prov.writer = projectManifest.Configure(cloudFlag) + prov.writer = projectManifest.Configure(cloudFlag, prov.Cluster()) prov.bucket = projectManifest.Bucket return } diff --git a/pkg/provider/equinix.go b/pkg/provider/equinix.go index 96a1ed4d..40fe3b0b 100644 --- a/pkg/provider/equinix.go +++ b/pkg/provider/equinix.go @@ -108,7 +108,7 @@ func mkEquinix(conf config.Config) (provider *EQUINIXProvider, err error) { Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - provider.writer = projectManifest.Configure(cloudFlag) + provider.writer = projectManifest.Configure(cloudFlag, provider.Cluster()) provider.bucket = projectManifest.Bucket return } diff --git a/pkg/provider/gcp.go b/pkg/provider/gcp.go index 2beb9e48..bbbdb24e 100644 --- a/pkg/provider/gcp.go +++ b/pkg/provider/gcp.go @@ -160,7 +160,7 @@ func mkGCP(conf config.Config) (provider *GCPProvider, err error) { Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - provider.writer = projectManifest.Configure(cloudFlag) + provider.writer = projectManifest.Configure(cloudFlag, provider.Cluster()) provider.bucket = projectManifest.Bucket return } diff --git a/pkg/provider/kind.go b/pkg/provider/kind.go index 302b2650..e8f5782b 100644 --- a/pkg/provider/kind.go +++ b/pkg/provider/kind.go @@ -62,7 +62,7 @@ func mkKind(conf config.Config) (provider *KINDProvider, err error) { Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - provider.writer = projectManifest.Configure(cloudFlag) + provider.writer = projectManifest.Configure(cloudFlag, provider.Cluster()) provider.bucket = projectManifest.Bucket return } diff --git a/pkg/provider/linode.go b/pkg/provider/linode.go index e92dae29..bb2c879f 100644 --- a/pkg/provider/linode.go +++ b/pkg/provider/linode.go @@ -87,7 +87,7 @@ func mkLinode(conf config.Config) (provider *LinodeProvider, err error) { Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint}, } - provider.writer = projectManifest.Configure(cloudFlag) + provider.writer = projectManifest.Configure(cloudFlag, provider.Cluster()) provider.bucket = projectManifest.Bucket return }