Skip to content

Commit

Permalink
feat: Add public IP resources (#1)
Browse files Browse the repository at this point in the history
* feat: Add public IP resources

* feat: Add public IP outputs

* fix: Allow null values and fix descriptor name

* improvement: set nullable to false for objects
  • Loading branch information
jakubigla authored Dec 15, 2022
1 parent 56a3f65 commit e87a95f
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
args: ["."]

- repo: https://github.com/bridgecrewio/checkov.git
rev: '2.2.130' # Get the latest from: https://github.com/bridgecrewio/checkov/releases
rev: '2.2.158' # Get the latest from: https://github.com/bridgecrewio/checkov/releases
hooks:
- id: checkov
args: [--skip-check, "CKV2_GHA_1"] #Flase positive for top-level permissions
Expand Down
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# Terraform Azure Module Template


> **Warning**:
> This is a template document. Remember to **remove** all text in _italics_ and **update** Module name, Repo name and links/badges to the acual name of your GitHub repository/module!!!
# Azure Public IP Terraform Module

![Azure](https://img.shields.io/badge/azure-%230072C6.svg?style=for-the-badge&logo=microsoftazure&logoColor=white)
![Terraform](https://img.shields.io/badge/terraform-%235835CC.svg?style=for-the-badge&logo=terraform&logoColor=white)

<!--- Replace repository name -->
![License](https://badgen.net/github/license/getindata/terraform-azurerm-module-tempate/)
![Release](https://badgen.net/github/release/getindata/terraform-azurerm-module-tempate/)
![License](https://badgen.net/github/license/getindata/terraform-azurerm-public-ip/)
![Release](https://badgen.net/github/release/getindata/terraform-azurerm-public-ip/)

<p align="center">
<img height="150" src="https://getindata.com/img/logo.svg">
Expand All @@ -18,28 +13,21 @@

---

_Brief Description of MODULE:_

* _What it does_
* _What techonlogies it uses_
This module is managing Azure Public IP instance.

## USAGE

_Example usage of the module - terraform code snippet_

```terraform
module "template" {
source = "github.com/getindata/terraform-azurerm-module-tempate"
module "public_ip" {
source = "github.com/getindata/terraform-azurerm-public-ip"
location = "West Europe"
resource_group_name = "example-rg"
allocation_method = "Dynamic"
}
```

## NOTES

_Additional information that should be made public, for ex. how to solve known issues, additional descriptions/suggestions_

## EXAMPLES

- [Simple](examples/simple)
Expand All @@ -55,6 +43,7 @@ _Additional information that should be made public, for ex. how to solve known i
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
| <a name="input_allocation_method"></a> [allocation\_method](#input\_allocation\_method) | Defines the allocation method for this IP address. Possible values are Static or Dynamic | `string` | n/a | yes |
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
Expand All @@ -64,6 +53,7 @@ _Additional information that should be made public, for ex. how to solve known i
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
| <a name="input_ip_version"></a> [ip\_version](#input\_ip\_version) | The IP Version to use, IPv6 or IPv4 | `string` | `null` | no |
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br>Does not affect keys of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br>set as tag values, and output by this module individually.<br>Does not affect values of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br>Default value: `lower`. | `string` | `null` | no |
Expand All @@ -73,9 +63,12 @@ _Additional information that should be made public, for ex. how to solve known i
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Azure resource group name where resources will be deployed | `string` | n/a | yes |
| <a name="input_sku"></a> [sku](#input\_sku) | The SKU of the Public IP. Accepted values are Basic and Standard. Defaults to Basic. Public IP Standard SKUs require allocation\_method to be set to Static | `string` | `null` | no |
| <a name="input_sku_tier"></a> [sku\_tier](#input\_sku\_tier) | The SKU Tier that should be used for the Public IP. Possible values are Regional and Global. Defaults to Regional. When sku\_tier is set to Global, sku must be set to Standard | `string` | `null` | no |
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
| <a name="input_zones"></a> [zones](#input\_zones) | A collection containing the availability zone to allocate the Public IP in | `list(string)` | `null` | no |

## Modules

Expand All @@ -88,9 +81,11 @@ _Additional information that should be made public, for ex. how to solve known i

| Name | Description |
|------|-------------|
| <a name="output_id"></a> [id](#output\_id) | ID of the resource |
| <a name="output_name"></a> [name](#output\_name) | Name of the resource |
| <a name="output_resource_group_name"></a> [resource\_group\_name](#output\_resource\_group\_name) | Name of the resource resource group |
| <a name="output_fqdn"></a> [fqdn](#output\_fqdn) | Fully qualified domain name of the A DNS record associated with the public IP |
| <a name="output_id"></a> [id](#output\_id) | ID of the Azure Public IP |
| <a name="output_ip_address"></a> [ip\_address](#output\_ip\_address) | The IP address value that was allocated |
| <a name="output_name"></a> [name](#output\_name) | Name of the Azure Public IP |
| <a name="output_resource_group_name"></a> [resource\_group\_name](#output\_resource\_group\_name) | Name of the Azure Public IP resource group |

## Providers

Expand Down Expand Up @@ -126,8 +121,8 @@ Apache 2 Licensed. See [LICENSE](LICENSE) for full details.
## AUTHORS

<!--- Replace repository name -->
<a href="https://github.com/getindata/terraform-azurerm-module-tempate/graphs/contributors">
<img src="https://contrib.rocks/image?repo=getindata/terraform-azurerm-module-tempate" />
<a href="https://github.com/getindata/terraform-azurerm-public-ip/graphs/contributors">
<img src="https://contrib.rocks/image?repo=getindata/terraform-azurerm-public-ip" />
</a>

Made with [contrib.rocks](https://contrib.rocks).
36 changes: 33 additions & 3 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
# Complete Example

```terraform
module "terraform_module_template" {
source = "../../"
module "resource_group" {
source = "github.com/getindata/terraform-azurerm-resource-group?ref=v1.2.0"
context = module.this.context
name = var.resource_group_name
location = var.location
resource_group_name = var.resource_group_name
}
resource "azurerm_log_analytics_workspace" "this" {
name = module.this.id
location = module.resource_group.location
resource_group_name = module.resource_group.name
tags = module.this.tags
sku = "PerGB2018"
}
module "public_ip" {
source = "../../"
context = module.this.context
location = module.resource_group.location
resource_group_name = module.resource_group.name
allocation_method = "Static"
zones = ["1", "2", "3"]
ip_version = "IPv4"
sku = "Standard"
sku_tier = "Regional"
diagnostic_settings = {
enabled = true
logs_destinations_ids = [
azurerm_log_analytics_workspace.this.id
]
}
}
```

Expand Down
4 changes: 4 additions & 0 deletions examples/complete/fixtures.west-europe.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ descriptor_formats = {
labels = ["name"]
format = "%v-rg"
}
azure-public-ip = {
labels = ["namespace", "environment", "stage", "name", "attributes"]
format = "%v-%v-%v-%v-%v-pip"
}
}

tags = {
Expand Down
9 changes: 8 additions & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ resource "azurerm_log_analytics_workspace" "this" {
sku = "PerGB2018"
}

module "terraform_module_template" {
module "public_ip" {
source = "../../"
context = module.this.context

location = module.resource_group.location
resource_group_name = module.resource_group.name

allocation_method = "Static"
zones = ["1", "2", "3"]
ip_version = "IPv4"

sku = "Standard"
sku_tier = "Regional"

diagnostic_settings = {
enabled = true
logs_destinations_ids = [
Expand Down
6 changes: 3 additions & 3 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "terraform_module_template" {
description = "Output of the template module"
value = module.terraform_module_template
output "public_ip" {
description = "Output of the public ip module"
value = module.public_ip
}
19 changes: 15 additions & 4 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# Simple Example

```terraform
module "terraform_module_template" {
source = "../../"
module "resource_group" {
source = "github.com/getindata/terraform-azurerm-resource-group?ref=v1.2.0"
location = var.location
resource_group_name = var.resource_group_name
name = var.resource_group_name
location = var.location
}
module "public_ip" {
source = "../../"
name = "public-ip"
location = module.resource_group.location
resource_group_name = module.resource_group.name
allocation_method = "Dynamic"
}
```

Expand Down
17 changes: 14 additions & 3 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
module "terraform_module_template" {
module "resource_group" {
source = "github.com/getindata/terraform-azurerm-resource-group?ref=v1.2.0"

name = var.resource_group_name
location = var.location
}

module "public_ip" {
source = "../../"

location = var.location
resource_group_name = var.resource_group_name
name = "public-ip"

location = module.resource_group.location
resource_group_name = module.resource_group.name

allocation_method = "Dynamic"
}
6 changes: 3 additions & 3 deletions examples/simple/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "terraform_module_template" {
description = "Output of the template module"
value = module.terraform_module_template
output "public_ip" {
description = "Output of the public ip module"
value = module.public_ip
}
2 changes: 1 addition & 1 deletion locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ locals {
# Get a name from the descriptor. If not available, use default naming convention.
# Trim and replace function are used to avoid bare delimiters on both ends of the name and situation of adjacent delimiters.
name_from_descriptor = trim(replace(
lookup(module.this.descriptors, var.descriptor_name, module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", ""
lookup(module.this.descriptors, var.descriptor_name, module.this.id), "/${module.this.delimiter}${module.this.delimiter}+/", module.this.delimiter
), module.this.delimiter)

location = coalesce(one(data.azurerm_resource_group.this[*].location), var.location)
Expand Down
10 changes: 8 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ data "azurerm_resource_group" "this" {
name = var.resource_group_name
}

#This is a sample resource
resource "azurerm_public_ip" "this" {
count = module.this.enabled ? 1 : 0

name = local.name_from_descriptor
location = local.location
resource_group_name = local.resource_group_name

allocation_method = "Dynamic"
allocation_method = var.allocation_method
zones = var.zones
ip_version = var.ip_version

sku = var.sku
sku_tier = var.sku_tier

tags = module.this.tags
}

module "diagnostic_settings" {
Expand Down
16 changes: 13 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
output "name" {
description = "Name of the resource"
description = "Name of the Azure Public IP"
value = one(azurerm_public_ip.this[*].name)
}

output "id" {
description = "ID of the resource"
description = "ID of the Azure Public IP"
value = one(azurerm_public_ip.this[*].id)
}

output "resource_group_name" {
description = "Name of the resource resource group"
description = "Name of the Azure Public IP resource group"
value = one(azurerm_public_ip.this[*].resource_group_name)
}

output "ip_address" {
description = "The IP address value that was allocated"
value = one(azurerm_public_ip.this[*].ip_address)
}

output "fqdn" {
description = "Fully qualified domain name of the A DNS record associated with the public IP"
value = one(azurerm_public_ip.this[*].fqdn)
}
52 changes: 51 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,55 @@ variable "diagnostic_settings" {
enabled = optional(bool, false)
logs_destinations_ids = optional(list(string), [])
})
default = {}
default = {}
nullable = false
}

variable "allocation_method" {
description = "Defines the allocation method for this IP address. Possible values are Static or Dynamic"
type = string

validation {
condition = contains(["Static", "Dynamic"], var.allocation_method)
error_message = "Possible values are Static or Dynamic for the allocation_method variable"
}
}

variable "zones" {
description = "A collection containing the availability zone to allocate the Public IP in"
type = list(string)
default = null
}

variable "ip_version" {
description = "The IP Version to use, IPv6 or IPv4"
type = string
default = null

validation {
condition = contains(["IPv4", "IPv6", "[null]"], coalesce(var.ip_version, "[null]"))
error_message = "Possible values are IPv6 or IPv6 for the ip_version variable"
}
}

variable "sku" {
description = "The SKU of the Public IP. Accepted values are Basic and Standard. Defaults to Basic. Public IP Standard SKUs require allocation_method to be set to Static"
type = string
default = null

validation {
condition = contains(["Basic", "Standard", "[null]"], coalesce(var.sku, "[null]"))
error_message = "Possible values are Basic or Standard for the sku variable"
}
}

variable "sku_tier" {
description = "The SKU Tier that should be used for the Public IP. Possible values are Regional and Global. Defaults to Regional. When sku_tier is set to Global, sku must be set to Standard"
type = string
default = null

validation {
condition = contains(["Regional", "Global", "[null]"], coalesce(var.sku_tier, "[null]"))
error_message = "Possible values are Regional or Global for the sku_tier variable"
}
}

0 comments on commit e87a95f

Please sign in to comment.