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

Firewall uses v1 API if the priority is unset or has the default value. #500

Merged
merged 3 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 39 additions & 10 deletions google/api_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TerraformResourceData interface {
func getComputeApiVersion(d TerraformResourceData, resourceVersion ComputeApiVersion, features []Feature) ComputeApiVersion {
versions := map[ComputeApiVersion]struct{}{resourceVersion: struct{}{}}
for _, feature := range features {
if feature.InUseBy(d) {
if feature.InUseByDefault(d) {
versions[feature.Version] = struct{}{}
}
}
Expand All @@ -58,8 +58,12 @@ func getComputeApiVersion(d TerraformResourceData, resourceVersion ComputeApiVer
// version, to determine what version of the API is required in order to update the resource.
func getComputeApiVersionUpdate(d TerraformResourceData, resourceVersion ComputeApiVersion, features, updateOnlyFields []Feature) ComputeApiVersion {
versions := map[ComputeApiVersion]struct{}{resourceVersion: struct{}{}}
schemaVersion := getComputeApiVersion(d, resourceVersion, features)
versions[schemaVersion] = struct{}{}

for _, feature := range features {
if feature.InUseByUpdate(d) {
versions[feature.Version] = struct{}{}
}
}

for _, feature := range updateOnlyFields {
if feature.HasChangeBy(d) {
Expand All @@ -83,6 +87,10 @@ type Feature struct {
//
// Note: beta field nested inside a SET are NOT supported at the moment.
Item string

// Optional, only set if your field has a default value.
// If the value for the field is equal to the DefaultValue, we assume the beta feature is not activated.
DefaultValue interface{}
}

// Returns true when a feature has been modified.
Expand All @@ -93,16 +101,37 @@ func (s Feature) HasChangeBy(d TerraformResourceData) bool {
return d.HasChange(s.Item)
}

// Return true when a feature appears in schema or has been modified.
func (s Feature) InUseBy(d TerraformResourceData) bool {
return inUseBy(d, s.Item)
type InUseFunc func(d TerraformResourceData, path string, defaultValue interface{}) bool

func defaultInUseFunc(d TerraformResourceData, path string, defaultValue interface{}) bool {
// At read and delete time, there is no change.
// At create time, all fields are marked has changed. We should only considered the feature active if the field has
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// a value set and that this value is not the default value.
value, ok := d.GetOk(path)
return ok && value != defaultValue
}

func updateInUseFunc(d TerraformResourceData, path string, defaultValue interface{}) bool {
// During a resource update, if the beta field has changes, the feature is considered active even if the new value
// is the default value. This is because the beta API must be call to change the value of the field back to the
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// default value.
value, ok := d.GetOk(path)
return (ok && value != defaultValue) || d.HasChange(path)
}

// Return true when a feature appears in schema and doesn't hold the default value.
func (s Feature) InUseByDefault(d TerraformResourceData) bool {
return inUseBy(d, s.Item, s.DefaultValue, defaultInUseFunc)
}

func (s Feature) InUseByUpdate(d TerraformResourceData) bool {
return inUseBy(d, s.Item, s.DefaultValue, updateInUseFunc)
}

func inUseBy(d TerraformResourceData, path string) bool {
func inUseBy(d TerraformResourceData, path string, defaultValue interface{}, inUseFunc InUseFunc) bool {
pos := strings.Index(path, "*")
if pos == -1 {
_, ok := d.GetOk(path)
return ok || d.HasChange(path)
return inUseFunc(d, path, defaultValue)
}

prefix := path[0:pos]
Expand All @@ -117,7 +146,7 @@ func inUseBy(d TerraformResourceData, path string) bool {
count := v.(int)
for i := 0; i < count; i++ {
nestedPath := fmt.Sprintf("%s%d%s", prefix, i, suffix)
if inUseBy(d, nestedPath) {
if inUseBy(d, nestedPath, defaultValue, inUseFunc) {
return true
}
}
Expand Down
66 changes: 60 additions & 6 deletions google/api_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestComputeApiVersion(t *testing.T) {
UpdateOnlyFields []Feature
ExpectedApiVersions
}{
"no beta fields": {
"no beta field": {
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
},
Expand All @@ -29,7 +29,7 @@ func TestComputeApiVersion(t *testing.T) {
Update: baseVersion,
},
},
"beta fields no set": {
"beta field no set": {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Features: []Feature{{Version: betaVersion, Item: "beta_field"}},
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
Expand All @@ -40,7 +40,7 @@ func TestComputeApiVersion(t *testing.T) {
Update: baseVersion,
},
},
"beta fields set": {
"beta field set": {
Features: []Feature{{Version: betaVersion, Item: "beta_field"}},
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
Expand All @@ -52,7 +52,7 @@ func TestComputeApiVersion(t *testing.T) {
Update: betaVersion,
},
},
"update only beta fields": {
"update only beta field": {
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
},
Expand All @@ -64,7 +64,7 @@ func TestComputeApiVersion(t *testing.T) {
Update: betaVersion,
},
},
"nested beta fields not set": {
"nested beta field not set": {
Features: []Feature{{Version: betaVersion, Item: "list_field.*.beta_nested_field"}},
FieldsInSchema: map[string]interface{}{
"list_field.#": 2,
Expand All @@ -77,7 +77,7 @@ func TestComputeApiVersion(t *testing.T) {
Update: baseVersion,
},
},
"nested beta fields set": {
"nested beta field set": {
Features: []Feature{{Version: betaVersion, Item: "list_field.*.beta_nested_field"}},
FieldsInSchema: map[string]interface{}{
"list_field.#": 2,
Expand All @@ -104,6 +104,60 @@ func TestComputeApiVersion(t *testing.T) {
Update: betaVersion,
},
},
"beta field has default value": {
Features: []Feature{{Version: betaVersion, Item: "beta_field", DefaultValue: "bar"}},
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
"beta_field": "bar",
},
ExpectedApiVersions: ExpectedApiVersions{
Create: baseVersion,
ReadDelete: baseVersion,
Update: baseVersion,
},
},
"beta fields is being updated to default value": {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Features: []Feature{{Version: betaVersion, Item: "beta_field", DefaultValue: "bar"}},
FieldsInSchema: map[string]interface{}{
"normal_field": "foo",
"beta_field": "bar",
},
UpdatedFields: []string{"beta_field"},
ExpectedApiVersions: ExpectedApiVersions{
Create: baseVersion,
ReadDelete: baseVersion,
Update: betaVersion,
},
},
"nested beta field has default value": {
Features: []Feature{{Version: betaVersion, Item: "list_field.*.beta_nested_field", DefaultValue: "baz"}},
FieldsInSchema: map[string]interface{}{
"list_field.#": 2,
"list_field.0.normal_field": "foo",
"list_field.1.normal_field": "bar",
"list_field.1.beta_nested_field": "baz",
},
ExpectedApiVersions: ExpectedApiVersions{
Create: baseVersion,
ReadDelete: baseVersion,
Update: baseVersion,
},
},
"nested beta field is being updated default value": {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: updated to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Features: []Feature{{Version: betaVersion, Item: "list_field.*.beta_nested_field", DefaultValue: "baz"}},
FieldsInSchema: map[string]interface{}{
"list_field.#": 2,
"list_field.0.normal_field": "foo",
"list_field.1.normal_field": "bar",
"list_field.1.beta_nested_field": "baz",
},
UpdatedFields: []string{"list_field.1.beta_nested_field"},
ExpectedApiVersions: ExpectedApiVersions{
Create: baseVersion,
ReadDelete: baseVersion,
Update: betaVersion,
},
},
}

for tn, tc := range cases {
Expand Down
2 changes: 1 addition & 1 deletion google/resource_compute_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var FirewallVersionedFeatures = []Feature{
Feature{Version: v0beta, Item: "deny"},
Feature{Version: v0beta, Item: "direction"},
Feature{Version: v0beta, Item: "destination_ranges"},
Feature{Version: v0beta, Item: "priority"},
Feature{Version: v0beta, Item: "priority", DefaultValue: COMPUTE_FIREWALL_PRIORITY_DEFAULT},
}

func resourceComputeFirewall() *schema.Resource {
Expand Down