From 6f8befc3273d3bdb623c066ce946dd5989896bd4 Mon Sep 17 00:00:00 2001 From: "Robert Barnes (DevOps Rob)" Date: Mon, 16 May 2022 10:15:18 +0100 Subject: [PATCH] added examples and refactored codebase --- .../consul-terraform-sync-example/main.tf | 18 ++++++ .../providers.tf | 7 +++ .../consul-terraform-sync-example/vars.tf | 55 +++++++++++++++++++ main.tf | 48 ++++++---------- outputs.tf | 6 +- vars.tf | 41 +++++--------- 6 files changed, 114 insertions(+), 61 deletions(-) create mode 100644 examples/consul-terraform-sync-example/main.tf create mode 100644 examples/consul-terraform-sync-example/providers.tf create mode 100644 examples/consul-terraform-sync-example/vars.tf diff --git a/examples/consul-terraform-sync-example/main.tf b/examples/consul-terraform-sync-example/main.tf new file mode 100644 index 0000000..47e7112 --- /dev/null +++ b/examples/consul-terraform-sync-example/main.tf @@ -0,0 +1,18 @@ +locals { + # Group service instances by service name + consul_services = { + for id, s in var.services : s.name => s... + } +} + +module "boundary_resources" { + for_each = var.services + + source = "github.com/hashicorp-dev-advocates/terraform-cts-boundary" + + project_scope_id = "p_1234567890" + service_name = each.value["name"] + service_address = each.value["address"] + service_port = each.value["port"] + +} \ No newline at end of file diff --git a/examples/consul-terraform-sync-example/providers.tf b/examples/consul-terraform-sync-example/providers.tf new file mode 100644 index 0000000..38def77 --- /dev/null +++ b/examples/consul-terraform-sync-example/providers.tf @@ -0,0 +1,7 @@ +provider "boundary" { + addr = var.boundary_address + + auth_method_id = var.auth_method_id + password_auth_method_login_name = var.login_name + password_auth_method_password = var.password +} diff --git a/examples/consul-terraform-sync-example/vars.tf b/examples/consul-terraform-sync-example/vars.tf new file mode 100644 index 0000000..5dec9a4 --- /dev/null +++ b/examples/consul-terraform-sync-example/vars.tf @@ -0,0 +1,55 @@ +# Boundary variables +variable "boundary_address" { + type = string + description = "Boundary address" + default = "http://localhost:9200" +} + +variable "project_scope_id" { + type = string + description = "Scope ID for Boundary project" + default = "p_1234567890" +} + +variable "auth_method_id" { + type = string + description = "Auth method ID for Terraform to authenticate to." +} + +variable "login_name" { + type = string + description = "Login name for Terraform to authenticate with." +} + +variable "password" { + type = string + description = "Password for Terraform to authenticate with." + sensitive = true +} + +#Consul Terraform Sync variables +variable "services" { + description = "Consul services monitored by Consul-Terraform-Sync" + type = map( + object({ + id = string + name = string + kind = string + address = string + port = number + meta = map(string) + tags = list(string) + namespace = string + status = string + + node = string + node_id = string + node_address = string + node_datacenter = string + node_tagged_addresses = map(string) + node_meta = map(string) + + cts_user_defined_meta = map(string) + }) + ) +} diff --git a/main.tf b/main.tf index 181cfe8..bed5844 100644 --- a/main.tf +++ b/main.tf @@ -1,53 +1,39 @@ -locals { - # Group service instances by service name - consul_services = { - for id, s in var.services : s.name => s... - } -} - -resource "boundary_host_catalog" "host_catalog" { +resource "boundary_host_catalog_static" "host_catalog" { - for_each = var.services - name = each.value["name"] - description = "${each.value["name"]} host catalog created by Consul Terraform Sync" - type = "static" + name = var.service_name + description = "${var.service_name} host catalog created by Consul Terraform Sync" scope_id = var.project_scope_id } -resource "boundary_host" "host" { +resource "boundary_host_static" "host" { - for_each = var.services - - name = each.value["name"] - description = "${each.value["name"]} host created by Consul Terraform Sync" - host_catalog_id = boundary_host_catalog.host_catalog[each.key].id - address = each.value["address"] + name = var.service_name + description = "${var.service_name} host created by Consul Terraform Sync" + host_catalog_id = boundary_host_catalog_static.host_catalog.id + address = var.service_address type = "static" } -resource "boundary_host_set" "host_set" { - for_each = var.services - - name = each.value["name"] - description = "${each.value["name"]} host set created by Consul Terraform Sync" - host_catalog_id = boundary_host_catalog.host_catalog[each.key].id +resource "boundary_host_set_static" "host_set" { + name = var.service_name + description = "${var.service_name} host set created by Consul Terraform Sync" + host_catalog_id = boundary_host_catalog_static.host_catalog.id type = "static" host_ids = [ - boundary_host.host[each.key].id + boundary_host_static.host.id ] } resource "boundary_target" "target" { - for_each = var.services - name = each.value["name"] + name = var.service_name type = "tcp" - description = "${each.value["name"]} target created by Consul Terraform Sync" - default_port = each.value["port"] + description = "${var.service_name} target created by Consul Terraform Sync" + default_port = var.service_port scope_id = var.project_scope_id host_source_ids = [ - boundary_host_set.host_set[each.key].id + boundary_host_set_static.host_set.id ] } diff --git a/outputs.tf b/outputs.tf index c794759..a90f6d9 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,3 +1,3 @@ -output "consul_services" { - value = local.consul_services -} +#output "consul_services" { +# value = local.consul_services +#} diff --git a/vars.tf b/vars.tf index f138a41..1be9044 100644 --- a/vars.tf +++ b/vars.tf @@ -1,32 +1,19 @@ +variable "project_scope_id" { + type = string + description = "Scope ID for Boundary project" +} -variable "services" { - description = "Consul services monitored by Consul-Terraform-Sync" - type = map( - object({ - id = string - name = string - kind = string - address = string - port = number - meta = map(string) - tags = list(string) - namespace = string - status = string - - node = string - node_id = string - node_address = string - node_datacenter = string - node_tagged_addresses = map(string) - node_meta = map(string) - - cts_user_defined_meta = map(string) - }) - ) +variable "service_name" { + type = string + description = "Name of Consul Service to register as a host and target in Boundary." } -variable "project_scope_id" { +variable "service_address" { type = string - description = "Scope ID for Boundary project" - default = "p_1234567890" + description = "IP address or DNS name for Consul service to register as a host in Boundary." +} + +variable "service_port" { + type = number + description = "Port of Consul service to register as a target in Boundary." }