diff --git a/env0/resource_configuration_variable.go b/env0/resource_configuration_variable.go index 7f6843ae..a6197eb7 100644 --- a/env0/resource_configuration_variable.go +++ b/env0/resource_configuration_variable.go @@ -143,50 +143,26 @@ func whichScope(d *schema.ResourceData) (client.Scope, string) { } func resourceConfigurationVariableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - apiClient := meta.(client.ApiClientInterface) scope, scopeId := whichScope(d) - name := d.Get("name").(string) - description := d.Get("description").(string) - value := d.Get("value").(string) - isSensitive := d.Get("is_sensitive").(bool) - typeAsString := d.Get("type").(string) - format := client.Format(d.Get("format").(string)) - isReadOnly := d.Get("is_read_only").(bool) - isRequired := d.Get("is_required").(bool) - regex := d.Get("regex").(string) + params := client.ConfigurationVariableCreateParams{Scope: scope, ScopeId: scopeId} + if err := readResourceData(¶ms, d); err != nil { + return diag.Errorf("schema resource data deserialization failed: %v", err) + } - if err := validateNilValue(isReadOnly, isRequired, value); err != nil { + if err := validateNilValue(params.IsReadOnly, params.IsRequired, params.Value); err != nil { return diag.Errorf(err.Error()) } - var type_ client.ConfigurationVariableType - switch typeAsString { - case "environment": - type_ = client.ConfigurationVariableTypeEnvironment - case "terraform": - type_ = client.ConfigurationVariableTypeTerraform - default: - return diag.Errorf("'type' can only receive either 'environment' or 'terraform': %s", typeAsString) - } - actualEnumValues, getEnumErr := getEnum(d, value) + actualEnumValues, getEnumErr := getEnum(d, params.Value) if getEnumErr != nil { return getEnumErr } - configurationVariable, err := apiClient.ConfigurationVariableCreate(client.ConfigurationVariableCreateParams{ - Name: name, - Value: value, - IsSensitive: isSensitive, - Scope: scope, - ScopeId: scopeId, - Type: type_, - EnumValues: actualEnumValues, - Description: description, - Format: format, - IsReadOnly: isReadOnly, - IsRequired: isRequired, - Regex: regex, - }) + params.EnumValues = actualEnumValues + + apiClient := meta.(client.ApiClientInterface) + + configurationVariable, err := apiClient.ConfigurationVariableCreate(params) if err != nil { return diag.Errorf("could not create configurationVariable: %v", err) } diff --git a/env0/resource_configuration_variable_test.go b/env0/resource_configuration_variable_test.go index 8b4d2b12..61d6f359 100644 --- a/env0/resource_configuration_variable_test.go +++ b/env0/resource_configuration_variable_test.go @@ -408,7 +408,7 @@ resource "%s" "test" { "value": configVar.Value, "type": 6, }), - ExpectError: regexp.MustCompile(`(Error: 'type' can only receive either 'environment' or 'terraform')`), + ExpectError: regexp.MustCompile("unknown configuration variable type 6"), }, }, } @@ -581,9 +581,9 @@ resource "%s" "test" { "name": newConfigVar.Name, "description": newConfigVar.Description, "value": newConfigVar.Value, - "type": newConfigVar.Type, + "type": "boooo", }), - ExpectError: regexp.MustCompile(`'type' can only receive either 'environment' or 'terraform'`), + ExpectError: regexp.MustCompile("unknown configuration variable type boooo"), }, }, } diff --git a/env0/utils.go b/env0/utils.go index 24979556..2cc0cf51 100644 --- a/env0/utils.go +++ b/env0/utils.go @@ -71,7 +71,20 @@ func readResourceData(i interface{}, d *schema.ResourceData) error { continue } - if customField, ok := field.Interface().(CustomResourceDataField); ok { + // custom field is a pointer. + if _, ok := field.Interface().(CustomResourceDataField); ok { + if field.IsNil() { + // Init the field a valid value (instead of nil). + field.Set(reflect.New(field.Type().Elem())) + } + if err := field.Interface().(CustomResourceDataField).ReadResourceData(fieldNameSC, d); err != nil { + return err + } + continue + } + + // custom field is a value, check the pointer. + if customField, ok := field.Addr().Interface().(CustomResourceDataField); ok { if err := customField.ReadResourceData(fieldNameSC, d); err != nil { return err } diff --git a/env0/utils_test.go b/env0/utils_test.go index b57be636..6d1a149b 100644 --- a/env0/utils_test.go +++ b/env0/utils_test.go @@ -199,6 +199,53 @@ func TestWriteCustomResourceData(t *testing.T) { assert.Equal(t, configurationVariable.Regex, d.Get("regex")) } +func TestReadByValueCustomResourceData(t *testing.T) { + d := schema.TestResourceDataRaw(t, dataConfigurationVariable().Schema, map[string]interface{}{ + "type": "terraform", + "name": "name", + "description": "description", + }) + + params := client.ConfigurationVariableCreateParams{} + + assert.Nil(t, readResourceData(¶ms, d)) + + assert.Equal(t, params.Name, "name") + assert.Equal(t, int(params.Type), 1) + assert.Equal(t, params.Description, "description") +} + +func TestReadByPointerCustomResourceData(t *testing.T) { + d := schema.TestResourceDataRaw(t, dataConfigurationVariable().Schema, map[string]interface{}{ + "type": "terraform", + "name": "name", + "description": "description", + }) + + params := client.ConfigurationVariable{} + + assert.Nil(t, readResourceData(¶ms, d)) + + assert.Equal(t, params.Name, "name") + assert.Equal(t, int(*params.Type), 1) + assert.Equal(t, params.Description, "description") +} + +func TestReadByPointerNilCustomResourceData(t *testing.T) { + d := schema.TestResourceDataRaw(t, dataConfigurationVariable().Schema, map[string]interface{}{ + "name": "name", + "description": "description", + }) + + params := client.ConfigurationVariable{} + + assert.Nil(t, readResourceData(¶ms, d)) + + assert.Equal(t, params.Name, "name") + assert.Nil(t, params.Type) + assert.Equal(t, params.Description, "description") +} + func TestWriteResourceDataSliceVariablesAgents(t *testing.T) { d := schema.TestResourceDataRaw(t, dataAgents().Schema, map[string]interface{}{})