Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update deployment from file blocks changing cluster #1013

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cloud/deployment/fromfile/fromfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
errCannotUpdateExistingDeployment = errors.New("already exists")
errNotFound = errors.New("does not exist")
errInvalidValue = errors.New("is not valid")
errNotPermitted = errors.New("is not permitted")
)

const (
Expand Down Expand Up @@ -227,6 +228,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
75 changes: 75 additions & 0 deletions cloud/deployment/fromfile/fromfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,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 @@ -1970,6 +1986,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 @@ -2312,6 +2344,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 @@ -3054,6 +3102,33 @@ 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
deploymentFromFile.Deployment.Configuration.Executor = deployment.CeleryExecutor
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 @@ -340,6 +340,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 @@ -353,9 +356,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 @@ -12,6 +12,7 @@ import (
astro "github.com/astronomer/astro-cli/astro-client"
astro_mocks "github.com/astronomer/astro-cli/astro-client/mocks"
"github.com/astronomer/astro-cli/cloud/deployment"
"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 @@ -331,6 +332,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 @@ -402,8 +417,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