Skip to content

Commit

Permalink
Feat: refactor resources/data to use the new serialization/deserializ… (
Browse files Browse the repository at this point in the history
#413)

* Feat: refactor resources/data to use the new serialization/deserialization logic

* added a unit test

* fix issues with custom read resource
  • Loading branch information
TomerHeber committed Jun 13, 2022
1 parent 24f515b commit 369c1ca
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
46 changes: 11 additions & 35 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params, 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)
}
Expand Down
6 changes: 3 additions & 3 deletions env0/resource_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
}
Expand Down Expand Up @@ -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"),
},
},
}
Expand Down
15 changes: 14 additions & 1 deletion env0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions env0/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params, 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(&params, 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(&params, 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{}{})

Expand Down

0 comments on commit 369c1ca

Please sign in to comment.