From 783ec692b6b89f3c450abfd2309cd466a62b245d Mon Sep 17 00:00:00 2001 From: kushalmalani Date: Tue, 16 May 2023 18:22:20 -0700 Subject: [PATCH] Refactoring for list deployments (#1205) * Refactoring for list deployments * Movings calls to single function * Correcting variable name --- cloud/deployment/deployment.go | 23 +++++++++++++---------- cloud/deployment/fromfile/fromfile.go | 2 +- cmd/cloud/setup.go | 3 ++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cloud/deployment/deployment.go b/cloud/deployment/deployment.go index cf7a9f8ee..2b8df0963 100644 --- a/cloud/deployment/deployment.go +++ b/cloud/deployment/deployment.go @@ -76,7 +76,7 @@ func List(ws string, all bool, client astro.Client, out io.Writer) error { ws = "" tab = newTableOutAll() } - deployments, err := client.ListDeployments(c.Organization, ws) + deployments, err := GetDeployments(ws, c.Organization, client) if err != nil { return errors.Wrap(err, astro.AstronomerConnectionErrMsg) } @@ -221,7 +221,7 @@ func Create(label, workspaceID, description, clusterID, runtimeVersion, dagDeplo if label == "" { return errors.New("you must give your Deployment a name") } - deployments, err := GetDeployments(workspaceID, client) + deployments, err := GetDeployments(workspaceID, organizationID, client) if err != nil { return errors.Wrap(err, errInvalidDeployment.Error()) } @@ -549,7 +549,7 @@ func healthPoll(deploymentID, ws string, client astro.Client) error { // Got a tick, we should check if deployment is healthy case <-ticker.C: buf.Reset() - deployments, err := GetDeployments(ws, client) + deployments, err := GetDeployments(ws, "", client) if err != nil { return err } @@ -745,13 +745,16 @@ func Delete(deploymentID, ws, deploymentName string, forceDelete bool, client as return nil } -var GetDeployments = func(ws string, client astro.Client) ([]astro.Deployment, error) { - c, err := config.GetCurrentContext() - if err != nil { - return []astro.Deployment{}, err +var GetDeployments = func(ws, org string, client astro.Client) ([]astro.Deployment, error) { + if org == "" { + c, err := config.GetCurrentContext() + if err != nil { + return []astro.Deployment{}, err + } + org = c.Organization } - deployments, err := client.ListDeployments(c.Organization, ws) + deployments, err := client.ListDeployments(org, ws) if err != nil { return deployments, errors.Wrap(err, astro.AstronomerConnectionErrMsg) } @@ -805,7 +808,7 @@ var SelectDeployment = func(deployments []astro.Deployment, message string) (ast } func GetDeployment(ws, deploymentID, deploymentName string, client astro.Client, coreClient astrocore.CoreClient) (astro.Deployment, error) { - deployments, err := GetDeployments(ws, client) + deployments, err := GetDeployments(ws, "", client) if err != nil { return astro.Deployment{}, errors.Wrap(err, errInvalidDeployment.Error()) } @@ -879,7 +882,7 @@ func deploymentSelectionProcess(ws string, deployments []astro.Deployment, clien } // get a new deployment list - deployments, err = GetDeployments(ws, client) + deployments, err = GetDeployments(ws, "", client) if err != nil { return astro.Deployment{}, err } diff --git a/cloud/deployment/fromfile/fromfile.go b/cloud/deployment/fromfile/fromfile.go index 3fd84c2c5..2a4a7141b 100644 --- a/cloud/deployment/fromfile/fromfile.go +++ b/cloud/deployment/fromfile/fromfile.go @@ -84,7 +84,7 @@ func CreateOrUpdate(inputFile, action string, client astro.Client, out io.Writer return err } - existingDeployments, err = client.ListDeployments(c.Organization, workspaceID) + existingDeployments, err = deployment.GetDeployments(workspaceID, c.Organization, client) if err != nil { return err } diff --git a/cmd/cloud/setup.go b/cmd/cloud/setup.go index 7dff28c29..a65131e31 100644 --- a/cmd/cloud/setup.go +++ b/cmd/cloud/setup.go @@ -15,6 +15,7 @@ import ( astro "github.com/astronomer/astro-cli/astro-client" astrocore "github.com/astronomer/astro-cli/astro-client-core" "github.com/astronomer/astro-cli/cloud/auth" + "github.com/astronomer/astro-cli/cloud/deployment" "github.com/astronomer/astro-cli/cloud/organization" "github.com/astronomer/astro-cli/context" "github.com/astronomer/astro-cli/pkg/httputil" @@ -324,7 +325,7 @@ func checkAPIKeys(astroClient astro.Client, coreClient astrocore.CoreClient, isD orgProduct := fmt.Sprintf("%s", *org.Product) //nolint // get workspace ID - deployments, err := astroClient.ListDeployments(orgID, "") + deployments, err := deployment.GetDeployments("", orgID, astroClient) if err != nil { return false, errors.Wrap(err, astro.AstronomerConnectionErrMsg) }