Skip to content

Commit

Permalink
Public Cloud: Reuse resources in Google and Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
pdostal committed Nov 27, 2023
1 parent 8c52a98 commit b79f7f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 80 deletions.
43 changes: 5 additions & 38 deletions data/publiccloud/terraform/azure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ variable "vm_create_timeout" {
default = "20m"
}

variable "subnet_id" {
default = ""
}

resource "random_id" "service" {
count = var.instance_count
keepers = {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
44 changes: 5 additions & 39 deletions data/publiccloud/terraform/azure_nfstest.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ variable "vm_create_timeout" {
default = "20m"
}

variable "subnet_id" {
default = ""
}

## ---- data ---------------------------------------------------------------- ##

// IP address of the client
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions data/publiccloud/terraform/gce.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/publiccloud/provider.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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-resource-group' --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);
Expand Down

0 comments on commit b79f7f8

Please sign in to comment.