Skip to content

Commit

Permalink
made changes based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Oct 8, 2024
1 parent dbfe817 commit 351976c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type ApiClientInterface interface {
EnvironmentScheduling(environmentId string) (EnvironmentScheduling, error)
EnvironmentSchedulingUpdate(environmentId string, payload EnvironmentScheduling) (EnvironmentScheduling, error)
EnvironmentSchedulingDelete(environmentId string) error
EnvironmentDeployment(id string) (*DeploymentLog, error)
EnvironmentDeploymentLog(id string) (*DeploymentLog, error)
WorkflowTrigger(environmentId string) ([]WorkflowTrigger, error)
WorkflowTriggerUpsert(environmentId string, request WorkflowTriggerUpsertPayload) ([]WorkflowTrigger, error)
EnvironmentDriftDetection(environmentId string) (EnvironmentSchedulingExpression, error)
Expand Down
12 changes: 6 additions & 6 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (client *ApiClient) Environment(id string) (Environment, error) {
return result, nil
}

func (client *ApiClient) EnvironmentDeployment(id string) (*DeploymentLog, error) {
func (client *ApiClient) EnvironmentDeploymentLog(id string) (*DeploymentLog, error) {
var result DeploymentLog
err := client.http.Get("/environments/deployments/"+id, nil, &result)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ var _ = Describe("Environment Client", func() {
*response = mockDeployment
}).Times(1)

deployment, err = apiClient.EnvironmentDeployment(mockDeployment.Id)
deployment, err = apiClient.EnvironmentDeploymentLog(mockDeployment.Id)
})

It("Should return deployment", func() {
Expand Down
1 change: 1 addition & 0 deletions env0/resource_approval_policy_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func resourceApprovalPolicyAssignmentRead(ctx context.Context, d *schema.Resourc
for _, approvalPolicyByScope := range approvalPolicyByScopeArr {
if approvalPolicyByScope.ApprovalPolicy.Id == assignment.BlueprintId {
found = true

break
}
}
Expand Down
26 changes: 20 additions & 6 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,20 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta
if err := validateTemplateProjectAssignment(d, apiClient); err != nil {
return diag.Errorf("%v\n", err)
}

environment, err = apiClient.EnvironmentCreate(environmentPayload)
} else {
templatePayload, createTemPayloadErr := templateCreatePayloadFromParameters("without_template_settings.0", d)

if createTemPayloadErr != nil {
return createTemPayloadErr
}

payload := client.EnvironmentCreateWithoutTemplate{
EnvironmentCreate: environmentPayload,
TemplateCreate: templatePayload,
}

// Note: the blueprint id field of the environment is returned only during creation of a template without environment.
// Afterward, it will be omitted from future response.
// setEnvironmentSchema() (several lines below) sets the blueprint id in the resource (under "without_template_settings.0.id").
Expand Down Expand Up @@ -895,15 +899,15 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta
}

if d.Get("wait_for_destroy").(bool) {
if err := waitForEnvironmentDestroy(apiClient, res.Id); err != nil {
if err := waitForEnvironmentDestroy(ctx, apiClient, res.Id); err != nil {
return diag.FromErr(err)
}
}

return nil
}

func waitForEnvironmentDestroy(apiClient client.ApiClientInterface, deploymentId string) error {
func waitForEnvironmentDestroy(ctx context.Context, apiClient client.ApiClientInterface, deploymentId string) error {
waitInteval := time.Second * 10
timeout := time.Minute * 30

Expand All @@ -918,13 +922,13 @@ func waitForEnvironmentDestroy(apiClient client.ApiClientInterface, deploymentId

go func() {
for {
deployment, err := apiClient.EnvironmentDeployment(deploymentId)
deployment, err := apiClient.EnvironmentDeploymentLog(deploymentId)
if err != nil {
results <- fmt.Errorf("failed to get environment deployment '%s': %w", deploymentId, err)
return
}

if slices.Contains([]string{"WAITING_FOR_USER", "TIMEOUT", "FAILURE", "CANCELLED", "INTERNAL_FAILURE", "ABORTING", "ABORTED", "SKIPPED", "NEVER_DEPLOYED"}, deployment.Status) {
if slices.Contains([]string{"TIMEOUT", "FAILURE", "CANCELLED", "INTERNAL_FAILURE", "ABORTING", "ABORTED", "SKIPPED", "NEVER_DEPLOYED"}, deployment.Status) {
results <- fmt.Errorf("failed to wait for environment destroy to complete, deployment status is: %s", deployment.Status)
return
}
Expand All @@ -934,9 +938,14 @@ func waitForEnvironmentDestroy(apiClient client.ApiClientInterface, deploymentId
return
}

tflog.Info(ctx, "current 'destroy' deployment status", map[string]interface{}{"deploymentId": deploymentId, "status": deployment.Status})
if deployment.Status == "WAITING_FOR_USER" {
tflog.Warn(ctx, "waiting for user approval (Env0 UI) to proceed with 'destroy' deployment")
}

select {
case <-timer.C:
results <- fmt.Errorf("timeout! last destroy deployment status was '%s'", deployment.Status)
results <- fmt.Errorf("timeout! last 'destroy' deployment status was '%s'", deployment.Status)
return
case <-ticker.C:
continue
Expand Down Expand Up @@ -1208,13 +1217,16 @@ func getDeployPayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
if configuration, ok := d.GetOk("configuration"); ok && isRedeploy {
configurationChanges := getConfigurationVariablesFromSchema(configuration.([]interface{}))
scope := client.ScopeEnvironment

if _, ok := d.GetOk("sub_environment_configuration"); ok {
scope = client.ScopeWorkflow
}

configurationChanges, err = getUpdateConfigurationVariables(configurationChanges, d.Get("id").(string), scope, apiClient)
if err != nil {
return client.DeployRequest{}, err
}

payload.ConfigurationChanges = &configurationChanges
}

Expand Down Expand Up @@ -1250,6 +1262,7 @@ func getUpdateConfigurationVariables(configurationChanges client.ConfigurationCh
if err != nil {
return client.ConfigurationChanges{}, fmt.Errorf("could not get environment configuration variables: %w", err)
}

configurationChanges = linkToExistConfigurationVariables(configurationChanges, existVariables)
configurationChanges = deleteUnusedConfigurationVariables(configurationChanges, existVariables)

Expand Down Expand Up @@ -1339,6 +1352,7 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta
var getErr diag.Diagnostics

var environment client.Environment

_, err := uuid.Parse(id)
if err == nil {
tflog.Info(ctx, "Resolving environment by id", map[string]interface{}{"id": id})
Expand Down Expand Up @@ -1369,7 +1383,7 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta

environmentVariableSetIds, err := getEnvironmentVariableSetIdsFromApi(d, apiClient)
if err != nil {
return nil, fmt.Errorf("could not get environment variable sets: %v", err)
return nil, fmt.Errorf("could not get environment variable sets: %w", err)
}

d.Set("deployment_id", environment.LatestDeploymentLogId)
Expand Down
24 changes: 12 additions & 12 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,9 @@ func TestUnitEnvironmentResource(t *testing.T) {
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("QUEUED"), nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("IN_PROGRESS"), nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("QUEUED"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("IN_PROGRESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
)
})
})
Expand All @@ -889,7 +889,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
{
Config: config,
Destroy: true,
ExpectError: regexp.MustCompile("failed to wait for environment destroy to complete, deployment status is: WAITING_FOR_USER"),
ExpectError: regexp.MustCompile("failed to wait for environment destroy to complete, deployment status is: CANCELLED"),
},
},
}
Expand All @@ -905,9 +905,9 @@ func TestUnitEnvironmentResource(t *testing.T) {
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("WAITING_FOR_USER"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("CANCELLED"), nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
)
})
})
Expand Down Expand Up @@ -938,9 +938,9 @@ func TestUnitEnvironmentResource(t *testing.T) {
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(nil, errors.New("error")),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(nil, errors.New("error")),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
)
})
})
Expand All @@ -955,7 +955,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
{
Config: config,
Destroy: true,
ExpectError: regexp.MustCompile("timeout! last destroy deployment status was 'IN_PROGRESS'"),
ExpectError: regexp.MustCompile("timeout! last 'destroy' deployment status was 'IN_PROGRESS'"),
},
},
}
Expand All @@ -971,10 +971,10 @@ func TestUnitEnvironmentResource(t *testing.T) {
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("QUEUED"), nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).AnyTimes().Return(deploymentWithStatus("IN_PROGRESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("QUEUED"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).AnyTimes().Return(deploymentWithStatus("IN_PROGRESS"), nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1).Return(destroyResponse, nil),
mock.EXPECT().EnvironmentDeployment(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
mock.EXPECT().EnvironmentDeploymentLog(deploymentLog.Id).Times(1).Return(deploymentWithStatus("SUCCESS"), nil),
)
})
})
Expand Down

0 comments on commit 351976c

Please sign in to comment.