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… #381

Merged
merged 2 commits into from
May 17, 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
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Check [resource_module.go](./env0/resource_module.go) that uses the utilities vs

Pay attention to the following caveats:
* The utilities leverage golang reflection. And work well for most simple types. Complex types may need additional code to be implemented.
* The golang fields are in CamalCase, while the terraform fields are in snake_case. They must match. E.g., ProjectName (golang) == project_name (Terraform). To override the default CamalCase to snake_case conversion you may use the tag `tfschema`.
* The golang fields are in CamalCase, while the terraform fields are in snake_case. They must match. E.g., ProjectName (golang) == project_name (Terraform). To override the default CamalCase to snake_case conversion you may use the tag `tfschema`. To ignore a field set the `tfschema` tag value to `-`.

### Handling drifts

Expand Down
5 changes: 3 additions & 2 deletions env0/data_aws_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func dataAwsCredentialsRead(ctx context.Context, d *schema.ResourceData, meta in
}
}

d.SetId(credentials.Id)
d.Set("name", credentials.Name)
if err := writeResourceData(&credentials, d); err != nil {
return diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions env0/data_azure_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func dataAzureCredentialsRead(ctx context.Context, d *schema.ResourceData, meta
}
}

d.SetId(credentials.Id)
d.Set("name", credentials.Name)
if err := writeResourceData(&credentials, d); err != nil {
return diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
}
Expand Down
25 changes: 7 additions & 18 deletions env0/data_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type ConfigurationVariableParams struct {
Scope client.Scope
ScopeId string
Scope client.Scope `tfschema:"-"`
ScopeId string `tfschema:"-"`
Id string
Name string
configurationType string
ConfigurationType string `json:"-" tfschema:"type"`
}

func dataConfigurationVariable() *schema.Resource {
Expand Down Expand Up @@ -121,22 +121,11 @@ func dataConfigurationVariable() *schema.Resource {

func dataConfigurationVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scope, scopeId := getScopeAndId(d)
id, idOk := d.GetOk("id")
name, nameOk := d.GetOk("name")
configurationType, configurationOk := d.GetOk("type")
parsedId, parsedName, parsedConfigurationType := "", "", ""

if idOk {
parsedId = id.(string)
}
if nameOk {
parsedName = name.(string)
params := ConfigurationVariableParams{Scope: scope, ScopeId: scopeId}
if err := readResourceData(&params, d); err != nil {
return diag.Errorf("schema resource data serialization failed: %v", err)
}
if configurationOk {
parsedConfigurationType = configurationType.(string)
}

params := ConfigurationVariableParams{scope, scopeId, parsedId, parsedName, parsedConfigurationType}

variable, err := getConfigurationVariable(params, meta)
if err != nil {
Expand Down Expand Up @@ -209,7 +198,7 @@ func getConfigurationVariable(params ConfigurationVariableParams, meta interface
}

name, nameOk := params.Name, params.Name != ""
typeString, ok := params.configurationType, params.configurationType != ""
typeString, ok := params.ConfigurationType, params.ConfigurationType != ""
type_ := -1
if ok {
if !nameOk {
Expand Down
8 changes: 8 additions & 0 deletions env0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func readResourceData(i interface{}, d *schema.ResourceData) error {
// This behavior can be overrided be used in the 'tfschema' tag.
fieldNameSC := toSnakeCase(fieldName)
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("tfschema"); ok {
if resFieldName == "-" {
continue
}

// 'resource' tag found. Override to tag value.
fieldNameSC = resFieldName
}
Expand Down Expand Up @@ -110,6 +114,10 @@ func writeResourceData(i interface{}, d *schema.ResourceData) error {
// This behavior can be overrided be used in the 'tfschema' tag.
fieldNameSC := toSnakeCase(fieldName)
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("tfschema"); ok {
if resFieldName == "-" {
continue
}

// 'resource' tag found. Override to tag value.
fieldNameSC = resFieldName
}
Expand Down