Skip to content

Commit 6da8493

Browse files
author
Sean Sundberg
authored
Splits subnet and gateway logic out of vpc module (#14)
Signed-off-by: Sean Sundberg <[email protected]>
1 parent 9c6a6fa commit 6da8493

File tree

12 files changed

+28
-230
lines changed

12 files changed

+28
-230
lines changed

.github/scripts/validate-deploy.sh

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ echo "terraform.tfvars"
66
cat terraform.tfvars
77

88
PREFIX_NAME=$(cat terraform.tfvars | grep name_prefix | sed "s/name_prefix=//g" | sed 's/"//g' | sed "s/_/-/g")
9-
PUBLIC_GATEWAY=$(cat terraform.tfvars | grep vpc_public_gateway | sed "s/vpc_public_gateway=//g" | sed 's/"//g')
109
REGION=$(cat terraform.tfvars | grep -E "^region" | sed "s/region=//g" | sed 's/"//g')
1110
RESOURCE_GROUP_NAME=$(cat terraform.tfvars | grep resource_group_name | sed "s/resource_group_name=//g" | sed 's/"//g')
1211

1312
echo "PREFIX_NAME: ${PREFIX_NAME}"
14-
echo "PUBLIC_GATEWAY: ${PUBLIC_GATEWAY}"
1513
echo "REGION: ${REGION}"
1614
echo "RESOURCE_GROUP_NAME: ${RESOURCE_GROUP_NAME}"
1715
echo "IBMCLOUD_API_KEY: ${IBMCLOUD_API_KEY}"
1816

19-
if [[ -z "${PUBLIC_GATEWAY}" ]]; then
20-
PUBLIC_GATEWAY="false"
21-
fi
22-
2317
VPC_NAME="${PREFIX_NAME}-vpc"
2418

2519
ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_NAME}" --apikey "${IBMCLOUD_API_KEY}"
@@ -33,33 +27,10 @@ if [[ -z "${VPC_ID}" ]]; then
3327
fi
3428

3529
echo "Retrieving VPC info for id: ${VPC_ID}"
30+
ibmcloud is vpc "${VPC_ID}"
3631
if ! ibmcloud is vpc "${VPC_ID}"; then
3732
echo "Unable to find vpc for id: ${VPC_ID}"
3833
exit 1
3934
fi
4035

41-
echo "Retrieving VPC subnets for VPC: ${VPC_NAME}"
42-
ibmcloud is subnets | grep "${VPC_NAME}"
43-
SUBNETS=$(ibmcloud is subnets | grep "${VPC_NAME}")
44-
45-
if [[ -z "${SUBNETS}" ]]; then
46-
echo "Subnets not found: ${VPC_NAME}"
47-
exit 1
48-
fi
49-
50-
echo "Retrieving public gateways for VPC: ${VPC_NAME}"
51-
ibmcloud is pubgws | grep "${VPC_NAME}"
52-
PGS=$(ibmcloud is pubgws | grep "${VPC_NAME}")
53-
54-
if [[ "${PUBLIC_GATEWAY}" == "true" ]] && [[ -z "${PGS}" ]]; then
55-
echo "Public gateways not found: ${VPC_NAME}"
56-
exit 1
57-
elif [[ "${PUBLIC_GATEWAY}" == "false" ]] && [[ -n "${PGS}" ]]; then
58-
echo "Public gateways found: ${VPC_NAME}"
59-
exit 1
60-
fi
61-
62-
cat "./subnet_label_counts.json" | jq '.'
63-
cat "./subnets.json" | jq '.'
64-
6536
exit 0

.github/workflows/verify.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
matrix:
2020
platform:
2121
- vpc_count
22-
- vpc_subnets
2322
fail-fast: false
2423

2524
env:

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
Provisions a VPC instance and related resources. The full list of resources provisioned is as follows:
44

55
- VPC instance
6-
- VPC public gateway (if `public_gateway` is `true`)
76
- VPC network acl
8-
- VPC subnet (number of instances based on `subnet_count`)
97
- VPC security group rules
10-
- *k8s* - tcp ports `30000`-`32767`
118
- *ping* - icmp type 8
129
- *public dns* - `161.26.0.10` and `161.26.0.11`
1310
- *private dns* - `161.26.0.7` and `161.26.0.8`
@@ -18,27 +15,26 @@ The module depends on the following software components:
1815

1916
### Command-line tools
2017

21-
- terraform - v12
18+
- terraform - v13
2219

2320
### Terraform providers
2421

25-
- IBM Cloud provider >= 1.8.1
22+
- IBM Cloud provider >= 1.22.0
2623

2724
## Module dependencies
2825

