Skip to content

Commit

Permalink
SDAF calculate subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
lpalovsky committed Sep 26, 2024
1 parent 89c9971 commit 9509e6d
Show file tree
Hide file tree
Showing 13 changed files with 1,006 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ location = "%PUBLIC_CLOUD_REGION%"
network_logical_name = "%SDAF_VNET_CODE%"

# The name is optional - it can be used to override the default naming
#network_name = ""
network_name = "%SDAF_SUT_VNET_NAME%"

# network_arm_id is an optional parameter that if provided specifies Azure resource identifier for the existing Virtual Network
#network_arm_id = ""

# network_address_space is a mandatory parameter when an existing Virtual network is not used
network_address_space = "10.10.0.0/16"
network_address_space = "%NETWORK_ADDRESS_SPACE%"

# use_private_endpoint is a boolean flag controlling if the key vaults and storage accounts have private endpoints
use_private_endpoint = false
Expand Down Expand Up @@ -112,7 +112,7 @@ nat_gateway_public_ip_tags = {
#admin_subnet_name = ""

# admin_subnet_address_prefix is a mandatory parameter if the subnets are not defined in the workload or if existing subnets are not used
admin_subnet_address_prefix = "10.10.0.0/19"
admin_subnet_address_prefix = "%ADMIN_SUBNET_ADDRESS_PREFIX%"

# admin_subnet_arm_id is an optional parameter that if provided specifies Azure resource identifier for the existing subnet to use
#admin_subnet_arm_id = ""
Expand All @@ -134,7 +134,7 @@ admin_subnet_address_prefix = "10.10.0.0/19"
#db_subnet_name = ""

# db_subnet_address_prefix is a mandatory parameter if the subnets are not defined in the workload or if existing subnets are not used
db_subnet_address_prefix = "10.10.96.0/19"
db_subnet_address_prefix = "%DB_SUBNET_ADDRESS_PREFIX%"

# db_subnet_arm_id is an optional parameter that if provided specifies Azure resource identifier for the existing subnet to use
#db_subnet_arm_id = ""
Expand All @@ -156,7 +156,7 @@ db_subnet_address_prefix = "10.10.96.0/19"
#app_subnet_name = ""

# app_subnet_address_prefix is a mandatory parameter if the subnets are not defined in the workload or if existing subnets are not used
app_subnet_address_prefix = "10.10.32.0/19"
app_subnet_address_prefix = "%APP_SUBNET_ADDRESS_PREFIX%"

# app_subnet_arm_id is an optional parameter that if provided specifies Azure resource identifier for the existing subnet to use
#app_subnet_arm_id = ""
Expand All @@ -178,7 +178,7 @@ app_subnet_address_prefix = "10.10.32.0/19"
#web_subnet_name = ""

# web_subnet_address_prefix is a mandatory parameter if the subnets are not defined in the workload or if existing subnets are not used
web_subnet_address_prefix = "10.10.128.0/19"
web_subnet_address_prefix = "%WEB_SUBNET_ADDRESS_PREFIX%"

# web_subnet_arm_id is an optional parameter that if provided specifies Azure resource identifier for the existing subnet to use
#web_subnet_arm_id = ""
Expand Down
171 changes: 169 additions & 2 deletions lib/sles4sap/azure_cli.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use testapi;
use Carp qw(croak);
use Exporter qw(import);
use Mojo::JSON qw(decode_json);

use qesapdeployment qw(qesap_az_validate_uuid_pattern);

=head1 SYNOPSIS
Expand Down Expand Up @@ -50,6 +50,10 @@ our @EXPORT = qw(
az_ipconfig_update
az_ipconfig_pool_add
az_storage_account_create
az_storage_blob_list
az_storage_blob_upload
az_storage_blob_update
az_storage_blob_lease_acquire
az_network_peering_create
az_network_peering_list
az_network_peering_delete
Expand Down Expand Up @@ -1243,7 +1247,7 @@ sub az_network_peering_create {
my $res = az_network_peering_list(
resource_group => 'openqa-rg',
vnet => 'openqa-this-vnet')
vnet => 'openqa-this-vnet' [, query=>'[].name'])
Return HASH representing existing net peering
Expand Down Expand Up @@ -1374,3 +1378,166 @@ sub az_resource_delete {

assert_script_run(join(' ', @az_command), timeout => $args{timeout});
}

=head2 az_storage_blob_upload
az_storage_blob_upload(
container_name=>'somecontainer',
storage_account_name=>'storageaccount',
file=>'somefilename' [, timeout=>42]);
Uploads file to a storage container.
=over 4
=item B<container_name> Existing storage container name.
=item B<storage_account_name> Storage account name.
=item B<file> File to upload.
=item B<timeout> Timeout for az command.
=back
=cut

sub az_storage_blob_upload {
my (%args) = @_;
foreach ('container_name', 'storage_account_name', 'file') {
croak "Missing mandatory argument: '$_'" unless $args{$_};
}
$args{timeout} //= '90';

my $az_cmd = join(' ',
'az storage blob upload',
'--only-show-errors',
"--container-name $args{container_name}",
"--account-name $args{storage_account_name}",
"--file $args{file}"
);
assert_script_run(join(' ', $az_cmd), timeout => $args{timeout});
}

=head2 az_storage_blob_lease_acquire
az_storage_blob_lease_acquire(
container_name=>'somecontainer',
storage_account_name=>'storageaccount',
blob_name => 'somefilename' [, lease_duration=>'42']
);
Acquire a lease for a storage blob. Function returns UUID which is then required to modify the file and gives the
UUID owner exclusive rights.
Optionally B<lease_duration> can be defined to limit this file lock up to 60s instead of infinity.
=over 4
=item B<container_name> Existing storage container name.
=item B<storage_account_name> Storage account name.
=item B<blob_name> Blob name to acquire lease for.
=item B<lease_duration> Lease duration between 15-60s. Default: infinite
=back
=cut

sub az_storage_blob_lease_acquire {
my (%args) = @_;
foreach ('container_name', 'storage_account_name', 'blob_name') {
croak "Missing mandatory argument: '$_'" unless $args{$_};
}
$args{lease_duration} //= '-1'; # -1 = infinite lease

my $az_cmd = join(' ',
'az storage blob lease acquire',
'--only-show-errors',
"--container-name $args{container_name}",
"--account-name $args{storage_account_name}",
"--blob-name $args{blob_name}",
"--lease-duration $args{lease_duration}",
'--output tsv' # Json output won't work here.
# If it is not possible to acquire lease command will return a message which is not in json format.
# decode_json() would cause function to fail instead of just returning
);

my $lease_id = script_output($az_cmd, proceed_on_failure => 1);
return ($lease_id) if qesap_az_validate_uuid_pattern($lease_id); # Return only valid output.
}

=head2 az_storage_blob_list
az_storage_blob_list(
container_name=>'somecontainer',
storage_account_name=>'storageaccount' [, query=>'[].name']
);
List information about storage blob(s) specified by B<storage_account_name>, B<container_name> and B<query>.
=over 3
=item B<container_name> Existing storage container name.
=item B<storage_account_name> Storage account name.
=item B<query> Query in jmespath format
=back
=cut

sub az_storage_blob_list {
my (%args) = @_;
foreach ('container_name', 'storage_account_name') {
croak "Missing mandatory argument: '$_'" unless $args{$_};
}
$args{query} //= '[].name';

my $az_cmd = join(' ',
'az storage blob list',
'--only-show-errors',
"--container-name $args{container_name}",
"--account-name $args{storage_account_name}",
"--query \"$args{query}\"",
'--output json'
);

return decode_json(script_output($az_cmd));
}

=head2 az_storage_blob_update
az_storage_blob_update(container_name=>'container', account_name=>'stuff', name='blobby' [, lease_id=42]);
Update properties of storage blob. Returns az cli command exit code.
=over 4
=item B<container_name> Existing resource group name.
=item B<account_name> Name of the resource to delete
=item B<name> Blob name
=item B<lease_id> Lease id in case there is a lease put on a file
=back
=cut

sub az_storage_blob_update {
my (%args) = @_;
foreach ('container_name', 'account_name', 'name') {
croak "Missing mandatory argument: '$_'" unless $args{$_};
}

my @az_cmd = ('az storage blob update',
'--only-show-errors',
'--container-name', $args{container_name},
'--account-name', $args{account_name},
'--name', $args{name},
'--output json'
);
push(@az_cmd, "--lease-id $args{lease_id}") if $args{lease_id};

return script_run(join(' ', @az_cmd));
}
121 changes: 121 additions & 0 deletions lib/sles4sap/sap_deployment_automation_framework/configure_tfvars.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# SUSE's openQA tests
#
# Copyright SUSE LLC
# SPDX-License-Identifier: FSFAP
# Maintainer: QE-SAP <[email protected]>

package sles4sap::sap_deployment_automation_framework::configure_tfvars;

use strict;
use warnings;
use testapi;
use Exporter qw(import);
use Carp qw(croak);
use utils qw(file_content_replace);
use sles4sap::azure_cli qw(az_storage_blob_list);
use sles4sap::sap_deployment_automation_framework::deployment qw(get_os_variable);
use sles4sap::sap_deployment_automation_framework::deployment_connector qw(find_deployment_id);

=head1 SYNOPSIS
Library with common functions for Microsoft SDAF deployment automation that help with preparation of tfvars file.
=cut

our @EXPORT = qw(
prepare_tfvars_file
);

=head2 prepare_tfvars_file
prepare_tfvars_file(deployment_type=>$deployment_type);
=over 1
=item B<$deployment_type>: Type of the deployment (workload_zone, sap_system, library... etc)
=back
Downloads tfvars template files from openQA data dir and places them into correct place within SDAF repo structure.
Returns full path of the tfvars file.
=cut

sub prepare_tfvars_file {
my (%args) = @_;
croak 'Deployment type not specified' unless $args{deployment_type};
my %tfvars_os_variable = (
deployer => 'deployer_parameter_file',
sap_system => 'sap_system_parameter_file',
workload_zone => 'workload_zone_parameter_file',
library => 'library_parameter_file'
);
croak "Unknown deployment type: $args{deployment_type}" unless $tfvars_os_variable{$args{deployment_type}};

my %tfvars_template_url = (
deployer => data_url('sles4sap/sap_deployment_automation_framework/DEPLOYER.tfvars'),
sap_system => data_url('sles4sap/sap_deployment_automation_framework/SAP_SYSTEM.tfvars'),
workload_zone => data_url('sles4sap/sap_deployment_automation_framework/WORKLOAD_ZONE.tfvars'),
library => data_url('sles4sap/sap_deployment_automation_framework/LIBRARY.tfvars')
);

# replace default vnet name with shorter one to avoid naming restrictions
set_workload_vnet_name();

my $tfvars_file = get_os_variable($tfvars_os_variable{$args{deployment_type}});
my $retrieve_tfvars_cmd = join(' ', 'curl', '-v', '-fL', $tfvars_template_url{$args{deployment_type}}, '-o', $tfvars_file);

assert_script_run($retrieve_tfvars_cmd);
assert_script_run("test -f $tfvars_file");
replace_tfvars_variables($tfvars_file);
upload_logs($tfvars_file, log_name => "$args{deployment_type}.tfvars.txt");
return $tfvars_file;
}

=head2 replace_tfvars_variables
replace_tfvars_variables();
=over 1
=item B<$deployment_type>: Type of the deployment (workload_zone, sap_system, library... etc)
=back
Replaces placeholder pattern B<%OPENQA_VARIABLE%> with corresponding OpenQA variable value.
If OpenQA variable is not set, placeholder is replaced with empty value.
=cut

sub replace_tfvars_variables {
my ($tfvars_file) = @_;
croak 'Variable "$tfvars_file" undefined' unless defined($tfvars_file);
my @variables = split("\n", script_output("grep -oP \'(\?<=%)[A-Z_]+(?=%)\' $tfvars_file"));
my %to_replace = map { '%' . $_ . '%' => get_var($_, '') } @variables;
file_content_replace($tfvars_file, %to_replace);
}

=head2 set_workload_vnet_name
set_workload_vnet_name([job_id=>$job_id]);
=over 1
=item B<$job_id>: Specify job id to be used. Default: current deployment job ID
=back
Returns VNET name used for workload zone and sap systems resources. VNET name must be unique for each landscape,
therefore it contains test ID as an identifier.
=cut

sub set_workload_vnet_name {
my (%args) = @_;
$args{job_id} //= find_deployment_id();
die('no deployment ID found') unless $args{job_id};
# Try to keep vnet name as short as possible. Later this is used in the name for the peering in a format:
# deployer-vnet_to_workload-vnet
# if it is too long you might hit name length limit and test ID gets clipped.
set_var('SDAF_SUT_VNET_NAME', 'OpenQA-' . $args{job_id});
}
Loading

0 comments on commit 9509e6d

Please sign in to comment.