|
1 | 1 | ############################################################################## |
2 | | -# VPE Locals |
| 2 | +# Resource Group |
3 | 3 | ############################################################################## |
4 | | - |
5 | | -locals { |
6 | | - # List of Gateways to create |
7 | | - gateway_list = var.vpc_id == null ? [] : concat([ |
8 | | - # Create object for each service |
9 | | - for service in var.cloud_services : |
10 | | - { |
11 | | - name = "${var.vpc_name}-${service}" |
12 | | - service = service |
13 | | - crn = null |
14 | | - } |
15 | | - ], |
16 | | - [ |
17 | | - for service in var.cloud_service_by_crn : |
18 | | - { |
19 | | - name = "${var.vpc_name}-${service.name}" |
20 | | - service = null |
21 | | - crn = service.crn |
22 | | - } |
23 | | - ] |
24 | | - ) |
25 | | - |
26 | | - # List of IPs to create |
27 | | - endpoint_ip_list = var.vpc_id == null ? [] : flatten([ |
28 | | - # Create object for each subnet |
29 | | - for subnet in var.subnet_zone_list : |
30 | | - [ |
31 | | - for service in var.cloud_services : |
32 | | - { |
33 | | - ip_name = "${subnet.name}-${service}-gateway-${replace(subnet.zone, "/${var.region}-/", "")}-ip" |
34 | | - subnet_id = subnet.id |
35 | | - gateway_name = "${var.vpc_name}-${service}" |
36 | | - } |
37 | | - ] |
38 | | - ]) |
39 | | - |
40 | | - # Map of Services to endpoints |
41 | | - service_to_endpoint_map = { |
42 | | - kms = "crn:v1:bluemix:public:kms:${var.region}:::endpoint:${var.service_endpoints}.${var.region}.kms.cloud.ibm.com" |
43 | | - hs-crypt = "crn:v1:bluemix:public:hs-crypto:${var.region}:::endpoint:api.${var.service_endpoints}.${var.region}.hs-crypto.cloud.ibm.com" |
44 | | - cloud-object-storage = "crn:v1:bluemix:public:cloud-object-storage:global:::endpoint:s3.direct.${var.region}.cloud-object-storage.appdomain.cloud" |
45 | | - container-registry = "crn:v1:bluemix:public:container-registry:${var.region}:::endpoint:vpe.${var.region}.container-registry.cloud.ibm.com" |
46 | | - } |
| 4 | +module "resource_group" { |
| 5 | + source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5" |
| 6 | + # if an existing resource group is not set (null) create a new one using prefix |
| 7 | + resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null |
| 8 | + existing_resource_group_name = var.resource_group |
47 | 9 | } |
48 | 10 |
|
49 | 11 | ############################################################################## |
50 | 12 |
|
51 | 13 | ############################################################################## |
52 | | -# Create Reserved IPs |
| 14 | +# Create VPC |
53 | 15 | ############################################################################## |
54 | 16 |
|
55 | | -resource "ibm_is_subnet_reserved_ip" "ip" { |
56 | | - for_each = { |
57 | | - # Create a map based on endpoint IP name |
58 | | - for gateway_ip in local.endpoint_ip_list : |
59 | | - (gateway_ip.ip_name) => gateway_ip |
60 | | - } |
61 | | - subnet = each.value.subnet_id |
| 17 | +locals { |
| 18 | + # input variable validation |
| 19 | + # tflint-ignore: terraform_unused_declarations |
| 20 | + validate_vpc_inputs = var.vpc_id == null && !var.create_vpc ? tobool("var.create_vpc should be set to true if var.vpc_id is set to null") : true |
| 21 | + # tflint-ignore: terraform_unused_declarations |
| 22 | + validate_vpc_id_and_create_vpc_both_not_set_inputs = var.vpc_id != null && var.create_vpc ? tobool("var.vpc_id cannot be set whilst var.create_vpc is set to true") : true |
| 23 | + vpc_instance_id = var.vpc_id == null ? tolist(ibm_is_vpc.vpc[*].id)[0] : var.vpc_id |
62 | 24 | } |
63 | 25 |
|
64 | 26 | ############################################################################## |
65 | | - |
66 | | -############################################################################## |
67 | | -# Create Endpoint Gateways |
| 27 | +# Create a VPC for this example |
68 | 28 | ############################################################################## |
69 | 29 |
|
70 | | -resource "ibm_is_virtual_endpoint_gateway" "vpe" { |
71 | | - for_each = { |
72 | | - # Create map based on gateway name if enabled |
73 | | - for gateway in local.gateway_list : |
74 | | - (gateway.name) => gateway |
75 | | - } |
76 | | - |
77 | | - name = "${var.prefix}-${each.key}-endpoint-gateway" |
78 | | - vpc = var.vpc_id |
79 | | - resource_group = var.resource_group_id |
80 | | - security_groups = var.security_group_ids |
81 | | - target { |
82 | | - crn = each.value.service == null ? each.value.crn : local.service_to_endpoint_map[each.value.service] |
83 | | - resource_type = "provider_cloud_service" |
84 | | - } |
| 30 | +resource "ibm_is_vpc" "vpc" { |
| 31 | + count = var.create_vpc ? 1 : 0 |
| 32 | + name = "${var.prefix}-${var.vpc_name}" |
| 33 | + resource_group = module.resource_group.resource_group_id |
85 | 34 | } |
86 | 35 |
|
87 | 36 | ############################################################################## |
88 | | - |
89 | | -############################################################################## |
90 | | -# Attach Endpoint Gateways to Reserved IPs |
91 | | -############################################################################## |
92 | | - |
93 | | -resource "ibm_is_virtual_endpoint_gateway_ip" "endpoint_gateway_ip" { |
94 | | - for_each = { |
95 | | - # Create a map based on endpoint IP |
96 | | - for gateway_ip in local.endpoint_ip_list : |
97 | | - (gateway_ip.ip_name) => gateway_ip |
98 | | - } |
99 | | - gateway = ibm_is_virtual_endpoint_gateway.vpe[each.value.gateway_name].id |
100 | | - reserved_ip = ibm_is_subnet_reserved_ip.ip[each.key].reserved_ip |
| 37 | +# Create VPEs in the VPC |
| 38 | +############################################################################## |
| 39 | +module "vpes" { |
| 40 | + source = "../../" |
| 41 | + region = var.region |
| 42 | + prefix = var.prefix |
| 43 | + vpc_name = var.vpc_name |
| 44 | + vpc_id = local.vpc_instance_id |
| 45 | + subnet_zone_list = var.subnet_zone_list |
| 46 | + resource_group_id = module.resource_group.resource_group_id |
| 47 | + security_group_ids = var.security_group_ids |
| 48 | + cloud_services = var.cloud_services |
| 49 | + cloud_service_by_crn = var.cloud_service_by_crn |
| 50 | + service_endpoints = var.service_endpoints |
101 | 51 | } |
102 | 52 |
|
103 | 53 | ############################################################################## |
0 commit comments