From 47dd991fe579a267e6c26675e25dd5b9c81c3f92 Mon Sep 17 00:00:00 2001 From: Petro Protsakh Date: Tue, 8 Nov 2022 23:08:34 +0200 Subject: [PATCH 1/3] SCALRCORE-24000 Added var_files validation --- CHANGELOG.md | 4 ++++ docs/resources/scalr_workspace.md | 2 +- scalr/resource_scalr_workspace.go | 26 ++++++++++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a4d7932..6ecbf45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `module_verion`: data source: change relation from latest-module-version to module-version ([#181](https://github.com/Scalr/terraform-provider-scalr/pull/181)) +### Fixed + +- panic when creating workspace with empty var file value ([#191](https://github.com/Scalr/terraform-provider-scalr/pull/191)) + ## [1.0.0-rc38] - 2022-10-20 ### Added diff --git a/docs/resources/scalr_workspace.md b/docs/resources/scalr_workspace.md index daf2d389..7401393c 100644 --- a/docs/resources/scalr_workspace.md +++ b/docs/resources/scalr_workspace.md @@ -107,7 +107,7 @@ resource "scalr_workspace" "cli-driven" { * `execution-mode` - (Optional) Which execution mode to use. Valid values are `remote` and `local`. When set to `local`, the workspace will be used for state storage only. Defaults to `remote` (not set, backend default is used). * `terraform_version` - (Optional) The version of Terraform to use for this workspace. Defaults to the latest available version. * `working_directory` - (Optional) A relative path that Terraform will be run in. Defaults to the root of the repository `""`. -var_files` - (Optional) A list of paths to the `.tfvars` file(s) to be used as part of the workspace configuration. +* `var_files` - (Optional) A list of paths to the `.tfvars` file(s) to be used as part of the workspace configuration. * `run_operation_timeout` - (Optional) The number of minutes run operation can be executed before termination. Defaults to `0` (not set, backend default is used). * `module_version_id` - (Optional) The identifier of a module version in the format `modver-`. This attribute conflicts with `vcs_provider_id` and `vcs_repo` attributes. * `agent_pool_id` - (Optional) The identifier of an agent pool in the format `apool-`. diff --git a/scalr/resource_scalr_workspace.go b/scalr/resource_scalr_workspace.go index 6c2d5dd7..cd9b809f 100644 --- a/scalr/resource_scalr_workspace.go +++ b/scalr/resource_scalr_workspace.go @@ -292,6 +292,22 @@ func parseTriggerPrefixDefinitions(vcsRepo map[string]interface{}) ([]string, er return triggerPrefixes, nil } +func parseVarFilesDefinitions(d *schema.ResourceData) ([]string, error) { + varFiles := make([]string, 0) + + varFileIds := d.Get("var_files").([]interface{}) + err := ValidateIDsDefinitions(varFileIds) + if err != nil { + return nil, fmt.Errorf("Got error during parsing var files: %s", err.Error()) + } + + for _, varFileId := range varFileIds { + varFiles = append(varFiles, varFileId.(string)) + } + + return varFiles, nil +} + func resourceScalrWorkspaceCreate(d *schema.ResourceData, meta interface{}) error { scalrClient := meta.(*scalr.Client) @@ -390,13 +406,11 @@ func resourceScalrWorkspaceCreate(d *schema.ResourceData, meta interface{}) erro } } - if v, ok := d.Get("var_files").([]interface{}); ok { - varFiles := make([]string, 0) - for _, varFile := range v { - varFiles = append(varFiles, varFile.(string)) - } - options.VarFiles = varFiles + varFiles, err := parseVarFilesDefinitions(d) + if err != nil { + return err } + options.VarFiles = varFiles if tagIDs, ok := d.GetOk("tag_ids"); ok { tagIDsList := tagIDs.(*schema.Set).List() From dd40625701a8040c2c4018c0c19424e4b553fd2d Mon Sep 17 00:00:00 2001 From: Petro Protsakh Date: Fri, 11 Nov 2022 15:41:22 +0200 Subject: [PATCH 2/3] SCALRCORE-24000 Add validation on resource update --- scalr/resource_scalr_workspace.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scalr/resource_scalr_workspace.go b/scalr/resource_scalr_workspace.go index cd9b809f..02a5b9bd 100644 --- a/scalr/resource_scalr_workspace.go +++ b/scalr/resource_scalr_workspace.go @@ -609,13 +609,11 @@ func resourceScalrWorkspaceUpdate(d *schema.ResourceData, meta interface{}) erro options.TerraformVersion = scalr.String(tfVersion.(string)) } - if v, ok := d.Get("var_files").([]interface{}); ok { - varFiles := make([]string, 0) - for _, varFile := range v { - varFiles = append(varFiles, varFile.(string)) - } - options.VarFiles = varFiles + varFiles, err := parseVarFilesDefinitions(d) + if err != nil { + return err } + options.VarFiles = varFiles options.WorkingDirectory = scalr.String(d.Get("working_directory").(string)) @@ -675,7 +673,7 @@ func resourceScalrWorkspaceUpdate(d *schema.ResourceData, meta interface{}) erro } log.Printf("[DEBUG] Update workspace %s", id) - _, err := scalrClient.Workspaces.Update(ctx, id, options) + _, err = scalrClient.Workspaces.Update(ctx, id, options) if err != nil { return fmt.Errorf( "Error updating workspace %s: %v", id, err) From 2cfd34b2090e2d010b0207b2f6f784da3cec55f4 Mon Sep 17 00:00:00 2001 From: Petro Protsakh Date: Fri, 11 Nov 2022 16:44:27 +0200 Subject: [PATCH 3/3] SCALRCORE-24000 Added var_files validation --- scalr/resource_scalr_workspace.go | 43 ++++++++++++------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/scalr/resource_scalr_workspace.go b/scalr/resource_scalr_workspace.go index 02a5b9bd..f8c9499a 100644 --- a/scalr/resource_scalr_workspace.go +++ b/scalr/resource_scalr_workspace.go @@ -88,7 +88,10 @@ func resourceScalrWorkspace() *schema.Resource { "var_files": { Type: schema.TypeList, Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.NoZeroValues, + }, }, "operations": { @@ -292,22 +295,6 @@ func parseTriggerPrefixDefinitions(vcsRepo map[string]interface{}) ([]string, er return triggerPrefixes, nil } -func parseVarFilesDefinitions(d *schema.ResourceData) ([]string, error) { - varFiles := make([]string, 0) - - varFileIds := d.Get("var_files").([]interface{}) - err := ValidateIDsDefinitions(varFileIds) - if err != nil { - return nil, fmt.Errorf("Got error during parsing var files: %s", err.Error()) - } - - for _, varFileId := range varFileIds { - varFiles = append(varFiles, varFileId.(string)) - } - - return varFiles, nil -} - func resourceScalrWorkspaceCreate(d *schema.ResourceData, meta interface{}) error { scalrClient := meta.(*scalr.Client) @@ -406,11 +393,13 @@ func resourceScalrWorkspaceCreate(d *schema.ResourceData, meta interface{}) erro } } - varFiles, err := parseVarFilesDefinitions(d) - if err != nil { - return err + if v, ok := d.Get("var_files").([]interface{}); ok { + varFiles := make([]string, 0) + for _, varFile := range v { + varFiles = append(varFiles, varFile.(string)) + } + options.VarFiles = varFiles } - options.VarFiles = varFiles if tagIDs, ok := d.GetOk("tag_ids"); ok { tagIDsList := tagIDs.(*schema.Set).List() @@ -609,11 +598,13 @@ func resourceScalrWorkspaceUpdate(d *schema.ResourceData, meta interface{}) erro options.TerraformVersion = scalr.String(tfVersion.(string)) } - varFiles, err := parseVarFilesDefinitions(d) - if err != nil { - return err + if v, ok := d.Get("var_files").([]interface{}); ok { + varFiles := make([]string, 0) + for _, varFile := range v { + varFiles = append(varFiles, varFile.(string)) + } + options.VarFiles = varFiles } - options.VarFiles = varFiles options.WorkingDirectory = scalr.String(d.Get("working_directory").(string)) @@ -673,7 +664,7 @@ func resourceScalrWorkspaceUpdate(d *schema.ResourceData, meta interface{}) erro } log.Printf("[DEBUG] Update workspace %s", id) - _, err = scalrClient.Workspaces.Update(ctx, id, options) + _, err := scalrClient.Workspaces.Update(ctx, id, options) if err != nil { return fmt.Errorf( "Error updating workspace %s: %v", id, err)