Skip to content

Add support for Materialized Views #13251

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

Merged
merged 18 commits into from
Apr 23, 2025
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
83 changes: 83 additions & 0 deletions mmv1/products/bigtable/MaterializedView.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2025 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.

---
name: 'MaterializedView'
kind: 'bigtable#materializedView'
description: |
A materialized view object that can be referenced in SQL queries.
references:
guides:
api: 'https://cloud.google.com/bigtable/docs/reference/admin/rest/v2/projects.instances.materializedViews'
docs:
id_format: 'projects/{{project}}/instances/{{instance}}/materializedViews/{{materialized_view_id}}'
base_url: 'projects/{{project}}/instances/{{instance}}/materializedViews?materializedViewId={{materialized_view_id}}'
self_link: 'projects/{{project}}/instances/{{instance}}/materializedViews/{{materialized_view_id}}'
create_url: 'projects/{{project}}/instances/{{instance}}/materializedViews?materializedViewId={{materialized_view_id}}'
update_url: 'projects/{{project}}/instances/{{instance}}/materializedViews/{{materialized_view_id}}'
update_verb: 'PATCH'
update_mask: true
delete_url: 'projects/{{project}}/instances/{{instance}}/materializedViews/{{materialized_view_id}}'
import_format:
- 'projects/{{project}}/instances/{{instance}}/materializedViews/{{materialized_view_id}}'
timeouts:
insert_minutes: 120
update_minutes: 20
delete_minutes: 20
exclude_sweeper: true
examples:
- name: 'bigtable_materialized_view'
primary_resource_id: 'materialized_view'
vars:
instance_name: 'bt-instance'
table_name: 'bt-table'
materialized_view_name: 'bt-materialized-view'
deletion_protection: 'true'
test_vars_overrides:
'deletion_protection': 'false'
oics_vars_overrides:
'deletion_protection': 'false'
# bigtable instance does not use the shared HTTP client, this test creates an instance
skip_vcr: true
parameters:
- name: 'materializedViewId'
type: String
description:
'The unique name of the materialized view in the form
`[_a-zA-Z0-9][-_.a-zA-Z0-9]*`.'
url_param_only: true
required: true
immutable: true
- name: 'instance'
type: String
description: 'The name of the instance to create the materialized view within.'
url_param_only: true
immutable: true
diff_suppress_func: 'tpgresource.CompareResourceNames'
properties:
- name: 'name'
type: String
description:
'The unique name of the requested materialized view. Values are of the form
`projects/<project>/instances/<instance>/materializedViews/<materializedViewId>`.'
output: true
- name: 'query'
type: String
description:
'The materialized view''s select query.'
required: true
immutable: true
- name: 'deletionProtection'
type: Boolean
description:
'Set to true to make the MaterializedView protected against deletion.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
resource "google_bigtable_instance" "instance" {
name = "{{index $.Vars "instance_name"}}"
cluster {
cluster_id = "cluster-1"
zone = "us-east1-b"
num_nodes = 3
storage_type = "HDD"
}

deletion_protection = {{index $.Vars "deletion_protection"}}
}

resource "google_bigtable_table" "table" {
name = "{{index $.Vars "table_name"}}"
instance_name = google_bigtable_instance.instance.name

column_family {
family = "CF"
}
}

resource "google_bigtable_materialized_view" "{{$.PrimaryResourceId}}" {
materialized_view_id = "{{index $.Vars "materialized_view_name"}}"
instance = google_bigtable_instance.instance.name
deletion_protection = false
query = <<EOT
SELECT _key, COUNT(CF['col1']) as Count
FROM ` + "`{{index $.Vars "table_name"}}`" + `
GROUP BY _key
EOT

depends_on = [
google_bigtable_table.table
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package bigtable_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/plancheck"

"github.com/hashicorp/terraform-provider-google/google/acctest"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccBigtableMaterializedView_deletionProtection(t *testing.T) {
// bigtable instance does not use the shared HTTP client, this test creates an instance
acctest.SkipIfVcr(t)
t.Parallel()

instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
tableName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
mvName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccBigtableMaterializedView_deletionProtection(instanceName, tableName, mvName, true),
},
{
ResourceName: "google_bigtable_materialized_view.materialized_view",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccBigtableMaterializedView_deletionProtection(instanceName, tableName, mvName, false),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("google_bigtable_materialized_view.materialized_view", plancheck.ResourceActionUpdate),
},
},
},
{
ResourceName: "google_bigtable_materialized_view.materialized_view",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccBigtableMaterializedView_deletionProtection(instanceName, tableName, mvName string, deletion_protection bool) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"
cluster {
cluster_id = "%s-c"
zone = "us-east1-b"
}
deletion_protection = false
}
resource "google_bigtable_table" "table" {
name = "%s"
instance_name = google_bigtable_instance.instance.id
column_family {
family = "CF"
}
}
resource "google_bigtable_materialized_view" "materialized_view" {
materialized_view_id = "%s"
instance = google_bigtable_instance.instance.name
deletion_protection = %v
query = <<EOT
SELECT _key, COUNT(CF['col']) as Count
FROM %s
GROUP BY _key
EOT
depends_on = [
google_bigtable_table.table
]
}
`, instanceName, instanceName, tableName, mvName, deletion_protection, fmt.Sprintf("`%s`", tableName))
}
Loading