29-
None
26+
- Resource group - github.com/cloud-native-toolkit/terraform-ibm-resource-group.git
3027

3128
## Example usage
3229

3330
```hcl-terraform
3431
module "dev_vpc" {
35-
source = "github.com/cloud-native-toolkit/terraform-ibm-vpc.git?ref=v1.1.0"
36-
37-
resource_group_name = var.resource_group_name
32+
source = "github.com/cloud-native-toolkit/terraform-ibm-vpc.git"
33+
34+
resource_group_id = module.resource_group.id
35+
resource_group_name = module.resource_group.name
3836
region = var.region
3937
name_prefix = var.name_prefix
4038
ibmcloud_api_key = var.ibmcloud_api_key
41-
subnet_count = var.vpc_subnet_count
42-
public_gateway = var.vpc_public_gateway == "true"
4339
}
4440
```

main.tf

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,21 @@
11

22
locals {
3-
zone_count = 3
4-
subnet_count = length(var.subnets) > 0 ? length(var.subnets) : var.subnet_count
5-
vpc_zone_names = [ for index in range(local.subnet_count): "${var.region}-${(index % local.zone_count) + 1}" ]
63
prefix_name = var.name_prefix != "" ? var.name_prefix : var.resource_group_name
74
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
85
vpc_id = ibm_is_vpc.vpc.id
9-
subnet_ids = ibm_is_subnet.vpc_subnet[*].id
10-
gateway_ids = var.public_gateway ? ibm_is_public_gateway.vpc_gateway[*].id : [ for val in range(local.zone_count): "" ]
116
security_group_id = ibm_is_vpc.vpc.default_security_group
12-
ipv4_cidr_blocks = ibm_is_subnet.vpc_subnet[*].ipv4_cidr_block
13-
distinct_subnet_labels = distinct([ for val in var.subnets: val.label ])
14-
# creates an intermediate object where the key is the label and the value is an array of labels, one for each appearance
15-
# e.g. [{label = "basic"}, {label = "basic"}, {label = "test"}] would yield {basic = ["basic", "basic"], test = ["test"]}
16-
subnet_labels_tmp = { for subnet in var.subnets: subnet.label => subnet.label... }
17-
# creates an object where the key is the label and the value is number of times the label appears in the original list
18-
# e.g. {basic = ["basic", "basic"], test = ["test"]} would yield {basic = 2, test = 1}
19-
subnet_label_counts = length(var.subnets) > 0 ? [ for val in local.distinct_subnet_labels:
20-
{
21-
label = val
22-
count = length(local.subnet_labels_tmp[val])
23-
} ] : [ {
24-
label = "default"
25-
count = local.subnet_count
26-
} ]
27-
}
28-
29-
resource null_resource print_names {
30-
provisioner "local-exec" {
31-
command = "echo 'Resource group: ${var.resource_group_name}'"
32-
}
33-
provisioner "local-exec" {
34-
command = "echo 'Subnets: ${jsonencode(local.subnet_labels_tmp)}'"
35-
}
36-
}
37-
38-
data ibm_resource_group resource_group {
39-
depends_on = [null_resource.print_names]
40-
41-
name = var.resource_group_name
427
}
438

449
resource ibm_is_vpc vpc {
4510
name = local.vpc_name
46-
resource_group = data.ibm_resource_group.resource_group.id
11+
resource_group = var.resource_group_id
4712
default_security_group_name = "${local.vpc_name}-security-group"
4813
}
4914

