Skip to content

Commit

Permalink
Merge pull request #63 from athiththan11/main-frontdoor
Browse files Browse the repository at this point in the history
Introduce Front Door Module with AzAPI Provider
  • Loading branch information
athiththan11 authored May 6, 2024
2 parents bea9c3a + e11ca6d commit b47453a
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 0 deletions.
123 changes: 123 additions & 0 deletions modules/azapi/FrontDoor/frontdoor.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# --------------------------------------------------------------------------------------

resource "azapi_resource" "frontdoor" {
type = "Microsoft.Network/frontDoors@2021-06-01"
name = local.frontdoor_name
parent_id = var.resource_group_id
location = "Global"
body = jsonencode({
properties = {
enabledState = var.enabled

# Configure Frontend Endpoints
frontendEndpoints = [
for endpoint in var.frontend_endpoints :
{
name = join("-", [var.frontend_endpoints_abbreviation, endpoint.name])
properties = {
hostName = endpoint.hostname
sessionAffinityEnabledState = endpoint.sessionAffinityEnabledState
sessionAffinityTtlSeconds = endpoint.sessionAffinityTtlSeconds
webApplicationFirewallPolicyLink = endpoint.webApplicationFirewallPolicyLink
}
}
]

# Configure Routing Rules
routingRules = [
for rule in var.routing_rules :
{
name = rule.name
properties = {
acceptedProtocols = rule.acceptedProtocols
enabledState = rule.enabledState
patternsToMatch = rule.patternsToMatch
frontendEndpoints = rule.frontendEndpoints != null ? [
for frontend_endpoint in rule.frontendEndpoints :
{
id = "${var.resource_group_id}/providers/Microsoft.Network/frontDoors/${local.frontdoor_name}/frontendEndpoints/${join("-", [var.frontend_endpoints_abbreviation, frontend_endpoint])}"
}
] : null
routeConfiguration = rule.routeConfiguration
}
}
]

# Configure Load Balancing Settings
loadBalancingSettings = [
for load_balance in var.load_balancing_settings :
{
name = join("-", [var.load_balancing_abbreviation, load_balance.name])
properties = {
sampleSize = load_balance.sampleSize
successfulSamplesRequired = load_balance.successfulSamplesRequired
additionalLatencyMilliseconds = load_balance.additionalLatencyMilliseconds
}
}
]

# Configure Health Probes
healthProbeSettings = [
for health_probe in var.health_probe_settings :
{
name = join("-", [var.health_probe_abbreviation, health_probe.name])
properties = {
enabledState = health_probe.enabledState
path = health_probe.path
protocol = health_probe.protocol
healthProbeMethod = health_probe.healthProbeMethod
intervalInSeconds = health_probe.intervalInSeconds
}
}
]

# Configure Backend Pool
backendPools = [
for pool in var.backend_pools :
{
name = pool.name
properties = {
backends = [
for backend in pool.backends :
{
address = backend.address
backendHostHeader = backend.backendHostHeader
enabledState = backend.enabledState
httpPort = backend.httpPort
httpsPort = backend.httpsPort
priority = backend.priority
weight = backend.weight
}
]
loadBalancingSettings = pool.loadBalancingSettings != null ? {
id = "${var.resource_group_id}/providers/Microsoft.Network/frontDoors/${local.frontdoor_name}/loadBalancingSettings/${join("-", [var.load_balancing_abbreviation, pool.loadBalancingSettings])}"
} : null
healthProbeSettings = pool.healthProbeSettings != null ? {
id = "${var.resource_group_id}/providers/Microsoft.Network/frontDoors/${local.frontdoor_name}/healthProbeSettings/${join("-", [var.health_probe_abbreviation, pool.healthProbeSettings])}"
} : null
}
}
]
}
})

response_export_values = ["*"]
}
23 changes: 23 additions & 0 deletions modules/azapi/FrontDoor/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# --------------------------------------------------------------------------------------

locals {
frontdoor_name = join("-", [var.frontdoor_abbreviation, var.frontdoor_name])
}
34 changes: 34 additions & 0 deletions modules/azapi/FrontDoor/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# --------------------------------------------------------------------------------------

output "frontdoor_id" {
description = "The ID of the Front Door."
value = jsondecode(azapi_resource.frontdoor.output).properties.frontdoorId
}

output "frontdoor_frontend_endpoint_ids" {
description = "The Resource IDs of the Frontend Endpoints of the Front Door. This is used to configure custom HTTPS settings."
value = [for endpoint in jsondecode(azapi_resource.frontdoor.output).properties.frontendEndpoints : endpoint.id if endpoint.name != "${var.frontend_endpoints_abbreviation}-default"]
}

