From 0f779bc994c23dc19e085931971b3d2aa3169c36 Mon Sep 17 00:00:00 2001 From: Matt Sanders Date: Mon, 4 Jan 2021 21:46:43 -0800 Subject: [PATCH] Removing hyphens form user names. IP Address is now relying on powershell instead of external http request. --- main.tf | 35 ++++++++++++++++++++++------------- outputs.tf | 5 +++++ scripts/Get-CurrentIpV4.ps1 | 5 +++++ 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 scripts/Get-CurrentIpV4.ps1 diff --git a/main.tf b/main.tf index 4a3a73e..4dfe12f 100644 --- a/main.tf +++ b/main.tf @@ -36,7 +36,7 @@ locals { service_name = lower(var.service_name) environment_name = local.is_dev ? "${local.environment_differentiator}-${var.environment}" : var.environment service_environment_name = local.is_dev ? "${var.service_name}-${local.environment_differentiator}-${var.environment}" : "${var.service_name}-${var.environment}" - environment_differentiator = var.environment_differentiator != "" ? var.environment_differentiator : local.is_dev && length(data.azuread_user.current_user) > 0 ? split(".", split("_", split("#EXT#", data.azuread_user.current_user[0].mail_nickname)[0])[0])[0] : "" + environment_differentiator = var.environment_differentiator != "" ? var.environment_differentiator : local.is_dev && length(data.azuread_user.current_user) > 0 ? replace(split(".", split("_", split("#EXT#", data.azuread_user.current_user[0].mail_nickname)[0])[0])[0], "-", "") : "" has_cosmos = length({ for microservice in var.microservices : microservice.name => microservice if microservice.cosmos_containers != null ? length(microservice.cosmos_containers) > 0 : false }) > 0 has_queues = length({ for microservice in var.microservices : microservice.name => microservice if microservice.queues != null ? length(microservice.queues) > 0 : false }) > 0 has_sql_server_elastic = length({ for microservice in var.microservices : microservice.name => microservice if microservice.sql == "elastic" }) > 0 @@ -61,28 +61,37 @@ locals { environment_differentiator_short2 = local.max_environment_differentiator_short2 > 0 ? length(local.environment_differentiator) <= local.max_environment_differentiator_short2 ? local.environment_differentiator : substr(local.environment_differentiator, 0, local.max_environment_differentiator_short2) : "" } -# Getting current IP Address, only used for dev environment -# solution from here: https://stackoverflow.com/a/58959164/1362146 -data "http" "my_public_ip" { - count = local.is_dev ? 1 : 0 +# Commenting out the external http request dependency in favor of powershell method for looking up IP +# Leaving this in as it might be beneficial to offer different ip retrieval approaches depending on environment (will powershell be available?) + +# # Getting current IP Address, only used for dev environment +# # solution from here: https://stackoverflow.com/a/58959164/1362146 +# data "http" "my_public_ip" { +# count = local.is_dev ? 1 : 0 + +# # url = "https://ifconfig.co/json" +# # request_headers = { +# # Accept = "application/json" +# # } - # url = "https://ifconfig.co/json" - # request_headers = { - # Accept = "application/json" - # } +# url = "https://ipinfo.io/ip" +# } + +data "external" "current_ipv4" { + count = local.is_dev ? 1 : 0 - url = "https://ipinfo.io/ip" + program = ["Powershell.exe", "${path.module}/scripts/Get-CurrentIpV4.ps1"] } locals { # http_my_public_ip_response = jsondecode(data.http.my_public_ip[0].body) # current_ip = local.is_dev ? local.http_my_public_ip_response.ip : null - http_my_public_ip_response = chomp(data.http.my_public_ip[0].body) - current_ip = local.is_dev ? local.http_my_public_ip_response : null + # http_my_public_ip_response = chomp(data.http.my_public_ip[0].body) + # current_ip = local.is_dev ? local.http_my_public_ip_response : null # the current_ip is only retrieved and set for the dev environment to simplify developer workflow - key_vault_ip_rules = local.is_dev ? ["${local.current_ip}/32"] : null + key_vault_ip_rules = local.is_dev ? ["${data.external.current_ipv4[0].result.ip_address}/32"] : null } ################################# diff --git a/outputs.tf b/outputs.tf index 9805c27..77d9868 100644 --- a/outputs.tf +++ b/outputs.tf @@ -8,6 +8,11 @@ output "current_user" { value = data.azuread_user.current_user } +output "current_ip" { + description = "IP Address retrieved for simpilfying dev configurations. Will be null for non dev environments." + value = local.is_dev ? data.external.current_ipv4[0].result.ip_address : null +} + output "locals" { description = "local values created base on input data" value = { diff --git a/scripts/Get-CurrentIpV4.ps1 b/scripts/Get-CurrentIpV4.ps1 new file mode 100644 index 0000000..29763ce --- /dev/null +++ b/scripts/Get-CurrentIpV4.ps1 @@ -0,0 +1,5 @@ +$ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select IPV4Address + +@{ + ip_address = $ipv4.IPV4Address.IPAddressToString; +} | ConvertTo-Json \ No newline at end of file