50-
resource ibm_is_public_gateway vpc_gateway {
51-
count = var.public_gateway ? min(local.zone_count, local.subnet_count) : 0
52-
53-
name = "${local.vpc_name}-gateway-${format("%02s", count.index)}"
54-
vpc = local.vpc_id
55-
zone = local.vpc_zone_names[count.index]
56-
resource_group = data.ibm_resource_group.resource_group.id
57-
58-
//User can configure timeouts
59-
timeouts {
60-
create = "90m"
61-
}
62-
}
63-
6415
resource ibm_is_network_acl network_acl {
6516
name = "${local.vpc_name}-acl"
6617
vpc = ibm_is_vpc.vpc.id
67-
resource_group = data.ibm_resource_group.resource_group.id
18+
resource_group = var.resource_group_id
6819

6920
rules {
7021
name = "egress"
@@ -82,37 +33,6 @@ resource ibm_is_network_acl network_acl {
8233
}
8334
}
8435

85-
resource ibm_is_subnet vpc_subnet {
86-
count = local.subnet_count
87-
88-
name = "${local.vpc_name}-subnet-${format("%02s", count.index)}"
89-
zone = local.vpc_zone_names[count.index]
90-
vpc = local.vpc_id
91-
public_gateway = local.gateway_ids[count.index % local.zone_count]
92-
total_ipv4_address_count = 256
93-
resource_group = data.ibm_resource_group.resource_group.id
94-
network_acl = ibm_is_network_acl.network_acl.id
95-
}
96-
97-
data ibm_is_subnet vpc_subnet {
98-
count = local.subnet_count
99-
100-
identifier = ibm_is_subnet.vpc_subnet[count.index].id
101-
}
102-
103-
resource ibm_is_security_group_rule rule_tcp_k8s {
104-
count = local.subnet_count
105-
106-
group = local.security_group_id
107-
direction = "inbound"
108-
remote = local.ipv4_cidr_blocks[count.index]
109-
110-
tcp {
111-
port_min = 30000
112-
port_max = 32767
113-
}
114-
}
115-
11636
resource ibm_is_security_group_rule rule_icmp_ping {
11737
group = local.security_group_id
11838
direction = "inbound"

module.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ tags:
77
versions:
88
- platforms: []
99
dependencies:
10-
- id: resource_group
10+
- id: resource-group
1111
refs:
1212
- source: github.com/cloud-native-toolkit/terraform-ibm-resource-group
1313
version: ">= 2.1.0"
1414
variables:
15+
- name: resource_group_id
16+
moduleRef:
17+
id: resource-group
18+
output: id
1519
- name: resource_group_name
1620
moduleRef:
17-
id: resource_group
21+
id: resource-group
1822
output: name
1923
- name: region
2024
scope: global
@@ -24,9 +28,3 @@ versions:
2428
scope: global
2529
- name: ibmcloud_api_key
2630
scope: global
27-
- name: subnet_count
28-
scope: ignore
29-
- name: subnets
30-
scope: module
31-
- name: public_gateway
32-
scope: module

outputs.tf

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
11

22
output "name" {
33
value = local.vpc_name
4-
depends_on = [ibm_is_subnet.vpc_subnet]
4+
depends_on = [ibm_is_vpc.vpc]
55
description = "The name of the vpc instance"
66
}
77

88
output "id" {
99
value = local.vpc_id
10-
depends_on = [ibm_is_subnet.vpc_subnet]
10+
depends_on = [ibm_is_vpc.vpc]
1111
description = "The id of the vpc instance"
1212
}
1313

14-
output "subnet_count" {
15-
value = local.subnet_count
16-
description = "The total number of subnets for the vpc"
17-
}
18-
19-
output "subnet_label_counts" {
20-
value = local.subnet_label_counts
21-
description = "The number of subnets for each label. e.g. [{label = 'default', count = 2}, {label = 'test', count = 1}]"
22-
}
23-
24-
output "zone_names" {
25-
value = local.vpc_zone_names
26-
depends_on = [ibm_is_subnet.vpc_subnet]
27-
description = "The list of zone names that into which subnets were created"
28-
}
29-
30-
output "subnet_ids" {
31-
value = local.subnet_ids
32-
depends_on = [ibm_is_subnet.vpc_subnet]
33-
description = "The list of subnet ids"
34-
}
35-
36-
output "subnets" {
37-
value = [
38-
for subnet in data.ibm_is_subnet.vpc_subnet:
39-
{
40-
id = subnet.id
41-
zone = subnet.zone
42-
label = length(var.subnets) > 0 ? element(var.subnets, index(data.ibm_is_subnet.vpc_subnet[*].id, subnet.id)).label : "default"
43-
}
44-
]
45-
depends_on = [ibm_is_subnet.vpc_subnet]
46-
description = "List of subnet objects that contain the subnet id and label, e.g. [{label='', id=''}]"
14+
output "acl_id" {
15+
value = ibm_is_network_acl.network_acl.id
16+
description = "The id of the network acl"
4717
}

test/stages/print-module/main.tf

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/stages/print-module/variables.tf

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/stages/stage1-resource-group.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ module "resource_group" {
22
source = "github.com/cloud-native-toolkit/terraform-ibm-resource-group.git"
33

44
resource_group_name = var.resource_group_name
5-
provision = true
5+
provision = false
66
}

test/stages/stage2-vpc.tf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
module "dev_vpc" {
22
source = "./module"
33

4+
resource_group_id = module.resource_group.id
45
resource_group_name = module.resource_group.name
56
region = var.region
67
name_prefix = var.name_prefix
78
ibmcloud_api_key = var.ibmcloud_api_key
8-
subnet_count = var.vpc_subnet_count
9-
subnets = jsondecode(var.vpc_subnets)
10-
public_gateway = var.vpc_public_gateway == "true"
119
}

0 commit comments

Comments
 (0)