diff --git a/README.md b/README.md index 1a5416d..6d912eb 100644 --- a/README.md +++ b/README.md @@ -27,23 +27,23 @@ Truefoundry Azure Network Module |------|------| | [azurerm_private_dns_zone.postgres_dns](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_zone) | resource | | [azurerm_private_dns_zone_virtual_network_link.postgres_dns_link](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_zone_virtual_network_link) | resource | +| [azurerm_virtual_network.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/virtual_network) | data source | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [cluster\_name](#input\_cluster\_name) | AWS EKS cluster name needed for Shared cluster | `string` | n/a | yes | +| [cluster\_name](#input\_cluster\_name) | Cluster name to generate the virtual network name | `string` | n/a | yes | | [control\_plane\_enabled](#input\_control\_plane\_enabled) | Flag to check Control plane enabled | `bool` | n/a | yes | -| [location](#input\_location) | Vnet region | `string` | n/a | yes | +| [location](#input\_location) | Location to create the vnet | `string` | n/a | yes | | [resource\_group\_name](#input\_resource\_group\_name) | Azure Resource Group | `string` | n/a | yes | -| [shim](#input\_shim) | If true will not create the network and forward the input values to the same outputs. | `bool` | `false` | no | -| [shim\_vnet\_name](#input\_shim\_vnet\_name) | Vnet name for the shim network | `string` | `""` | no | | [subnet\_cidr](#input\_subnet\_cidr) | Assigns IPv4 subnet | `string` | n/a | yes | -| [subnet\_id](#input\_subnet\_id) | SHIM: Subnet ID | `string` | n/a | yes | +| [subnet\_id](#input\_subnet\_id) | Subnet ID. Used only when use\_existing\_vnet is enabled | `string` | n/a | yes | | [tags](#input\_tags) | AWS Tags common to all the resources created | `map(string)` | `{}` | no | +| [use\_existing\_vnet](#input\_use\_existing\_vnet) | Flag to enable existing network | `bool` | `false` | no | | [use\_for\_each](#input\_use\_for\_each) | Use `for_each` instead of `count` to create multiple resource instances. | `bool` | `false` | no | | [vnet\_cidr](#input\_vnet\_cidr) | The CIDR block for the VPC. | `string` | n/a | yes | -| [vnet\_id](#input\_vnet\_id) | SHIM: VPC Id | `string` | n/a | yes | +| [vnet\_id](#input\_vnet\_id) | VPC ID. Used only when use\_existing\_vnet is enabled | `string` | n/a | yes | ## Outputs @@ -52,6 +52,8 @@ Truefoundry Azure Network Module | [db\_private\_dns\_zone\_id](#output\_db\_private\_dns\_zone\_id) | n/a | | [subnet\_id](#output\_subnet\_id) | n/a | | [vnet\_address\_space](#output\_vnet\_address\_space) | n/a | +| [vnet\_end\_ip\_address](#output\_vnet\_end\_ip\_address) | n/a | | [vnet\_id](#output\_vnet\_id) | n/a | | [vnet\_name](#output\_vnet\_name) | n/a | +| [vnet\_start\_ip\_address](#output\_vnet\_start\_ip\_address) | n/a | \ No newline at end of file diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..3574fda --- /dev/null +++ b/data.tf @@ -0,0 +1,5 @@ +data "azurerm_virtual_network" "vnet" { + count = var.use_existing_vnet ? 1 : 0 + name = local.vnet_name + resource_group_name = var.resource_group_name +} \ No newline at end of file diff --git a/locals.tf b/locals.tf index f70d52c..be02922 100644 --- a/locals.tf +++ b/locals.tf @@ -1,5 +1,5 @@ locals { - vnet_name = var.shim ? var.shim_vnet_name : "${var.cluster_name}-vnet" + vnet_name = var.use_existing_vnet ? element(split("/", var.vnet_id), length(split("/", var.vnet_id)) - 1) : "${var.cluster_name}-vnet" tags = merge( { "terraform-module" = "terraform-azure-truefoundry-network" diff --git a/output.tf b/output.tf index 576809d..9669c70 100644 --- a/output.tf +++ b/output.tf @@ -3,20 +3,28 @@ #################################################################################### output "vnet_id" { - value = var.shim ? var.vnet_id : module.vnet[0].vnet_id + value = var.use_existing_vnet ? var.vnet_id : module.vnet[0].vnet_id } output "vnet_name" { value = local.vnet_name } output "vnet_address_space" { - value = var.shim ? [] : module.vnet[0].vnet_address_space + value = var.use_existing_vnet ? [] : module.vnet[0].vnet_address_space } output "subnet_id" { - value = var.shim ? var.subnet_id : module.vnet[0].vnet_subnets[0] + value = var.use_existing_vnet ? var.subnet_id : module.vnet[0].vnet_subnets[0] } output "db_private_dns_zone_id" { value = var.control_plane_enabled ? azurerm_private_dns_zone.postgres_dns[0].id : "" +} + +output "vnet_start_ip_address" { + value = var.use_existing_vnet ? cidrhost(data.azurerm_virtual_network.vnet[0].address_space[0], 0) : cidrhost(var.vnet_cidr, 0) +} + +output "vnet_end_ip_address" { + value = var.use_existing_vnet ? cidrhost(data.azurerm_virtual_network.vnet[0].address_space[0], -1) : cidrhost(var.vnet_cidr, -1) } \ No newline at end of file diff --git a/variables.tf b/variables.tf index 296b622..6c4632e 100644 --- a/variables.tf +++ b/variables.tf @@ -1,29 +1,31 @@ ################################################################################## -## Variables +## Existing network ################################################################################## -variable "shim" { - description = "If true will not create the network and forward the input values to the same outputs." +variable "use_existing_vnet" { + description = "Flag to enable existing network" type = bool default = false } -### Shim - variable "vnet_id" { - description = "SHIM: VPC Id" + description = "VPC ID. Used only when use_existing_vnet is enabled" type = string + validation { + condition = length(var.vnet_id) == "" || var.use_existing_vnet + error_message = "Vnet ID can't be empty if use_existing_vnet is enabled" + } } variable "subnet_id" { - description = "SHIM: Subnet ID" - type = string -} - -variable "shim_vnet_name" { - description = "Vnet name for the shim network" + description = "Subnet ID. Used only when use_existing_vnet is enabled" type = string - default = "" + validation { + condition = length(var.subnet_id) == "" || var.use_existing_vnet + error_message = "Subnet ID can't be empty if use_existing_vnet is enabled" + } } -### Non shim +################################################################################## +## New network +################################################################################## variable "vnet_cidr" { description = "The CIDR block for the VPC." type = string @@ -45,18 +47,21 @@ variable "use_for_each" { type = bool } +################################################################################## +## Common +################################################################################## variable "resource_group_name" { description = "Azure Resource Group" type = string } variable "cluster_name" { - description = "AWS EKS cluster name needed for Shared cluster" + description = "Cluster name to generate the virtual network name" type = string } variable "location" { - description = "Vnet region" + description = "Location to create the vnet" type = string } diff --git a/vnet.tf b/vnet.tf index d437662..3502499 100644 --- a/vnet.tf +++ b/vnet.tf @@ -2,7 +2,7 @@ # RESOURCES ############################################################################# module "vnet" { - count = var.shim ? 0 : 1 + count = var.use_existing_vnet ? 0 : 1 source = "Azure/vnet/azurerm" version = "4.1.0" @@ -27,6 +27,6 @@ resource "azurerm_private_dns_zone_virtual_network_link" "postgres_dns_link" { count = var.control_plane_enabled ? 1 : 0 name = "${replace(local.vnet_name, "-", "")}VnetZone.com" private_dns_zone_name = azurerm_private_dns_zone.postgres_dns[0].name - virtual_network_id = var.shim ? var.vnet_id : module.vnet[0].vnet_id + virtual_network_id = var.use_existing_vnet ? var.vnet_id : module.vnet[0].vnet_id resource_group_name = var.resource_group_name } \ No newline at end of file