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

fix crash if ddl item is nil #5071

Merged
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
3 changes: 3 additions & 0 deletions .changelog/6805.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
spanner: fixed crash when `google_spanner_database.ddl` item was nil
```
12 changes: 12 additions & 0 deletions google-beta/resource_compute_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,18 @@ func resourceComputeReservationUpdate(d *schema.ResourceData, meta interface{})
return err
}

if d.HasChange("share_settings") {
url, err = replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/reservations/{{name}}")
if err != nil {
return err
}
urlUpdateMask := obj["urlUpdateMask"]
if urlUpdateMask != nil {
url = url + urlUpdateMask.(string)
delete(obj, "urlUpdateMask")
}
}

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down
57 changes: 35 additions & 22 deletions google-beta/resource_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ func resourceSpannerDatabaseCreate(d *schema.ResourceData, meta interface{}) err

if ddlOk {
for i := 0; i < len(ddlStatements); i++ {
updateDdls = append(updateDdls, ddlStatements[i].(string))
if ddlStatements[i] != nil {
updateDdls = append(updateDdls, ddlStatements[i].(string))
}
}
}

Expand All @@ -352,30 +354,33 @@ func resourceSpannerDatabaseCreate(d *schema.ResourceData, meta interface{}) err
updateDdls = append(updateDdls, retentionDdl)
}

log.Printf("[DEBUG] Applying extra DDL statements to the new Database: %#v", updateDdls)
// Skip API call if there are no new ddl entries (due to ignoring nil values)
if len(updateDdls) > 0 {
log.Printf("[DEBUG] Applying extra DDL statements to the new Database: %#v", updateDdls)

obj["statements"] = updateDdls
obj["statements"] = updateDdls

url, err = replaceVars(d, config, "{{SpannerBasePath}}projects/{{project}}/instances/{{instance}}/databases/{{name}}/ddl")
if err != nil {
return err
}
url, err = replaceVars(d, config, "{{SpannerBasePath}}projects/{{project}}/instances/{{instance}}/databases/{{name}}/ddl")
if err != nil {
return err
}

res, err = sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error executing DDL statements on Database: %s", err)
}
res, err = sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error executing DDL statements on Database: %s", err)
}

// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = spannerOperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating Database", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to run DDL against newly-created Database: %s", err)
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = spannerOperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating Database", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to run DDL against newly-created Database: %s", err)
}
}
}

Expand Down Expand Up @@ -501,6 +506,12 @@ func resourceSpannerDatabaseUpdate(d *schema.ResourceData, meta interface{}) err
return err
}

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)
}

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
Expand Down Expand Up @@ -717,7 +728,9 @@ func resourceSpannerDatabaseUpdateEncoder(d *schema.ResourceData, meta interface

//Only new ddl statments to be add to update call
for i := len(oldDdls); i < len(newDdls); i++ {
updateDdls = append(updateDdls, newDdls[i].(string))
if newDdls[i] != nil {
updateDdls = append(updateDdls, newDdls[i].(string))
}
}

//Add statement to update version_retention_period property, if needed
Expand Down