Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Public gateway refactor #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/instance/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#####################################################
##############################################################################
# VPC Instance
# Copyright 2020 IBM
#####################################################
##############################################################################

/***************************************************
NOTE: To source a particular version of IBM terraform provider, configure the parameter `version` as follows
Expand Down
29 changes: 29 additions & 0 deletions examples/public_gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Public Gateway Module Example

This example illustrates how to use the `public_gateway` module.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Inputs

Name | Type | Description | Default
-------------------- | ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------
vpc | string | VPC name |
resource_group | string | Resource group name | null
zones | list(string) | A list of zones to create public gateways. These zones must be in the VPC region | ["us-south-1"]
prefix | string | Prefix to add to the beginning of created Public Gateways | pgw
floating_ip | object({ zone-1 = string zone-2 = string zone-3 = string }) | Optional. Floating IP `id`'s or `address`'es that you want to assign to the public gateway. Leave as empty string to disable. | { zone-1 = "" zone-2 = "" zone-3 = "" }
tags | list(string) | Optional. List of Tags for the Public Gateway | []
public_gateway_names | list(string) | Optional. A list of public gateways to get by name | []
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->


NOTE: We can configure the list of tags, floating_ip to be attached to a the Public Gateway by entering respective details in input.tfvars.

## Usage

terraform apply -var-file="input.tfvars"

## Note

For all optional fields, default values (Eg: `null`) are given in varaible.tf file. User can configure the same by overwriting with appropriate values.
15 changes: 15 additions & 0 deletions examples/public_gateway/input.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
vpc="<name of vpc where the gateways will be provisioned>"
resource_group="<name of resource group>"
zones=[ "<zone within vpc region>"]
prefix="<prefix to add to beginning of names of gateways created>"
floating_ip={
zone-1="<public_gateway_id_or_ip_address>"
zone-2=""
zone-3=""
}
tags=[
"<tag1>", "<tag2>"
]
public_gateway_names=[
"<get existing public gateway data by name>"
]
30 changes: 30 additions & 0 deletions examples/public_gateway/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
##############################################################################
# Public Gateway Configuration Example
# Copyright 2020 IBM
##############################################################################

provider ibm {
}

data ibm_is_vpc vpc {
name = var.vpc
}

data ibm_resource_group resource_group {
name = var.resource_group != null ? var.resource_group : "default"
}

module public_gateway {
// source = "terraform-ibm-modules/vpc/ibm//modules/public-gateway"
source = "../../modules/public-gateway"

vpc_id = data.ibm_is_vpc.vpc.id
resource_group_id = data.ibm_resource_group.resource_group.id
zones = var.zones
prefix = var.prefix
floating_ip = var.floating_ip
tags = var.tags
public_gateway_names = var.public_gateway_names
}

##############################################################################
72 changes: 72 additions & 0 deletions examples/public_gateway/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
##############################################################################
# Public Gateway Module Example Parameters
# Copyright 2020 IBM
##############################################################################

variable vpc {
description = "VPC name"
type = string
}

variable resource_group {
description = "Resource group name"
type = string
default = null
}

variable zones {
description = "A list of zones to create public gateways. These zones must be in the VPC region"
type = list(string)
default = ["us-south-1"]

validation {
condition = length(distinct(var.zones)) == length(var.zones)
error_message = "Only one gateway can be created in each zone."
}

validation {
condition = length(var.zones) <= 3 || length(var.zones) >= 1
error_message = "Public gatways can only be created in 1, 2, or 3 zones in a single region."
}
}

variable prefix {
description = "Prefix to add to the beginning of created Public Gateways"
type = string
default = "pgw"
}

##############################################################################


##############################################################################
# Optional Parameters
##############################################################################

variable floating_ip {
description = "Optional. Floating IP `id`'s or `address`'es that you want to assign to the public gateway. Leave as empty string to disable."
type = object({
zone-1 = string
zone-2 = string
zone-3 = string
})
default = {
zone-1 = ""
zone-2 = ""
zone-3 = ""
}
}

variable tags {
description = "Optional. List of Tags for the Public Gateway"
type = list(string)
default = []
}

variable public_gateway_names {
description = "Optional. A list of public gateways to get by name"
type = list(string)
default = []
}

##############################################################################
27 changes: 27 additions & 0 deletions examples/public_gateway/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##############################################################################
# Public Gateway
# Copyright 2020 IBM
##############################################################################

/***************************************************
NOTE: To source a particular version of IBM terraform provider, configure the parameter `version` as follows
terraform {
required_version = ">=0.13"
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.21.0"
}
}
}
If we dont configure the version parameter, it fetches the latest provider version.
****************************************************/

