Skip to content

Commit

Permalink
better streamline plural up --cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 7, 2024
1 parent 7817475 commit 3194a03
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 30 deletions.
25 changes: 16 additions & 9 deletions cmd/command/cd/cd_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 30 additions & 2 deletions cmd/command/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/provider/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/equinix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3194a03

Please sign in to comment.