Skip to content

Commit

Permalink
Merge pull request #41255 from hashicorp/f-flex_write_only
Browse files Browse the repository at this point in the history
flex: add write only flex functions
  • Loading branch information
johnsonaj authored Feb 5, 2025
2 parents 3174d23 + a0c2dd2 commit b65159c
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/flex/write_only.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package flex

import (
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
)

type writeOnlyAttrGetter interface {
GetRawConfigAt(path cty.Path) (cty.Value, diag.Diagnostics)
Id() string
}

// GetWriteOnlyStringValue returns the string value of the write-only attribute from the config.
func GetWriteOnlyStringValue(d writeOnlyAttrGetter, path cty.Path, attrType cty.Type) (string, diag.Diagnostics) {
valueWO, diags := GetWriteOnlyValue(d, path, attrType)
if diags.HasError() {
return "", diags
}

var value string
if attrType == cty.String && !valueWO.IsNull() {
value = valueWO.AsString()
}

return value, diags
}

// GetWriteOnlyValue returns the value of the write-only attribute from the config.
func GetWriteOnlyValue(d writeOnlyAttrGetter, path cty.Path, attrType cty.Type) (cty.Value, diag.Diagnostics) {
var diags diag.Diagnostics

valueWO, di := d.GetRawConfigAt(path)
if di.HasError() {
diags = append(diags, di...)
return cty.Value{}, diags
}

if !valueWO.Type().Equals(attrType) {
return cty.Value{}, sdkdiag.AppendErrorf(diags, "invalid type (%s) for resource(%s)", attrType, d.Id())
}

return valueWO, diags
}
132 changes: 132 additions & 0 deletions internal/flex/write_only_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package flex_test

import (
"fmt"
"testing"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
)

type mockWriteOnlyAttrGetter struct {
path cty.Path
value cty.Value
}

func (m *mockWriteOnlyAttrGetter) GetRawConfigAt(path cty.Path) (cty.Value, diag.Diagnostics) {
if !path.Equals(m.path) {
return cty.NilVal, diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid config Path",
Detail: fmt.Sprintf("expected: %v, got: %v", m.path, path),
AttributePath: path,
},
}
}
return m.value, nil
}

func (m *mockWriteOnlyAttrGetter) Id() string {
return "id"
}

func TestGetWriteOnlyValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input cty.Path
setPath cty.Path
inputType cty.Type
value cty.Value
expectError bool
}{
"valid value type": {
input: cty.GetAttrPath("test_path"),
setPath: cty.GetAttrPath("test_path"),
inputType: cty.String,
value: cty.StringVal("test_value"),
},
"invalid value type": {
input: cty.GetAttrPath("test_path"),
setPath: cty.GetAttrPath("test_path"),
inputType: cty.String,
value: cty.BoolVal(true),
expectError: true,
},
"invalid path": {
input: cty.GetAttrPath("invalid_path"),
setPath: cty.GetAttrPath("test_path"),
inputType: cty.String,
value: cty.StringVal("test_value"),
expectError: true,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

m := mockWriteOnlyAttrGetter{
path: testCase.setPath,
value: testCase.value,
}
_, diags := flex.GetWriteOnlyValue(&m, testCase.input, testCase.inputType)

if testCase.expectError && !diags.HasError() {
t.Fatalf("expected error, got none")
}
})
}
}

func TestGetWriteOnlyStringValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
input cty.Path
setPath cty.Path
inputType cty.Type
value cty.Value
expectedValue string
}{
"valid value": {
input: cty.GetAttrPath("test_path"),
setPath: cty.GetAttrPath("test_path"),
inputType: cty.String,
value: cty.StringVal("test_value"),
expectedValue: "test_value",
},
"value empty string": {
input: cty.GetAttrPath("test_path"),
setPath: cty.GetAttrPath("test_path"),
inputType: cty.String,
value: cty.StringVal(""),
expectedValue: "",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

m := mockWriteOnlyAttrGetter{
path: testCase.setPath,
value: testCase.value,
}
value, diags := flex.GetWriteOnlyStringValue(&m, testCase.input, testCase.inputType)

if diags.HasError() {
t.Fatalf("unexpected error: %v", diags)
}

if testCase.expectedValue != value {
t.Fatalf("expected value: %s, got: %s", testCase.expectedValue, value)
}
})
}
}

0 comments on commit b65159c

Please sign in to comment.