diff --git a/snippets/curl/activate_migration.sh b/snippets/curl/activate_migration.sh new file mode 100755 index 0000000..87f4048 --- /dev/null +++ b/snippets/curl/activate_migration.sh @@ -0,0 +1,123 @@ +#!/bin/sh + +#####################n##################################################### + +# This script demonstrates how to activate the new CA. +# To activate the CA migration, a user needs to have proper permissions. + +# This script requires jq command-line JSON parser +# if your system does not have jq installed, this will not work. +# jq can be downloaded from here: https://github.com/stedolan/jq/releases + +########################################################################### + +port=1556 +master_server="" +login_username="" +login_password="" +login_domainname="" +login_domaintype="" +force=0 +reason="" + +showHelp() +{ + echo "" + echo "Invalid command parameters" + echo "Usage:" + echo "./activate_migration.sh -nbmaster -login_username -login_password -login_domainname -login_domaintype [-reason | -r ] [-force | -f]" + echo "-nbmaster : Name of the NetBackup master server" + echo "-login_username : User name of the user performing action" + echo "-login_password : Password of the user performing action" + echo "-login_domainname : Domain name of the user performing action" + echo "-login_domaintype : Domain type of the user performing action" + echo "-reason | -r : Reason for activation of the new CA" + echo "-force | -f : Forcefully activate the new CA" + echo "" + exit 1 +} + +parseArguments() +{ + if [ $# -lt 10 ] && [ $# -gt 14 ]; then + showHelp + fi + + while [ "$1" != "" ]; do + case $1 in + -nbmaster) + master_server=$2 + ;; + -login_username) + login_username=$2 + ;; + -login_password) + login_password=$2 + ;; + -login_domainname) + login_domainname=$2 + ;; + -login_domaintype) + login_domaintype=$2 + ;; + -force|-f) + force=1 + ;; + -reason|-r) + reason=$2 + ;; + *) + showHelp + ;; + esac + shift 2 + done + + if [ -z "$master_server" ] || [ -z "$login_username" ] || [ -z "$login_password" ] || [ -z "$login_domainname" ] || [ -z "$login_domaintype" ]; then + showHelp + fi + + if [ "${login_domaintype^^}" = "WINDOWS" ] || [ "${login_domaintype^^}" = "NT" ]; then + login_domaintype="nt" + fi +} + +###############main############ + +parseArguments "$@" + +basepath="https://$master_server:$port/netbackup" +content_header='content-type:application/json' + +##############login############# + +uri="$basepath/login" + +data=$(jq --arg name $login_username --arg pass $login_password --arg dname $login_domainname --arg dtype $login_domaintype \ + --null-input '{userName: $name, password: $pass, domainName: $dname, domainType: $dtype}') + +jwt=$(curl --silent -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token') + +##############jobs############## +auth_header="authorization:$jwt" +content_header='content-type:application/vnd.netbackup+json;version=4.0' +uri="$basepath/security/certificate-authorities/activate" + +# Construct request body +request_body="{" +request_body="${request_body}\"data\": {" +request_body="${request_body}\"type\": \"nbcaMigrationActivateRequest\"," +request_body="${request_body}\"attributes\": {" +if [ $force == 1 ]; then + request_body="${request_body}\"force\" : \"true\"" +fi +request_body="${request_body}}}}" + +if [ -z $reason ]; then + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -d "$request_body" | jq +else + audit_reason="X-NetBackup-Audit-Reason:$reason"; + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -H "$audit_reason" -d "$request_body" | jq +fi + +exit 0 diff --git a/snippets/curl/complete_migration.sh b/snippets/curl/complete_migration.sh new file mode 100755 index 0000000..21fdefc --- /dev/null +++ b/snippets/curl/complete_migration.sh @@ -0,0 +1,122 @@ +#!/bin/sh + +#####################n##################################################### + +# This script demonstrates how to complete the CA migration. + +# This script requires jq command-line JSON parser +# if your system does not have jq installed, this will not work. +# jq can be downloaded from here: https://github.com/stedolan/jq/releases + +########################################################################### + +port=1556 +master_server="" +login_username="" +login_password="" +login_domainname="" +login_domaintype="" +reason="" +force=0 + +showHelp() +{ + echo "" + echo "Invalid command parameters" + echo "Usage:" + echo "./complete_migration.sh -nbmaster -login_username -login_password -login_domainname -login_domaintype [-reason | -r ] [-force | -f]" + echo "-nbmaster : Name of the NetBackup master server" + echo "-login_username : User name of the user performing action" + echo "-login_password : Password of the user performing action" + echo "-login_domainname : Domain name of the user performing action" + echo "-login_domaintype : Domain type of the user performing action" + echo "-reason | -r : Reason for completing the CA migration" + echo "-force | -f : Forcefully complete the CA migration" + echo "" + exit 1 +} + +parseArguments() +{ + if [ $# -ne 10 ] && [ $# -ne 11 ] && [ $# -ne 12 ] && [ $# -ne 13 ]; then + showHelp + fi + + while [ "$1" != "" ]; do + case $1 in + -nbmaster) + master_server=$2 + ;; + -login_username) + login_username=$2 + ;; + -login_password) + login_password=$2 + ;; + -login_domainname) + login_domainname=$2 + ;; + -login_domaintype) + login_domaintype=$2 + ;; + -force|-f) + force=1 + ;; + -reason|-r) + reason=$2 + ;; + *) + showHelp + ;; + esac + shift 2 + done + + if [ -z "$master_server" ] || [ -z "$login_username" ] || [ -z "$login_password" ] || [ -z "$login_domainname" ] || [ -z "$login_domaintype" ]; then + showHelp + fi + + if [ "${login_domaintype^^}" = "WINDOWS" ] || [ "${login_domaintype^^}" = "NT" ]; then + login_domaintype="nt" + fi +} + +###############main############ + +parseArguments "$@" + +basepath="https://$master_server:$port/netbackup" +content_header='content-type:application/json' + +##############login############# + +uri="$basepath/login" + +data=$(jq --arg name $login_username --arg pass $login_password --arg dname $login_domainname --arg dtype $login_domaintype \ + --null-input '{userName: $name, password: $pass, domainName: $dname, domainType: $dtype}') + +jwt=$(curl --silent -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token') + +##############jobs############## +auth_header="authorization:$jwt" +content_header='content-type:application/vnd.netbackup+json;version=4.0' +uri="$basepath/security/certificate-authorities/migration-complete" + +# Construct request body +request_body="{" +request_body="${request_body}\"data\": {" +request_body="${request_body}\"type\": \"nbcaMigrationCompleteRequest\"," +request_body="${request_body}\"attributes\": {" +if [ $force == 1 ]; then + request_body="${request_body}\"force\" : \"true\"" +fi +request_body="${request_body}}}}" + +if [ -z $reason ]; then + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -d "$request_body" | jq +else + audit_reason="X-NetBackup-Audit-Reason:$reason"; + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -H "$audit_reason" -d "$request_body" | jq +fi + +exit 0 diff --git a/snippets/curl/initiate_migration.sh b/snippets/curl/initiate_migration.sh new file mode 100755 index 0000000..e2ab65b --- /dev/null +++ b/snippets/curl/initiate_migration.sh @@ -0,0 +1,121 @@ +#!/bin/sh + +#####################n##################################################### + +# This script demonstrates how to initiate CA migration. +# To initiate the CA migration, a user needs to have proper permissions. + +# This script requires jq command-line JSON parser +# if your system does not have jq installed, this will not work. +# jq can be downloaded from here: https://github.com/stedolan/jq/releases + +########################################################################### + +port=1556 +master_server="" +login_username="" +login_password="" +login_domainname="" +login_domaintype="" +keysize="" +reason="" + +showHelp() +{ + echo "" + echo "Invalid command parameters" + echo "Usage:" + echo "./initiate_migration.sh -nbmaster -login_username -login_password -login_domainname -login_domaintype -keysize | -k [-reason | -r ]" + echo "-nbmaster : Name of the NetBackup master server" + echo "-login_username : User name of the user performing action" + echo "-login_password : Password of the user performing action" + echo "-login_domainname : Domain name of the user performing action" + echo "-login_domaintype : Domain type of the user performing action" + echo "-keysize | -k : NetBackup CA key strength" + echo "-reason | -r : Reason for initiating CA migration" + echo "" + exit 1 +} + +parseArguments() +{ + if [ $# -ne 12 ] && [ $# -ne 14 ]; then + showHelp + fi + + while [ "$1" != "" ]; do + case $1 in + -nbmaster) + master_server=$2 + ;; + -login_username) + login_username=$2 + ;; + -login_password) + login_password=$2 + ;; + -login_domainname) + login_domainname=$2 + ;; + -login_domaintype) + login_domaintype=$2 + ;; + -keysize | -k) + keysize=$2 + ;; + -reason | -r) + reason=$2 + ;; + *) + showHelp + ;; + esac + shift 2 + done + + if [ -z "$master_server" ] || [ -z "$login_username" ] || [ -z "$login_password" ] || [ -z "$login_domainname" ] || [ -z "$login_domaintype" ] || [ -z "$keysize" ]; then + showHelp + fi + + if [ "${login_domaintype^^}" = "WINDOWS" ] || [ "${login_domaintype^^}" = "NT" ]; then + login_domaintype="nt" + fi +} + +###############main############ + +parseArguments "$@" + +basepath="https://$master_server:$port/netbackup" +content_header='content-type:application/json' + +##############login############# + +uri="$basepath/login" + +data=$(jq --arg name $login_username --arg pass $login_password --arg dname $login_domainname --arg dtype $login_domaintype \ + --null-input '{userName: $name, password: $pass, domainName: $dname, domainType: $dtype}') + +jwt=$(curl --silent -k -X POST $uri -H $content_header -d "$data" | jq --raw-output '.token') + +##############jobs############## +auth_header="authorization:$jwt" +content_header='content-type:application/vnd.netbackup+json;version=4.0' +uri="$basepath/security/certificate-authorities/initiate-migration" + +# Construct request body +request_body="{" +request_body="${request_body}\"data\": {" +request_body="${request_body}\"type\": \"initiateCAMigrationRequest\"," +request_body="${request_body}\"attributes\": {" +request_body="${request_body}\"keySize\" : \"${keysize}\"" +request_body="${request_body}}}}" + +if [ -z $reason ]; then + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -d "$request_body" | jq +else + audit_reason="X-NetBackup-Audit-Reason:$reason"; + curl --silent -k -X POST "$uri" -H "$content_header" -H "$auth_header" -H "$audit_reason" -d "$request_body" | jq +fi + +exit 0 diff --git a/snippets/perl/README.md b/snippets/perl/README.md index 7ef4052..c6fb251 100644 --- a/snippets/perl/README.md +++ b/snippets/perl/README.md @@ -126,3 +126,15 @@ API key Details: - Use the following command to use API key instead of JWT to trigger a NetBackup REST API on your NetBackup Master server: - `perl apikey_usage.pl -nbmaster -apikey [--verbose]` + +CA Migration Details: + +- Use the following command to initiate the NetBackup CA migration on your NetBackup Master server: + - `perl initiate-migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] -keysize [-reason ] [--verbose]` + +- Use the following command to activate the new NetBackup CA on your NetBackup Master server: + - `perl activate_migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] [-reason ] [--force] [--verbose]` + +- Use the following command to complete the NetBackup CA migration on your NetBackup Master server: + - `perl complete_migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] [-reason ] [--force] [--verbose]` + diff --git a/snippets/perl/activate-migration.pl b/snippets/perl/activate-migration.pl new file mode 100755 index 0000000..a9b3c20 --- /dev/null +++ b/snippets/perl/activate-migration.pl @@ -0,0 +1,96 @@ +#Load module netbackup.pm from current directory +use lib '.'; + +use strict; +use warnings; +use LWP::UserAgent; +use LWP::Protocol::https; +print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n"; +print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n"; + +use JSON; +use Getopt::Long qw(GetOptions); +use netbackup; +use migration; + + +sub printUsage { + print "\nUsage : perl activate_migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] [-reason ] [--force] [--verbose]\n\n"; + print "-nbmaster : Name of the NetBackup master server\n"; + print "-login_username : User name of the user performing action\n"; + print "-login_password : Password of the user performing action\n"; + print "-login_domainname : Domain name of the user performing action\n"; + print "-login_domaintype : Domain type of the user performing action\n"; + print "-reason : A textual description to activate the new CA\n"; + print "--force : Forcefully activate the new CA in migration process\n"; + print "--verbose : Detail logging\n\n\n"; + die; +} + +my $protocol = "https"; +my $port = 1556; +my $fqdn_hostname; +my $login_username; +my $login_password; +my $login_domainname; +my $login_domaintype; +my $reason; +my $verbose; +my $force = 0; + +GetOptions( + 'nbmaster=s' => \$fqdn_hostname, + 'login_username=s' => \$login_username, + 'login_password=s' => \$login_password, + 'login_domainname=s' => \$login_domainname, + 'login_domaintype=s' => \$login_domaintype, + 'reason=s' => \$reason, + 'verbose' => \$verbose, + 'force' => \$force +) or printUsage(); + +if (!$fqdn_hostname || !$login_username || !$login_password || !$login_domainname || !$login_domaintype) { + printUsage(); +} + +if($verbose){ + print "\nRecieved the following parameters : \n"; + print " FQDN Hostname : $fqdn_hostname\n"; + print " Login Username : $login_username\n"; + print " Login Password : $login_password\n"; + print " Login Domain Name : $login_domainname\n" if (defined $login_domainname); + print " Login Domain Type : $login_domaintype\n" if (defined $login_domaintype); + print " Reason for activate the new CA: $reason\n" if (defined $reason); + print " Force: $force\n\n"; +} + +my $ua = LWP::UserAgent->new( + ssl_opts => { verify_hostname => 0, verify_peer => 0}, + ); + +my $base_url = "$protocol://$fqdn_hostname:$port/netbackup"; + +my $token; +if (defined $login_domainname && defined $login_domaintype) { + $token = netbackup::login($fqdn_hostname, $login_username, $login_password, $login_domainname, $login_domaintype); +} +else{ + $token = netbackup::login($fqdn_hostname, $login_username, $login_password); +} + +if(!defined $token) { + print "\nFailed to login using credentials provided for user [$login_username]\n"; + exit -1; +} + +if (defined $reason) { + migration::activate_migration($base_url, $token, $reason, $force); +} else { + migration::activate_migration($base_url, $token, $force); +} + +netbackup::logout($fqdn_hostname, $token); + +print "\n"; + +exit 0; diff --git a/snippets/perl/complete-migration.pl b/snippets/perl/complete-migration.pl new file mode 100755 index 0000000..360f548 --- /dev/null +++ b/snippets/perl/complete-migration.pl @@ -0,0 +1,99 @@ +#Load module netbackup.pm from current directory +use lib '.'; + +use strict; +use warnings; +use LWP::UserAgent; +use LWP::Protocol::https; +print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n"; +print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n"; + +use JSON; +use Getopt::Long qw(GetOptions); +use Data::Dumper; +use migration; +use netbackup; + + +sub printUsage { + print "\nUsage : perl complete_migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] [-reason ] [--force] [--verbose]\n\n"; + print "-nbmaster : Name of the NetBackup master server\n"; + print "-login_username : User name of the user performing action\n"; + print "-login_password : Password of the user performing action\n"; + print "-login_domainname : Domain name of the user performing action\n"; + print "-login_domaintype : Domain type of the user performing action\n"; + print "-reason : A textual description to complete the CA migration process\n"; + print "--force : Forcefully complete the CA migration process\n"; + print "--verbose : Detail logging\n\n\n"; + die; +} + +my $protocol = "https"; +my $port = 1556; +my $fqdn_hostname; +my $login_username; +my $login_password; +my $login_domainname; +my $login_domaintype; +my $reason; +my $verbose; +my $force = 0; + +GetOptions( + 'nbmaster=s' => \$fqdn_hostname, + 'login_username=s' => \$login_username, + 'login_password=s' => \$login_password, + 'login_domainname=s' => \$login_domainname, + 'login_domaintype=s' => \$login_domaintype, + 'reason=s' => \$reason, + 'verbose' => \$verbose, + 'force' => \$force +) or printUsage(); + +if (!$fqdn_hostname || !$login_username || !$login_password || !$login_domainname || !$login_domaintype) { + printUsage(); +} + +if($verbose){ + print "\nRecieved the following parameters : \n"; + print " FQDN Hostname : $fqdn_hostname\n"; + print " Login Username : $login_username\n"; + print " Login Password : $login_password\n"; + print " Login Domain Name : $login_domainname\n" if (defined $login_domainname); + print " Login Domain Type : $login_domaintype\n" if (defined $login_domaintype); + print " Reason for activate the new CA: $reason\n" if (defined $reason); + print " Force: $force\n"; +} + +my $ua = LWP::UserAgent->new( + ssl_opts => { verify_hostname => 0, verify_peer => 0}, + ); + +my $base_url = "$protocol://$fqdn_hostname:$port/netbackup"; + +my $token; +if (defined $login_domainname && defined $login_domaintype) { + $token = netbackup::login($fqdn_hostname, $login_username, $login_password, $login_domainname, $login_domaintype); +} +else{ + $token = netbackup::login($fqdn_hostname, $login_username, $login_password); +} + +if(!defined $token) { + print "\nFailed to login using credentials provided for user [$login_username]\n"; + exit -1; +} + +if (defined $reason) { + migration::complete_migration($base_url, $token, $reason, $force); +} elsif ($force) { + migration::complete_migration($base_url, $token, $force); +} else { + migration::complete_migration($base_url, $token); +} + +netbackup::logout($fqdn_hostname, $token); + +print "\n"; + +exit 0; diff --git a/snippets/perl/initiate-migration.pl b/snippets/perl/initiate-migration.pl new file mode 100755 index 0000000..80bfdd5 --- /dev/null +++ b/snippets/perl/initiate-migration.pl @@ -0,0 +1,97 @@ +#Load module netbackup.pm from current directory +use lib '.'; + +use strict; +use warnings; +use LWP::UserAgent; +use LWP::Protocol::https; +print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n"; +print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n"; + +use JSON; +use Getopt::Long qw(GetOptions); +use netbackup; +use migration; + +sub printUsage { + print "\nUsage : perl initiate-migration.pl -nbmaster -login_username -login_password [-login_domainname -login_domaintype ] -keysize [-reason ] [--verbose]\n\n"; + print "-nbmaster : Name of the NetBackup master server\n"; + print "-login_username : User name of the user performing action\n"; + print "-login_password : Password of the user performing action\n"; + print "-login_domainname : Domain name of the user performing action\n"; + print "-login_domaintype : Domain type of the user performing action\n"; + print "-keysize : Keysize of the CA\n"; + print "-reason : A textual description to initiate the CA migration\n"; + print "--verbose : Detail logging\n\n\n"; + die; +} + +my $protocol = "https"; +my $port = 1556; +my $fqdn_hostname; +my $login_username; +my $login_password; +my $login_domainname; +my $login_domaintype; +my $reason; +my $key_size; +my $description; +my $verbose; + +GetOptions( + 'nbmaster=s' => \$fqdn_hostname, + 'login_username=s' => \$login_username, + 'login_password=s' => \$login_password, + 'login_domainname=s' => \$login_domainname, + 'login_domaintype=s' => \$login_domaintype, + 'keysize=s' => \$key_size, + 'reason=s' => \$reason, + 'verbose' => \$verbose +) or printUsage(); + +if (!$fqdn_hostname || !$login_username || !$login_password || !$login_domainname || !$login_domaintype || !$key_size) { + printUsage(); +} + +if($verbose){ + print "\nRecieved the following parameters : \n"; + print " FQDN Hostname : $fqdn_hostname\n"; + print " Login Username : $login_username\n"; + print " Login Password : $login_password\n"; + print " Login Domain Name : $login_domainname\n" if (defined $login_domainname); + print " Login Domain Type : $login_domaintype\n" if (defined $login_domaintype); + print " Keysize of the CA : $key_size\n"; + print " Reason for initiating migration : $reason\n" if (defined $reason); +} + +my $ua = LWP::UserAgent->new( + ssl_opts => { verify_hostname => 0, verify_peer => 0}, + ); + +my $base_url = "$protocol://$fqdn_hostname:$port/netbackup"; + +my $token; +if (defined $login_domainname && defined $login_domaintype) { + $token = netbackup::login($fqdn_hostname, $login_username, $login_password, $login_domainname, $login_domaintype); +} +else{ + $token = netbackup::login($fqdn_hostname, $login_username, $login_password); +} + +if(!defined $token) { + print "\nFailed to login using credentials provided for user [$login_username]\n"; + exit -1; +} + +if (defined $reason) { + migration::initiate_migration($base_url, $token, $key_size, $reason); +} +else { + migration::initiate_migration($base_url, $token, $key_size); +} + +netbackup::logout($fqdn_hostname, $token); + +print "\n"; + +exit 0; diff --git a/snippets/perl/migration.pm b/snippets/perl/migration.pm new file mode 100644 index 0000000..aeaaba5 --- /dev/null +++ b/snippets/perl/migration.pm @@ -0,0 +1,201 @@ +package migration; + +use strict; +use warnings; + +use JSON; +use LWP::UserAgent; +use LWP::Protocol::https; +use Try::Tiny; + +# constants +my $BASE_URL; +my $MIGRATION_NB_CONTENT_TYPE_V4 = "application/vnd.netbackup+json; version=4.0"; + +my $ua = LWP::UserAgent->new( + ssl_opts => { verify_hostname => 0, verify_peer => 0}, + ); + +=head1 initiate_migration + SYNOPSIS + This subroutine is used to call /security/certificate-authorities/initiate-migration api to + initiate CA migration with specified keysize. + PARAMETERS + @param $_[0] - string + The name of the master server. + Ex: myhostname.myDomain.com + @param $_[1] - string + The authentication token fetched after invoking /login api using + user credentials or apikey. + @param $_[2] - int + Keysize of the CA. + @param $_[3] - string - optional + A textual description for initiating the CA migration process. + + RETURNS + None +=cut + +sub initiate_migration { + my @argument_list = @_; + my $base_url = $argument_list[0]; + my $token = $argument_list[1]; + my $keysize = $argument_list[2]; + my $des; + if (defined $argument_list[3]) { + $des = $argument_list[3]; + } + + my $url = "$base_url/security/certificate-authorities/initiate-migration"; + my $req = HTTP::Request->new(POST => $url); + $req->header('Content-Type' => $MIGRATION_NB_CONTENT_TYPE_V4); + $req->header('Authorization' => $token); + $req->header('X-NetBackup-Audit-Reason' => $des) if (defined $des); + my $post_data = qq({ "data": { "type": "initiateCAMigrationRequest", "attributes": { + "keySize": $keysize } } }); + + $req->content($post_data); + + print "\n**************************************************************"; + print "\n Making POST Request to initiate CA migration \n"; + + my $resp = $ua->request($req); + if ($resp->is_success) { + my $message = decode_json($resp->content); + print "Successfully initiated the CA migration with status code: ", $resp->code, "\n"; + print "Printing the response content: ", $resp->decoded_content, "\n"; + } else { + printErrorResponse($resp); + } +} + +=head1 activate_migration + SYNOPSIS + This subroutine is used to call /security/certificate-authorities/activate api to + activate the new CA in migration process. + PARAMETERS + @param $_[0] - string + The name of the master server. + Ex: myhostname.myDomain.com + @param $_[1] - string + The authentication token fetched after invoking /login api using + user credentials or apikey. + @param $_[2] - string - optional + A textual description to activate the new CA. + @param $_[3] - int [0/1] - optional + Forcefully activate the new CA. + + RETURNS + None +=cut + +sub activate_migration { + my @argument_list = @_; + my $force = 0; + my $reason; + my $base_url = $argument_list[0]; + my $token = $argument_list[1]; + if (@argument_list == 4) { + $reason = $argument_list[2]; + $force = $argument_list[3]; + } elsif (@argument_list == 3) { + $force = $argument_list[2]; + } + + my $url = "$base_url/security/certificate-authorities/activate"; + my $req = HTTP::Request->new(POST => $url); + $req->header('Content-Type' => $MIGRATION_NB_CONTENT_TYPE_V4); + $req->header('Authorization' => $token); + $req->header('X-NetBackup-Audit-Reason' => $reason) if (defined $reason); + my $post_data; + if ($force) { + $post_data = qq({ "data": { "type": "nbcaMigrationActivateRequest", "attributes": { + "force": "true" } } }); + } + else { + $post_data = qq({ "data": { "type": "nbcaMigrationActivateRequest", "attributes": { }}}); + } + + $req->content($post_data); + + print "\n**************************************************************"; + print "\n Making POST Request to activate the new CA \n"; + + my $resp = $ua->request($req); + if ($resp->is_success) { + print "Successfully activated the new CA with status code: ", $resp->code, "\n"; + } else { + printErrorResponse($resp); + } +} + +=head1 complete_migration + SYNOPSIS + This subroutine is used to call /security/certificate-authorities/complete-migration api to + complete the CA migration process. + PARAMETERS + @param $_[0] - string + The name of the master server. + Ex: myhostname.myDomain.com + @param $_[1] - string + The authentication token fetched after invoking /login api using + user credentials or apikey. + @param $_[2] - string - optional + A textual description to complete the CA migration process. + @param $_[3] - int [0/1] - optional + Forcefully complete the CA migration process. + + RETURNS + None +=cut + +sub complete_migration { + my @argument_list = @_; + my $force = 0; + my $reason; + my $base_url = $argument_list[0]; + my $token = $argument_list[1]; + + if (@argument_list == 4) { + $reason = $argument_list[2]; + $force = $argument_list[3]; + } elsif (@argument_list == 3) { + $force = $argument_list[2]; + } + + my $url = "$base_url/security/certificate-authorities/migration-complete"; + my $req = HTTP::Request->new(POST => $url); + $req->header('Content-Type' => $MIGRATION_NB_CONTENT_TYPE_V4); + $req->header('Authorization' => $token); + $req->header('X-NetBackup-Audit-Reason' => $reason) if (defined $reason); + my $post_data; + if ($force) { + $post_data = qq({ "data": { "type": "nbcaMigrationCompleteRequest", "attributes": { + "force": "true" } } }); + } + else { + $post_data = qq({ "data": { "type": "nbcaMigrationCompleteRequest", "attributes": { }}}); + } + + $req->content($post_data); + + print "\n**************************************************************"; + print "\n Making POST Request to complete the CA migration\n"; + + my $resp = $ua->request($req); + if ($resp->is_success) { + print "CA migration completed successfully with status code: ", $resp->code, "\n"; + } else { + printErrorResponse($resp); + } +} + +sub printErrorResponse { + my @argument_list = @_; + my $resp = $argument_list[0]; + + print "Request failed with status code: ", $resp->code, "\n\n"; + print "Response: ", $resp->content, "\n"; +} + +1;