From 366a1b51094162a9c041a72bbdd6248eba0d4b4b Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Mon, 16 May 2022 12:54:49 -0500 Subject: [PATCH] Feat: refactor resources/data to use the new serialization/deserialization logic --- DEVELOPMENT.md | 2 +- env0/data_aws_credentials.go | 5 +++-- env0/data_azure_credentials.go | 5 +++-- env0/data_configuration_variable.go | 25 +++++++------------------ env0/utils.go | 8 ++++++++ 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 05abf562..1c2501bb 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -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 diff --git a/env0/data_aws_credentials.go b/env0/data_aws_credentials.go index 8edcbf9e..9a025c3a 100644 --- a/env0/data_aws_credentials.go +++ b/env0/data_aws_credentials.go @@ -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 } diff --git a/env0/data_azure_credentials.go b/env0/data_azure_credentials.go index 18c81671..d438ab27 100644 --- a/env0/data_azure_credentials.go +++ b/env0/data_azure_credentials.go @@ -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 } diff --git a/env0/data_configuration_variable.go b/env0/data_configuration_variable.go index 02b7c95c..c6e940ab 100644 --- a/env0/data_configuration_variable.go +++ b/env0/data_configuration_variable.go @@ -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 { @@ -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(¶ms, 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 { @@ -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 { diff --git a/env0/utils.go b/env0/utils.go index 3d956628..16841da1 100644 --- a/env0/utils.go +++ b/env0/utils.go @@ -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 } @@ -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 }