Skip to content

Commit 5a7221d

Browse files
author
Sean Sundberg
authored
Adds support for common tags across resources (#71)
- Adds common_tags variable set to global scope in the metadata - Concatenates common_tags and tags values when applied to vpc - Updates resource_group dependency reference to new location closes #70 Signed-off-by: Sean Sundberg <[email protected]>
1 parent a111d29 commit 5a7221d

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ SCRIPT_DIR=$(cd $(dirname "$0"); pwd -P)
44

55
BIN_DIR=$(cat .bin_dir)
66

7+
if [[ -n "${BIN_DIR}" ]]; then
8+
export PATH="${BIN_DIR}:${PATH}"
9+
fi
10+
711
echo "terraform.tfvars"
812
cat terraform.tfvars
913

@@ -18,28 +22,28 @@ echo "IBMCLOUD_API_KEY: ${IBMCLOUD_API_KEY}"
1822

1923
VPC_NAME="${PREFIX_NAME}-vpc"
2024

21-
${BIN_DIR}/ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_NAME}" --apikey "${IBMCLOUD_API_KEY}"
25+
ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_NAME}" --apikey "${IBMCLOUD_API_KEY}"
2226

2327
echo "Retrieving VPC_ID for name: ${VPC_NAME}"
24-
VPC_ID=$(${BIN_DIR}/ibmcloud is vpcs | grep "${VPC_NAME}" | sed -E "s/^([A-Za-z0-9-]+).*/\1/g")
28+
VPC_ID=$(ibmcloud is vpcs | grep "${VPC_NAME}" | sed -E "s/^([A-Za-z0-9-]+).*/\1/g")
2529

2630
if [[ -z "${VPC_ID}" ]]; then
2731
echo "VPC id not found: ${VPC_NAME}"
2832
exit 1
2933
fi
3034

3135
echo "Retrieving VPC info for id: ${VPC_ID}"
32-
${BIN_DIR}/ibmcloud is vpc "${VPC_ID}"
33-
if ! ${BIN_DIR}/ibmcloud is vpc "${VPC_ID}"; then
36+
ibmcloud is vpc "${VPC_ID}" --output JSON
37+
if ! ibmcloud is vpc "${VPC_ID}"; then
3438
echo "Unable to find vpc for id: ${VPC_ID}"
3539
exit 1
3640
fi
3741

3842
SG_NAME="${VPC_NAME}-base"
3943

4044
echo "Testing security group rules"
41-
${BIN_DIR}/ibmcloud is security-groups --output JSON | ${BIN_DIR}/jq '.[]'
42-
OPEN_RULES=$(${BIN_DIR}/ibmcloud is security-groups --output JSON | ${BIN_DIR}/jq -c --arg SG_NAME "${SG_NAME}" '.[] | select(.name == $SG_NAME) | .rules[]')
45+
ibmcloud is security-groups --output JSON | jq '.[]'
46+
OPEN_RULES=$(ibmcloud is security-groups --output JSON | jq -c --arg SG_NAME "${SG_NAME}" '.[] | select(.name == $SG_NAME) | .rules[]')
4347
if [[ -z "${OPEN_RULES}" ]]; then
4448
echo "No rules found for '${SG_NAME}'"
4549
exit 1

.github/workflows/verify-pr.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ jobs:
1616

1717
verifyMetadata:
1818
uses: cloud-native-toolkit/action-workflows/.github/workflows/verify-module-metadata.yaml@v1
19+
20+
verifyAll:
21+
needs: [verify,verifyMetadata]
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- run: echo "Success"

main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ locals {
1616
base_security_group_name = var.base_security_group_name != null && var.base_security_group_name != "" ? var.base_security_group_name : "${local.vpc_name}-base"
1717
vpc = data.ibm_is_vpc.vpc
1818
resource_group_id = data.ibm_resource_group.resource_group.id
19+
tags = distinct(concat(var.common_tags, var.tags))
1920
}
2021

2122
resource null_resource print_names {
@@ -39,7 +40,7 @@ resource ibm_is_vpc vpc {
3940
default_security_group_name = "${local.vpc_name}-default"
4041
default_network_acl_name = "${local.vpc_name}-default"
4142
default_routing_table_name = "${local.vpc_name}-default"
42-
tags = var.tags
43+
tags = local.tags
4344
}
4445

4546
data ibm_is_vpc vpc {
@@ -52,14 +53,14 @@ resource ibm_resource_tag sg-tag {
5253
count = var.provision ? 1 : 0
5354

5455
resource_id = local.vpc.default_security_group_crn
55-
tags = var.tags
56+
tags = local.tags
5657
}
5758

5859
resource ibm_resource_tag nacl-tag {
5960
count = var.provision ? 1 : 0
6061

6162
resource_id = local.vpc.default_network_acl_crn
62-
tags = var.tags
63+
tags = local.tags
6364
}
6465

6566
resource ibm_is_vpc_address_prefix cidr_prefix {
@@ -148,7 +149,7 @@ resource ibm_is_security_group base {
148149
name = local.base_security_group_name
149150
vpc = lookup(local.vpc, "id", "")
150151
resource_group = local.resource_group_id
151-
tags = var.tags
152+
tags = local.tags
152153
}
153154

154155
data ibm_is_security_group base {

module.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ versions:
1515
dependencies:
1616
- id: resource_group
1717
refs:
18-
- source: github.com/cloud-native-toolkit/terraform-ibm-resource-group
18+
- source: github.com/terraform-ibm-modules/terraform-ibm-toolkit-resource-group
1919
version: ">= 2.1.0"
2020
variables:
2121
- name: resource_group_name
@@ -28,3 +28,6 @@ versions:
2828
scope: module
2929
- name: name_prefix
3030
scope: global
31+
- name: common_tags
32+
scope: global
33+
important: true

test/stages/stage2-vpc.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module "dev_vpc" {
66
name_prefix = var.name_prefix
77
address_prefix_count = var.address_prefix_count
88
address_prefixes = jsondecode(var.address_prefixes)
9+
common_tags = ["common", "test"]
910
tags = ["test"]
1011
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ variable "tags" {
5656
default = []
5757
description = "Tags that should be added to the instance"
5858
}
59+
60+
variable "common_tags" {
61+
type = list(string)
62+
default = []
63+
description = "Common tags that should be added to the instance"
64+
}

0 commit comments

Comments
 (0)