Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dns resource #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
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 |
| 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 |
<!-- 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.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
}
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" {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using here such a configuration:

provider "humanitec" {
  host   = var.humanitec_host
  org_id = var.humanitec_org_id
  token  = var.humanitec_token
}

But I don't have any opinion about its existence rather than it should be consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For AWS I generally left those values out to reduce the amount of inputs, but we can also keep them.

21 changes: 21 additions & 0 deletions examples/dns/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -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"
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 "app_name" {
type = string
default = "dns-test"
johanneswuerbach marked this conversation as resolved.
Show resolved Hide resolved
}

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 -->
33 changes: 33 additions & 0 deletions humanitec-resource-defs/dns/basic/main.tf
Original file line number Diff line number Diff line change
@@ -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}"
johanneswuerbach marked this conversation as resolved.
Show resolved Hide resolved
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}"
}
})
}
}
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
}
43 changes: 43 additions & 0 deletions modules/dns/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!-- 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 |
| 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. |
<!-- 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
}
Loading