Skip to content

Commit

Permalink
update deployment from file blocks changing cluster (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemishp authored and neel-astro committed Jan 12, 2023
1 parent f0edf27 commit 8373de9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
6 changes: 6 additions & 0 deletions cloud/deployment/fromfile/fromfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
errInvalidEmail = errors.New("invalid email")
errCannotUpdateExistingDeployment = errors.New("already exists")
errNotFound = errors.New("does not exist")
errNotPermitted = errors.New("is not permitted")
)

const (
Expand Down Expand Up @@ -209,6 +210,11 @@ func getCreateOrUpdateInput(deploymentFromFile *inspect.FormattedDeployment, clu
WorkerQueues: listQueues,
}
case updateAction:
// check if cluster is being changed
if clusterID != existingDeployment.Cluster.ID {
return astro.CreateDeploymentInput{}, astro.UpdateDeploymentInput{},
fmt.Errorf("changing an existing deployment's cluster %w", errNotPermitted)
}
updateInput = astro.UpdateDeploymentInput{
ID: existingDeployment.ID,
ClusterID: clusterID,
Expand Down
74 changes: 74 additions & 0 deletions cloud/deployment/fromfile/fromfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,22 @@ deployment:
ID: "test-deployment-id",
Label: "test-deployment-label",
Description: "description",
Cluster: astro.Cluster{
ID: "test-cluster-id",
Name: "test-cluster",
NodePools: []astro.NodePool{
{
ID: "test-pool-id",
IsDefault: false,
NodeInstanceType: "test-worker-1",
},
{
ID: "test-pool-id-2",
IsDefault: false,
NodeInstanceType: "test-worker-2",
},
},
},
}
updatedDeployment := astro.Deployment{
ID: "test-deployment-id",
Expand Down Expand Up @@ -1952,6 +1968,22 @@ deployment:
ID: "test-deployment-id",
Label: "test-deployment-label",
Description: "description",
Cluster: astro.Cluster{
ID: "test-cluster-id",
Name: "test-cluster",
NodePools: []astro.NodePool{
{
ID: "test-pool-id",
IsDefault: false,
NodeInstanceType: "test-worker-1",
},
{
ID: "test-pool-id-2",
IsDefault: false,
NodeInstanceType: "test-worker-2",
},
},
},
}
updatedDeployment := astro.Deployment{
ID: "test-deployment-id",
Expand Down Expand Up @@ -2291,6 +2323,22 @@ deployment:
ID: "test-deployment-id",
Label: "test-deployment-label",
Description: "description",
Cluster: astro.Cluster{
ID: "test-cluster-id",
Name: "test-cluster",
NodePools: []astro.NodePool{
{
ID: "test-pool-id",
IsDefault: false,
NodeInstanceType: "test-worker-1",
},
{
ID: "test-pool-id-2",
IsDefault: false,
NodeInstanceType: "test-worker-2",
},
},
},
}
orgID = "test-org-id"
mockWorkerQueueDefaultOptions = astro.WorkerQueueDefaultOptions{
Expand Down Expand Up @@ -2777,6 +2825,32 @@ func TestGetCreateOrUpdateInput(t *testing.T) {
assert.Equal(t, expectedUpdateDeploymentInput, actualUpdateInput)
mockClient.AssertExpectations(t)
})
t.Run("returns an error if the cluster is being changed", func(t *testing.T) {
deploymentID = "test-deployment-id"
deploymentFromFile = inspect.FormattedDeployment{}
expectedUpdateDeploymentInput = astro.UpdateDeploymentInput{}
deploymentFromFile.Deployment.Configuration.ClusterName = "test-cluster-1"
deploymentFromFile.Deployment.Configuration.Name = "test-deployment-modified"
deploymentFromFile.Deployment.Configuration.Description = "test-description"
deploymentFromFile.Deployment.Configuration.RunTimeVersion = "test-runtime-v"
deploymentFromFile.Deployment.Configuration.SchedulerAU = 4
deploymentFromFile.Deployment.Configuration.SchedulerCount = 2
existingDeployment := astro.Deployment{
ID: deploymentID,
Label: "test-deployment",
Cluster: astro.Cluster{
ID: "test-cluster-id",
},
}

expectedUpdateDeploymentInput = astro.UpdateDeploymentInput{}
mockClient := new(astro_mocks.Client)
_, actualUpdateInput, err = getCreateOrUpdateInput(&deploymentFromFile, "diff-cluster", workspaceID, "update", &existingDeployment, nil, mockClient)
assert.ErrorIs(t, err, errNotPermitted)
assert.ErrorContains(t, err, "changing an existing deployment's cluster is not permitted")
assert.Equal(t, expectedUpdateDeploymentInput, actualUpdateInput)
mockClient.AssertExpectations(t)
})
t.Run("returns correct update deployment input when multiple queues are requested", func(t *testing.T) {
deploymentID = "test-deployment-id"
deploymentFromFile = inspect.FormattedDeployment{}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cloud/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ func deploymentUpdate(cmd *cobra.Command, args []string, out io.Writer) error {
return errors.Wrap(err, "failed to find a valid workspace")
}

// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

// request is to update from a file
if inputFile != "" {
requestedFlags := cmd.Flags().NFlag()
Expand All @@ -344,9 +347,6 @@ func deploymentUpdate(cmd *cobra.Command, args []string, out io.Writer) error {
return errors.New("Invalid --dag-deploy value)")
}

// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

// Get release name from args, if passed
if len(args) > 0 {
deploymentID = args[0]
Expand Down
20 changes: 18 additions & 2 deletions cmd/cloud/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
airflowversions "github.com/astronomer/astro-cli/airflow_versions"
astro "github.com/astronomer/astro-cli/astro-client"
astro_mocks "github.com/astronomer/astro-cli/astro-client/mocks"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/pkg/fileutil"
testUtil "github.com/astronomer/astro-cli/pkg/testing"

Expand Down Expand Up @@ -315,6 +316,20 @@ func TestDeploymentUpdate(t *testing.T) {
_, err = execDeploymentCmd(cmdArgs...)
assert.Error(t, err)

t.Run("returns an error when getting workspace fails", func(t *testing.T) {
testUtil.InitTestConfig(testUtil.CloudPlatform)
ctx, err := config.GetCurrentContext()
assert.NoError(t, err)
ctx.Workspace = ""
err = ctx.SetContext()
assert.NoError(t, err)
defer testUtil.InitTestConfig(testUtil.CloudPlatform)
expectedOut := "Usage:\n"
cmdArgs := []string{"update", "-n", "doesnotexist"}
resp, err := execDeploymentCmd(cmdArgs...)
assert.ErrorContains(t, err, "failed to find a valid workspace")
assert.Contains(t, resp, expectedOut)
})
t.Run("updates a deployment from file", func(t *testing.T) {
orgID := "test-org-id"
filePath := "./test-deployment.yaml"
Expand Down Expand Up @@ -385,8 +400,9 @@ deployment:
},
}
updatedDeployment := astro.Deployment{
ID: "test-deployment-id",
Label: "test-deployment-label",
ID: "test-deployment-id",
Label: "test-deployment-label",
Cluster: astro.Cluster{ID: "test-cluster-id", Name: "test-cluster"},
}
mockWorkerQueueDefaultOptions := astro.WorkerQueueDefaultOptions{
MinWorkerCount: astro.WorkerQueueOption{
Expand Down

0 comments on commit 8373de9

Please sign in to comment.