Skip to content
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
6 changes: 5 additions & 1 deletion bootstrap/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Runner:
terraform_cloud_token: str | None = None
terraform_cloud_organization: str | None = None
terraform_cloud_organization_create: bool | None = None
terraform_cloud_project_create: bool = True
terraform_cloud_admin_email: str | None = None
vault_token: str | None = None
vault_url: str | None = None
Expand Down Expand Up @@ -253,7 +254,10 @@ def init_terraform_cloud(self):
"TF_VAR_create_organization": self.terraform_cloud_organization_create
and "true"
or "false",
"TF_VAR_environments": json.dumps(list(map(itemgetter("slug"), self.envs))),
"TF_VAR_create_project": self.terraform_cloud_project_create
and "true"
or "false",
"TF_VAR_environments": json.dumps(list(map(itemgetter("name"), self.envs))),
"TF_VAR_hostname": self.terraform_cloud_hostname,
"TF_VAR_organization_name": self.terraform_cloud_organization,
"TF_VAR_project_name": self.project_name,
Expand Down
80 changes: 36 additions & 44 deletions terraform/terraform-cloud/main.tf
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
locals {
organization = var.create_organization ? tfe_organization.main[0] : data.tfe_organization.main[0]
project = var.create_project ? tfe_project.main[0] : data.tfe_project.main[0]

workspaces = concat(
flatten(
[
for stage in ["base", "cluster"] :
[
for stack in var.stacks :
{
name = "${var.project_slug}_${var.service_slug}_${stage}_${stack}"
description = "${var.project_name} project, ${var.service_slug} service, ${stack} stack, ${stage} stage"
tags = [
"project:${var.project_slug}",
"service:${var.service_slug}",
"stage:${stage}",
"stack:${stack}",
]
}
]
workspaces = [
for env in var.environments : {
name = "${var.project_slug}_${var.service_slug}_${env}"
description = "${var.project_name} ${var.service_slug} service, ${env} environment."
tags = [
"project:${var.project_slug}",
"layer:service",
"service:${var.service_slug}",
"environment:${env}",
]
),
[
for env in var.environments :
{
name = "${var.project_slug}_${var.service_slug}_environment_${env}"
description = "${var.project_name} project, ${var.service_slug} service, ${env} environment"
tags = [
"project:${var.project_slug}",
"service:${var.service_slug}",
"stage:environment",
"env:${env}",
]
}
]
)
}
]
}

terraform {
Expand All @@ -43,7 +23,7 @@ terraform {
required_providers {
tfe = {
source = "hashicorp/tfe"
version = "~> 0.53"
version = "~> 0.70"
}
}
}
Expand All @@ -68,20 +48,32 @@ resource "tfe_organization" "main" {
email = var.admin_email
}

/* Project */

data "tfe_project" "main" {
count = var.create_project ? 0 : 1

name = var.project_slug
organization = local.organization.name
}

resource "tfe_project" "main" {
count = var.create_project ? 1 : 0

organization = local.organization.name
name = var.project_slug
description = "${var.project_name} project workspaces."
default_execution_mode = "local"
}

/* Workspaces */

resource "tfe_workspace" "main" {
for_each = { for i in local.workspaces : i.name => i }

name = each.value.name
description = each.value.description
organization = local.organization.name
tag_names = each.value.tags
name = each.value.name
description = each.value.description
organization = local.organization.name
project_id = local.project.id
tag_names = each.value.tags
}

resource "tfe_workspace_settings" "main" {
for_each = tfe_workspace.main

workspace_id = each.value.id
execution_mode = "local"
}
14 changes: 7 additions & 7 deletions terraform/terraform-cloud/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ variable "create_organization" {
default = false
}

variable "create_project" {
description = "Tell if the Terraform Cloud project should be created (false when Talos parent has already created it)."
type = bool
default = true
}

variable "environments" {
description = "The list of environment slugs."
description = "The list of environment names (used as workspace suffix)."
type = list(string)
default = []
}
Expand Down Expand Up @@ -42,12 +48,6 @@ variable "service_slug" {
type = string
}

variable "stacks" {
description = "The list of stack slugs."
type = list(string)
default = []
}

variable "terraform_cloud_token" {
description = "The Terraform Cloud token."
type = string
Expand Down
Loading