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

Feat: refactor resources/data to use the new serialization/deserializ… #413

Merged
merged 5 commits into from
Jun 13, 2022
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
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serializer knows how to handle this use case.
see environment.go func (c *ConfigurationVariableType) ReadResourceData(fieldName string, d *schema.ResourceData) error {...

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.
sagilaufer1992 marked this conversation as resolved.
Show resolved Hide resolved
if customField, ok := field.Addr().Interface().(CustomResourceDataField); ok {
sagilaufer1992 marked this conversation as resolved.
Show resolved Hide resolved
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)
sagilaufer1992 marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, params.Description, "description")
}

func TestReadByPointerCustomResourceData(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional test for the use-case.

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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to be sure if not set in schema, params.Type remains nil.

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