terraform {
required_version = ">=0.13"
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
}
}
}
1 change: 1 addition & 0 deletions modules/instance/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#####################################################
# Instance Resource
# Copyright 2020 IBM
Expand Down
4 changes: 2 additions & 2 deletions modules/instance/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#####################################################
##############################################################################
# Instance Module
# Copyright 2020 IBM
#####################################################
##############################################################################

/***************************************************
NOTE: To source a particular version of IBM terraform provider, configure the parameter `version` as follows
Expand Down
50 changes: 50 additions & 0 deletions modules/public_gateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Public Gateway Example

This module is used to create Public Gateways in a single vpc

## Example Usage
```
data ibm_is_vpc vpc {
name = var.vpc
}

data ibm_resource_group resource_group {
name = (var.resource_group != null ? var.resource_group : "default")
}

module public_gateway {
source = "terraform-ibm-modules/vpc/ibm//modules/public-gateway"
vpc_id = var.vpc_id
zones = var.zones
prefix = var.prefix
resource_group_id = var.resource_group_id
floating_ip = var.floating_ip
tags = var.tags
public_gateway_names = var.public_gateway_names
}
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Inputs

Name | Description | Type | Default
-------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | ---------------------------------------
vpc_id | VPC ID | string |
zones | A list of zones to create public gateways. These zones must be in the VPC region | list(string) | ["us-south-1"]
prefix | Prefix to add to the beginning of created Public Gateways | string | pgw
resource_group_id | Resource group ID | string | null
floating_ip | Optional. Floating IP `id`'s or `address`'es that you want to assign to the public gateway. Leave as empty string to disable. | object({ zone-1 = string zone-2 = string zone-3 = string }) | { zone-1 = "" zone-2 = "" zone-3 = "" }
tags | Optional. List of Tags for the Public Gateway | list(string) | []
public_gateway_names | Optional. A list of public gateways to get by name | list(string) | []

## Outputs

Name | Description
----------------- | -------------------------------
ids | The ID of the Public Gateways
ids_by_zone | A list of IDs by zone
addresses | List of public gatway addresses
addresses_by_zone | A list of addresses by zone

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
62 changes: 62 additions & 0 deletions modules/public_gateway/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
##############################################################################
# Public Gateway Resource
# Copyright 2020 IBM
##############################################################################

locals {
fip_objects = {
# Create an object of objects to use as `floating_ip` param
for zone in keys(var.floating_ip):
zone => {
# Get address if matches address regex
address = can(regex("^(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2})$", var.floating_ip[zone])) ? var.floating_ip[zone] : null
# Otherwise use ID
id = can(regex("^(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2}).(2[0-5][0-9]|1[0-9]{1,2}|[0-9]{1,2})$", var.floating_ip[zone])) ? null : var.floating_ip[zone]
}
}
}

resource ibm_is_public_gateway gateway {
count = length(var.zones)
name = "${var.prefix}-public-gateway-${var.zones[count.index]}"
resource_group = var.resource_group_id
vpc = var.vpc_id
zone = var.zones[count.index]
floating_ip = (
# Check if floating IP in the zone is an empty string
var.floating_ip["zone-${split("-", var.zones[count.index])[2]}"] == ""
# If if is, return null
? null
# Otherwise return the value from fip objects
:local.fip_objects["zone-${split("-", var.zones[count.index])[2]}"]
)
tags = var.tags
}

##############################################################################


##############################################################################
# Public Gateway Data Sources
##############################################################################

data ibm_is_public_gateway gateway {
count = length(var.public_gateway_names)
name = var.public_gateway_names[count.index]
}

##############################################################################


##############################################################################
# Locals For Outputs
##############################################################################

locals {
public_gateways = flatten([
ibm_is_public_gateway.gateway,
data.ibm_is_public_gateway.gateway
])
}

##############################################################################
35 changes: 35 additions & 0 deletions modules/public_gateway/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
##############################################################################
# Public Gateway Resource Output
# Copyright 2020 IBM
##############################################################################

output ids {
description = "The ID of the Public Gateways"
value = local.public_gateways.*.id
}

output ids_by_zone {
description = "A list of IDs by zone"
value = {
for gateway in local.public_gateways:
("zone-${split("-", gateway.zone)[2]}") => gateway.id
}
}

output addresses {
description = "List of public gatway addresses"
value = [
for gateway in local.public_gateways:
lookup(gateway.floating_ip, "address", null) if (contains(keys(gateway), "floating_ip"))
]
}

output addresses_by_zone {
description = "A list of addresses by zone"
value = {
for gateway in local.public_gateways:
("zone-${split("-", gateway.zone)[2]}") => lookup(gateway.floating_ip, "address", null)
}
}

##############################################################################
Loading