Skip to content

Commit

Permalink
Merge pull request #7 from Matthewsre/feature/communication
Browse files Browse the repository at this point in the history
Removing hyphens form user names. IP Address is now relying on powers…
  • Loading branch information
Matthewsre authored Jan 5, 2021
2 parents 7d98d84 + 0f779bc commit e2403cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
35 changes: 22 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

#################################
Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
5 changes: 5 additions & 0 deletions scripts/Get-CurrentIpV4.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | Select IPV4Address

@{
ip_address = $ipv4.IPV4Address.IPAddressToString;
} | ConvertTo-Json

0 comments on commit e2403cd

Please sign in to comment.