output "frontdoor_frontend_endpoint_names" {
description = "The Resource names of the Frontend Endpoints of the Front Door."
value = [for endpoint in jsondecode(azapi_resource.frontdoor.output).properties.frontendEndpoints : endpoint.name if endpoint.name != "${var.frontend_endpoints_abbreviation}-default"]
}
70 changes: 70 additions & 0 deletions modules/azapi/FrontDoor/rules_engine.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# --------------------------------------------------------------------------------------

# Define the rules engine resource
resource "azapi_resource" "frontdoor_rules_engine" {
count = length(var.rules_engines)
type = "Microsoft.Network/frontDoors/rulesEngines@2021-06-01"
name = var.rules_engines[count.index].name
parent_id = azapi_resource.frontdoor.id

body = jsonencode({
properties = {
rules = var.rules_engines[count.index].rules
}
})

depends_on = [
azapi_resource.frontdoor
]
}

# Update the frontdoor resource with the rules engine
resource "azapi_update_resource" "frontdoor" {
type = "Microsoft.Network/frontDoors@2021-06-01"
resource_id = azapi_resource.frontdoor.id
body = jsonencode({
properties = {
routingRules = [
for rule in jsondecode(azapi_resource.frontdoor.output).properties.routingRules :
{
id = rule.id
name = rule.name
properties = {
acceptedProtocols = rule.properties.acceptedProtocols
enabledState = rule.properties.enabledState
patternsToMatch = rule.properties.patternsToMatch
frontendEndpoints = rule.properties.frontendEndpoints
routeConfiguration = rule.properties.routeConfiguration

rulesEngine = rule.properties.rulesEngine != null ? rule.properties.rulesEngine : contains(keys(var.rules_engine_map), rule.name) ? {
id = "${var.resource_group_id}/providers/Microsoft.Network/frontDoors/${local.frontdoor_name}/RulesEngines/${var.rules_engine_map[rule.name]}"
} : null
}
}
]
}
})

depends_on = [
azapi_resource.frontdoor,
azapi_resource.frontdoor_rules_engine
]
}
128 changes: 128 additions & 0 deletions modules/azapi/FrontDoor/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# --------------------------------------------------------------------------------------

variable "frontdoor_name" {
description = "The name of the Front Door."
type = string
}

variable "frontdoor_abbreviation" {
description = "The abbreviation to be used in the name of the Front Door."
type = string
default = "fd"
}

variable "resource_group_id" {
description = "The ID of the resource group in which the Front Door will be created."
type = string
}

variable "enabled" {
description = "The state of the Front Door. Values are Enabled or Disabled."
type = string
default = "Enabled"
}

variable "frontend_endpoints" {
description = "A list of frontend endpoints to be associated with the Front Door."
type = list(object({
name = string
hostname = string
sessionAffinityEnabledState = string
sessionAffinityTtlSeconds = number
webApplicationFirewallPolicyLink = string
}))
}

variable "frontend_endpoints_abbreviation" {
description = "The abbreviation to be used in the name of the frontend endpoints."
type = string
default = "fe"
}

variable "routing_rules" {
description = "A list of routing rules to be associated with the Front Door. Check https://learn.microsoft.com/en-us/azure/templates/microsoft.network/frontdoors?pivots=deployment-language-terraform for more information."
type = any
}

variable "load_balancing_settings" {
description = "A list of load balancing settings to be associated with the Front Door."
type = list(object({
name = string
sampleSize = number
successfulSamplesRequired = number
additionalLatencyMilliseconds = number
}))
}

variable "load_balancing_abbreviation" {
description = "The abbreviation to be used in the name of the load balancing settings."
type = string
default = "lb"
}

variable "health_probe_settings" {
description = "A list of health probe settings to be associated with the Front Door."
type = list(object({
name = string
enabledState = string
path = string
protocol = string
healthProbeMethod = string
intervalInSeconds = number
}))
}

variable "health_probe_abbreviation" {
description = "The abbreviation to be used in the name of the health probe settings."
type = string
default = "hp"
}

variable "backend_pools" {
description = "A list of backend pools to be associated with the Front Door."
type = list(object({
name = string
backends = list(object({
address = string
backendHostHeader = string
enabledState = string
httpPort = number
httpsPort = number
priority = number
weight = number
}))
loadBalancingSettings = string
healthProbeSettings = string
}))
}

variable "rules_engines" {
description = "A list of rules engines to be associated with the Front Door. Check https://learn.microsoft.com/en-us/azure/templates/microsoft.network/frontdoors/rulesengines?pivots=deployment-language-terraform for more information."
type = list(object({
name = string
rules = any
}))
}

variable "rules_engine_map" {
description = "A map of rules engines to be associated with the Front Door."
type = map(string)
}
Loading

0 comments on commit b47453a

Please sign in to comment.