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
#383)

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

* minor rename of variable
  • Loading branch information
TomerHeber authored May 18, 2022
1 parent 6a9dfde commit 68281e1
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 61 deletions.
8 changes: 4 additions & 4 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ConfigurationVariable struct {
Type *ConfigurationVariableType `json:"type,omitempty"`
Schema *ConfigurationVariableSchema `json:"schema,omitempty"`
ToDelete *bool `json:"toDelete,omitempty"`
IsReadonly *bool `json:"isReadonly,omitempty"`
IsReadOnly *bool `json:"isReadonly,omitempty"`
IsRequired *bool `json:"isRequired,omitempty"`
Regex string `json:"regex,omitempty"`
}
Expand All @@ -57,7 +57,7 @@ type ConfigurationVariableCreateParams struct {
EnumValues []string
Description string
Format Format
IsReadonly bool
IsReadOnly bool
IsRequired bool
Regex string
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (client *ApiClient) ConfigurationVariableCreate(params ConfigurationVariabl
"type": params.Type,
"organizationId": organizationId,
"isRequired": params.IsRequired,
"isReadonly": params.IsReadonly,
"isReadonly": params.IsReadOnly,
"regex": params.Regex,
}
if params.Scope != ScopeGlobal {
Expand Down Expand Up @@ -189,7 +189,7 @@ func (client *ApiClient) ConfigurationVariableUpdate(updateParams ConfigurationV
"type": commonParams.Type,
"organizationId": organizationId,
"isRequired": commonParams.IsRequired,
"isReadonly": commonParams.IsReadonly,
"isReadonly": commonParams.IsReadOnly,
"regex": commonParams.Regex,
}
if commonParams.Scope != ScopeGlobal {
Expand Down
14 changes: 7 additions & 7 deletions client/configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var _ = Describe("Configuration Variable", func() {
Type: "string",
Format: HCL,
}
isReadonly := true
isReadOnly := true
isRequired := true

mockConfigurationVariable := ConfigurationVariable{
Expand All @@ -33,7 +33,7 @@ var _ = Describe("Configuration Variable", func() {
ScopeId: "project-123",
UserId: "user|123",
Schema: &schema,
IsReadonly: &isReadonly,
IsReadOnly: &isReadOnly,
IsRequired: &isRequired,
Regex: "regex",
}
Expand All @@ -50,7 +50,7 @@ var _ = Describe("Configuration Variable", func() {
ScopeId: "project-123",
UserId: "user|123",
Schema: &schema,
IsReadonly: &isReadonly,
IsReadOnly: &isReadOnly,
IsRequired: &isRequired,
Regex: "regex",
}
Expand Down Expand Up @@ -123,7 +123,7 @@ var _ = Describe("Configuration Variable", func() {
"scope": mockConfig.Scope,
"type": *mockConfig.Type,
"schema": schema,
"isReadonly": *mockConfig.IsReadonly,
"isReadonly": *mockConfig.IsReadOnly,
"isRequired": *mockConfig.IsRequired,
"regex": mockConfig.Regex,
}}
Expand Down Expand Up @@ -151,7 +151,7 @@ var _ = Describe("Configuration Variable", func() {
Type: *mockConfig.Type,
EnumValues: nil,
Format: mockConfig.Schema.Format,
IsReadonly: *mockConfig.IsReadonly,
IsReadOnly: *mockConfig.IsReadOnly,
IsRequired: *mockConfig.IsRequired,
Regex: mockConfig.Regex,
},
Expand Down Expand Up @@ -227,7 +227,7 @@ var _ = Describe("Configuration Variable", func() {
"schema": map[string]interface{}{
"type": mockConfigurationVariable.Schema.Type,
},
"isReadonly": *mockConfigurationVariable.IsReadonly,
"isReadonly": *mockConfigurationVariable.IsReadOnly,
"isRequired": *mockConfigurationVariable.IsRequired,
"regex": mockConfigurationVariable.Regex,
}}
Expand All @@ -250,7 +250,7 @@ var _ = Describe("Configuration Variable", func() {
ScopeId: mockConfigurationVariable.ScopeId,
Type: *mockConfigurationVariable.Type,
EnumValues: nil,
IsReadonly: *mockConfigurationVariable.IsReadonly,
IsReadOnly: *mockConfigurationVariable.IsReadOnly,
IsRequired: *mockConfigurationVariable.IsRequired,
Regex: mockConfigurationVariable.Regex,
},
Expand Down
31 changes: 31 additions & 0 deletions client/environment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
package client

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ConfigurationVariableType int

func (c *ConfigurationVariableType) ReadResourceData(fieldName string, d *schema.ResourceData) error {
val := d.Get(fieldName).(string)
intVal, ok := VariableTypes[val]
if !ok {
return fmt.Errorf("unknown configuration variable type %s", val)
}
*c = intVal

return nil
}

func (c *ConfigurationVariableType) WriteResourceData(fieldName string, d *schema.ResourceData) error {
val := *c
valStr := ""
if val == 0 {
valStr = "environment"
} else if val == 1 {
valStr = "terraform"
} else {
return fmt.Errorf("unknown configuration variable type %d", val)
}

return d.Set(fieldName, valStr)
}

const (
ConfigurationVariableTypeEnvironment ConfigurationVariableType = 0
ConfigurationVariableTypeTerraform ConfigurationVariableType = 1
Expand Down
21 changes: 4 additions & 17 deletions env0/data_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,14 @@ func dataConfigurationVariableRead(ctx context.Context, d *schema.ResourceData,
return err
}

d.SetId(variable.Id)
d.Set("name", variable.Name)
d.Set("description", variable.Description)
d.Set("value", variable.Value)
d.Set("is_sensitive", variable.IsSensitive)
d.Set("scope", variable.Scope)
d.Set("enum", variable.Schema.Enum)
d.Set("is_read_only", variable.IsReadonly)
d.Set("is_required", variable.IsRequired)
d.Set("regex", variable.Regex)
if err := writeResourceData(&variable, d); err != nil {
return diag.Errorf("schema resource data serialization failed: %v", err)
}

d.Set("enum", variable.Schema.Enum)
if variable.Schema.Format != client.Text {
d.Set("format", string(variable.Schema.Format))
}
if *variable.Type == client.ConfigurationVariableTypeEnvironment {
d.Set("type", "environment")
} else if *variable.Type == client.ConfigurationVariableTypeTerraform {
d.Set("type", "terraform")
} else {
return diag.Errorf("Unknown variable type: %d", int(*variable.Type))
}

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
Scope: client.ScopeEnvironment,
Type: &variableType,
Schema: &client.ConfigurationVariableSchema{Type: "string", Format: client.HCL},
IsReadonly: &isReadonly,
IsReadOnly: &isReadonly,
IsRequired: &isRequired,
Regex: "regex",
}
Expand All @@ -45,7 +45,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "scope", string(configurationVariable.Scope)),
resource.TestCheckResourceAttr(accessor, "is_sensitive", strconv.FormatBool(*configurationVariable.IsSensitive)),
resource.TestCheckResourceAttr(accessor, "format", string(configurationVariable.Schema.Format)),
resource.TestCheckResourceAttr(accessor, "is_read_only", strconv.FormatBool(*configurationVariable.IsReadonly)),
resource.TestCheckResourceAttr(accessor, "is_read_only", strconv.FormatBool(*configurationVariable.IsReadOnly)),
resource.TestCheckResourceAttr(accessor, "is_required", strconv.FormatBool(*configurationVariable.IsRequired)),
resource.TestCheckResourceAttr(accessor, "regex", "regex"),
)
Expand Down
6 changes: 3 additions & 3 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func resourceConfigurationVariableCreate(ctx context.Context, d *schema.Resource
EnumValues: actualEnumValues,
Description: description,
Format: format,
IsReadonly: isReadOnly,
IsReadOnly: isReadOnly,
IsRequired: isRequired,
Regex: regex,
})
Expand Down Expand Up @@ -232,7 +232,7 @@ func resourceConfigurationVariableRead(ctx context.Context, d *schema.ResourceDa
d.Set("description", variable.Description)
d.Set("value", variable.Value)
d.Set("is_sensitive", variable.IsSensitive)
d.Set("is_read_only", variable.IsReadonly)
d.Set("is_read_only", variable.IsReadOnly)
d.Set("is_required", variable.IsRequired)
d.Set("regex", variable.Regex)
if variable.Type != nil && *variable.Type == client.ConfigurationVariableTypeTerraform {
Expand Down Expand Up @@ -295,7 +295,7 @@ func resourceConfigurationVariableUpdate(ctx context.Context, d *schema.Resource
EnumValues: actualEnumValues,
Description: description,
Format: format,
IsReadonly: isReadOnly,
IsReadOnly: isReadOnly,
IsRequired: isRequired,
Regex: regex,
}})
Expand Down
Loading

0 comments on commit 68281e1

Please sign in to comment.