-
Notifications
You must be signed in to change notification settings - Fork 13
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: Add glob_pattern validation when cd/PrPlan enabled #190
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9cc7b19
Fix: Add glob_pattern validation when cd/PrPlan enabled
liranfarage89 048a12f
Merge branch 'main' of https://github.com/env0/terraform-provider-env…
liranfarage89 02cf5a3
fixed tests
liranfarage89 754c9b4
updateResource validation + IT fix
liranfarage89 6d59854
changed auto_deploy_path_changes valiation to check 'false' case beca…
liranfarage89 1247c08
added failed test
liranfarage89 9e3efa6
fixed UT!
liranfarage89 1e5ef1a
removed comment
liranfarage89 e174b69
Merge branch 'main' of https://github.com/env0/terraform-provider-env…
liranfarage89 9f47cc3
refactoring
liranfarage89 f99f7e4
pr-comments
liranfarage89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,11 +81,10 @@ func resourceEnvironment() *schema.Resource { | |
Default: true, | ||
}, | ||
"auto_deploy_by_custom_glob": { | ||
Type: schema.TypeString, | ||
Description: "redeploy on file filter pattern", | ||
RequiredWith: []string{"auto_deploy_on_path_changes_only"}, | ||
Optional: true, | ||
Default: "", | ||
Type: schema.TypeString, | ||
Description: "redeploy on file filter pattern", | ||
Optional: true, | ||
Default: "", | ||
}, | ||
"deployment_id": { | ||
Type: schema.TypeString, | ||
|
@@ -220,7 +219,11 @@ func setEnvironmentConfigurationSchema(d *schema.ResourceData, configurationVari | |
func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
payload := getCreatePayload(d, apiClient) | ||
payload, createEnvPayloadErr := getCreatePayload(d, apiClient) | ||
|
||
if createEnvPayloadErr != nil { | ||
return diag.Errorf("%v", createEnvPayloadErr) | ||
} | ||
|
||
environment, err := apiClient.EnvironmentCreate(payload) | ||
if err != nil { | ||
|
@@ -285,7 +288,7 @@ func shouldDeploy(d *schema.ResourceData) bool { | |
} | ||
|
||
func shouldUpdate(d *schema.ResourceData) bool { | ||
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob") | ||
return d.HasChanges("name", "approve_plan_automatically", "deploy_on_push", "run_plan_on_pull_requests", "auto_deploy_by_custom_glob", "auto_deploy_on_path_changes_only") | ||
} | ||
|
||
func shouldUpdateTTL(d *schema.ResourceData) bool { | ||
|
@@ -303,7 +306,12 @@ func deploy(d *schema.ResourceData, apiClient client.ApiClientInterface) diag.Di | |
} | ||
|
||
func update(d *schema.ResourceData, apiClient client.ApiClientInterface) diag.Diagnostics { | ||
payload := getUpdatePayload(d) | ||
payload, updateEnvPayloadErr := getUpdatePayload(d) | ||
|
||
if updateEnvPayloadErr != nil { | ||
return diag.Errorf("%v", updateEnvPayloadErr) | ||
} | ||
|
||
_, err := apiClient.EnvironmentUpdate(d.Id(), payload) | ||
if err != nil { | ||
return diag.Errorf("could not update environment: %v", err) | ||
|
@@ -337,7 +345,7 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta | |
return nil | ||
} | ||
|
||
func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterface) client.EnvironmentCreate { | ||
func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterface) (client.EnvironmentCreate, diag.Diagnostics) { | ||
payload := client.EnvironmentCreate{} | ||
|
||
if name, ok := d.GetOk("name"); ok { | ||
|
@@ -351,8 +359,8 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac | |
payload.WorkspaceName = workspace.(string) | ||
} | ||
|
||
continuousDeployment := d.Get("deploy_on_push").(bool) | ||
if d.HasChange("deploy_on_push") { | ||
continuousDeployment := d.Get("deploy_on_push").(bool) | ||
payload.ContinuousDeployment = &continuousDeployment | ||
} | ||
|
||
|
@@ -361,15 +369,19 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac | |
payload.RequiresApproval = &requiresApproval | ||
} | ||
|
||
pullRequestPlanDeployments := d.Get("run_plan_on_pull_requests").(bool) | ||
if d.HasChange("run_plan_on_pull_requests") { | ||
pullRequestPlanDeployments := d.Get("run_plan_on_pull_requests").(bool) | ||
payload.PullRequestPlanDeployments = &pullRequestPlanDeployments | ||
} | ||
|
||
autoDeployOnPathChangesOnly := d.Get("auto_deploy_on_path_changes_only").(bool) | ||
payload.AutoDeployOnPathChangesOnly = &autoDeployOnPathChangesOnly | ||
|
||
payload.AutoDeployByCustomGlob = d.Get("auto_deploy_by_custom_glob").(string) | ||
err := assertDeploymentTriggers(payload.AutoDeployByCustomGlob, continuousDeployment, pullRequestPlanDeployments, autoDeployOnPathChangesOnly) | ||
if err != nil { | ||
return client.EnvironmentCreate{}, err | ||
} | ||
|
||
if configuration, ok := d.GetOk("configuration"); ok { | ||
configurationChanges := getConfigurationVariables(configuration.([]interface{})) | ||
|
@@ -384,10 +396,23 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac | |
|
||
payload.DeployRequest = &deployPayload | ||
|
||
return payload | ||
return payload, nil | ||
} | ||
|
||
func getUpdatePayload(d *schema.ResourceData) client.EnvironmentUpdate { | ||
func assertDeploymentTriggers(autoDeployByCustomGlob string, continuousDeployment bool, pullRequestPlanDeployments bool, autoDeployOnPathChangesOnly bool) diag.Diagnostics { | ||
if autoDeployByCustomGlob != "" { | ||
if (continuousDeployment == false) && | ||
(pullRequestPlanDeployments == false) { | ||
return diag.Errorf("run_plan_on_pull_requests or deploy_on_push must be enabled for auto_deploy_by_custom_glob") | ||
} | ||
if autoDeployOnPathChangesOnly == false { | ||
return diag.Errorf("cannot set auto_deploy_by_custom_glob when auto_deploy_on_path_changes_only is disabled") | ||
} | ||
} | ||
return nil | ||
} | ||
Comment on lines
+401
to
+412
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
|
||
func getUpdatePayload(d *schema.ResourceData) (client.EnvironmentUpdate, diag.Diagnostics) { | ||
payload := client.EnvironmentUpdate{} | ||
|
||
if name, ok := d.GetOk("name"); ok { | ||
|
@@ -397,19 +422,25 @@ func getUpdatePayload(d *schema.ResourceData) client.EnvironmentUpdate { | |
requiresApproval := !d.Get("approve_plan_automatically").(bool) | ||
payload.RequiresApproval = &requiresApproval | ||
} | ||
|
||
continuousDeployment := d.Get("deploy_on_push").(bool) | ||
if d.HasChange("deploy_on_push") { | ||
continuousDeployment := d.Get("deploy_on_push").(bool) | ||
payload.ContinuousDeployment = &continuousDeployment | ||
} | ||
pullRequestPlanDeployments := d.Get("run_plan_on_pull_requests").(bool) | ||
if d.HasChange("run_plan_on_pull_requests") { | ||
pullRequestPlanDeployments := d.Get("run_plan_on_pull_requests").(bool) | ||
payload.PullRequestPlanDeployments = &pullRequestPlanDeployments | ||
} | ||
autoDeployOnPathChangesOnly := d.Get("auto_deploy_on_path_changes_only").(bool) | ||
payload.AutoDeployOnPathChangesOnly = &autoDeployOnPathChangesOnly | ||
payload.AutoDeployByCustomGlob = d.Get("auto_deploy_by_custom_glob").(string) | ||
|
||
return payload | ||
err := assertDeploymentTriggers(payload.AutoDeployByCustomGlob, continuousDeployment, pullRequestPlanDeployments, autoDeployOnPathChangesOnly) | ||
if err != nil { | ||
return client.EnvironmentUpdate{}, err | ||
} | ||
|
||
return payload, nil | ||
} | ||
|
||
func getDeployPayload(d *schema.ResourceData, apiClient client.ApiClientInterface, isRedeploy bool) client.DeployRequest { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be redundant (I think* that d.get always returns the false value of that type)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f99f7e4