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

Add schema update support to spanner db 2082 #3947

Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 4 additions & 3 deletions products/spanner/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ objects:
- !ruby/object:Api::Resource
name: 'Database'
base_url: projects/{{project}}/instances/{{instance}}/databases
input: true
description: |
A Cloud Spanner Database which is hosted on a Spanner instance.
input: true
references: !ruby/object:Api::Resource::ReferenceLinks
guides:
'Official Documentation': 'https://cloud.google.com/spanner/'
api: 'https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases'
async: !ruby/object:Api::OpAsync
actions: ['create', 'update']
actions: ['create','update','delete']
operation: !ruby/object:Api::OpAsync::Operation
path: 'name'
base_url: '{{op_id}}'
Expand Down Expand Up @@ -168,12 +168,13 @@ objects:
- !ruby/object:Api::Type::Array
item_type: Api::Type::String
name: 'extraStatements'
update_url: projects/{{project}}/instances/{{instance}}/databases/{{name}}/ddl
update_verb: :PATCH
description: |
An optional list of DDL statements to run inside the newly created
database. Statements can create tables, indexes, etc. These statements
execute atomically with the creation of the database: if there is an
error in any statement, the database is not created.
input: true
- !ruby/object:Api::Type::Enum
name: 'state'
description: An explanation of the status of the database.
Expand Down
4 changes: 3 additions & 1 deletion products/spanner/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ overrides: !ruby/object:Overrides::ResourceOverrides
state: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: false
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: 'templates/terraform/constants/spanner_database.go.erb'
encoder: templates/terraform/encoders/spanner_database.go.erb
decoder: templates/terraform/decoders/spanner_database.go.erb
update_encoder: templates/terraform/update_encoder/spanner_database.go.erb
resource_definition: 'templates/terraform/resource_definition/spanner_database.go.erb'
InstanceConfig: !ruby/object:Overrides::Terraform::ResourceOverride
# This is appropriate for a datasource - doesn't have a create method.
exclude: true
Expand Down Expand Up @@ -87,7 +90,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides
update_encoder: templates/terraform/encoders/spanner_instance_update.go.erb
decoder: templates/terraform/decoders/spanner_instance.go.erb
post_create: templates/terraform/post_create/sleep.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
33 changes: 33 additions & 0 deletions templates/terraform/constants/spanner_database.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%- # the license inside this block applies to this file
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
// customizeDiff func for additional checks on google_spanner_database properties:
func resourceSpannerDBDdlCustomDiff(diff *schema.ResourceDiff, meta interface{}) error {
venkykuberan marked this conversation as resolved.
Show resolved Hide resolved
old, new := diff.GetChange("ddl")
oldDdls := old.([]interface{})
newDdls := new.([]interface{})

if len(newDdls) < len(oldDdls) {
diff.ForceNew("ddl")
return nil
}

for i := range oldDdls {
if newDdls[i].(string) != oldDdls[i].(string) {
diff.ForceNew("ddl")
return nil
}
}
return nil
}
15 changes: 15 additions & 0 deletions templates/terraform/resource_definition/spanner_database.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%# The license inside this block applies to this file.
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
CustomizeDiff: resourceSpannerDBDdlCustomDiff,
29 changes: 29 additions & 0 deletions templates/terraform/update_encoder/spanner_database.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%# The license inside this block applies to this file.
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
old, new := d.GetChange("ddl")
oldDdls := old.([]interface{})
newDdls := new.([]interface{})
updateDdls := []string{}

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

obj["statements"] = strings.Join(updateDdls, ",")
delete(obj, "name")
delete(obj, "instance")
delete(obj, "extraStatements")
return obj, nil
30 changes: 30 additions & 0 deletions third_party/terraform/tests/resource_spanner_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func TestAccSpannerDatabase_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("google_spanner_database.basic", "state"),
),
},
{
Config: testAccSpannerDatabase_basicUpdate(instanceName, databaseName),
Check: resource.ComposeTestCheckFunc(
venkykuberan marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttrSet("google_spanner_database.basic", "state"),
),
},
{
// Test import with default Terraform ID
ResourceName: "google_spanner_database.basic",
Expand Down Expand Up @@ -66,6 +72,30 @@ resource "google_spanner_instance" "basic" {
resource "google_spanner_database" "basic" {
instance = google_spanner_instance.basic.name
name = "%s"
ddl = [
"CREATE TABLE t1 (t1 INT64 NOT NULL,) PRIMARY KEY(t1)",
"CREATE TABLE t2 (t2 INT64 NOT NULL,) PRIMARY KEY(t2)",
]
}
`, instanceName, instanceName, databaseName)
}

func testAccSpannerDatabase_basicUpdate(instanceName, databaseName string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic" {
name = "%s"
config = "regional-us-central1"
display_name = "display-%s"
num_nodes = 1
}

resource "google_spanner_database" "basic" {
instance = google_spanner_instance.basic.name
name = "%s"
ddl = [
"CREATE TABLE t1 (t1 INT64 NOT NULL,) PRIMARY KEY(t1)",
"CREATE TABLE t3 (t3 INT64 NOT NULL,) PRIMARY KEY(t3)",
venkykuberan marked this conversation as resolved.
Show resolved Hide resolved
]
}
`, instanceName, instanceName, databaseName)
}
Expand Down