Skip to content

Commit 6e7aa6c

Browse files
author
Sean Sundberg
authored
Adds enabled flag to completely disable resources (#50)
Signed-off-by: Sean Sundberg <[email protected]>
1 parent a9e2ff4 commit 6e7aa6c

File tree

8 files changed

+77
-36
lines changed

8 files changed

+77
-36
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ SCRIPT_DIR=$(cd $(dirname "$0"); pwd -P)
55
echo "terraform.tfvars"
66
cat terraform.tfvars
77

8+
ENABLED=$(cat .enabled)
9+
10+
if [[ "${ENABLED}" == "false" ]]; then
11+
echo "The VPC is not enabled. Listing terraform state."
12+
13+
terraform state list
14+
exit 0
15+
fi
16+
817
PREFIX_NAME=$(cat terraform.tfvars | grep name_prefix | sed "s/name_prefix=//g" | sed 's/"//g' | sed "s/_/-/g")
918
REGION=$(cat terraform.tfvars | grep -E "^region" | sed "s/region=//g" | sed 's/"//g')
1019
RESOURCE_GROUP_NAME=$(cat terraform.tfvars | grep resource_group_name | sed "s/resource_group_name=//g" | sed 's/"//g')

.github/workflows/verify.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ jobs:
1313
verify:
1414
if: ${{ !contains( github.event.pull_request.labels.*.name, 'skip ci' ) }}
1515
runs-on: ubuntu-latest
16-
container: quay.io/ibmgaragecloud/cli-tools:v0.12.0-lite
16+
container: quay.io/ibmgaragecloud/cli-tools:v0.15
1717

1818
strategy:
1919
matrix:
2020
platform:
2121
- vpc_count
2222
- vpc_count_cidr
23+
enabled:
24+
- true
25+
- false
2326
max-parallel: 1
24-
fail-fast: false
27+
fail-fast: true
2528

2629
env:
2730
HOME: /home/devops
@@ -38,6 +41,7 @@ jobs:
3841
validateDeployScript: .github/scripts/validate-deploy.sh
3942
env:
4043
TF_VAR_ibmcloud_api_key: ${{ secrets.IBMCLOUD_API_KEY }}
44+
TF_VAR_enabled: ${{ matrix.enabled }}
4145
IBMCLOUD_API_KEY: ${{ secrets.IBMCLOUD_API_KEY }}
4246

4347
- name: Verify destroy on ${{ matrix.platform }}
@@ -47,6 +51,7 @@ jobs:
4751
clusterId: ${{ matrix.platform }}
4852
env:
4953
TF_VAR_ibmcloud_api_key: ${{ secrets.IBMCLOUD_API_KEY }}
54+
TF_VAR_enabled: ${{ matrix.enabled }}
5055
IBMCLOUD_API_KEY: ${{ secrets.IBMCLOUD_API_KEY }}
5156

5257
verifyMetadata:

main.tf

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ locals {
55
vpc_zone_names = [ for index in range(max(local.zone_count, var.address_prefix_count)): "${var.region}-${(index % local.zone_count) + 1}" ]
66
prefix_name = var.name_prefix != "" && var.name_prefix != null ? var.name_prefix : var.resource_group_name
77
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
8-
vpc_id = data.ibm_is_vpc.vpc.id
8+
vpc_id = lookup(local.vpc, "id", "")
99
security_group_count = var.provision ? 2 : 0
10-
security_group_ids = var.provision ? [ data.ibm_is_vpc.vpc.default_security_group, data.ibm_is_security_group.base.id ] : []
11-
acl_id = data.ibm_is_vpc.vpc.default_network_acl
12-
crn = data.ibm_is_vpc.vpc.resource_crn
10+
security_group_ids = var.provision && var.enabled ? [ lookup(local.vpc, "default_security_group", ""), data.ibm_is_security_group.base[0].id ] : []
11+
acl_id = lookup(local.vpc, "default_network_acl", "")
12+
crn = lookup(local.vpc, "resource_crn", "")
1313
ipv4_cidr_provided = var.address_prefix_count > 0 && length(var.address_prefixes) >= var.address_prefix_count
1414
ipv4_cidr_block = local.ipv4_cidr_provided ? var.address_prefixes : [ for val in range(var.address_prefix_count): "" ]
1515
provision_cidr = var.provision && local.ipv4_cidr_provided
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"
17+
vpc = try(var.enabled ? data.ibm_is_vpc.vpc[0] : tomap(false), {})
1718
}
1819

1920
resource ibm_is_vpc vpc {
20-
count = var.provision ? 1 : 0
21+
count = var.provision && var.enabled ? 1 : 0
2122

2223
name = local.vpc_name
2324
resource_group = var.resource_group_id
@@ -28,23 +29,26 @@ resource ibm_is_vpc vpc {
2829
}
2930

3031
data ibm_is_vpc vpc {
32+
count = var.enabled ? 1 : 0
3133
depends_on = [ibm_is_vpc.vpc]
3234

3335
name = local.vpc_name
3436
}
3537

3638
resource ibm_is_vpc_address_prefix cidr_prefix {
37-
count = local.provision_cidr ? var.address_prefix_count : 0
39+
count = local.provision_cidr && var.enabled ? var.address_prefix_count : 0
3840

3941
name = "${local.vpc_name}-cidr-${format("%02s", count.index)}"
4042
zone = local.vpc_zone_names[count.index]
41-
vpc = data.ibm_is_vpc.vpc.id
43+
vpc = lookup(local.vpc, "id", "")
4244
cidr = local.ipv4_cidr_block[count.index]
4345
is_default = count.index < local.zone_count
4446
}
4547

4648
resource ibm_is_network_acl_rule allow_internal_egress {
47-
network_acl = data.ibm_is_vpc.vpc.default_network_acl
49+
count = var.enabled ? 1 : 0
50+
51+
network_acl = lookup(local.vpc, "default_network_acl", "")
4852
name = "allow-internal-egress"
4953
action = "allow"
5054
source = var.internal_cidr
@@ -53,17 +57,21 @@ resource ibm_is_network_acl_rule allow_internal_egress {
5357
}
5458

5559
resource ibm_is_network_acl_rule allow_internal_ingress {
56-
network_acl = data.ibm_is_vpc.vpc.default_network_acl
60+
count = var.enabled ? 1 : 0
61+
62+
network_acl = lookup(local.vpc, "default_network_acl", "")
5763
name = "allow-internal-ingress"
5864
action = "allow"
5965
source = var.internal_cidr
6066
destination = var.internal_cidr
6167
direction = "inbound"
62-
before = ibm_is_network_acl_rule.deny_external_ssh.rule_id
68+
before = lookup(ibm_is_network_acl_rule.deny_external_ssh[0], "rule_id", "")
6369
}
6470

6571
resource ibm_is_network_acl_rule deny_external_ssh {
66-
network_acl = data.ibm_is_vpc.vpc.default_network_acl
72+
count = var.enabled ? 1 : 0
73+
74+
network_acl = lookup(local.vpc, "default_network_acl", "")
6775
name = "deny-external-ssh"
6876
action = "deny"
6977
source = "0.0.0.0/0"
@@ -75,11 +83,13 @@ resource ibm_is_network_acl_rule deny_external_ssh {
7583
source_port_max = 22
7684
source_port_min = 22
7785
}
78-
before = ibm_is_network_acl_rule.deny_external_rdp.rule_id
86+
before = lookup(ibm_is_network_acl_rule.deny_external_rdp[0], "rule_id", "")
7987
}
8088

8189
resource ibm_is_network_acl_rule deny_external_rdp {
82-
network_acl = data.ibm_is_vpc.vpc.default_network_acl
90+
count = var.enabled ? 1 : 0
91+
92+
network_acl = lookup(local.vpc, "default_network_acl", "")
8393
name = "deny-external-rdp"
8494
action = "deny"
8595
source = "0.0.0.0/0"
@@ -91,11 +101,13 @@ resource ibm_is_network_acl_rule deny_external_rdp {
91101
source_port_max = 3389
92102
source_port_min = 3389
93103
}
94-
before = ibm_is_network_acl_rule.deny_external_ingress.rule_id
104+
before = lookup(ibm_is_network_acl_rule.deny_external_ingress[0], "rule_id", "")
95105
}
96106

97107
resource ibm_is_network_acl_rule deny_external_ingress {
98-
network_acl = data.ibm_is_vpc.vpc.default_network_acl
108+
count = var.enabled ? 1 : 0
109+
110+
network_acl = lookup(local.vpc, "default_network_acl", "")
99111
name = "deny-external-ingress"
100112
action = "deny"
101113
source = "0.0.0.0/0"
@@ -104,30 +116,25 @@ resource ibm_is_network_acl_rule deny_external_ingress {
104116
}
105117

106118
resource ibm_is_security_group base {
107-
count = var.provision ? 1 : 0
119+
count = var.provision && var.enabled ? 1 : 0
108120

109121
name = local.base_security_group_name
110-
vpc = data.ibm_is_vpc.vpc.id
122+
vpc = lookup(local.vpc, "id", "")
111123
resource_group = var.resource_group_id
112124
}
113125

114126
data ibm_is_security_group base {
127+
count = var.enabled ? 1 : 0
115128
depends_on = [ibm_is_security_group.base]
116129

117130
name = local.base_security_group_name
118131
}
119132

120-
resource null_resource print_sg_name {
121-
depends_on = [data.ibm_is_security_group.base]
122-
123-
provisioner "local-exec" {
124-
command = "echo 'SG name: ${data.ibm_is_security_group.base.name != null ? data.ibm_is_security_group.base.name : "null"}'"
125-
}
126-
}
127-
128133
# from https://cloud.ibm.com/docs/vpc?topic=vpc-service-endpoints-for-vpc
129134
resource ibm_is_security_group_rule default_inbound_ping {
130-
group = data.ibm_is_vpc.vpc.default_security_group
135+
count = var.enabled ? 1 : 0
136+
137+
group = lookup(local.vpc, "default_security_group", "")
131138
direction = "inbound"
132139
remote = "0.0.0.0/0"
133140

@@ -137,7 +144,9 @@ resource ibm_is_security_group_rule default_inbound_ping {
137144
}
138145

139146
resource ibm_is_security_group_rule default_inbound_http {
140-
group = data.ibm_is_vpc.vpc.default_security_group
147+
count = var.enabled ? 1 : 0
148+
149+
group = lookup(local.vpc, "default_security_group", "")
141150
direction = "inbound"
142151
remote = "0.0.0.0/0"
143152

@@ -148,7 +157,7 @@ resource ibm_is_security_group_rule default_inbound_http {
148157
}
149158

150159
resource ibm_is_security_group_rule cse_dns_1 {
151-
count = local.security_group_count
160+
count = var.enabled ? local.security_group_count : 0
152161

153162
group = local.security_group_ids[count.index]
154163
direction = "outbound"
@@ -160,7 +169,7 @@ resource ibm_is_security_group_rule cse_dns_1 {
160169
}
161170

162171
resource ibm_is_security_group_rule cse_dns_2 {
163-
count = local.security_group_count
172+
count = var.enabled ? local.security_group_count : 0
164173

165174
group = local.security_group_ids[count.index]
166175
direction = "outbound"
@@ -172,7 +181,7 @@ resource ibm_is_security_group_rule cse_dns_2 {
172181
}
173182

174183
resource ibm_is_security_group_rule private_dns_1 {
175-
count = local.security_group_count
184+
count = var.enabled ? local.security_group_count : 0
176185

177186
group = local.security_group_ids[count.index]
178187
direction = "outbound"
@@ -184,7 +193,7 @@ resource ibm_is_security_group_rule private_dns_1 {
184193
}
185194

186195
resource ibm_is_security_group_rule private_dns_2 {
187-
count = local.security_group_count
196+
count = var.enabled ? local.security_group_count : 0
188197

189198
group = local.security_group_ids[count.index]
190199
direction = "outbound"

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ output "ids" {
4040
}
4141

4242
output "base_security_group" {
43-
value = data.ibm_is_security_group.base.id
43+
value = var.enabled ? data.ibm_is_security_group.base[0].id : ""
4444
description = "The id of the base security group to be shared by other resources. The base group is different from the default security group."
4545
}
4646

4747
output "addresses" {
48-
value = data.ibm_is_vpc.vpc.cse_source_addresses[*].address
48+
value = [for obj in lookup(local.vpc, "cse_source_addresses[*]", []): obj.address]
4949
description = "The ip address ranges for the VPC"
5050
}

test/stages/stage2-vpc.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ module "dev_vpc" {
77
name_prefix = var.name_prefix
88
address_prefix_count = var.address_prefix_count
99
address_prefixes = jsondecode(var.address_prefixes)
10+
enabled = var.enabled
11+
}
12+
13+
resource null_resource print_enabled {
14+
provisioner "local-exec" {
15+
command = "echo -n '${var.enabled}' > .enabled"
16+
}
1017
}

test/stages/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ variable "address_prefixes" {
5757
variable "address_prefix_count" {
5858
default = 0
5959
}
60+
61+
variable "enabled" {
62+
default = true
63+
type = bool
64+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ variable "internal_cidr" {
5555
description = "The cidr range of the internal network"
5656
default = "10.0.0.0/8"
5757
}
58+
59+
variable "enabled" {
60+
type = bool
61+
description = "Flag to indicate that IBM VPC module should be enabled"
62+
default = true
63+
}

version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 0.13.0"
2+
required_version = ">= 0.15.0"
33

44
required_providers {
55
ibm = {

0 commit comments

Comments
 (0)