diff --git a/README.md b/README.md index e3412cc..048508b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ A collection of GCP resources ready to be used with [Humanitec](https://humanite The following resources are included: -* [redis/basic](./humanitec-resource-defs/redis/basic): GCP Memorystore Redis resource. +* [dns/basic](./humanitec-resource-defs/dns/basic): GCP Cloud DNS `dns` resource. +* [redis/basic](./humanitec-resource-defs/redis/basic): GCP Memorystore `redis` resource. The `humanitec-resource-defs` directory includes the respective resource definitions. diff --git a/examples/dns/README.md b/examples/dns/README.md new file mode 100644 index 0000000..9741330 --- /dev/null +++ b/examples/dns/README.md @@ -0,0 +1,40 @@ + +## Requirements + +| Name | Version | +|------|---------| +| terraform | >= 1.3.0 | +| humanitec | ~> 0 | + +## Providers + +| Name | Version | +|------|---------| +| humanitec | ~> 0 | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| dns\_basic | ../../humanitec-resource-defs/dns/basic | n/a | + +## Resources + +| Name | Type | +|------|------| +| [humanitec_application.example](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/application) | resource | +| [humanitec_resource_definition_criteria.dns_basic](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| credentials | GCP credentials | `string` | n/a | yes | +| managed\_zone | The name of the zone in which this record set will reside. | `string` | n/a | yes | +| managed\_zone\_dns\_name | The DNS name of the managed zone. | `string` | n/a | yes | +| project | GCP project ID | `string` | n/a | yes | +| region | n/a | `string` | n/a | yes | +| app\_name | n/a | `string` | `"dns-test"` | no | +| resource\_packs\_gcp\_rev | GCP Resource Pack git ref | `string` | `"refs/heads/main"` | no | +| resource\_packs\_gcp\_url | GCP Resource Pack git url | `string` | `"https://github.com/humanitec-architecture/resource-packs-gcp.git"` | no | + \ No newline at end of file diff --git a/examples/dns/main.tf b/examples/dns/main.tf new file mode 100644 index 0000000..6bdb3cf --- /dev/null +++ b/examples/dns/main.tf @@ -0,0 +1,31 @@ +locals { + res_def_prefix = "${var.app_name}-" +} + +resource "humanitec_application" "example" { + id = var.app_name + name = var.app_name +} + +# cloud dns + +module "dns_basic" { + source = "../../humanitec-resource-defs/dns/basic" + + prefix = local.res_def_prefix + + resource_packs_gcp_url = var.resource_packs_gcp_url + resource_packs_gcp_rev = var.resource_packs_gcp_rev + + project = var.project + region = var.region + credentials = var.credentials + + managed_zone = var.managed_zone + managed_zone_dns_name = var.managed_zone_dns_name +} + +resource "humanitec_resource_definition_criteria" "dns_basic" { + resource_definition_id = module.dns_basic.id + app_id = humanitec_application.example.id +} diff --git a/examples/dns/providers.tf b/examples/dns/providers.tf new file mode 100644 index 0000000..53e6404 --- /dev/null +++ b/examples/dns/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + humanitec = { + source = "humanitec/humanitec" + version = "~> 0" + } + } + + required_version = ">= 1.3.0" +} + +provider "humanitec" {} diff --git a/examples/dns/terraform.tfvars.example b/examples/dns/terraform.tfvars.example new file mode 100644 index 0000000..8f44cc8 --- /dev/null +++ b/examples/dns/terraform.tfvars.example @@ -0,0 +1,21 @@ +app_name = "dns-test" + +# GCP credentials +credentials = "" + +# The name of the zone in which this record set will reside. +managed_zone = "" + +# The DNS name of the managed zone. +managed_zone_dns_name = "" + +# GCP project ID +project = "" + +region = "" + +# GCP Resource Pack git ref +resource_packs_gcp_rev = "refs/heads/main" + +# GCP Resource Pack git url +resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git" \ No newline at end of file diff --git a/examples/dns/variables.tf b/examples/dns/variables.tf new file mode 100644 index 0000000..2b00f5a --- /dev/null +++ b/examples/dns/variables.tf @@ -0,0 +1,40 @@ +variable "project" { + description = "GCP project ID" + type = string +} + +variable "region" { + type = string +} + +variable "credentials" { + description = "GCP credentials" + type = string +} + +variable "managed_zone" { + description = "The name of the zone in which this record set will reside." + type = string +} + +variable "managed_zone_dns_name" { + description = "The DNS name of the managed zone." + type = string +} + +variable "app_name" { + type = string + default = "dns-test" +} + +variable "resource_packs_gcp_url" { + description = "GCP Resource Pack git url" + type = string + default = "https://github.com/humanitec-architecture/resource-packs-gcp.git" +} + +variable "resource_packs_gcp_rev" { + description = "GCP Resource Pack git ref" + type = string + default = "refs/heads/main" +} diff --git a/humanitec-resource-defs/dns/basic/README.md b/humanitec-resource-defs/dns/basic/README.md new file mode 100644 index 0000000..4f9ed87 --- /dev/null +++ b/humanitec-resource-defs/dns/basic/README.md @@ -0,0 +1,39 @@ + +## Requirements + +| Name | Version | +|------|---------| +| terraform | >= 1.3.0 | +| humanitec | ~> 0 | + +## Providers + +| Name | Version | +|------|---------| +| humanitec | ~> 0 | + +## Resources + +| Name | Type | +|------|------| +| [humanitec_resource_definition.main](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| credentials | n/a | `any` | n/a | yes | +| managed\_zone | The name of the zone in which this record set will reside. | `string` | n/a | yes | +| managed\_zone\_dns\_name | The DNS name of the managed zone. | `string` | n/a | yes | +| prefix | n/a | `string` | n/a | yes | +| project | n/a | `string` | n/a | yes | +| region | n/a | `any` | n/a | yes | +| resource\_packs\_gcp\_rev | n/a | `string` | `"ref/heads/main"` | no | +| resource\_packs\_gcp\_url | n/a | `string` | `"https://github.com/humanitec-architecture/resource-packs-gcp.git"` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| id | n/a | + \ No newline at end of file diff --git a/humanitec-resource-defs/dns/basic/main.tf b/humanitec-resource-defs/dns/basic/main.tf new file mode 100644 index 0000000..6f02f3c --- /dev/null +++ b/humanitec-resource-defs/dns/basic/main.tf @@ -0,0 +1,33 @@ +resource "humanitec_resource_definition" "main" { + driver_type = "humanitec/terraform" + id = "${var.prefix}clouddns-basic" + name = "${var.prefix}clouddns-basic" + type = "dns" + + driver_inputs = { + secrets_string = jsonencode({ + variables = { + credentials = var.credentials + } + }) + + values_string = jsonencode({ + source = { + path = "modules/dns/basic" + rev = var.resource_packs_gcp_rev + url = var.resource_packs_gcp_url + } + + variables = { + project = var.project + region = var.region + app_id = "$${context.app.id}" + env_id = "$${context.env.id}" + res_id = "$${context.res.id}" + managed_zone = var.managed_zone + managed_zone_dns_name = var.managed_zone_dns_name + subdomain = "$${context.app.id}-$${context.env.id}" + } + }) + } +} diff --git a/humanitec-resource-defs/dns/basic/outputs.tf b/humanitec-resource-defs/dns/basic/outputs.tf new file mode 100644 index 0000000..28542ec --- /dev/null +++ b/humanitec-resource-defs/dns/basic/outputs.tf @@ -0,0 +1,3 @@ +output "id" { + value = humanitec_resource_definition.main.id +} diff --git a/humanitec-resource-defs/dns/basic/providers.tf b/humanitec-resource-defs/dns/basic/providers.tf new file mode 100644 index 0000000..86c4fc3 --- /dev/null +++ b/humanitec-resource-defs/dns/basic/providers.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + humanitec = { + source = "humanitec/humanitec" + version = "~> 0" + } + } + + required_version = ">= 1.3.0" +} diff --git a/humanitec-resource-defs/dns/basic/terraform.tfvars.example b/humanitec-resource-defs/dns/basic/terraform.tfvars.example new file mode 100644 index 0000000..83354ce --- /dev/null +++ b/humanitec-resource-defs/dns/basic/terraform.tfvars.example @@ -0,0 +1,13 @@ +credentials = "" + +# The name of the zone in which this record set will reside. +managed_zone = "" + +# The DNS name of the managed zone. +managed_zone_dns_name = "" + +prefix = "" +project = "" +region = "" +resource_packs_gcp_rev = "ref/heads/main" +resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git" \ No newline at end of file diff --git a/humanitec-resource-defs/dns/basic/variables.tf b/humanitec-resource-defs/dns/basic/variables.tf new file mode 100644 index 0000000..4658337 --- /dev/null +++ b/humanitec-resource-defs/dns/basic/variables.tf @@ -0,0 +1,35 @@ +variable "prefix" { + type = string +} + +variable "resource_packs_gcp_rev" { + type = string + default = "ref/heads/main" +} + +variable "resource_packs_gcp_url" { + type = string + default = "https://github.com/humanitec-architecture/resource-packs-gcp.git" +} + +variable "project" { + type = string +} + +variable "region" { + type = any +} + +variable "credentials" { + type = any +} + +variable "managed_zone" { + description = "The name of the zone in which this record set will reside." + type = string +} + +variable "managed_zone_dns_name" { + description = "The DNS name of the managed zone." + type = string +} diff --git a/modules/dns/basic/README.md b/modules/dns/basic/README.md new file mode 100644 index 0000000..4e955db --- /dev/null +++ b/modules/dns/basic/README.md @@ -0,0 +1,43 @@ + +## Requirements + +| Name | Version | +|------|---------| +| terraform | >= 1.3.0 | +| google | ~> 5.10 | + +## Providers + +| Name | Version | +|------|---------| +| google | ~> 5.10 | + +## Resources + +| Name | Type | +|------|------| +| [google_dns_record_set.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| app\_id | n/a | `string` | n/a | yes | +| credentials | n/a | `string` | n/a | yes | +| env\_id | n/a | `string` | n/a | yes | +| managed\_zone | The name of the zone in which this record set will reside. | `string` | n/a | yes | +| managed\_zone\_dns\_name | The DNS name of the managed zone. | `string` | n/a | yes | +| project | n/a | `string` | n/a | yes | +| region | n/a | `string` | n/a | yes | +| res\_id | n/a | `string` | n/a | yes | +| subdomain | The subdomain of the DNS name that the DNS record is for. | `string` | n/a | yes | +| ip\_address | The IPv4 address that the DNS name should resolve to. | `string` | `""` | no | +| ipv6\_address | The IPv6 address that the DNS name should resolve to. | `string` | `""` | no | +| name | A valid fully qualified domain name that the DNS name should resolve to. | `string` | `""` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| host | The DNS name. | + \ No newline at end of file diff --git a/modules/dns/basic/main.tf b/modules/dns/basic/main.tf new file mode 100644 index 0000000..48f5faf --- /dev/null +++ b/modules/dns/basic/main.tf @@ -0,0 +1,32 @@ +# Disabled due to https://github.com/hashicorp/terraform-provider-google/issues/16832 +# data "google_dns_managed_zone" "main" { +# name = var.managed_zone +# } + +locals { + types = ["A", "AAAA", "CNAME"] + records = [var.ip_address, var.ipv6_address, var.name] + rrdatas = [for r in local.records : r if r != ""] + + # If every record is empty, then the type is A, but this will be caught below. + type = local.types[index(local.records, length(local.rrdatas) > 0 ? local.rrdatas[0] : "")] +} + +resource "google_dns_record_set" "main" { + # name = "${var.subdomain}.${data.google_dns_managed_zone.main.dns_name}" + # managed_zone = data.google_dns_managed_zone.main.name + + name = "${var.subdomain}.${var.managed_zone_dns_name}" + managed_zone = var.managed_zone + type = local.type + ttl = 300 + + rrdatas = local.rrdatas + + lifecycle { + precondition { + condition = length(local.rrdatas) > 0 + error_message = "Only one of ip_address, ipv6_address or name is supported." + } + } +} diff --git a/modules/dns/basic/outputs.tf b/modules/dns/basic/outputs.tf new file mode 100644 index 0000000..0aa9af1 --- /dev/null +++ b/modules/dns/basic/outputs.tf @@ -0,0 +1,4 @@ +output "host" { + description = "The DNS name." + value = google_dns_record_set.main.name +} diff --git a/modules/dns/basic/providers.tf b/modules/dns/basic/providers.tf new file mode 100644 index 0000000..16f70be --- /dev/null +++ b/modules/dns/basic/providers.tf @@ -0,0 +1,23 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google" + version = "~> 5.10" + } + } + + required_version = ">= 1.3.0" +} + +provider "google" { + project = var.project + region = var.region + credentials = var.credentials + + default_labels = { + "managed-by" = "humanitec" + "hum-app-id" = var.app_id + "hum-env-id" = var.env_id + "hum-res-id" = replace(var.res_id, ".", "-") + } +} diff --git a/modules/dns/basic/terraform.tfvars.example b/modules/dns/basic/terraform.tfvars.example new file mode 100644 index 0000000..521e0f1 --- /dev/null +++ b/modules/dns/basic/terraform.tfvars.example @@ -0,0 +1,25 @@ +app_id = "" +credentials = "" +env_id = "" + +# The IPv4 address that the DNS name should resolve to. +ip_address = "" + +# The IPv6 address that the DNS name should resolve to. +ipv6_address = "" + +# The name of the zone in which this record set will reside. +managed_zone = "" + +# The DNS name of the managed zone. +managed_zone_dns_name = "" + +# A valid fully qualified domain name that the DNS name should resolve to. +name = "" + +project = "" +region = "" +res_id = "" + +# The subdomain of the DNS name that the DNS record is for. +subdomain = "" \ No newline at end of file diff --git a/modules/dns/basic/variables.tf b/modules/dns/basic/variables.tf new file mode 100644 index 0000000..16993a0 --- /dev/null +++ b/modules/dns/basic/variables.tf @@ -0,0 +1,57 @@ +variable "project" { + type = string +} + +variable "region" { + type = string +} + +variable "credentials" { + type = string +} + +variable "app_id" { + type = string +} + +variable "env_id" { + type = string +} + +variable "res_id" { + type = string +} + +variable "managed_zone" { + description = "The name of the zone in which this record set will reside." + type = string +} + +variable "managed_zone_dns_name" { + description = "The DNS name of the managed zone." + type = string +} + +variable "ip_address" { + description = "The IPv4 address that the DNS name should resolve to." + type = string + default = "" +} + +variable "ipv6_address" { + description = "The IPv6 address that the DNS name should resolve to." + type = string + default = "" +} + +variable "name" { + description = "A valid fully qualified domain name that the DNS name should resolve to." + type = string + default = "" +} + +# TODO: Check if we want to also provide template support here. +variable "subdomain" { + description = "The subdomain of the DNS name that the DNS record is for." + type = string +}