Skip to content

Commit f14895f

Browse files
Azure vWAN | Update Routing Intent to use AzApi (#488)
1 parent 6c3be80 commit f14895f

7 files changed

Lines changed: 65 additions & 49 deletions

File tree

terraform/azure/modules/add-routing-intent.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

terraform/azure/nva-into-existing-hub/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ please see the [CloudGuard Network for Azure Virtual WAN Deployment Guide](https
1212

1313
## Configurations
1414
- Install and configure Terraform to provision Azure resources: [Configure Terraform for Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure).
15-
- In order to configure hub routing-intent policies it is **required** to have Python and 'requests' library installed.
1615

1716
## Usage
1817
- Choose the preferred login method to Azure in order to deploy the solution:
@@ -156,6 +155,9 @@ please see the [CloudGuard Network for Azure Virtual WAN Deployment Guide](https
156155
existing-public-ip = ""
157156
new-public-ip = "yes"
158157

158+
## Known limitations
159+
1. 'terraform destroy' doesn't work if routing-intent is configured. To destroy the deployment, the routing-intent should be deleted manually first.
160+
159161
## Revision History
160162
In order to check the template version refer to the [sk116585](https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk116585)
161163

terraform/azure/nva-into-existing-hub/main.tf

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ locals {
5656
"nextHop": "/subscriptions/${var.subscription_id}/resourcegroups/${var.nva-rg-name}/providers/Microsoft.Network/networkVirtualAppliances/${var.nva-name}"
5757
}
5858
routing-intent-policies = var.routing-intent-internet-traffic == "yes" ? (var.routing-intent-private-traffic == "yes" ? tolist([local.routing_intent-internet-policy, local.routing_intent-private-policy]) : tolist([local.routing_intent-internet-policy])) : (var.routing-intent-private-traffic == "yes" ? tolist([local.routing_intent-private-policy]) : [])
59-
req_body = jsonencode({"properties": {"routingPolicies": local.routing-intent-policies}})
60-
req_url = "https://management.azure.com/subscriptions/${var.subscription_id}/resourceGroups/${var.vwan-hub-resource-group}/providers/Microsoft.Network/virtualHubs/${var.vwan-hub-name}/routingIntent/hubRoutingIntent?api-version=2022-01-01"
61-
public_ip_resource_group = var.new-public-ip == "yes" ? azurerm_resource_group.managed-app-rg.name : "/subscriptions/${var.subscription_id}/resourceGroups/${split("/", var.existing-public-ip)[4]}"
59+
public_ip_resource_group = "/subscriptions/${var.subscription_id}/resourceGroups/${var.new-public-ip == "yes" ? azurerm_resource_group.managed-app-rg.name : var.existing-public-ip != "" ? split("/", var.existing-public-ip)[4] : ""}"
6260
}
6361

6462
//********************** Marketplace Terms & Solution Registration **************************//
@@ -234,14 +232,47 @@ resource "azapi_resource" "managed-app" {
234232

235233
//********************** Routing Intent **************************//
236234

235+
data "azapi_resource_list" "existing_routing_intent" {
236+
type = "Microsoft.Network/virtualHubs/routingIntent@2024-05-01"
237+
parent_id = data.azurerm_virtual_hub.vwan-hub.id
238+
response_export_values = {
239+
"values" = "value[].{routingPolicies: properties.routingPolicies}"
240+
}
237241

238-
data "external" "update-routing-intent" {
239-
count = length(local.routing-intent-policies) != 0 ? 1 : 0
240-
depends_on = [azapi_resource.managed-app]
241-
program = ["python", "../modules/add-routing-intent.py", "${local.req_url}", "${local.req_body}", "${local.access_token}"]
242242
}
243243

244-
output "api_request_result" {
245-
value = length(local.routing-intent-policies) != 0 ? data.external.update-routing-intent[0].result : {routing-intent: "not changed"}
244+
locals {
245+
routing_intent_exists = length([for intent in data.azapi_resource_list.existing_routing_intent.output.values : intent]) > 0
246+
existing_policies = try(data.azapi_resource_list.existing_routing_intent.output.values[0].routingPolicies, [])
247+
merged_policies = concat(
248+
local.routing-intent-policies,
249+
[for policy in local.existing_policies : policy if !contains([for p in local.routing-intent-policies : p.destinations[0]], policy.destinations[0])]
250+
)
251+
}
252+
253+
resource "azapi_resource" "routing_intent" {
254+
count = length(local.routing-intent-policies) != 0 && !local.routing_intent_exists ? 1 : 0
255+
depends_on = [azapi_resource.managed-app]
256+
type = "Microsoft.Network/virtualHubs/routingIntent@2024-05-01"
257+
name = "hubRoutingIntent"
258+
parent_id = data.azurerm_virtual_hub.vwan-hub.id
259+
260+
body = {
261+
properties = {
262+
routingPolicies = local.routing-intent-policies
263+
}
264+
}
246265
}
247266

267+
resource "azapi_update_resource" "update_routing_intent" {
268+
count = length(local.routing-intent-policies) != 0 && local.routing_intent_exists ? 1 : 0
269+
depends_on = [azapi_resource.managed-app]
270+
type = "Microsoft.Network/virtualHubs/routingIntent@2024-05-01"
271+
resource_id = "${data.azurerm_virtual_hub.vwan-hub.id}/routingIntent/hubRoutingIntent"
272+
273+
body = {
274+
properties = {
275+
routingPolicies = local.merged_policies
276+
}
277+
}
278+
}

terraform/azure/nva-into-existing-hub/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ terraform {
1616
}
1717

1818
provider "azapi" {
19+
subscription_id = var.subscription_id
20+
client_id = var.client_id
21+
client_secret = var.client_secret
22+
tenant_id = var.tenant_id
1923
}
2024

2125
provider "azurerm" {

terraform/azure/nva-into-new-vwan/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ please see the [CloudGuard Network for Azure Virtual WAN Deployment Guide](https
1515

1616
## Configurations
1717
- Install and configure Terraform to provision Azure resources: [Configure Terraform for Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure).
18-
- In order to configure hub routing-intent policies it is **required** to have Python and 'requests' library installed.
1918

2019
## Usage
2120
- Choose the preferred login method to Azure in order to deploy the solution:
@@ -166,6 +165,9 @@ please see the [CloudGuard Network for Azure Virtual WAN Deployment Guide](https
166165
existing-public-ip = ""
167166
new-public-ip = "yes"
168167

168+
## Known limitations
169+
1. 'terraform destroy' doesn't work if routing-intent is configured. To destroy the deployment, the routing-intent should be deleted manually first.
170+
169171
## Revision History
170172
In order to check the template version refer to the [sk116585](https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk116585)
171173

terraform/azure/nva-into-new-vwan/main.tf

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ locals {
6565
"nextHop": "/subscriptions/${var.subscription_id}/resourcegroups/${var.nva-rg-name}/providers/Microsoft.Network/networkVirtualAppliances/${var.nva-name}"
6666
}
6767
routing-intent-policies = var.routing-intent-internet-traffic == "yes" ? (var.routing-intent-private-traffic == "yes" ? tolist([local.routing_intent-internet-policy, local.routing_intent-private-policy]) : tolist([local.routing_intent-internet-policy])) : (var.routing-intent-private-traffic == "yes" ? tolist([local.routing_intent-private-policy]) : [])
68-
req_body = jsonencode({"properties": {"routingPolicies": local.routing-intent-policies}})
69-
req_url = "https://management.azure.com/subscriptions/${var.subscription_id}/resourceGroups/${azurerm_resource_group.managed-app-rg.name}/providers/Microsoft.Network/virtualHubs/${var.vwan-hub-name}/routingIntent/hubRoutingIntent?api-version=2022-01-01"
70-
public_ip_resource_group = var.new-public-ip == "yes" ? azurerm_resource_group.managed-app-rg.id : "/subscriptions/${var.subscription_id}/resourceGroups/${split("/", var.existing-public-ip)[4]}"
71-
68+
public_ip_resource_group = "/subscriptions/${var.subscription_id}/resourceGroups/${var.new-public-ip == "yes" ? azurerm_resource_group.managed-app-rg.name : var.existing-public-ip != "" ? split("/", var.existing-public-ip)[4] : ""}"
7269
}
7370

7471
//********************** Marketplace Terms & Solution Registration **************************//
@@ -245,12 +242,17 @@ resource "azapi_resource" "managed-app" {
245242

246243

247244
//********************** Routing Intent **************************//
248-
data "external" "update-routing-intent" {
245+
246+
resource "azapi_resource" "routing_intent" {
249247
count = length(local.routing-intent-policies) != 0 ? 1 : 0
250248
depends_on = [azapi_resource.managed-app]
251-
program = ["python", "../modules/add-routing-intent.py", "${local.req_url}", "${local.req_body}", "${local.access_token}"]
252-
}
249+
type = "Microsoft.Network/virtualHubs/routingIntent@2024-05-01"
250+
name = "hubRoutingIntent"
251+
parent_id = azurerm_virtual_hub.vwan-hub.id
253252

254-
output "api_request_result" {
255-
value = length(local.routing-intent-policies) != 0 ? data.external.update-routing-intent[0].result : {routing-intent: "not changed"}
253+
body = {
254+
properties = {
255+
routingPolicies = local.routing-intent-policies
256+
}
257+
}
256258
}

terraform/azure/nva-into-new-vwan/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ terraform {
1616
}
1717

1818
provider "azapi" {
19+
subscription_id = var.subscription_id
20+
client_id = var.client_id
21+
client_secret = var.client_secret
22+
tenant_id = var.tenant_id
1923
}
2024

2125
provider "azurerm" {

0 commit comments

Comments
 (0)