Skip to content

Commit

Permalink
fixed virtual field update issues (GoogleCloudPlatform#7318)
Browse files Browse the repository at this point in the history
* fixed virtual field upadte issues

* update funcs

---------

Co-authored-by: Edward Sun <sunedward@google.com>
  • Loading branch information
2 people authored and kubalaguna committed Feb 27, 2023
1 parent f631ed5 commit 23377c0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions mmv1/products/spanner/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
post_create: templates/terraform/post_create/sleep.go.erb
pre_delete: 'templates/terraform/pre_delete/spanner_instance.go.erb'
constants: 'templates/terraform/constants/spanner_instance.go.erb'
pre_update: 'templates/terraform/pre_update/spanner_instance.go.erb'
# This is for copying files over
files: !ruby/object:Provider::Config::Files
# These files have templating (ERB) code that will be run.
Expand Down
16 changes: 16 additions & 0 deletions mmv1/templates/terraform/constants/spanner_database.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,20 @@ func validateDatabaseRetentionPeriod(v interface{}, k string) (ws []string, erro
}

return
}

func resourceSpannerDBVirtualUpdate(d *schema.ResourceData, resourceSchema map[string]*schema.Schema) bool {
// deletion_protection is the only virtual field
if d.HasChange("deletion_protection") {
for field := range resourceSchema {
if field == "deletion_protection" {
continue
}
if d.HasChange(field) {
return false
}
}
return true
}
return false
}
18 changes: 17 additions & 1 deletion mmv1/templates/terraform/constants/spanner_instance.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ func deleteSpannerBackups(d *schema.ResourceData, config *Config, res map[string
}
}
return nil
}
}

func resourceSpannerInstanceVirtualUpdate(d *schema.ResourceData, resourceSchema map[string]*schema.Schema) bool {
// force_destroy is the only virtual field
if d.HasChange("force_destroy") {
for field := range resourceSchema {
if field == "force_destroy" {
continue
}
if d.HasChange(field) {
return false
}
}
return true
}
return false
}
9 changes: 9 additions & 0 deletions mmv1/templates/terraform/pre_update/spanner_database.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ if len(obj["statements"].([]string)) == 0 {
// Return early to avoid making an API call that errors,
// due to containing no DDL SQL statements
return resourceSpannerDatabaseRead(d, meta)
}

if resourceSpannerDBVirtualUpdate(d, resourceSpannerInstance().Schema) {
if d.Get("deletion_protection") != nil {
if err := d.Set("deletion_protection", d.Get("deletion_protection")); err != nil {
return fmt.Errorf("Error reading Instance: %s", err)
}
}
return nil
}
8 changes: 8 additions & 0 deletions mmv1/templates/terraform/pre_update/spanner_instance.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if resourceSpannerInstanceVirtualUpdate(d, resourceSpannerInstance().Schema) {
if d.Get("force_destroy") != nil {
if err := d.Set("force_destroy", d.Get("force_destroy")); err != nil {
return fmt.Errorf("Error reading Instance: %s", err)
}
}
return nil
}

0 comments on commit 23377c0

Please sign in to comment.