Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Front Door Module with AzAPI Provider #63

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
24 changes: 24 additions & 0 deletions modules/azapi/FrontDoor/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -------------------------------------------------------------------------------------
#
# 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
}
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
Loading