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

Fix: configuration variable defined in env0_environment resource gett… #768

Merged
merged 3 commits into from
Dec 13, 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
2 changes: 1 addition & 1 deletion .github/workflows/pr-labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Updated Paths
uses: actions/labeler@main # Reads from .github/labeler.yml
uses: actions/labeler@v4 # Reads from .github/labeler.yml
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
ScopeEnvironment Scope = "ENVIRONMENT"
ScopeDeployment Scope = "DEPLOYMENT"
ScopeDeploymentLog Scope = "DEPLOYMENT_LOG"
ScopeWorkflow Scope = "WORKFLOW"
)

type Format string
Expand Down Expand Up @@ -104,6 +105,8 @@ func (client *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId stri
return nil, errors.New("no api to fetch configuration variables by deployment")
case scope == ScopeDeploymentLog:
params["deploymentLogId"] = scopeId
case scope == ScopeWorkflow:
params["workflowEnvironmentId"] = scopeId
}
err = client.http.Get("/configuration", params, &result)
if err != nil {
Expand Down
27 changes: 21 additions & 6 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,12 @@ func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta i
return diag.Errorf("could not get environment: %v", err)
}

environmentConfigurationVariables, err := apiClient.ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id)
scope := client.ScopeEnvironment
if _, ok := d.GetOk("sub_environment_configuration"); ok {
scope = client.ScopeWorkflow
}

environmentConfigurationVariables, err := apiClient.ConfigurationVariablesByScope(scope, environment.Id)
if err != nil {
return diag.Errorf("could not get environment configuration variables: %v", err)
}
Expand Down Expand Up @@ -711,7 +716,7 @@ func deploy(d *schema.ResourceData, apiClient client.ApiClientInterface) diag.Di
for i, subEnvironment := range subEnvironments {
configuration := d.Get(fmt.Sprintf("sub_environment_configuration.%d.configuration", i)).([]interface{})
configurationChanges := getConfigurationVariablesFromSchema(configuration)
configurationChanges = getUpdateConfigurationVariables(configurationChanges, subEnvironment.Id, apiClient)
configurationChanges = getUpdateConfigurationVariables(configurationChanges, subEnvironment.Id, client.ScopeWorkflow, apiClient)

for i := range configurationChanges {
configurationChanges[i].Scope = client.ScopeEnvironment
Expand Down Expand Up @@ -936,7 +941,11 @@ func getDeployPayload(d *schema.ResourceData, apiClient client.ApiClientInterfac

if configuration, ok := d.GetOk("configuration"); ok && isRedeploy {
configurationChanges := getConfigurationVariablesFromSchema(configuration.([]interface{}))
configurationChanges = getUpdateConfigurationVariables(configurationChanges, d.Get("id").(string), apiClient)
scope := client.ScopeEnvironment
if _, ok := d.GetOk("sub_environment_configuration"); ok {
scope = client.ScopeWorkflow
}
configurationChanges = getUpdateConfigurationVariables(configurationChanges, d.Get("id").(string), scope, apiClient)
payload.ConfigurationChanges = &configurationChanges
}

Expand All @@ -961,8 +970,8 @@ func getTTl(date string) client.TTL {
}
}

func getUpdateConfigurationVariables(configurationChanges client.ConfigurationChanges, environmentId string, apiClient client.ApiClientInterface) client.ConfigurationChanges {
existVariables, err := apiClient.ConfigurationVariablesByScope(client.ScopeEnvironment, environmentId)
func getUpdateConfigurationVariables(configurationChanges client.ConfigurationChanges, environmentId string, scope client.Scope, apiClient client.ApiClientInterface) client.ConfigurationChanges {
existVariables, err := apiClient.ConfigurationVariablesByScope(scope, environmentId)
if err != nil {
diag.Errorf("could not get environment configuration variables: %v", err)
}
Expand Down Expand Up @@ -1133,7 +1142,13 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta
}
apiClient := meta.(client.ApiClientInterface)
d.SetId(environment.Id)
environmentConfigurationVariables, err := apiClient.ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id)

scope := client.ScopeEnvironment
if environment.LatestDeploymentLog.WorkflowFile != nil && len(environment.LatestDeploymentLog.WorkflowFile.Environments) > 0 {
scope = client.ScopeWorkflow
}

environmentConfigurationVariables, err := apiClient.ConfigurationVariablesByScope(scope, environment.Id)
if err != nil {
return nil, fmt.Errorf("could not get environment configuration variables: %v", err)
}
Expand Down
35 changes: 31 additions & 4 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2179,9 +2179,32 @@ func TestUnitEnvironmentWithSubEnvironment(t *testing.T) {
},
}

configurationVariable := client.ConfigurationVariable{
Value: "v1",
Name: "n1",
Type: (*client.ConfigurationVariableType)(intPtr(0)),
Schema: &client.ConfigurationVariableSchema{
Type: "string",
},
}

environmentCreatePayload := client.EnvironmentCreate{
Name: environment.Name,
ProjectId: environment.ProjectId,
ConfigurationChanges: &client.ConfigurationChanges{
{
Name: "n1",
Value: "v1",
Scope: client.ScopeDeployment,
IsSensitive: boolPtr(false),
IsReadOnly: boolPtr(false),
IsRequired: boolPtr(false),
Schema: &client.ConfigurationVariableSchema{
Type: "string",
},
Type: (*client.ConfigurationVariableType)(intPtr(0)),
},
},
DeployRequest: &client.DeployRequest{
BlueprintId: environment.BlueprintId,
SubEnvironments: map[string]client.SubEnvironment{
Expand Down Expand Up @@ -2220,6 +2243,10 @@ func TestUnitEnvironmentWithSubEnvironment(t *testing.T) {
project_id = "%s"
template_id = "%s"
force_destroy = true
configuration {
name = "n1"
value = "v1"
}
sub_environment_configuration {
alias = "%s"
revision = "%s"
Expand Down Expand Up @@ -2309,15 +2336,15 @@ func TestUnitEnvironmentWithSubEnvironment(t *testing.T) {
mock.EXPECT().Template(environmentCreatePayload.DeployRequest.BlueprintId).Times(1).Return(template, nil),
mock.EXPECT().EnvironmentCreate(environmentCreatePayload).Times(1).Return(environment, nil),
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeWorkflow, environment.Id).Times(1).Return(client.ConfigurationChanges{configurationVariable}, nil),
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, subEnvrionmentWithId.Id).Times(1).Return(subEnvironment.Configuration, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeWorkflow, environment.Id).Times(1).Return(client.ConfigurationChanges{configurationVariable}, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeWorkflow, subEnvrionmentWithId.Id).Times(1).Return(subEnvironment.Configuration, nil),
mock.EXPECT().EnvironmentDeploy(environment.Id, deployRequest).Times(1).Return(client.EnvironmentDeployResponse{
Id: environment.Id,
}, nil),
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil),
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeWorkflow, environment.Id).Times(1).Return(client.ConfigurationChanges{configurationVariable}, nil),
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1),
)
})
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ resource "env0_environment" "workflow-environment" {
template_id = env0_template.workflow_template.id
approve_plan_automatically = true

configuration {
name = "n1"
value = "v1"
}

sub_environment_configuration {
alias = "rootService1"
revision = "master"
Expand Down
Loading