Skip to content

Commit

Permalink
Feat: add support for namespace in env0_environment resource (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Apr 17, 2024
1 parent 282d73c commit a79eb73
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type Environment struct {
IsRemoteBackend *bool `json:"isRemoteBackend" tfschema:"-"`
IsArchived *bool `json:"isArchived" tfschema:"-"`
IsRemoteApplyEnabled bool `json:"isRemoteApplyEnabled"`
K8sNamespace string `json:"k8s_namespace"`
}

type EnvironmentCreate struct {
Expand All @@ -139,6 +140,7 @@ type EnvironmentCreate struct {
Type string `json:"type,omitempty"`
DriftDetectionRequest *DriftDetectionRequest `json:"driftDetectionRequest,omitempty" tfschema:"-"`
PreventAutoDeploy *bool `json:"preventAutoDeploy,omitempty" tfschema:"-"`
K8sNamespace string `json:"k8s_namespace,omitempty"`
}

// When converted to JSON needs to be flattened. See custom MarshalJSON below.
Expand Down
6 changes: 6 additions & 0 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ func resourceEnvironment() *schema.Resource {
Default: "destroy",
ValidateDiagFunc: NewStringInValidator([]string{"destroy", "mark_as_archived"}),
},
"k8s_namespace": {
Type: schema.TypeString,
Description: "kubernetes (or helm) namespace to be used. If modified deletes current environment and creates a new one",
Optional: true,
ForceNew: true,
},
},
CustomizeDiff: customdiff.ValidateChange("template_id", func(ctx context.Context, oldValue, newValue, meta interface{}) error {
if oldValue != "" && oldValue != newValue {
Expand Down
14 changes: 14 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: "/terragrunt/directory/",
VcsCommandsAlias: "alias",
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: "namespace",
}

updatedEnvironment := client.Environment{
Expand All @@ -59,6 +60,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
VcsCommandsAlias: "alias2",
IsRemoteBackend: &isRemoteBackendTrue,
IsArchived: boolPtr(true),
K8sNamespace: "namespace",
}

template := client.Template{
Expand All @@ -79,6 +81,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"force_destroy": true,
"vcs_commands_alias": environment.VcsCommandsAlias,
"is_remote_backend": *(environment.IsRemoteBackend),
"k8s_namespace": environment.K8sNamespace,
}

if environment.IsArchived != nil {
Expand All @@ -98,6 +101,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"force_destroy": true,
"vcs_commands_alias": environment.VcsCommandsAlias,
"is_remote_backend": *(environment.IsRemoteBackend),
"k8s_namespace": environment.K8sNamespace,
}

if environment.IsArchived != nil {
Expand Down Expand Up @@ -128,6 +132,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "revision", environment.LatestDeploymentLog.BlueprintRevision),
resource.TestCheckResourceAttr(accessor, "is_remote_backend", "false"),
resource.TestCheckResourceAttr(accessor, "output", string(updatedEnvironment.LatestDeploymentLog.Output)),
resource.TestCheckResourceAttr(accessor, "k8s_namespace", environment.K8sNamespace),
resource.TestCheckNoResourceAttr(accessor, "deploy_on_push"),
resource.TestCheckNoResourceAttr(accessor, "run_plan_on_pull_requests"),
),
Expand All @@ -146,6 +151,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "is_remote_backend", "true"),
resource.TestCheckResourceAttr(accessor, "output", string(updatedEnvironment.LatestDeploymentLog.Output)),
resource.TestCheckResourceAttr(accessor, "is_inactive", "true"),
resource.TestCheckResourceAttr(accessor, "k8s_namespace", updatedEnvironment.K8sNamespace),
resource.TestCheckNoResourceAttr(accessor, "deploy_on_push"),
resource.TestCheckNoResourceAttr(accessor, "run_plan_on_pull_requests"),
),
Expand All @@ -166,6 +172,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
Expand Down Expand Up @@ -489,6 +496,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(environment.Id).Times(3).Return(environment, nil)
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down Expand Up @@ -556,6 +564,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
Enabled: true,
Cron: driftDetectionCron,
},
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
Expand Down Expand Up @@ -636,6 +645,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
Enabled: true,
Cron: driftDetectionCron,
},
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Name: updatedEnvironment.Name,
Expand Down Expand Up @@ -1787,6 +1797,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(client.Environment{}, errors.New("error"))
})

Expand Down Expand Up @@ -1818,6 +1829,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(2).Return(client.ConfigurationChanges{}, nil)
mock.EXPECT().EnvironmentUpdate(updatedEnvironment.Id, client.EnvironmentUpdate{
Expand Down Expand Up @@ -1859,6 +1871,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
"force_destroy": true,
"terragrunt_working_directory": environment.TerragruntWorkingDirectory,
"vcs_commands_alias": environment.VcsCommandsAlias,
"k8s_namespace": environment.K8sNamespace,
}),
ExpectError: regexp.MustCompile("failed deploying environment: error"),
},
Expand Down Expand Up @@ -1902,6 +1915,7 @@ func TestUnitEnvironmentResource(t *testing.T) {
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(gomock.Any()).Return(client.Environment{}, errors.New("error"))
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
Expand Down

0 comments on commit a79eb73

Please sign in to comment.