Skip to content

Commit

Permalink
Fix: property isArchived is sent in patch request even if it was not … (
Browse files Browse the repository at this point in the history
#595)

* Fix: property isArchived is sent in patch request even if it was not changed

* Fix unit test

* fix bug
  • Loading branch information
TomerHeber authored Feb 16, 2023
1 parent 957aaf9 commit 07f75c9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ type Environment struct {
LifespanEndAt string `json:"lifespanEndAt" tfschema:"ttl"`
LatestDeploymentLogId string `json:"latestDeploymentLogId" tfschema:"deployment_id"`
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
IsArchived bool `json:"isArchived" tfschema:"is_inactive,omitempty"`
TerragruntWorkingDirectory string `json:"terragruntWorkingDirectory,omitempty"`
VcsCommandsAlias string `json:"vcsCommandsAlias"`
BlueprintId string `json:"blueprintId" tfschema:"-"`
IsRemoteBackend *bool `json:"isRemoteBackend" tfschema:"-"`
IsArchived *bool `json:"isArchived" tfschema:"-"`
}

type EnvironmentCreate struct {
Expand Down Expand Up @@ -178,7 +178,7 @@ type EnvironmentUpdate struct {
PullRequestPlanDeployments *bool `json:"pullRequestPlanDeployments,omitempty" tfschema:"-"`
AutoDeployOnPathChangesOnly *bool `json:"autoDeployOnPathChangesOnly,omitempty" tfschema:"-"`
IsRemoteBackend *bool `json:"isRemoteBackend,omitempty" tfschema:"-"`
IsArchived bool `json:"isArchived" tfschema:"is_inactive"`
IsArchived *bool `json:"isArchived,omitempty" tfschema:"-"`
}

type EnvironmentDeployResponse struct {
Expand Down
2 changes: 1 addition & 1 deletion env0/data_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestEnvironmentDataSource(t *testing.T) {
archivedEnvironment := client.Environment{
Id: "id-archived",
Name: environment.Name,
IsArchived: true,
IsArchived: boolPtr(true),
}

environmentFieldsByName := map[string]interface{}{"name": environment.Name}
Expand Down
6 changes: 5 additions & 1 deletion env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ func getUpdatePayload(d *schema.ResourceData) (client.EnvironmentUpdate, diag.Di
payload.IsRemoteBackend = boolPtr(d.Get("is_remote_backend").(bool))
}

if d.HasChange("is_inactive") {
payload.IsArchived = boolPtr(d.Get("is_inactive").(bool))
}

if err := assertDeploymentTriggers(d); err != nil {
return client.EnvironmentUpdate{}, err
}
Expand Down Expand Up @@ -997,7 +1001,7 @@ func getEnvironmentByName(name interface{}, meta interface{}, excludeArchived bo

var environmentsByName []client.Environment
for _, candidate := range environments {
if excludeArchived && candidate.IsArchived {
if excludeArchived && candidate.IsArchived != nil && *candidate.IsArchived {
continue
}

Expand Down
15 changes: 10 additions & 5 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: "/terragrunt/directory2/",
VcsCommandsAlias: "alias2",
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: true,
IsArchived: boolPtr(true),
}

template := client.Template{
Expand All @@ -64,7 +64,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
}

createEnvironmentResourceConfig := func(environment client.Environment) string {
return resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
config := map[string]interface{}{
"name": environment.Name,
"project_id": environment.ProjectId,
"template_id": environment.LatestDeploymentLog.BlueprintId,
Expand All @@ -73,8 +73,13 @@ func TestUnitEnvironmentResource(t *testing.T) {
"force_destroy": true,
"vcs_commands_alias": environment.VcsCommandsAlias,
"is_remote_backend": *(environment.IsRemoteBackend),
"is_inactive": environment.IsArchived,
})
}

if environment.IsArchived != nil {
config["is_inactive"] = *(environment.IsArchived)
}

return resourceConfigCreate(resourceType, resourceName, config)
}

autoDeployByCustomGlobDefault := ""
Expand Down Expand Up @@ -1162,7 +1167,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: updatedEnvironment.TerragruntWorkingDirectory,
VcsCommandsAlias: updatedEnvironment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: true,
IsArchived: boolPtr(true),
}).Times(1).Return(client.Environment{}, errors.New("error"))
mock.EXPECT().Environment(gomock.Any()).Times(2).Return(environment, nil) // 1 after create, 1 before update
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down
2 changes: 1 addition & 1 deletion env0/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func resourceProjectAssertCanDelete(ctx context.Context, d *schema.ResourceData,
}
}

if !env.IsArchived {
if env.IsArchived == nil || !*env.IsArchived {
return &ActiveEnvironmentError{
retry: true,
message: fmt.Sprintf("found an active environment %s (remove the environment or use the force_destroy flag)", env.Name),
Expand Down

0 comments on commit 07f75c9

Please sign in to comment.