From 1cbd947852280ec6dd57da5c89710a684808b7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Dost=C3=A1l?= Date: Mon, 27 Nov 2023 13:02:35 +0100 Subject: [PATCH] Public Cloud: Reuse resources in Google and Azure --- data/publiccloud/terraform/azure.tf | 43 +++----------------- data/publiccloud/terraform/azure_nfstest.tf | 44 +++------------------ data/publiccloud/terraform/gce.tf | 7 ++-- lib/publiccloud/provider.pm | 2 + 4 files changed, 16 insertions(+), 80 deletions(-) diff --git a/data/publiccloud/terraform/azure.tf b/data/publiccloud/terraform/azure.tf index 8d5fc853fd52..a2f410efce4e 100644 --- a/data/publiccloud/terraform/azure.tf +++ b/data/publiccloud/terraform/azure.tf @@ -80,6 +80,10 @@ variable "vm_create_timeout" { default = "20m" } +variable "subnet_id" { + default = "" +} + resource "random_id" "service" { count = var.instance_count keepers = { @@ -100,20 +104,6 @@ resource "azurerm_resource_group" "openqa-group" { }, var.tags) } -resource "azurerm_virtual_network" "openqa-network" { - name = "${azurerm_resource_group.openqa-group.name}-vnet" - address_space = ["10.0.0.0/16"] - location = var.region - resource_group_name = azurerm_resource_group.openqa-group.name -} - -resource "azurerm_subnet" "openqa-subnet" { - name = "${azurerm_resource_group.openqa-group.name}-subnet" - resource_group_name = azurerm_resource_group.openqa-group.name - virtual_network_name = azurerm_virtual_network.openqa-network.name - address_prefixes = ["10.0.1.0/24"] -} - resource "azurerm_public_ip" "openqa-publicip" { name = "${var.name}-${element(random_id.service.*.hex, count.index)}-public-ip" location = var.region @@ -122,29 +112,6 @@ resource "azurerm_public_ip" "openqa-publicip" { count = var.instance_count } -resource "azurerm_network_security_group" "openqa-nsg" { - name = "${azurerm_resource_group.openqa-group.name}-nsg" - location = var.region - resource_group_name = azurerm_resource_group.openqa-group.name - - security_rule { - name = "SSH" - priority = 1001 - direction = "Inbound" - access = "Allow" - protocol = "Tcp" - source_port_range = "*" - destination_port_range = "22" - source_address_prefix = "*" - destination_address_prefix = "*" - } -} - -resource "azurerm_subnet_network_security_group_association" "openqa-net-sec-association" { - subnet_id = azurerm_subnet.openqa-subnet.id - network_security_group_id = azurerm_network_security_group.openqa-nsg.id -} - resource "azurerm_network_interface" "openqa-nic" { name = "${var.name}-${element(random_id.service.*.hex, count.index)}-nic" location = var.region @@ -153,7 +120,7 @@ resource "azurerm_network_interface" "openqa-nic" { ip_configuration { name = "${element(random_id.service.*.hex, count.index)}-nic-config" - subnet_id = azurerm_subnet.openqa-subnet.id + subnet_id = "${var.subnet_id}" private_ip_address_allocation = "Dynamic" public_ip_address_id = element(azurerm_public_ip.openqa-publicip.*.id, count.index) } diff --git a/data/publiccloud/terraform/azure_nfstest.tf b/data/publiccloud/terraform/azure_nfstest.tf index 840409d48b2b..518d8d3ea76d 100644 --- a/data/publiccloud/terraform/azure_nfstest.tf +++ b/data/publiccloud/terraform/azure_nfstest.tf @@ -57,6 +57,10 @@ variable "vm_create_timeout" { default = "20m" } +variable "subnet_id" { + default = "" +} + ## ---- data ---------------------------------------------------------------- ## // IP address of the client @@ -88,21 +92,6 @@ resource "azurerm_resource_group" "openqa-group" { ## virtual network -resource "azurerm_virtual_network" "openqa-network" { - name = "${azurerm_resource_group.openqa-group.name}-vnet" - address_space = ["192.168.0.0/16"] - location = var.region - resource_group_name = azurerm_resource_group.openqa-group.name -} - -resource "azurerm_subnet" "openqa-subnet" { - name = "${azurerm_resource_group.openqa-group.name}-subnet" - resource_group_name = azurerm_resource_group.openqa-group.name - virtual_network_name = azurerm_virtual_network.openqa-network.name - address_prefixes = ["192.168.1.0/24"] - service_endpoints = ["Microsoft.Storage"] -} - resource "azurerm_public_ip" "openqa-publicip" { name = "${azurerm_resource_group.openqa-group.name}-public-ip" location = var.region @@ -111,29 +100,6 @@ resource "azurerm_public_ip" "openqa-publicip" { count = var.instance_count } -resource "azurerm_network_security_group" "openqa-nsg" { - name = "${azurerm_resource_group.openqa-group.name}-nsg" - location = var.region - resource_group_name = azurerm_resource_group.openqa-group.name - - security_rule { - name = "SSH" - priority = 1001 - direction = "Inbound" - access = "Allow" - protocol = "Tcp" - source_port_range = "*" - destination_port_range = "22" - source_address_prefix = "*" - destination_address_prefix = "*" - } -} - -resource "azurerm_subnet_network_security_group_association" "openqa-net-sec-association" { - subnet_id = azurerm_subnet.openqa-subnet.id - network_security_group_id = azurerm_network_security_group.openqa-nsg.id -} - resource "azurerm_network_interface" "openqa-nic" { name = "${azurerm_resource_group.openqa-group.name}-nic" location = var.region @@ -142,7 +108,7 @@ resource "azurerm_network_interface" "openqa-nic" { ip_configuration { name = "${element(random_id.service.*.hex, count.index)}-nic-config" - subnet_id = azurerm_subnet.openqa-subnet.id + subnet_id = "${var.subnet_id}" private_ip_address_allocation = "Dynamic" public_ip_address_id = element(azurerm_public_ip.openqa-publicip.*.id, count.index) } diff --git a/data/publiccloud/terraform/gce.tf b/data/publiccloud/terraform/gce.tf index 74c8de73da1b..a8fc0e881b50 100644 --- a/data/publiccloud/terraform/gce.tf +++ b/data/publiccloud/terraform/gce.tf @@ -131,9 +131,10 @@ resource "google_compute_instance" "openqa" { }, var.tags) network_interface { - network = "default" - access_config { - } + network = "tf-network" + subnetwork = "tf-subnetwork" + access_config {} + stack_type = "IPV4_ONLY" } service_account { diff --git a/lib/publiccloud/provider.pm b/lib/publiccloud/provider.pm index 53f0c9abbbab..e7a2917632dd 100644 --- a/lib/publiccloud/provider.pm +++ b/lib/publiccloud/provider.pm @@ -478,6 +478,8 @@ sub terraform_apply { $cmd .= "-var 'subnet_id=$subnet_id' " if ($subnet_id); $cmd .= "-var 'ipv6_address_count=$ipv6_address_count' " if ($ipv6_address_count); } elsif (is_azure) { + my $subnet_id = script_output("az network vnet subnet list -g 'tf-" . $self->provider_client->region . "-rg' --vnet-name 'tf-network' --query '[0].id' --output 'tsv'"); + $cmd .= "-var 'subnet_id=$subnet_id' " if ($subnet_id); # Note: Only the default Azure terraform profiles contains the 'storage-account' variable my $storage_account = get_var('PUBLIC_CLOUD_STORAGE_ACCOUNT'); $cmd .= "-var 'storage-account=$storage_account' " if ($storage_account);