Skip to content

Commit

Permalink
feat: dns resource
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jan 8, 2024
1 parent 2d065e5 commit c731572
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/dns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!-- BEGIN_TF_DOCS -->
## 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 |
| humanitec\_application | 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 |
<!-- END_TF_DOCS -->
31 changes: 31 additions & 0 deletions examples/dns/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
locals {
res_def_prefix = "${var.humanitec_application}-"
}

resource "humanitec_application" "example" {
id = var.humanitec_application
name = var.humanitec_application
}

# 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
}
12 changes: 12 additions & 0 deletions examples/dns/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
humanitec = {
source = "humanitec/humanitec"
version = "~> 0"
}
}

required_version = ">= 1.3.0"
}

provider "humanitec" {}
22 changes: 22 additions & 0 deletions examples/dns/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# GCP credentials
credentials = ""

humanitec_application = "dns-test"

# 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"
40 changes: 40 additions & 0 deletions examples/dns/variables.tf
Original file line number Diff line number Diff line change
@@ -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 "humanitec_application" {
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"
}
39 changes: 39 additions & 0 deletions humanitec-resource-defs/dns/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- BEGIN_TF_DOCS -->
## 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 |
<!-- END_TF_DOCS -->
32 changes: 32 additions & 0 deletions humanitec-resource-defs/dns/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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}"
managed_zone = var.managed_zone
managed_zone_dns_name = var.managed_zone_dns_name
subdomain = "$${context.app.id}-$${context.env.id}"
}
})
}
}
3 changes: 3 additions & 0 deletions humanitec-resource-defs/dns/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "id" {
value = humanitec_resource_definition.main.id
}
10 changes: 10 additions & 0 deletions humanitec-resource-defs/dns/basic/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_providers {
humanitec = {
source = "humanitec/humanitec"
version = "~> 0"
}
}

required_version = ">= 1.3.0"
}
13 changes: 13 additions & 0 deletions humanitec-resource-defs/dns/basic/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -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"
35 changes: 35 additions & 0 deletions humanitec-resource-defs/dns/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions modules/dns/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- BEGIN_TF_DOCS -->
## 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 |
| 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. |
<!-- END_TF_DOCS -->
32 changes: 32 additions & 0 deletions modules/dns/basic/main.tf
Original file line number Diff line number Diff line change
@@ -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."
}
}
}
4 changes: 4 additions & 0 deletions modules/dns/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "host" {
description = "The DNS name."
value = google_dns_record_set.main.name
}
22 changes: 22 additions & 0 deletions modules/dns/basic/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
}
}
Loading

0 comments on commit c731572

Please sign in to comment.