diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8660f18 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +# +# example.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* +examples/basic_data_catalog_usage/terraform.tfvars +examples/basic_data_catalog_usage/backend.tf +examples/basic_data_catalog_usage/key.json +examples/basic_data_catalog_usage/.terraform.lock.hcl +examples/basic_data_catalog_usage/plan diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..73b8797 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# terraform-google-datacatalog + +*UNOFFICIAL* + +This module allows the creation of Google Cloud Platform Data Catalog Tag Templates, Entry Groups and Tags. + +## Compatibility + +This module is meant for use with Terraform 0.15. + +## Usage + +Basic usage of this module can be found in the [basic data catalog example](examples/basic_data_catalog_usage) + +## Requirements + +These sections describe requirements for using this module. + +### Software + +The following dependencies must be available: + +- [Terraform](https://www.terraform.io/downloads.html) >= 0.15.0 +- [Terraform Provider for GCP](https://registry.terraform.io/providers/hashicorp/google/latest) >= v3.50 + +### Service Account + +A service account with the following roles must be used to provision +the resources of this module: + +- TagTemplate Creator: `roles/datacatalog.tagTemplateCreator` +- TagTemplate User: `roles/datacatalog.tagTemplateUser` +- Tag Editor: `roles/datacatalog.tagEditor` +- EntryGroup Creator: `roles/datacatalog.entryGroupCreator` + +### APIs + +A project with the following APIs enabled must be used to host the +resources of this module: + +- Data Catalog API: `datacatalog.googleapis.com` + +## Install + +### Terraform +Be sure you have the correct Terraform version (0.15.x), you can choose the binary here: +- https://releases.hashicorp.com/terraform/ + +## File structure +The project has the following folders and files: + +- /: root folder +- /examples: examples for using this module +- /main.tf: main file for this module, contains all the resources to create +- /variables.tf: all the variables for the module +- /output.tf: the outputs of the module +- /README.md: this file diff --git a/examples/basic_data_catalog_usage/README.md b/examples/basic_data_catalog_usage/README.md new file mode 100644 index 0000000..9d20cdc --- /dev/null +++ b/examples/basic_data_catalog_usage/README.md @@ -0,0 +1,107 @@ +# Basic Data Catalog Usage + +This example illustrates showcase some of the basic data catalog features. + +It will: +- Create a tag template +- Create an entry group +- Create a tag using the tag template and assigning that to the entry group + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|-------------------|---------|:--------:| +| tag\_templates | A list of objects which include id, description, display_name and fields |
list(object({ 
id = string
display_name = optional(string)
force_delete = optional(bool)
fields = list(object({
id = string
type = string
values = optional(list(string))
display_name = optional(string)
is_required = optional(bool)
order = optional(number)
}))
}))
| n/a | yes | +| project\_id | The project ID to host the tag templates in | `string` | n/a | yes | +| region | The region to host the tag templates in | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| tag\_template\_names | The name of the tag templates resource(s) | +| tag\_template\_self\_links | The URI of the tag template resource(s) | + + +## Example Usage + +```hcl +project_id = [YOUR PROJECT ID HERE] +region = [YOUR REGION HERE] + +tag_templates = [ + { + id = "my_template" + display_name = "Demo Tag Template" + force_delete = true + fields = [ + { + id = "source" + display_name = "Source of data asset" + type = "STRING" + is_required = true + }, + { + id = "num_rows" + display_name = "Number of rows in the data asset" + type = "DOUBLE" + }, + { + id = "pii_type" + display_name = "PII type" + type = "ENUM" + values = [ + "EMAIL", + "SOCIAL SECURITY NUMBER", + "NONE" + ] + } + ] + } +] + +entry_groups = [ + { + id = "my_entry_group" + } +] + +tags = [ + { + id = "my_first_tag" + tag_template_id = "my_template" + parent = "my_entry_group" + fields = [ + { + name = "source", + type = "STRING", + value = "oracle-db" + }, + { + name = "num_rows" + type = "DOUBLE" + value = "100" + }, + { + name = "pii_type" + type = "ENUM", + value = "NONE" + } + ] + } +] +``` + +## Notes + +### Tag Templates + +* The Tag Template ID must contain only lowercase letters (a-z), numbers (0-9), or underscores (_), and must start with a letter or underscore. +* Must contain at least one field and at most 500 fields. +* Field IDs can contain letters (both uppercase and lowercase), numbers (0-9), underscores (_) and dashes (-). Field IDs must be at least 1 character long and at most 64 characters long. Field IDs must also be unique within their template. Field IDs must start with a letter or underscore. +* The valid values for type in the tag_templates object are; `BOOL`, `DOUBLE`, `ENUM` and `STRING`, and `TIMESTAMP`. +* If you provide the `ENUM` type you are expected to provide a set of `values`. This set must not be empty and can include up to 100 allowed values. The display names of the values in this set must not be empty and must be case-insensitively unique within this set. + +### Entry Groups + +### Tags diff --git a/examples/basic_data_catalog_usage/main.tf b/examples/basic_data_catalog_usage/main.tf new file mode 100644 index 0000000..8452210 --- /dev/null +++ b/examples/basic_data_catalog_usage/main.tf @@ -0,0 +1,10 @@ + +module "google_data_catalog" { + source = "../../" + + project_id = var.project_id + region = var.region + tag_templates = var.tag_templates + entry_groups = var.entry_groups + tags = var.tags +} \ No newline at end of file diff --git a/examples/basic_data_catalog_usage/outputs.tf b/examples/basic_data_catalog_usage/outputs.tf new file mode 100644 index 0000000..68e220f --- /dev/null +++ b/examples/basic_data_catalog_usage/outputs.tf @@ -0,0 +1,10 @@ +output "tag_template_names" { + value = module.google_data_catalog.tag_template_names + description = "The ID of the tag templates with format {name}" +} + +output "tag_template_self_links" { + value = module.google_data_catalog.tag_template_self_links + description = "The URI of the tag templates created" +} + diff --git a/examples/basic_data_catalog_usage/providers.tf b/examples/basic_data_catalog_usage/providers.tf new file mode 100644 index 0000000..b467727 --- /dev/null +++ b/examples/basic_data_catalog_usage/providers.tf @@ -0,0 +1,9 @@ +provider "google" { + project = var.project_id + region = var.region +} + +provider "google-beta" { + project = var.project_id + region = var.region +} diff --git a/examples/basic_data_catalog_usage/run.bat b/examples/basic_data_catalog_usage/run.bat new file mode 100644 index 0000000..09fa075 --- /dev/null +++ b/examples/basic_data_catalog_usage/run.bat @@ -0,0 +1,4 @@ +SET GOOGLE_APPLICATION_CREDENTIALS=key.json +terraform init +terraform plan -out plan +terraform apply \ No newline at end of file diff --git a/examples/basic_data_catalog_usage/run.sh b/examples/basic_data_catalog_usage/run.sh new file mode 100644 index 0000000..10b7c24 --- /dev/null +++ b/examples/basic_data_catalog_usage/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash +GOOGLE_APPLICATION_CREDENTIALS=key.json +terraform init +terraform plan -out plan +terraform apply \ No newline at end of file diff --git a/examples/basic_data_catalog_usage/variables.tf b/examples/basic_data_catalog_usage/variables.tf new file mode 100644 index 0000000..ec8f4be --- /dev/null +++ b/examples/basic_data_catalog_usage/variables.tf @@ -0,0 +1,55 @@ +variable "tag_templates" { + type = list(object({ + id = string + display_name = optional(string) + force_delete = optional(bool) + fields = list(object({ + id = string + type = string + values = optional(list(string)) + description = optional(string) + display_name = optional(string) + is_required = optional(bool) + order = optional(number) + })) + })) + description = "(Required) A list of tag template resource objects" +} + +variable "entry_groups" { + type = list(object({ + id = string + display_name = optional(string) + description = optional(string) + })) + description = "(Required) A list of entry group objects" +} + +variable "tags" { + type = list(object({ + id = string + tag_template_id = string + parent = optional(string) + column = optional(string) + fields = list(object({ + name = string + type = string + value = string + spec = optional(string) + display_name = optional(string) + order = optional(number) + })) + })) + description = "(Optional) A list of tag resource objects" + default = [] +} + +variable "project_id" { + type = string + description = "(Required) The project ID to host the resource in" +} + +variable "region" { + type = string + description = "(Required) The region to host the resource in" +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..e04db34 --- /dev/null +++ b/main.tf @@ -0,0 +1,28 @@ +module "data_catalog_tag_templates" { + source = "./modules/tag-template" + + project_id = var.project_id + region = var.region + tag_templates = var.tag_templates +} + +module "data_catalog_entry_groups" { + source = "./modules/entry-group" + + project = var.project_id + region = var.region + entry_groups = var.entry_groups +} + +module "data_catalog_tags" { + source = "./modules/tag" + + tags = var.tags + entry_groups = module.data_catalog_entry_groups.self_links + tag_templates = module.data_catalog_tag_templates.self_links + + depends_on = [ + module.data_catalog_tag_templates, + module.data_catalog_entry_groups + ] +} diff --git a/modules/entry-group/main.tf b/modules/entry-group/main.tf new file mode 100644 index 0000000..f7dcb8b --- /dev/null +++ b/modules/entry-group/main.tf @@ -0,0 +1,13 @@ +locals { + groups = { for group in var.entry_groups : group["id"] => group } +} + +resource "google_data_catalog_entry_group" "entry_group" { + for_each = local.groups + entry_group_id = each.key + display_name = each.value.display_name + description = each.value.description + + project = var.project + region = var.region +} \ No newline at end of file diff --git a/modules/entry-group/outputs.tf b/modules/entry-group/outputs.tf new file mode 100644 index 0000000..26d9b3f --- /dev/null +++ b/modules/entry-group/outputs.tf @@ -0,0 +1,4 @@ +output "self_links" { + value = { for entry_group in google_data_catalog_entry_group.entry_group : element(split("/", entry_group["id"]), 5) => entry_group } + description = "Map of the entry groups created" +} \ No newline at end of file diff --git a/modules/entry-group/variables.tf b/modules/entry-group/variables.tf new file mode 100644 index 0000000..dbfbc88 --- /dev/null +++ b/modules/entry-group/variables.tf @@ -0,0 +1,18 @@ +variable "entry_groups" { + type = list(object({ + id = string + display_name = optional(string) + description = optional(string) + })) + description = "(Required) A list of entry group objects" +} + +variable "project" { + type = string + description = "(Required) The project ID to host the resource in" +} + +variable "region" { + type = string + description = "(Required) The region to host the resource in" +} diff --git a/modules/entry-group/versions.tf b/modules/entry-group/versions.tf new file mode 100644 index 0000000..bdac4c6 --- /dev/null +++ b/modules/entry-group/versions.tf @@ -0,0 +1,16 @@ +terraform { + required_version = ">= 0.15.0" + + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.50" + } + google-beta = { + source = "hashicorp/google-beta" + version = ">= 3.50" + } + } + + experiments = [module_variable_optional_attrs] +} \ No newline at end of file diff --git a/modules/tag-template/main.tf b/modules/tag-template/main.tf new file mode 100644 index 0000000..cfc3bdd --- /dev/null +++ b/modules/tag-template/main.tf @@ -0,0 +1,46 @@ +locals { + primitive_types = ["DOUBLE", "STRING", "BOOL", "TIMESTAMP"] + enum_type = ["ENUM"] + tag_template = { for tag_template in var.tag_templates : tag_template["id"] => tag_template } +} + +resource "google_data_catalog_tag_template" "tag_template" { + for_each = local.tag_template + project = var.project_id + region = var.region + tag_template_id = lower(each.key) + display_name = each.value["display_name"] + + dynamic "fields" { + for_each = { for field in each.value["fields"] : field["id"] => field } + content { + field_id = fields.value["id"] + display_name = fields.value["display_name"] + description = fields.value["description"] + is_required = fields.value["is_required"] + order = fields.value["order"] + dynamic "type" { + for_each = { for idx, prim_type in [fields.value["type"]] : idx => prim_type if contains(local.primitive_types, prim_type) } + content { + primitive_type = type.value + } + } + dynamic "type" { + for_each = { for idx, enum_type in [fields.value["type"]] : idx => enum_type if contains(local.enum_type, enum_type) } + content { + enum_type { + dynamic "allowed_values" { + for_each = toset(fields.value["values"]) + content { + display_name = allowed_values.value + } + } + } + } + } + + } + } + + force_delete = each.value["force_delete"] +} diff --git a/modules/tag-template/outputs.tf b/modules/tag-template/outputs.tf new file mode 100644 index 0000000..a1d64d9 --- /dev/null +++ b/modules/tag-template/outputs.tf @@ -0,0 +1,4 @@ +output "self_links" { + value = { for tag_template in google_data_catalog_tag_template.tag_template : element(split("/", tag_template["id"]), 5) => tag_template } + description = "Map of the tag templates created" +} \ No newline at end of file diff --git a/modules/tag-template/variables.tf b/modules/tag-template/variables.tf new file mode 100644 index 0000000..857eb3a --- /dev/null +++ b/modules/tag-template/variables.tf @@ -0,0 +1,47 @@ +variable "tag_templates" { + type = list(object({ + id = string + display_name = optional(string) + force_delete = optional(bool) + fields = list(object({ + id = string + type = string + values = optional(list(string)) + description = optional(string) + display_name = optional(string) + is_required = optional(bool) + order = optional(number) + })) + })) + description = "(Required) A list of tag template resource objects" + + validation { + condition = can([for tag_template in var.tag_templates : regex("^[a-z_][a-z0-9_]{0,63}$", tag_template["id"])]) + error_message = "Each of the 'tag_templates' id values must start with a letter (a-z) or underscore (_) and only contain letters (a-z), numbers(0-9) or underscores(_). It can be at most 64 bytes long when encoded in UTF-8." + } + + validation { + condition = can([for tag_template in var.tag_templates : regex("^[^\\s][a-zA-Z0-9_\\s]{0,199}[^\\s]$", tag_template["display_name"])]) + error_message = "Each of the 'tag_templates' display names must NOT start or end with a space. It must contain only unicode letters, numbers, underscores, dashes and spaces. It can be at most 200 bytes long when encoded in UTF-8." + } + + validation { + condition = alltrue([for tag_template in var.tag_templates : alltrue([for field in tag_template["fields"] : contains(["BOOL", "DOUBLE", "ENUM", "STRING", "TIMESTAMP"], field["type"])])]) + error_message = "Supported types are 'BOOL', 'DOUBLE', 'ENUM', 'STRING' and 'TIMESTAMP'." + } + + validation { + condition = alltrue([for tag_template in var.tag_templates : alltrue([for field in tag_template["fields"] : length(regexall("^[a-zA-Z_][a-zA-Z0-9_]{0,63}$", field["id"])) > 0])]) + error_message = "Field IDs must start with a letter or underscore. Field IDs must be unique within their template. Field IDs must be at least 1 character long and at most 64 bytes long when encoded in UTF-8." + } +} + +variable "project_id" { + type = string + description = "(Optional) The project ID to host the resource in" +} + +variable "region" { + type = string + description = "(Optional) The region to host the resource in" +} diff --git a/modules/tag-template/versions.tf b/modules/tag-template/versions.tf new file mode 100644 index 0000000..b0e7b7a --- /dev/null +++ b/modules/tag-template/versions.tf @@ -0,0 +1,12 @@ +terraform { + required_version = ">= 0.15.0" + + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.50" + } + } + + experiments = [module_variable_optional_attrs] +} \ No newline at end of file diff --git a/modules/tag/main.tf b/modules/tag/main.tf new file mode 100644 index 0000000..1a96f62 --- /dev/null +++ b/modules/tag/main.tf @@ -0,0 +1,61 @@ +locals { + tags = { for tag in var.tags : tag["id"] => tag } +} + +resource "google_data_catalog_tag" "tag" { + for_each = local.tags + //template = "projects/${var.project}/locations/${var.region}/tagTemplates/${each.value.tag_template_id}" + template = coalesce(var.tag_templates[each.value.tag_template_id]["name"]) + parent = coalesce(var.entry_groups[each.value.parent]["name"]) + column = each.value.column + + dynamic "fields" { + for_each = { for field in each.value.fields : field["name"] => field if field["type"] == "BOOL" } + content { + field_name = fields.key + display_name = fields.value["display_name"] + order = fields.value["order"] + bool_value = tobool(fields.value["value"]) + } + } + + dynamic "fields" { + for_each = { for field in each.value.fields : field["name"] => field if field["type"] == "DOUBLE" } + content { + field_name = fields.key + display_name = fields.value["display_name"] + order = fields.value["order"] + double_value = tonumber(fields.value["value"]) + } + } + + dynamic "fields" { + for_each = { for field in each.value.fields : field["name"] => field if field["type"] == "ENUM" } + content { + field_name = fields.key + display_name = fields.value["display_name"] + order = fields.value["order"] + enum_value = fields.value["value"] + } + } + + dynamic "fields" { + for_each = { for field in each.value.fields : field["name"] => field if field["type"] == "STRING" } + content { + field_name = fields.key + display_name = fields.value["display_name"] + order = fields.value["order"] + string_value = fields.value["value"] + } + } + + dynamic "fields" { + for_each = { for field in each.value.fields : field["name"] => field if field["type"] == "TIMESTAMP" } + content { + field_name = fields.key + display_name = fields.value["display_name"] + order = fields.value["order"] + timestamp_value = formatdate(fields.value["spec"], fields.value["value"]) + } + } +} \ No newline at end of file diff --git a/modules/tag/outputs.tf b/modules/tag/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/modules/tag/variables.tf b/modules/tag/variables.tf new file mode 100644 index 0000000..4a8062e --- /dev/null +++ b/modules/tag/variables.tf @@ -0,0 +1,33 @@ +variable "tags" { + type = list(object({ + id = string + tag_template_id = string + parent = string + column = optional(string) + fields = list(object({ + name = string + type = string + value = string + spec = optional(string) + display_name = optional(string) + order = optional(number) + })) + })) + description = "(Required) A list of tag resource objects" +} + +variable "tag_templates" { + type = map(object({ + name = string + })) + default = {} + description = "(Required) A map of tag template objects" +} + +variable "entry_groups" { + type = map(object({ + name = string + })) + default = {} + description = "(Optional) A map of entry group objects" +} diff --git a/modules/tag/versions.tf b/modules/tag/versions.tf new file mode 100644 index 0000000..bdac4c6 --- /dev/null +++ b/modules/tag/versions.tf @@ -0,0 +1,16 @@ +terraform { + required_version = ">= 0.15.0" + + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.50" + } + google-beta = { + source = "hashicorp/google-beta" + version = ">= 3.50" + } + } + + experiments = [module_variable_optional_attrs] +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..1d7abab --- /dev/null +++ b/outputs.tf @@ -0,0 +1,10 @@ +output "tag_template_names" { + value = keys(module.data_catalog_tag_templates.self_links) + description = "The ID of the tag templates with format {name}" +} + +output "tag_template_self_links" { + value = [ for tag_template in module.data_catalog_tag_templates.self_links : tag_template["id"] ] + description = "The URI of the tag templates created" +} + diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..f3fbe6b --- /dev/null +++ b/variables.tf @@ -0,0 +1,86 @@ +variable "tag_templates" { + type = list(object({ + id = string + display_name = optional(string) + force_delete = optional(bool) + fields = list(object({ + id = string + type = string + values = optional(list(string)) + description = optional(string) + display_name = optional(string) + is_required = optional(bool) + order = optional(number) + })) + })) + description = "(Required) A list of tag template resource objects" + + validation { + condition = can([for tag_template in var.tag_templates : regex("^[a-z_][a-z0-9_]{0,63}$", tag_template["id"])]) + error_message = "Each of the 'tag_templates' id values must start with a letter (a-z) or underscore (_) and only contain letters (a-z), numbers(0-9) or underscores(_). It can be at most 64 bytes long when encoded in UTF-8." + } + + validation { + condition = can([for tag_template in var.tag_templates : regex("^[^\\s][a-zA-Z0-9_\\s]{0,199}[^\\s]$", tag_template["display_name"])]) + error_message = "Each of the 'tag_templates' display names must NOT start or end with a space. It must contain only unicode letters, numbers, underscores, dashes and spaces. It can be at most 200 bytes long when encoded in UTF-8." + } + + validation { + condition = alltrue([for tag_template in var.tag_templates : alltrue([for field in tag_template["fields"] : contains(["BOOL", "DOUBLE", "ENUM", "STRING", "TIMESTAMP"], field["type"])])]) + error_message = "Supported types are 'BOOL', 'DOUBLE', 'ENUM', 'STRING' and 'TIMESTAMP'." + } + + validation { + condition = alltrue([for tag_template in var.tag_templates : alltrue([for field in tag_template["fields"] : length(regexall("^[a-zA-Z_][a-zA-Z0-9_]{0,63}$", field["id"])) > 0])]) + error_message = "Field IDs must start with a letter or underscore. Field IDs must be unique within their template. Field IDs must be at least 1 character long and at most 64 bytes long when encoded in UTF-8." + } +} + +variable "entry_groups" { + type = list(object({ + id = string + display_name = optional(string) + description = optional(string) + })) + default = [] + description = "(Optional) A list of entry group objects" +} + +variable "tags" { + type = list(object({ + id = string + tag_template_id = string + parent = string + column = optional(string) + fields = list(object({ + name = string + type = string + value = string + spec = optional(string) + display_name = optional(string) + order = optional(number) + })) + })) + description = "(Optional) A list of tag resource objects" + default = [] + + validation { + condition = alltrue([for tag in var.tags : alltrue([for field in tag["fields"] : contains(["BOOL", "DOUBLE", "ENUM", "STRING", "TIMESTAMP"], field["type"])])]) + error_message = "Supported types are 'BOOL', 'DOUBLE', 'ENUM', 'STRING' and 'TIMESTAMP'." + } + + validation { + condition = alltrue([for tag in var.tags : alltrue([for field in tag["fields"] : field["type"] == "TIMESTAMP" && field["spec"] != null ? false : true])]) + error_message = "A 'spec' must be supplied with a type of 'TIMESTAMP'." + } +} + +variable "project_id" { + type = string + description = "(Optional) The project ID to host the resource in" +} + +variable "region" { + type = string + description = "(Optional) The region to host the resource in" +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..b0e7b7a --- /dev/null +++ b/versions.tf @@ -0,0 +1,12 @@ +terraform { + required_version = ">= 0.15.0" + + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.50" + } + } + + experiments = [module_variable_optional_attrs] +} \ No newline at end of file