Skip to content

Commit

Permalink
feat(TPLAT-340): added tfe-workspace module (#2)
Browse files Browse the repository at this point in the history
* feat(TPLAT-340): moved module here

Signed-off-by: tom-hauschke <[email protected]>
Co-authored-by: Leon Steinhäuser <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 13, 2023
1 parent c8cece1 commit 490075c
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 44 deletions.
22 changes: 22 additions & 0 deletions .terraform.lock.hcl

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

98 changes: 56 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
# terraform-module-template

A template repository to provide a basic setup for Terraform modules.

## Module structure

The module structure is based on the [Terraform module documentation](https://www.terraform.io/docs/modules/index.html#standard-module-structure). The following tree shows the structure of the module.

```txt
├── .gitignore
├── LICENSE
├── README.md
├── docs
│ └── README.md
├── examples
│ ├── complete
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── variables.tf
│ │ └── versions.tf
│ ├── minimal
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── variables.tf
│ │ └── versions.tf
├── main.tf
├── outputs.tf
├── variables.tf
└── versions.tf
```

## Working with this template

In order to use this template, you can use the GitHub template feature. This will create a new repository based on this template. After that, you can clone the repository and start working on it.

### Creating a new repository based on this template

To get started with this template, you have to navigate https://github.com/new and select the Tagesspiegel organization. After that, you can select the `terraform-module-template` repository, enter a name for your new repository and click on `Create repository`. Please note that you have to define a name for your new repository that is not already taken and follows the naming conventions (`terraform-<provider>-<name>`).

![Create GitHub repository based on template](docs/github_create_repository.png)

If everything worked as expected, you should now have a new repository based on this template. You can now clone the repository and start working on it.
# Workspace

A module to simplify the creation of a Terraform Cloud workspace.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_tfe"></a> [tfe](#requirement\_tfe) | 0.48.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_tfe"></a> [tfe](#provider\_tfe) | 0.48.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [tfe_workspace.this](https://registry.terraform.io/providers/hashicorp/tfe/0.48.0/docs/resources/workspace) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_agent_pool_id"></a> [agent\_pool\_id](#input\_agent\_pool\_id) | The ID of the agent pool to use for this workspace. Defaults to ''. Should not be changed, unless you know what you are doing. | `string` | `""` | no |
| <a name="input_allow_destroy_plan"></a> [allow\_destroy\_plan](#input\_allow\_destroy\_plan) | Whether destroy plans are allowed for this workspace. Defaults to 'false'. Should not be changed, unless you know what you are doing. | `bool` | `false` | no |
| <a name="input_auto_apply"></a> [auto\_apply](#input\_auto\_apply) | Whether to automatically apply changes when a Terraform plan is successful. Defaults to 'true'. Should not be changed, unless you know what you are doing. | `bool` | `true` | no |
| <a name="input_description"></a> [description](#input\_description) | The description of the workspace | `string` | n/a | yes |
| <a name="input_execution_mode"></a> [execution\_mode](#input\_execution\_mode) | The execution mode to use for this workspace. Defaults to 'remote'. Should not be changed, unless you know what you are doing. | `string` | `"remote"` | no |
| <a name="input_file_triggers_enabled"></a> [file\_triggers\_enabled](#input\_file\_triggers\_enabled) | Whether to enable file triggers for this workspace. Defaults to 'true'. Should not be changed, unless you know what you are doing. | `bool` | `true` | no |
| <a name="input_global_accessable_state"></a> [global\_accessable\_state](#input\_global\_accessable\_state) | Whether the state of this workspace should be accessable by all other workspaces. Defaults to 'false'. | `bool` | `false` | no |
| <a name="input_name"></a> [name](#input\_name) | The name of the workspace | `string` | n/a | yes |
| <a name="input_organization_github_app_installation_id"></a> [organization\_github\_app\_installation\_id](#input\_organization\_github\_app\_installation\_id) | The ID of the GitHub App Installation. Should not be changed, unless you know what you are doing. | `string` | n/a | yes |
| <a name="input_organization_name"></a> [organization\_name](#input\_organization\_name) | The name of the organization. Should not be changed, unless you know what you are doing. | `string` | n/a | yes |
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The ID of the project. E.g. 'tfe\_project.<resource-name>.id' | `string` | n/a | yes |
| <a name="input_remote_state_consumer_ids"></a> [remote\_state\_consumer\_ids](#input\_remote\_state\_consumer\_ids) | A list of workspace IDs to be used as data sources. Defaults to '[]'. | `list(string)` | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | The tags of the workspace | `set(string)` | n/a | yes |
| <a name="input_terraform_version"></a> [terraform\_version](#input\_terraform\_version) | The version of Terraform to use for this workspace. | `string` | n/a | yes |
| <a name="input_trigger_prefix_paths"></a> [trigger\_prefix\_paths](#input\_trigger\_prefix\_paths) | A list of trigger prefixes. These prefixes determite which paths should trigger a run. Defaults to '[]'. If working\_directory is set, this variable will be merged with the working\_directory variable. | `list(string)` | `[]` | no |
| <a name="input_vcs_git_branch"></a> [vcs\_git\_branch](#input\_vcs\_git\_branch) | The git branch of the workspace. Defaults to 'main'. | `string` | `"main"` | no |
| <a name="input_vcs_git_repository"></a> [vcs\_git\_repository](#input\_vcs\_git\_repository) | The git repository of the workspace. | `string` | n/a | yes |
| <a name="input_working_directory"></a> [working\_directory](#input\_working\_directory) | The working directory of the workspace. Defaults to ''. This should be the path to the directory containing the Terraform configuration files. | `string` | `""` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_workspace_id"></a> [workspace\_id](#output\_workspace\_id) | n/a |
<!-- END_TF_DOCS -->
Binary file removed docs/github_create_repository.png
Binary file not shown.
31 changes: 31 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "tfe_workspace" "this" {
// Search related Workspace settings
name = var.name
description = var.description
tag_names = var.tags
// Organization related settings
organization = var.organization_name
project_id = var.project_id
// Workspace related settings
allow_destroy_plan = var.allow_destroy_plan
auto_apply = var.auto_apply
file_triggers_enabled = var.file_triggers_enabled
trigger_prefixes = distinct(concat(var.trigger_prefix_paths, [var.working_directory]))
working_directory = var.working_directory
vcs_repo {
identifier = var.vcs_git_repository
branch = var.vcs_git_branch
github_app_installation_id = var.organization_github_app_installation_id
}
// not every workspace needs to be a remote state consumer
// of this workspace.
global_remote_state = var.global_accessable_state
// a list of workspace IDs to be used as data sources
remote_state_consumer_ids = var.remote_state_consumer_ids
// Terraform version to use
terraform_version = var.terraform_version

// set agent specific settings
agent_pool_id = var.execution_mode == "agent" ? var.agent_pool_id : null
execution_mode = var.execution_mode
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "workspace_id" {
value = tfe_workspace.this.id
}
104 changes: 104 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
variable "name" {
type = string
description = "The name of the workspace"
}

variable "description" {
type = string
description = "The description of the workspace"
}

variable "tags" {
type = set(string)
description = "The tags of the workspace"
}

variable "organization_name" {
type = string
description = "The name of the organization. Should not be changed, unless you know what you are doing."
}

variable "organization_github_app_installation_id" {
type = string
description = "The ID of the GitHub App Installation. Should not be changed, unless you know what you are doing."
}

variable "project_id" {
type = string
description = "The ID of the project. E.g. 'tfe_project.<resource-name>.id'"
}

variable "allow_destroy_plan" {
type = bool
description = "Whether destroy plans are allowed for this workspace. Defaults to 'false'. Should not be changed, unless you know what you are doing."
default = false
}

variable "auto_apply" {
type = bool
description = "Whether to automatically apply changes when a Terraform plan is successful. Defaults to 'true'. Should not be changed, unless you know what you are doing."
default = true
}

variable "file_triggers_enabled" {
type = bool
description = "Whether to enable file triggers for this workspace. Defaults to 'true'. Should not be changed, unless you know what you are doing."
default = true
}

variable "working_directory" {
type = string
description = "The working directory of the workspace. Defaults to ''. This should be the path to the directory containing the Terraform configuration files."
default = ""
}

variable "trigger_prefix_paths" {
type = list(string)
description = "A list of trigger prefixes. These prefixes determite which paths should trigger a run. Defaults to '[]'. If working_directory is set, this variable will be merged with the working_directory variable."
default = []
}

variable "vcs_git_repository" {
type = string
description = "The git repository of the workspace."
}

variable "vcs_git_branch" {
type = string
description = "The git branch of the workspace. Defaults to 'main'."
default = "main"
}

variable "global_accessable_state" {
type = bool
description = "Whether the state of this workspace should be accessable by all other workspaces. Defaults to 'false'."
default = false
}

variable "remote_state_consumer_ids" {
type = list(string)
description = "A list of workspace IDs to be used as data sources. Defaults to '[]'."
default = []
}

variable "terraform_version" {
type = string
description = "The version of Terraform to use for this workspace."
}

variable "agent_pool_id" {
type = string
description = "The ID of the agent pool to use for this workspace. Defaults to ''. Should not be changed, unless you know what you are doing."
default = ""
}

variable "execution_mode" {
type = string
description = "The execution mode to use for this workspace. Defaults to 'remote'. Should not be changed, unless you know what you are doing."
default = "remote"

validation {
condition = contains(["remote", "agent"], var.execution_mode)
error_message = "execution_mode must be either 'remote' or 'agent'"
}
}
6 changes: 4 additions & 2 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
terraform {
required_providers {
tfe = {
source = "hashicorp/tfe"
version = "0.48.0"
}
}

required_version = "=> v1.5.6"
}

0 comments on commit 490075c

Please sign in to comment.