Skip to content

Commit

Permalink
normalize code
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Mar 24, 2023
1 parent aa055bb commit 708ee4c
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 93 deletions.
20 changes: 20 additions & 0 deletions .tf-control
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# .tf-control
# allows for setting a specific command to be used for tf-* commands under this git repo
# see tf-control.sh help for more info

TFCONTROL_VERSION="1.0.5"

TFCOMMAND="terraform_latest"
# TF_CLI_CONFIG_FILE=PATH-TO-FILE/.tf-control.tfrc
# TFARGS=""
# TFNOLOG=""
# TFNOCOLOR=""

# use the following to force a specific version. An upgrade of an existing 0.12.31 to 1.x
# needs you to cycle through 0.13.17, 0.14.11, and then latest (0.15.5 not needed). Other
# steps in between. See https://github.e.it.census.gov/terraform/support/tree/master/docs/how-to/terraform-upgrade for details
#
#TFCOMMAND="terraform_0.12.31"
#TFCOMMAND="terraform_0.13.7"
#TFCOMMAND="terraform_0.14.11"
#TFCOMMAND="terraform_0.15.5"
24 changes: 24 additions & 0 deletions .tf-control.tfrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TFCONTROL_VERSION="1.0.5"

# https://www.terraform.io/docs/cli/config/config-file.html
plugin_cache_dir = "/data/terraform/terraform.d/plugin-cache"
#disable_checkpoint = true

provider_installation {
# filesystem_mirror {
# path = "/apps/terraform/terraform.d/providers"
# include = [ "*/*/*" ]
# }
filesystem_mirror {
path = "/data/terraform/terraform.d/providers"
include = [ "*/*/*" ]
}
# filesystem_mirror {
# path = "/apps/terraform/terraform.d/providers"
# include = [ "external.terraform.census.gov/*/*" ]
# }
direct {
include = [ "*/*/*" ]
}
}

44 changes: 44 additions & 0 deletions cname/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cname/common.tf
1 change: 1 addition & 0 deletions cname/entry.tf
1 change: 1 addition & 0 deletions cname/entry_heritage.tf
1 change: 1 addition & 0 deletions cname/heritage_tags.tf
50 changes: 3 additions & 47 deletions cname/main.tf
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
locals {
account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id
account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew"

base_tags = {
"boc:tf_module_version" = local._module_version
"boc:created_by" = "terraform"
}
}

locals {
name_parts = split(".", var.name)
host_name = var.name_parts[0]
zone = trimprefix(var.name, format("%v.", local.host_name))
private_zone = local.account_environment == "gov" ? true : var.private_zone
rr_type = uppercase(var.type)
default_enable_ptr = lookup(local._defaults.enable_ptr, var.type, false)
default_heritage_prefix = lookup(local._defaults.heritage_prefix, var.type, "") != "" ? format("%v.", lookup(local._defaults.heritage_prefix, var.type)) : ""
is_cname = var.type == "cname"
base_heritage_tags = [
format("heritage=%v", local._defaults.heritage_label),
format("%v/account_id=%v", local._defaults.heritage_label, data.aws_caller_identity.current.account_id),
format("%v/region=%v", local._defaults.heritage_label, local.region),
format("%v/create_time=%d", local._defaults.heritage_label, time_static.timestamp.unix)
]
heritage_tags = [for k, v in var.heritage_tags : format("%v/%v", local._defaults.heritage_label, k)]
local {
# force to cname for this module
record_type = "cname"
}


# if the zone grab fails here, it either doesn't exist or it is not associated with this VPC
data "aws_route53_zone" "zone" {
name = local.zone
private_zone = local.private_zone
}

resource "time_static" "timestamp" {}

resource "aws_route53_record" "entry" {
count = local.is_cname ? 1 : 0
zone_id = data.aws_route53_zone.zone.zone_id
name = var.name
type = local.rr_type
ttl = var.ttl
records = var.values
}


resource "aws_route53_record" "entry_heritage" {
count = var.enable_heritage ? 1 : 0
zone_id = data.aws_route53_zone.zone.zone_id
name = format("%v%v", aws_route53_record.entry.name)

type = "TXT"
ttl = var.ttl
records = [join(",", concat(local.base_heritage_tags, local.heritage_tags))]
}
4 changes: 4 additions & 0 deletions cname/x
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
../common/entry_heritage.tf
../common/entry.tf
../common/heritage_tags.tf
../common/zone_forward.tf
1 change: 1 addition & 0 deletions cname/zone_forward.tf
23 changes: 23 additions & 0 deletions common/common.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
locals {
account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id
account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew"
region = var.region

base_tags = {
"boc:tf_module_version" = local._module_version
"boc:created_by" = "terraform"
}
}

locals {
name_parts = split(".", var.name)
host_name = var.name_parts[0]
zone = trimprefix(var.name, format("%v.", local.host_name))
private_zone = local.account_environment == "gov" ? true : var.private_zone
rr_type = uppercase(local.record_type)
default_enable_ptr = lookup(local._defaults.enable_ptr, local.record_type, false)
default_heritage_prefix = lookup(local._defaults.heritage_prefix, local.record_type, "") != "" ? format("%v.", lookup(local._defaults.heritage_prefix, local.record_type)) : ""
values = type(var.values) == "string" ? [var.values] : var.values
}

resource "time_static" "timestamp" {}
46 changes: 0 additions & 46 deletions common/entries.tf

This file was deleted.

7 changes: 7 additions & 0 deletions common/entry.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_route53_record" "entry" {
zone_id = data.aws_route53_zone.zone.zone_id
name = var.name
type = local.rr_type
ttl = var.ttl
records = var.values
}
9 changes: 9 additions & 0 deletions common/entry_heritage.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_route53_record" "entry_heritage" {
count = var.enable_heritage ? 1 : 0
zone_id = data.aws_route53_zone.zone.zone_id
name = format("%v%v", aws_route53_record.entry[0].name)

type = "TXT"
ttl = var.ttl
records = [join(",", concat(local.base_heritage_tags, local.heritage_tags))]
}
9 changes: 9 additions & 0 deletions common/heritage_tags.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
locals {
base_heritage_tags = [
format("heritage=%v", local._defaults.heritage_label),
format("%v/account_id=%v", local._defaults.heritage_label, data.aws_caller_identity.current.account_id),
format("%v/region=%v", local._defaults.heritage_label, local.region),
format("%v/create_time=%d", local._defaults.heritage_label, time_static.timestamp.unix)
]
heritage_tags = [for k, v in var.heritage_tags : format("%v/%v", local._defaults.heritage_label, k)]
}
5 changes: 5 additions & 0 deletions common/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ variable "ttl" {
type = number
default = 900
}

variable "values" {
description = "DNS value to set for the name. May be a string or list of strings (like multiple IP addresses)"
type = any
}
4 changes: 4 additions & 0 deletions common/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ terraform {
source = "hashicorp/aws"
version = ">= 4"
}
time = {
source = "hashicorp/time"
version = ">= 0.9.1"
}
}
}
5 changes: 5 additions & 0 deletions common/zone_forward.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# if the zone grab fails here, it either doesn't exist or it is not associated with this VPC
data "aws_route53_zone" "zone" {
name = local.zone
private_zone = local.private_zone
}

0 comments on commit 708ee4c

Please sign in to comment.