Skip to content

Commit

Permalink
added examples and refactored codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
devops-rob committed May 16, 2022
1 parent 7708dae commit 6f8befc
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 61 deletions.
18 changes: 18 additions & 0 deletions examples/consul-terraform-sync-example/main.tf
Original file line number Diff line number Diff line change
@@ -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"]

}
7 changes: 7 additions & 0 deletions examples/consul-terraform-sync-example/providers.tf
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions examples/consul-terraform-sync-example/vars.tf
Original file line number Diff line number Diff line change
@@ -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)
})
)
}
48 changes: 17 additions & 31 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
]
}
6 changes: 3 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "consul_services" {
value = local.consul_services
}
#output "consul_services" {
# value = local.consul_services
#}
41 changes: 14 additions & 27 deletions vars.tf
Original file line number Diff line number Diff line change
@@ -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."
}

0 comments on commit 6f8befc

Please sign in to comment.