Skip to content

Commit c966988

Browse files
author
Sean Sundberg
authored
Adds logic to create address prefixes for the VPC if provided (#27)
- Adds address_prefix_count and address_prefixes variables - Generates address prefixes and sets the first three to default values if prefix_count and prefixes have values - Handles null values in template and updates test - Adds auth_id variable to coordinate creation of authorization and flow log - Consolodates tests and enhances for address_prefixes and auth_id - Removes source resource group from flow-log auth in test to get it to pass (evidently the auth for flow-log cannot be resource group to resource group) Signed-off-by: Sean Sundberg <[email protected]>
1 parent c4ff1a5 commit c966988

File tree

8 files changed

+108
-43
lines changed

8 files changed

+108
-43
lines changed

.github/workflows/verify.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
matrix:
2020
platform:
2121
- vpc_count
22+
- vpc_count_cidr
23+
max-parallel: 1
2224
fail-fast: false
2325

2426
env:

main.tf

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,75 @@
11

22
locals {
3+
tmp_dir = "${path.cwd}/.tmp"
4+
zone_count = 3
5+
vpc_zone_names = [ for index in range(local.zone_count): "${var.region}-${(index % local.zone_count) + 1}" ]
36
prefix_name = var.name_prefix != "" ? var.name_prefix : var.resource_group_name
47
vpc_name = lower(replace(var.name != "" ? var.name : "${local.prefix_name}-vpc", "_", "-"))
58
vpc_id = data.ibm_is_vpc.vpc.id
69
security_group_id = data.ibm_is_vpc.vpc.default_security_group
710
acl_id = data.ibm_is_vpc.vpc.default_network_acl
811
crn = data.ibm_is_vpc.vpc.resource_crn
12+
ipv4_cidr_provided = length(var.address_prefixes) >= var.address_prefix_count
13+
ipv4_cidr_block = local.ipv4_cidr_provided ? var.address_prefixes : [ for val in range(var.address_prefix_count): "" ]
14+
address_prefix_management = local.ipv4_cidr_provided ? "manual" : "auto"
15+
provision_cidr = var.provision && local.ipv4_cidr_provided
16+
}
17+
18+
resource null_resource print_values {
19+
provisioner "local-exec" {
20+
command = "echo 'Bucket name: ${var.flow_log_cos_bucket_name != null ? var.flow_log_cos_bucket_name : ""}'"
21+
}
22+
provisioner "local-exec" {
23+
command = "echo 'Auth policy id: ${var.auth_id}'"
24+
}
25+
}
26+
27+
resource ibm_is_vpc_address_prefix cidr_prefix {
28+
count = local.provision_cidr ? var.address_prefix_count : 0
29+
30+
name = "${local.vpc_name}-cidr-${format("%02s", count.index)}"
31+
zone = local.vpc_zone_names[count.index]
32+
vpc = data.ibm_is_vpc.vpc.id
33+
cidr = local.ipv4_cidr_block[count.index]
934
}
1035

1136
resource ibm_is_vpc vpc {
1237
count = var.provision ? 1 : 0
1338

1439
name = local.vpc_name
1540
resource_group = var.resource_group_id
41+
address_prefix_management = local.address_prefix_management
1642
default_security_group_name = "${local.vpc_name}-security-group"
1743
default_network_acl_name = "${local.vpc_name}-acl"
1844
default_routing_table_name = "${local.vpc_name}-routing"
1945
}
2046

47+
# Set the address prefixes as the default. This will allow us to specify the number of ips required
48+
# in each subnet, instead of figuring out specific cidrs.
49+
# Note the "split" function call - this is because the id returned from creating the address
50+
# comes back as <vpc_id>/<address_range_id> and the update call wants these passed as separate
51+
# arguments. I suspect this is actually a defect in what is returned from ibm_is_vpc_address_prefix
52+
# and it may one day be fixed and trip up this code.
53+
resource null_resource post_vpc_address_pfx_default {
54+
count = local.provision_cidr ? var.address_prefix_count : 0
55+
depends_on = [ibm_is_vpc_address_prefix.cidr_prefix]
56+
57+
provisioner "local-exec" {
58+
command = <<COMMAND
59+
ibmcloud login --apikey ${var.ibmcloud_api_key} -r ${var.region} -g ${var.resource_group_name} --quiet ; \
60+
ibmcloud is vpc-address-prefix-update '${local.provision_cidr ? ibm_is_vpc.vpc[0].id : ""}' '${split("/", local.provision_cidr ? ibm_is_vpc_address_prefix.cidr_prefix[0].id : "/")[1]}' --default true ; \
61+
ibmcloud is vpc-address-prefix-update '${local.provision_cidr ? ibm_is_vpc.vpc[0].id : ""}' '${split("/", local.provision_cidr ? ibm_is_vpc_address_prefix.cidr_prefix[1].id : "/")[1]}' --default true ; \
62+
ibmcloud is vpc-address-prefix-update '${local.provision_cidr ? ibm_is_vpc.vpc[0].id : ""}' '${split("/", local.provision_cidr ? ibm_is_vpc_address_prefix.cidr_prefix[2].id : "/")[1]}' --default true ; \
63+
COMMAND
64+
}
65+
}
66+
2167
data ibm_is_vpc vpc {
2268
depends_on = [ibm_is_vpc.vpc]
2369

2470
name = local.vpc_name
2571
}
2672

27-
resource ibm_is_network_acl network_acl {
28-
count = var.provision ? 1 : 0
29-
30-
name = "${local.vpc_name}-acl2"
31-
resource_group = var.resource_group_id
32-
vpc = data.ibm_is_vpc.vpc.id
33-
34-
rules {
35-
name = "egress"
36-
action = "allow"
37-
source = "0.0.0.0/0"
38-
destination = "0.0.0.0/0"
39-
direction = "outbound"
40-
}
41-
rules {
42-
name = "ingress"
43-
action = "allow"
44-
source = "0.0.0.0/0"
45-
destination = "0.0.0.0/0"
46-
direction = "inbound"
47-
}
48-
}
49-
5073
resource ibm_is_security_group_rule rule_icmp_ping {
5174
count = var.provision ? 1 : 0
5275

@@ -109,7 +132,8 @@ resource ibm_is_security_group_rule private_dns_2 {
109132

110133
resource ibm_is_flow_log flowlog_instance {
111134
count = length(var.flow_log_cos_bucket_name) > 0 ? 1 : 0
112-
depends_on = [ibm_is_vpc.vpc]
135+
depends_on = [ibm_is_vpc.vpc, null_resource.print_values]
136+
113137
name = "${local.vpc_name}-flowlog"
114138
active = true
115139
//target can be VPC or Virtual Server Instance or Subnet or Primary Network Interface or Secondary Network Interface

module.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ versions:
1616
- source: github.com/cloud-native-toolkit/terraform-ibm-object-storage-bucket
1717
version: ">= 0.0.1"
1818
optional: true
19+
- id: auth
20+
refs: []
21+
optional: true
1922
variables:
2023
- name: resource_group_id
2124
moduleRef:
@@ -38,3 +41,8 @@ versions:
3841
id: cos_bucket
3942
output: bucket_name
4043
optional: true
44+
- name: auth_id
45+
moduleRef:
46+
id: auth
47+
output: id
48+
optional: true
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
module "cos" {
22
source = "github.com/ibm-garage-cloud/terraform-ibm-object-storage.git"
33

4-
resource_group_name = var.resource_group_name
4+
resource_group_name = module.resource_group.name
55
name_prefix = var.name_prefix
6-
name = "flow-log-cos-instance"
76
}
87

98
resource null_resource print_cos_id {
109
depends_on = [module.cos.id]
1110
provisioner "local-exec" {
12-
command = "echo 'Provisioning bucket into COS instance: ${module.cos.id}'"
11+
command = "echo 'Provisioning bucket into COS instance: ${module.cos.id != null ? module.cos.id : ""}'"
12+
}
13+
}
14+
15+
resource null_resource pre_print_bucket {
16+
provisioner "local-exec" {
17+
command = "echo 'Name prefix: ${var.name_prefix}'"
1318
}
1419
}
1520

@@ -20,26 +25,12 @@ module "dev_cos_bucket" {
2025
cos_instance_id = module.cos.id
2126
name_prefix = var.name_prefix
2227
ibmcloud_api_key = var.ibmcloud_api_key
23-
name = "fl-testing-gsi2"
2428
region = var.region
29+
label = "flow-log"
2530
}
2631

2732
resource null_resource print_bucket {
2833
provisioner "local-exec" {
29-
command = "echo 'Bucket created: ${module.dev_cos_bucket.bucket_name}'"
34+
command = "echo 'Bucket created: ${module.dev_cos_bucket.bucket_name != null ? module.dev_cos_bucket.bucket_name : ""}'"
3035
}
3136
}
32-
33-
34-
module "dev_vpc_with_flowlog" {
35-
source = "./module"
36-
37-
38-
resource_group_id = module.resource_group.id
39-
resource_group_name = module.resource_group.name
40-
region = var.region
41-
name_prefix = var.name_prefix
42-
name = "vpc-with-fl-${module.cos.name}-${length(null_resource.print_bucket)}"
43-
ibmcloud_api_key = var.ibmcloud_api_key
44-
flow_log_cos_bucket_name = module.dev_cos_bucket.bucket_name
45-
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module "flow-log-auth" {
2+
source = "github.com/cloud-native-toolkit/terraform-ibm-iam-service-authorization"
3+
4+
source_service_name = "is"
5+
source_resource_type = "flow-log-collector"
6+
provision = true
7+
target_service_name = "cloud-object-storage"
8+
target_resource_group_id = module.resource_group.id
9+
roles = ["Writer"]
10+
}

test/stages/stage2-vpc.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ module "dev_vpc" {
66
region = var.region
77
name_prefix = var.name_prefix
88
ibmcloud_api_key = var.ibmcloud_api_key
9+
address_prefix_count = var.address_prefix_count
10+
address_prefixes = tolist(setsubtract(split(",", var.address_prefixes), [""]))
11+
auth_id = module.flow-log-auth.id
12+
flow_log_cos_bucket_name = module.dev_cos_bucket.bucket_name
913
}

test/stages/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ variable "vpc_subnets" {
4949
description = "JSON representation of list of object, e.g. [{\"label\"=\"default\"}]"
5050
default = "[]"
5151
}
52+
53+
variable "address_prefixes" {
54+
default = ""
55+
}
56+
57+
variable "address_prefix_count" {
58+
default = 0
59+
}

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,21 @@ variable "flow_log_cos_bucket_name" {
4141
description = "Cloud Object Storage bucket id for flow logs (optional)"
4242
default = ""
4343
}
44+
45+
variable "address_prefix_count" {
46+
type = number
47+
description = "The number of ipv4_cidr_blocks"
48+
default = 0
49+
}
50+
51+
variable "address_prefixes" {
52+
type = list(string)
53+
description = "List of ipv4 cidr blocks for the address prefixes (e.g. ['10.10.10.0/24']). If you are providing cidr blocks then a value must be provided for each of the subnets. If you don't provide cidr blocks for each of the subnets then values will be generated using the {ipv4_address_count} value."
54+
default = []
55+
}
56+
57+
variable "auth_id" {
58+
type = string
59+
description = "The id of the authorization policy that allows the Flow Log to access the Object Storage bucket. This is optional and provided to sequence the authorization before the flow log creation."
60+
default = ""
61+
}

0 commit comments

Comments
 (0)