-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41255 from hashicorp/f-flex_write_only
flex: add write only flex functions
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |