Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Added GetOkAllowZero method to the ResourceData #14139

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions helper/schema/resource_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ func (d *ResourceData) GetOk(key string) (interface{}, bool) {
return r.Value, exists
}

// GetOkAllowZero returns the data for the given key and whether or
// not the key has been set to some value at some point.
// This method does not check if the value is non-zero, unlike GetOk.
//
// The first result will not necessarilly be nil if the value doesn't exist.
// The second result should be checked to determine this information.
func (d *ResourceData) GetOkAllowZero(key string) (interface{}, bool) {
r := d.getRaw(key, getSourceSet)
exists := r.Exists && !r.Computed

return r.Value, exists
}

func (d *ResourceData) getRaw(key string, level getSource) getResult {
var parts []string
if key != "" {
Expand Down
301 changes: 301 additions & 0 deletions helper/schema/resource_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,307 @@ func TestResourceDataGetOk(t *testing.T) {
}
}

func TestResourceDataGetOkAllowZero(t *testing.T) {
cases := []struct {
Schema map[string]*Schema
State *terraform.InstanceState
Diff *terraform.InstanceDiff
Key string
Value interface{}
Ok bool
}{
/*
* Primitives
*/
// This case is regarded to be OK since
// the Computed field in the diff is not set.
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},

State: nil,

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "",
},
},
},

Key: "availability_zone",
Value: "",
Ok: true,
},

{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},

State: nil,

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
},
},
},

Key: "availability_zone",
Value: "",
Ok: false,
},

{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},

State: nil,

Diff: nil,

Key: "availability_zone",
Value: "",
Ok: false,
},

/*
* Lists
*/

{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeInt},
},
},

State: nil,

Diff: nil,

Key: "ports",
Value: []interface{}{},
Ok: false,
},

/*
* Map
*/

{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeMap,
Optional: true,
},
},

State: nil,

Diff: nil,

Key: "ports",
Value: map[string]interface{}{},
Ok: false,
},

/*
* Set
*/

{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int { return a.(int) },
},
},

State: nil,

Diff: nil,

Key: "ports",
Value: []interface{}{},
Ok: false,
},

{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int { return a.(int) },
},
},

State: nil,

Diff: nil,

Key: "ports.0",
Value: 0,
Ok: false,
},

{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int { return a.(int) },
},
},

State: nil,

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "0",
New: "0",
},
},
},

Key: "ports",
Value: []interface{}{},
Ok: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another point: I re-checked and thought twice about the correctness of the test cases, then I noticed this is not the expected behavior.

},

// Unlike the GetOk, the GetOkAllowZero treats zero-value as zero-value,
// rather than an unset value.
{
Schema: map[string]*Schema{
"from_port": &Schema{
Type: TypeInt,
Optional: true,
},
},

State: nil,

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"from_port": &terraform.ResourceAttrDiff{
Old: "",
New: "0",
},
},
},

Key: "from_port",
Value: 0,
Ok: true,
},

{
Schema: map[string]*Schema{
"from_port": &Schema{
Type: TypeInt,
Optional: true,
},
},

State: &terraform.InstanceState{
Attributes: map[string]string{
"from_port": "80",
},
},

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"from_port": &terraform.ResourceAttrDiff{
Old: "80",
New: "0",
NewRemoved: true,
},
},
},

Key: "from_port",
Value: 0,
Ok: false,
},

{
Schema: map[string]*Schema{
"from_port": &Schema{
Type: TypeInt,
Optional: true,
},
},

State: &terraform.InstanceState{
Attributes: map[string]string{
"from_port": "80",
},
},

Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"from_port": &terraform.ResourceAttrDiff{
Old: "80",
New: "0",
},
},
},

Key: "from_port",
Value: 0,
Ok: true,
},
}

for i, tc := range cases {
d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff)
if err != nil {
t.Fatalf("err: %s", err)
}

v, ok := d.GetOkAllowZero(tc.Key)
if s, ok := v.(*Set); ok {
v = s.List()
}

if !reflect.DeepEqual(v, tc.Value) {
t.Fatalf("Bad: %d\n\n%#v", i, v)
}
if ok != tc.Ok {
t.Fatalf("%d: expected ok: %t, got: %t", i, tc.Ok, ok)
}
}
}

func TestResourceDataTimeout(t *testing.T) {
cases := []struct {
Name string
Expand Down