Skip to content

Cleanup and refactor #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2024
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
4 changes: 2 additions & 2 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ changelog:
labels:
- dependencies

- title: 🔩 Dependencies
- title: 🔩 Dependencies
labels:
- dependencies

# This file is managed by the osinfra-io/github-organization-management repository and should not be edited directly.
# This file is managed by the osinfra-io/github-organization-management repository and should not be edited directly.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# <img align="left" width="45" height="45" src="https://github.com/user-attachments/assets/5297c02c-c310-4e26-9d8f-3dc71995575a"> Terraform Core - Workspace Terraform Module
# <img align="left" width="45" height="45" src="https://github.com/user-attachments/assets/5297c02c-c310-4e26-9d8f-3dc71995575a"> Terraform Core - Helpers Terraform Module

**[GitHub Actions](https://github.com/osinfra-io/terraform-core-workspace/actions):**
**[GitHub Actions](https://github.com/osinfra-io/terraform-core-helpers/actions):**

[![Terraform Tests](https://github.com/osinfra-io/terraform-core-workspace/actions/workflows/test.yml/badge.svg)](https://github.com/osinfra-io/terraform-core-workspace/actions/workflows/test.yml) [![Dependabot](https://github.com/osinfra-io/terraform-core-workspace/actions/workflows/dependabot.yml/badge.svg)](https://github.com/osinfra-io/terraform-core-workspace/actions/workflows/dependabot.yml)
[![Terraform Tests](https://github.com/osinfra-io/terraform-core-helpers/actions/workflows/test.yml/badge.svg)](https://github.com/osinfra-io/terraform-core-helpers/actions/workflows/test.yml) [![Dependabot](https://github.com/osinfra-io/terraform-core-helpers/actions/workflows/dependabot.yml/badge.svg)](https://github.com/osinfra-io/terraform-core-helpers/actions/workflows/dependabot.yml)

## Repository Description

Terraform **example** module for extracting workspace information and generating labels for resources.
Terraform **example** module for helpers, supports extracting workspace information and generating labels for resources.

> [!NOTE]
> We do not recommend consuming this module like you might a [public module](https://registry.terraform.io/browse/modules). It is a baseline, something you can fork, potentially maintain, and modify to fit your organization's needs. Using public modules vs. writing your own has various [drivers and trade-offs](https://docs.osinfra.io/fundamentals/architecture-decision-records/adr-0003) that your organization should evaluate.
Expand Down Expand Up @@ -42,10 +42,6 @@ Links to documentation and other resources required to develop and iterate in th

All tests are [mocked](https://developer.hashicorp.com/terraform/language/tests/mocking) allowing us to test the module without creating infrastructure or requiring credentials. The trade-offs are acceptable in favor of speed and simplicity. In a Terraform test, a mocked provider or resource will generate fake data for all computed attributes that would normally be provided by the underlying provider APIs.

```none
cd fixtures/default
```

```none
terraform init
```
Expand Down Expand Up @@ -90,8 +86,9 @@ No resources.

| Name | Description |
|------|-------------|
| <a name="output_environment"></a> [environment](#output\_environment) | n/a |
| <a name="output_labels"></a> [labels](#output\_labels) | n/a |
| <a name="output_region"></a> [region](#output\_region) | n/a |
| <a name="output_zone"></a> [zone](#output\_zone) | n/a |
| <a name="output_env"></a> [env](#output\_env) | The short name for the environment for example prod, nonprod, sb |
| <a name="output_environment"></a> [environment](#output\_environment) | The environment name for example production, non-production, sandbox |
| <a name="output_labels"></a> [labels](#output\_labels) | A map of labels to apply to resources |
| <a name="output_region"></a> [region](#output\_region) | The region where resources will be deployed |
| <a name="output_zone"></a> [zone](#output\_zone) | The zone where resources will be deployed |
<!-- END_TF_DOCS -->
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ the community, but it does come with some risks.
## Reporting a Vulnerability

Privately discuss, fix, and publish information about security vulnerabilities in this repository by drafting a new
[security advisory](https://github.com/osinfra-io/terraform-core-workspace/security/advisories/new).
[security advisory](https://github.com/osinfra-io/terraform-core-helpers/security/advisories/new).

<!-- This file is managed by the osinfra-io/github-organization-management repository and should not be edited directly. -->
34 changes: 18 additions & 16 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
# https://www.terraform.io/docs/language/values/locals.html

locals {
# The regex is used to parse the workspace name into its components, the components are used to set the region, zone, and environment variables.
# This requires structured workspace names, the structured workspace names are defined by the regex.

# Examples:
# main-sandbox -> region = null, zone = null, environment = sandbox
# us-west1-sandbox -> region = us-west1, zone = null, environment = sandbox
# us-west1-foo-sandbox -> region = us-west1, zone = null, environment = sandbox
# us-west1-a-sandbox -> region = us-west1, zone = a, environment = sandbox
# us-west1-a-foo-sandbox -> region = us-west1, zone = a, environment = sandbox

environment_regex = "-(non-production|sandbox|production)$"
region_regex = "^(us-[a-z]+\\d+)"
zone_regex = "^us-[a-z]+\\d+-([abcd])"

env = local.environment != null ? lookup(local.env_map, local.environment, "none") : null

env_map = {
Expand All @@ -13,7 +27,9 @@ locals {
environment = local.parsed_workspace.environment

labels = {

# Datadog expects the label env for unified service tagging

env = local.environment
cost-center = var.cost_center
data-classification = var.data_classification
Expand Down Expand Up @@ -43,21 +59,7 @@ locals {

region = local.parsed_workspace.region

workspace = var.workspace != null ? var.workspace : terraform.workspace

# The regex is used to parse the workspace name into its components, the components are used to set the region, zone, and environment variables.
# This requires structured workspace names, the structured workspace names are defined by the regex.

# Examples:
# main-sandbox -> region = null, zone = null, environment = sandbox
# us-west1-sandbox -> region = us-west1, zone = null, environment = sandbox
# us-west1-foo-sandbox -> region = us-west1, zone = null, environment = sandbox
# us-west1-a-sandbox -> region = us-west1, zone = a, environment = sandbox
# us-west1-a-foo-sandbox -> region = us-west1, zone = a, environment = sandbox

environment_regex = "-(non-production|sandbox|production)$"
region_regex = "^(us-[a-z]+\\d+)"
zone_regex = "^us-[a-z]+\\d+-([abcd])"

zone = local.parsed_workspace.zone
workspace = var.workspace != null ? var.workspace : terraform.workspace
zone = local.parsed_workspace.zone
}
17 changes: 13 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# Terraform Output Values
# https://www.terraform.io/language/values/outputs

output "env" {
description = "The short name for the environment for example prod, nonprod, sb"
value = local.env
}

output "environment" {
value = local.environment
description = "The environment name for example production, non-production, sandbox"
value = local.environment
}

output "labels" {
value = local.labels
description = "A map of labels to apply to resources"
value = local.labels
}

output "region" {
value = local.region
description = "The region where resources will be deployed"
value = local.region
}

output "zone" {
value = local.zone
description = "The zone where resources will be deployed"
value = local.zone
}