Skip to content

Commit

Permalink
improvements based on CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed May 13, 2022
1 parent d3cdc90 commit 7d06da7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
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).
* 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`.

### Handling drifts

Expand Down
2 changes: 1 addition & 1 deletion client/cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type AwsCredentialsCreatePayload struct {
}

type AwsCredentialsValuePayload struct {
RoleArn string `json:"roleArn" resource:"arn"`
RoleArn string `json:"roleArn" tfschema:"arn"`
ExternalId string `json:"externalId"`
AccessKeyId string `json:"accessKeyId"`
SecretAccessKey string `json:"secretAccessKey"`
Expand Down
10 changes: 6 additions & 4 deletions env0/resource_aws_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ func resourceAwsCredentialsRead(ctx context.Context, d *schema.ResourceData, met
return ResourceGetFailure("aws credentials", d, err)
}

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

return nil
}
Expand All @@ -133,8 +134,9 @@ func resourceAwsCredentialsImport(ctx context.Context, d *schema.ResourceData, m
return nil, err
}

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

return []*schema.ResourceData{d}, nil
}
8 changes: 4 additions & 4 deletions env0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func readResourceData(i interface{}, d *schema.ResourceData) error {
for i := 0; i < val.NumField(); i++ {
fieldName := val.Type().Field(i).Name
// Assumes golang is CamalCase and Terraform is snake_case.
// This behavior can be overrided be used in the 'resource' tag.
// This behavior can be overrided be used in the 'tfschema' tag.
fieldNameSC := toSnakeCase(fieldName)
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("resource"); ok {
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("tfschema"); ok {
// 'resource' tag found. Override to tag value.
fieldNameSC = resFieldName
}
Expand Down Expand Up @@ -107,9 +107,9 @@ func writeResourceData(i interface{}, d *schema.ResourceData) error {
for i := 0; i < val.NumField(); i++ {
fieldName := val.Type().Field(i).Name
// Assumes golang is CamalCase and Terraform is snake_case.
// This behavior can be overrided be used in the 'resource' tag.
// This behavior can be overrided be used in the 'tfschema' tag.
fieldNameSC := toSnakeCase(fieldName)
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("resource"); ok {
if resFieldName, ok := val.Type().Field(i).Tag.Lookup("tfschema"); ok {
// 'resource' tag found. Override to tag value.
fieldNameSC = resFieldName
}
Expand Down

0 comments on commit 7d06da7

Please sign in to comment.