|
| 1 | +/** |
| 2 | +* Copyright 2024 Google LLC |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +# [START bigquery_create_table_attach_tag] |
| 18 | + |
| 19 | +# Create tag keys and values |
| 20 | +data "google_project" "default" {} |
| 21 | + |
| 22 | +resource "google_tags_tag_key" "env_tag_key" { |
| 23 | + parent = "projects/${data.google_project.default.project_id}" |
| 24 | + short_name = "env3" |
| 25 | +} |
| 26 | + |
| 27 | +resource "google_tags_tag_key" "department_tag_key" { |
| 28 | + parent = "projects/${data.google_project.default.project_id}" |
| 29 | + short_name = "department3" |
| 30 | +} |
| 31 | + |
| 32 | +resource "google_tags_tag_value" "env_tag_value" { |
| 33 | + parent = "tagKeys/${google_tags_tag_key.env_tag_key.name}" |
| 34 | + short_name = "prod" |
| 35 | +} |
| 36 | + |
| 37 | +resource "google_tags_tag_value" "department_tag_value" { |
| 38 | + parent = "tagKeys/${google_tags_tag_key.department_tag_key.name}" |
| 39 | + short_name = "sales" |
| 40 | +} |
| 41 | + |
| 42 | +# Create a dataset |
| 43 | +resource "google_bigquery_dataset" "default" { |
| 44 | + dataset_id = "MyDataset" |
| 45 | + default_partition_expiration_ms = 2592000000 # 30 days |
| 46 | + default_table_expiration_ms = 31536000000 # 365 days |
| 47 | + description = "dataset description" |
| 48 | + location = "US" |
| 49 | + max_time_travel_hours = 96 # 4 days |
| 50 | +} |
| 51 | + |
| 52 | +# Create a table |
| 53 | +resource "google_bigquery_table" "default" { |
| 54 | + dataset_id = google_bigquery_dataset.default.dataset_id |
| 55 | + table_id = "mytable" |
| 56 | + description = "table description" |
| 57 | + deletion_protection = false # set to "true" in production |
| 58 | + |
| 59 | + # Attach tags to the table |
| 60 | + resource_tags = { |
| 61 | + (google_tags_tag_key.env_tag_key.namespaced_name) : google_tags_tag_value.env_tag_value.short_name, |
| 62 | + (google_tags_tag_key.department_tag_key.namespaced_name) : google_tags_tag_value.department_tag_value.short_name |
| 63 | + } |
| 64 | +} |
| 65 | +# [END bigquery_create_table_attach_tag] |
0 commit comments