diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index 009ce5b..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,56 +0,0 @@ -workflow "Terraform" { - resolves = "terraform-validate" - on = "pull_request" -} - -workflow "Release" { - on = "push" - resolves = ["goreleaser"] -} - -action "filter-to-pr-open-synced" { - uses = "actions/bin/filter@master" - args = "action 'opened|synchronize'" -} - -action "terraform-fmt" { - uses = "hashicorp/terraform-github-actions/fmt@v0.3.5" - needs = "filter-to-pr-open-synced" - secrets = ["GITHUB_TOKEN"] - - env = { - TF_ACTION_WORKING_DIR = "." - } -} - -action "terraform-init" { - uses = "hashicorp/terraform-github-actions/init@v0.3.5" - needs = "terraform-fmt" - secrets = ["GITHUB_TOKEN"] - - env = { - TF_ACTION_WORKING_DIR = "./examples/basic" - } -} - -action "terraform-validate" { - uses = "hashicorp/terraform-github-actions/validate@v0.3.5" - needs = "terraform-init" - secrets = ["GITHUB_TOKEN"] - - env = { - TF_ACTION_WORKING_DIR = "./examples/basic" - } -} - -action "is-tag" { - uses = "actions/bin/filter@master" - args = "tag" -} - -action "goreleaser" { - uses = "docker://goreleaser/goreleaser" - secrets = ["GITHUB_TOKEN"] - args = "release" - needs = ["is-tag"] -} diff --git a/.gitignore b/.gitignore index 1fef4ab..21b9328 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ # .tfvars files *.tfvars + +# tflock files +.terraform.lock.hcl \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml deleted file mode 100644 index fa08e9b..0000000 --- a/.goreleaser.yml +++ /dev/null @@ -1,17 +0,0 @@ -builds: - - - main: ./.goreleaser/main.go - binary: dirty-hack - ignore: - - goos: darwin - goarch: 386 - - goos: darwin - goarch: amd64 - - goos: linux - goarch: 386 -archive: - name_template: "dirty-hack-for-goreleaser" - files: - - README.md -release: - name_template: "{{.Tag}}" diff --git a/.goreleaser/main.go b/.goreleaser/main.go deleted file mode 100644 index da29a2c..0000000 --- a/.goreleaser/main.go +++ /dev/null @@ -1,4 +0,0 @@ -package main - -func main() { -} diff --git a/LICENSE b/LICENSE index 0c2364c..3644c32 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2019 Vladimir Syromyatnikov + Copyright 2019-2021 Vladimir Syromyatnikov Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index c81c350..81b050e 100644 --- a/README.md +++ b/README.md @@ -14,27 +14,53 @@ module "my_awesome_project" { } ``` + +## Requirements + +| Name | Version | +|------|---------| +| terraform | >= 0.14 | +| google | >= 3.0 | + +## Providers + +| Name | Version | +|------|---------| +| google | >= 3.0 | + ## Inputs -| Name | Description | Type | Default | Required | -|:---------------------------|:------------------------------------------------------------------------------------------------|:------:|:---------:|:--------:| -| name | Solution name, e.g. 'app' or 'jenkins' | string | n/a | yes | -| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | string | n/a | yes | -| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | n/a | yes | -| attributes | Additional attributes (e.g. `1`) | list | `` | no | -| auto_create_network | Create the 'default' network automatically | string | `"true"` | no | -| billing_account_id | The alphanumeric ID of the billing account this project belongs to | string | `""` | no | -| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | string | `"-"` | no | -| enable_deletion_protection | If true, the Terraform resource can be deleted without deleting the Project via the Google API. | string | `"false"` | no | -| enabled | Set to false to prevent the module from creating any resources | string | `"true"` | no | -| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `""` | no | -| folder_id | The numeric ID of the folder this project should be created under | string | `""` | no | -| org_id | The numeric ID of the organization this project belongs to | string | `""` | no | -| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `` | no | +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no | +| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no | +| auto\_create\_network | Create the `default` network automatically. | `bool` | `true` | no | +| billing\_account\_id | The alphanumeric ID of the billing account this project belongs to. | `string` | `null` | no | +| context | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {}
}
| no | +| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | +| enabled | Set to false to prevent the module from creating any resources | `bool` | `null` | no | +| environment | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | +| folder\_id | The numeric ID of the folder this project should be created under. | `string` | `null` | no | +| id\_length\_limit | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for default, which is `0`.
Does not affect `id_full`. | `number` | `null` | no | +| label\_key\_case | The letter case of label keys (`tag` names) (i.e. `name`, `namespace`, `environment`, `stage`, `attributes`) to use in `tags`.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `"lower"` | no | +| label\_order | The naming order of the id output and Name tag.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no | +| label\_value\_case | The letter case of output label values (also used in `tags` and `id`).
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Default value: `lower`. | `string` | `null` | no | +| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no | +| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no | +| org\_id | The numeric ID of the organization this project belongs to. | `string` | `null` | no | +| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | +| skip\_delete | If true, the Terraform resource can be deleted without deleting the Project via the Google API. | `bool` | `false` | no | +| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | +| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no | ## Outputs -| Name | Description | -|:-------|:--------------------------------------| -| name | The project ID | +| Name | Description | +|------|-------------| +| name | The project ID | | number | The numeric identifier of the project | + + + +## License +The Apache-2.0 license \ No newline at end of file diff --git a/context.tf b/context.tf new file mode 100644 index 0000000..db0666a --- /dev/null +++ b/context.tf @@ -0,0 +1,202 @@ +# +# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label +# All other instances of this file should be a copy of that one +# +# +# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf +# and then place it in your Terraform module to automatically get +# Cloud Posse's standard configuration inputs suitable for passing +# to Cloud Posse modules. +# +# Modules should access the whole context as `module.this.context` +# to get the input variables with nulls for defaults, +# for example `context = module.this.context`, +# and access individual variables as `module.this.`, +# with final values filled in. +# +# For example, when using defaults, `module.this.context.delimiter` +# will be null, and `module.this.delimiter` will be `-` (hyphen). +# + +module "this" { + source = "cloudposse/label/null" + version = "0.24.1" # requires Terraform >= 0.13.0 + + enabled = var.enabled + namespace = var.namespace + environment = var.environment + stage = var.stage + name = var.name + delimiter = var.delimiter + attributes = var.attributes + tags = var.tags + additional_tag_map = var.additional_tag_map + label_order = var.label_order + regex_replace_chars = var.regex_replace_chars + id_length_limit = var.id_length_limit + label_key_case = var.label_key_case + label_value_case = var.label_value_case + + context = var.context +} + +# Copy contents of cloudposse/terraform-null-label/variables.tf here + +variable "context" { + type = any + default = { + enabled = true + namespace = null + environment = null + stage = null + name = null + delimiter = null + attributes = [] + tags = {} + additional_tag_map = {} + regex_replace_chars = null + label_order = [] + id_length_limit = null + label_key_case = null + label_value_case = null + } + description = <<-EOT + Single object for setting entire context at once. + See description of individual variables for details. + Leave string and numeric variables as `null` to use default value. + Individual variable settings (non-null) override settings in context object, + except for attributes, tags, and additional_tag_map, which are merged. + EOT + + validation { + condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) + error_message = "Allowed values: `lower`, `title`, `upper`." + } + + validation { + condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) + error_message = "Allowed values: `lower`, `title`, `upper`, `none`." + } +} + +variable "enabled" { + type = bool + default = null + description = "Set to false to prevent the module from creating any resources" +} + +variable "namespace" { + type = string + default = null + description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'" +} + +variable "environment" { + type = string + default = null + description = "Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT'" +} + +variable "stage" { + type = string + default = null + description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'" +} + +variable "name" { + type = string + default = null + description = "Solution name, e.g. 'app' or 'jenkins'" +} + +variable "delimiter" { + type = string + default = null + description = <<-EOT + Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`. + Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. + EOT +} + +variable "attributes" { + type = list(string) + default = [] + description = "Additional attributes (e.g. `1`)" +} + +variable "tags" { + type = map(string) + default = {} + description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`" +} + +variable "additional_tag_map" { + type = map(string) + default = {} + description = "Additional tags for appending to tags_as_list_of_maps. Not added to `tags`." +} + +variable "label_order" { + type = list(string) + default = null + description = <<-EOT + The naming order of the id output and Name tag. + Defaults to ["namespace", "environment", "stage", "name", "attributes"]. + You can omit any of the 5 elements, but at least one must be present. + EOT +} + +variable "regex_replace_chars" { + type = string + default = null + description = <<-EOT + Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. + If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. + EOT +} + +variable "id_length_limit" { + type = number + default = null + description = <<-EOT + Limit `id` to this many characters (minimum 6). + Set to `0` for unlimited length. + Set to `null` for default, which is `0`. + Does not affect `id_full`. + EOT + validation { + condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 + error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." + } +} + +variable "label_key_case" { + type = string + default = "lower" + description = <<-EOT + The letter case of label keys (`tag` names) (i.e. `name`, `namespace`, `environment`, `stage`, `attributes`) to use in `tags`. + Possible values: `lower`, `title`, `upper`. + Default value: `title`. + EOT + + validation { + condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) + error_message = "Allowed values: `lower`, `title`, `upper`." + } +} + +variable "label_value_case" { + type = string + default = null + description = <<-EOT + The letter case of output label values (also used in `tags` and `id`). + Possible values: `lower`, `title`, `upper` and `none` (no transformation). + Default value: `lower`. + EOT + + validation { + condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) + error_message = "Allowed values: `lower`, `title`, `upper`, `none`." + } +} +#### End of copy of cloudposse/terraform-null-label/variables.tf diff --git a/examples/basic/README.md b/examples/basic/README.md new file mode 100644 index 0000000..e9066c7 --- /dev/null +++ b/examples/basic/README.md @@ -0,0 +1,22 @@ +# Usage + +## Requirements + +| Name | Version | +|------|---------| +| terraform | >= 0.14 | +| google | >= 3.0 | + +## Providers + +No provider. + +## Inputs + +No input. + +## Outputs + +No output. + + diff --git a/examples/basic/versions.tf b/examples/basic/versions.tf new file mode 100644 index 0000000..8000033 --- /dev/null +++ b/examples/basic/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.0" + } + } + required_version = ">= 0.14" +} diff --git a/main.tf b/main.tf index c6e3f9b..1098e70 100644 --- a/main.tf +++ b/main.tf @@ -1,23 +1,12 @@ -module "label" { - source = "git::https://github.com/SweetOps/terraform-null-label.git?ref=tags/0.7.0" - enabled = var.enabled - namespace = var.namespace - name = var.name - stage = var.stage - environment = var.environment - delimiter = var.delimiter - attributes = var.attributes - tags = var.tags -} - resource "google_project" "default" { - count = var.enabled ? 1 : 0 - name = module.label.id - project_id = module.label.id - labels = module.label.gcp_labels + count = module.this.enabled ? 1 : 0 + + name = module.this.id + project_id = module.this.id + labels = module.this.tags org_id = var.org_id folder_id = var.folder_id - skip_delete = var.enable_deletion_protection + skip_delete = var.skip_delete auto_create_network = var.auto_create_network billing_account = var.billing_account_id } diff --git a/variables.tf b/variables.tf index 93d8a76..742f0bf 100644 --- a/variables.tf +++ b/variables.tf @@ -1,64 +1,22 @@ -variable "namespace" { - type = string - default = "" - description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'" -} - -variable "environment" { - type = string - default = "" - description = "Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT'" -} - -variable "stage" { - type = string - default = "" - description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'" -} - -variable "name" { - type = string - default = "" - description = "Solution name, e.g. 'app' or 'jenkins'" -} - -variable "enabled" { - type = bool - default = true - description = "Set to false to prevent the module from creating any resources" -} - -variable "delimiter" { +variable "org_id" { type = string - default = "-" - description = "Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`" -} - -variable "attributes" { - type = list(string) - default = [] - description = "Additional attributes (e.g. `1`)" + default = null + description = "The numeric ID of the organization this project belongs to." } -variable "tags" { - type = map(string) - default = {} - description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`" -} - -variable "org_id" { +variable "folder_id" { type = string - default = "" - description = "The numeric ID of the organization this project belongs to" + default = null + description = "The numeric ID of the folder this project should be created under." } -variable "folder_id" { +variable "billing_account_id" { type = string - default = "" - description = "The numeric ID of the folder this project should be created under" + default = null + description = "The alphanumeric ID of the billing account this project belongs to." } -variable "enable_deletion_protection" { +variable "skip_delete" { type = bool default = false description = "If true, the Terraform resource can be deleted without deleting the Project via the Google API." @@ -67,11 +25,5 @@ variable "enable_deletion_protection" { variable "auto_create_network" { type = bool default = true - description = "Create the 'default' network automatically" -} - -variable "billing_account_id" { - type = string - default = "" - description = "The alphanumeric ID of the billing account this project belongs to" + description = "Create the `default` network automatically." } diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..8000033 --- /dev/null +++ b/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google" + version = ">= 3.0" + } + } + required_version = ">= 0.14" +}