Skip to content

Commit

Permalink
Ipaddr2 network peering az functions (#20946)
Browse files Browse the repository at this point in the history
Move get_vnet from qesap to az library. Rename both create and clean
function name. Add argument documentation. Add UT.
  • Loading branch information
mpagot authored Jan 14, 2025
1 parent 682d637 commit 6bd780f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 39 deletions.
30 changes: 16 additions & 14 deletions lib/sles4sap/ipaddr2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package sles4sap::ipaddr2;
use strict;
use warnings FATAL => 'all';
use testapi;
use qesapdeployment qw (qesap_az_calculate_address_range qesap_az_vnet_peering qesap_az_vnet_peering_delete qesap_az_clean_old_peerings qesap_az_get_vnet);
use qesapdeployment qw (qesap_az_calculate_address_range qesap_az_vnet_peering qesap_az_vnet_peering_delete qesap_az_clean_old_peerings);
use Carp qw( croak );
use Exporter qw(import);
use Mojo::JSON qw( decode_json );
Expand Down Expand Up @@ -49,12 +49,12 @@ our @EXPORT = qw(
ipaddr2_test_other_vm
ipaddr2_cloudinit_create
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_azure_resource_group
ipaddr2_ssh_internal
ipaddr2_bastion_ssh_addr
ipaddr2_get_internal_vm_private_ip
ipaddr2_network_peering
ipaddr2_network_peering_create
ipaddr2_network_peering_clean
ipaddr2_patch_system
ipaddr2_register_addons
ipaddr2_registration
Expand Down Expand Up @@ -1952,47 +1952,49 @@ sub ipaddr2_test_other_vm {
}
}

=head2 ipaddr2_clean_network_peering
=head2 ipaddr2_network_peering_clean
ipaddr2_clean_network_peering(ibsm_rg => );
ipaddr2_network_peering_clean(ibsm_rg => );
Cleanup the network peering if needed.
=over
=item B<ibsm_rg> - Resource group of the IBSm
=back
=cut

sub ipaddr2_clean_network_peering {
sub ipaddr2_network_peering_clean {
my (%args) = @_;
croak 'Missing mandatory argument < ibsm_rg >' unless $args{'ibsm_rg'};
qesap_az_vnet_peering_delete(source_group => ipaddr2_azure_resource_group(), target_group => $args{'ibsm_rg'});
}

=head2 ipaddr2_network_peering
=head2 ipaddr2_network_peering_create
ipaddr2_network_peering(ibsm_rg => );
ipaddr2_network_peering_create(ibsm_rg => 'IBSmMyRg');
Create network peering
=over
=item B<ibsm_rg> - Resource group of the IBSm
=back
=cut

sub ipaddr2_network_peering {
sub ipaddr2_network_peering_create {
my (%args) = @_;
croak 'Missing mandatory argument < ibsm_rg >' unless $args{ibsm_rg};
my $ibs_mirror_resource_group = $args{ibsm_rg};

# remove the older peering
my $vnet_name = qesap_az_get_vnet($ibs_mirror_resource_group);
qesap_az_clean_old_peerings(rg => $ibs_mirror_resource_group, vnet => $vnet_name);
my $vnet_name = az_network_vnet_get(resource_group => $args{ibsm_rg}, query => "[0].name");
qesap_az_clean_old_peerings(rg => $args{ibsm_rg}, vnet => $vnet_name);

my $rg = ipaddr2_azure_resource_group();
qesap_az_vnet_peering(source_group => $rg, target_group => $ibs_mirror_resource_group);
qesap_az_vnet_peering(source_group => ipaddr2_azure_resource_group(), target_group => $args{ibsm_rg});
}

=head2 ipaddr2_add_server_repos_to_hosts
Expand Down
19 changes: 16 additions & 3 deletions t/22_ipaddr2.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use List::Util qw(any none all);
use testapi qw(set_var);

use sles4sap::ipaddr2;
use qesapdeployment;

subtest '[ipaddr2_infra_deploy]' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
Expand Down Expand Up @@ -956,7 +955,7 @@ subtest '[get_private_ip_range]' => sub {
set_var('WORKER_ID', undef);
};

subtest 'ipaddr2_clean_network_peering' => sub {
subtest 'ipaddr2_network_peering_clean' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
$ipaddr2->redefine(ipaddr2_azure_resource_group => sub { return 'test'; });

Expand All @@ -967,8 +966,22 @@ subtest 'ipaddr2_clean_network_peering' => sub {
$peering_string = "source_group is $args{source_group}, target_group is $args{target_group}";
return;
});
ipaddr2_clean_network_peering(ibsm_rg => 'ibsm');
ipaddr2_network_peering_clean(ibsm_rg => 'ibsm');
is $peering_string, "source_group is test, target_group is ibsm", "Clean network peering successfully";
};

subtest '[ipaddr2_network_peering_create]' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
$ipaddr2->redefine(az_network_vnet_get => sub { return 'DavidCuartielles'; });
$ipaddr2->redefine(qesap_az_clean_old_peerings => sub { return; });
$ipaddr2->redefine(ipaddr2_azure_resource_group => sub { return 'Volta'; });

my $create_peering = 0;
$ipaddr2->redefine(qesap_az_vnet_peering => sub { $create_peering = 1; });

ipaddr2_network_peering_create(ibsm_rg => 'MassimoBanzi');

ok $create_peering, "qesap_az_vnet_peering called";
};

done_testing;
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/cluster_create.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_infra_destroy
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand All @@ -41,7 +41,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/configure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_registeration_check
ipaddr2_registeration_set
ipaddr2_refresh_repo
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand Down Expand Up @@ -87,7 +87,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
7 changes: 3 additions & 4 deletions tests/sles4sap/ipaddr2/destroy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_infra_destroy
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand All @@ -25,7 +25,7 @@ sub run {
select_serial_terminal;

if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
}
Expand All @@ -39,9 +39,8 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs();
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_clean_network_peering();
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/sles4sap/ipaddr2/network_peering.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use serial_terminal qw( select_serial_terminal );
use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
ipaddr2_infra_destroy
ipaddr2_network_peering
ipaddr2_network_peering_create
ipaddr2_add_server_repos_to_hosts
);

Expand All @@ -24,7 +24,7 @@ sub run {
select_serial_terminal;

# Create network peering
ipaddr2_network_peering(ibsm_rg => get_required_var('IBSM_RG'));
ipaddr2_network_peering_create(ibsm_rg => get_required_var('IBSM_RG'));

ipaddr2_add_server_repos_to_hosts(ibsm_ip => get_required_var('IBSM_IP'), incident_repo => get_var('INCIDENT_REPO', ''));
}
Expand All @@ -38,7 +38,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/patch_system.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serial_terminal qw( select_serial_terminal );
use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
ipaddr2_infra_destroy
ipaddr2_patch_system
);
Expand All @@ -34,7 +34,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/registration.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use publiccloud::utils;
use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
ipaddr2_infra_destroy
ipaddr2_registration
ipaddr2_register_addons
Expand All @@ -39,7 +39,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/sanity_cluster.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_deployment_logs
ipaddr2_infra_destroy
ipaddr2_cloudinit_logs
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand All @@ -44,7 +44,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/sanity_os.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_infra_destroy
ipaddr2_cloudinit_logs
ipaddr2_os_sanity
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand Down Expand Up @@ -46,7 +46,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down
4 changes: 2 additions & 2 deletions tests/sles4sap/ipaddr2/test_move_resource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sles4sap::ipaddr2 qw(
ipaddr2_test_master_vm
ipaddr2_test_other_vm
ipaddr2_wait_for_takeover
ipaddr2_clean_network_peering
ipaddr2_network_peering_clean
);

sub run {
Expand Down Expand Up @@ -92,7 +92,7 @@ sub post_fail_hook {
ipaddr2_deployment_logs() if check_var('IPADDR2_DIAGNOSTIC', 1);
ipaddr2_cloudinit_logs() unless check_var('IPADDR2_CLOUDINIT', 0);
if (my $ibsm_rg = get_var('IBSM_RG')) {
ipaddr2_clean_network_peering(ibsm_rg => $ibsm_rg);
ipaddr2_network_peering_clean(ibsm_rg => $ibsm_rg);
}
ipaddr2_infra_destroy();
$self->SUPER::post_fail_hook;
Expand Down

0 comments on commit 6bd780f

Please sign in to comment.