Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config changes #47

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
239 changes: 239 additions & 0 deletions recipes/powershell/config/Config_trust_management_crud_operation.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<#
.SYNOPSIS
This sample script demonstrates the use of NetBackup Trust Management APIs.

.DESCRIPTION
The script can be run using NetBackup 8.2 or higher.
It updates the exclude list configuration on the specified client. The exclude list is specified within the script below.

.EXAMPLE
./Config_trust_management_crud_operation.ps1 -MasterServer <masterServer> -UserName <username> -Password <password> -TrustedMasterServerName <Trusted master Server Name> [-DomainName <domainName> -DomainType <domainType>]
#>

#Requires -Version 4.0

Param (
[string]$MasterServer = $(Throw "Please specify the name of the NetBackup Master Server using the -MasterServer parameter."),
[string]$UserName = $(Throw "Please specify the user name using the -UserName parameter."),
[string]$Password = $(Throw "Please specify the password using the -Password parameter."),
[string]$TrustedMasterServerName = $(Throw "Please specify the name of the NetBackup remote Master Server using the -TrustedMasterServerName parameter."),
[string]$DomainName,
[string]$DomainType
)


###############################################################
# Setup to allow self-signed certificates and enable TLS v1.2
###############################################################
Function Setup()
{
# Allow self-signed certificates
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
{
Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
}

# Force TLS v1.2
try {
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
}
catch {
Write-Host "`n"$_.Exception.InnerException.Message
}
}

####################
# Global Variables
####################

$port = 1556
$basepath = "https://" + $MasterServer + ":" + $port + "/netbackup"
$contentType = "application/vnd.netbackup+json;version=4.0"
$hostName = $client

######################################
# Login to the NetBackup webservices
######################################
Function Login()
{

$uri = $basepath + "/login"

$body = @{
userName=$UserName
password=$Password
}
if ($DomainName -ne "") {
$body.add("domainName", $DomainName)
}
if ($DomainType -ne "") {
$body.add("domainType", $DomainType)
}
Write-Host "`nSending a POST request to login to the NetBackup webservices...`n"

$response = Invoke-WebRequest `
-Uri $uri `
-Method POST `
-Body (ConvertTo-Json -InputObject $body) `
-ContentType $contentType

if ($response.StatusCode -ne 201)
{
throw "Unable to connect to the NetBackup Master Server"
}

Write-Host "Login successful.`n"
$content = (ConvertFrom-Json -InputObject $response)
return $content
}
#####################################################################
# POST NetBackup Storage server
#####################################################################
Function CreateTrust()
{
$base_uri = $basepath + "/config/servers/trusted-master-servers"

$json = '{
"data": {
"type": "trustedMasterServer",
"attributes": {
"trustedMasterServerName": "'+$TrustedMasterServerName+'",
Comment on lines +109 to +113
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and the similar code at 174 should do like is done in Delete-NB-delete-trust.ps1, which creates a hash table and then converts it to JSON with ConvertTo-Json. It avoids the awkwardness of terminating the string literal midway through, inserting a variable (which hasn't been escaped for use as a JSON string), and then resuming the string.

"rootCAType": "NBCA",
"authenticationType": "CREDENTIAL",
"domainName": "DOMAIN",
"userName": "USER",
"password": "PASSWORD",
"fingerprint": "FINGERPRINT"
}
}
}
'
$response_create_trust = Invoke-WebRequest `
-Uri $base_uri `
-Method POST `
-Body ($json) `
-ContentType $contentType `
-Headers $headers

if ($response_create_trust.StatusCode -ne 201)
{
throw "Unable to create trust between master servers."
}

Write-Host "Trust between master servers created successfully.`n"
echo $response_create_trust
Write-Host $response_create_trust

$response_create_trust = (ConvertFrom-Json -InputObject $response_create_trust)
}
#####################################################################
# GET NetBackup Trusted Master Server
#####################################################################
Function GetTrustedMaster()
{

$base_uri = $basepath + "/config/servers/trusted-master-servers/" + $TrustedMasterServerName


$response_get = Invoke-WebRequest `
-Uri $base_uri `
-Method GET `
-ContentType $contentType `
-Headers $headers

if ($response_get.StatusCode -ne 200)
{
throw "Unable to fetch scpecified trusted master server"
}

Write-Host "Scpecified trusted master server fetched successfully.`n"
Write-Host $response_get

$response_get = (ConvertFrom-Json -InputObject $response_get)
}
#####################################################################
# PATCH NetBackup trust between master servers
#####################################################################
Function UpdateTrust()
{
$base_uri = $basepath + "/config/servers/trusted-master-servers/" + $TrustedMasterServerName

$json = '{
"data": {
"type": "trustedMasterServer",
"attributes": {
"trustedMasterServerName": "'+$TrustedMasterServerName+'",
"rootCAType": "ECA"
}
}
}
'

$response_update = Invoke-WebRequest `
-Uri $base_uri `
-Method PATCH `
-Body ($json) `
-ContentType $contentType `
-Headers $headers

if ($response_update.StatusCode -ne 200)
{
throw "Unable to update trust between masters."
}

Write-Host "Trust between masters updated successfully.`n"
echo $response_update
Write-Host $response_update

$response_update = (ConvertFrom-Json -InputObject $response_update)

}


#####################################################################
# Delete NetBackup Trust between master Server
#####################################################################
Function DeleteTrust()
{
$base_uri = $basepath + "/config/servers/trusted-master-servers/" + $TrustedMasterServerName


$response_delete = Invoke-WebRequest `
-Uri $base_uri `
-Method DELETE `
-ContentType $contentType `
-Headers $headers

if ($response_delete.StatusCode -ne 204 )
{
throw "Unable to delete trust between masters."
}

Write-Host "Trust between masters deleted successfully.`n"
Write-Host $response_delete

$response_delete = (ConvertFrom-Json -InputObject $response_delete)
}

###########################################################################

Setup
$loginResponse = Login
$headers = @{"Authorization" = $loginResponse.token}
CreateTrust
GetTrustedMaster
UpdateTrust
DeleteTrust
3 changes: 2 additions & 1 deletion recipes/powershell/config/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### NetBackup API Code Samples for PowerShell
#### NetBackup API Code Samples for PowerShell
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reduce the header level? The first header should be level 1, not 3 or 4.


This directory contains code samples to invoke NetBackup config APIs using PowerShell.

Expand All @@ -14,3 +14,4 @@ Pre-requisites:

Use the following commands to run the PowerShell samples.
- `./configManagement_curd_operations.ps1 -MasterServer <masterServer> -UserName <username> -Password <password> -Client <client> [-DomainName <domainName> -DomainType <domainType>]`
- `./Config_trust_management_crud_operation.ps1 -MasterServer <masterServer> -UserName <username> -Password <password> -TrustedMasterServerName <Trusted master Server Name> [-DomainName <domainName> -DomainType <domainType>]`
3 changes: 2 additions & 1 deletion recipes/python/config/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### NetBackup Hosts Configuration Management API Code Samples
#### NetBackup Hosts Configuration Management API Code Samples

This directory contains Python scripts demonstrating the use of NetBackup Hosts Configuration Management APIs to update exclude list on a NetBackup host.

Expand All @@ -18,4 +18,5 @@ Use the following command to run the script. The command should be run from the

`python -W ignore -m config.hosts_exclude_list -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`

`python -W ignore configure_storage_unit_end_to_end.py -nbmaster <master_server> -username <username> -password <password> -trust_payload <input JSON for trusted masters> -trusted_master_server_name <Trusted Master Server Name> [-domainname <domain_name>] [-domaintype <domain_type>]`
`Note: hostName is the name of the NetBackup host to set the exclude configuration. The exclude list is specified in the config/exclude_list file.`
74 changes: 74 additions & 0 deletions recipes/python/config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import requests

content_type = "application/vnd.netbackup+json; version=4.0"


def perform_login(username, password, base_url, domain_name, domain_type):
url = base_url + "/login"

if domain_name != "" and domain_type != "":
req_body = {"userName": username, "password": password, "domainName": domain_name, "domainType": domain_type}
else:
req_body = {"userName": username, "password": password}

headers = {'Content-Type': content_type}

print("performing POST on {} for user '{}'\n".format(url, req_body['userName']))

resp = requests.post(url, headers=headers, json=req_body, verify=False)

if resp.status_code != 201:
raise Exception('Login API failed with status code {} and {}'.format(resp.status_code, resp.json()))

return resp.json()['token']


def get_trusted_master_server_by_name(jwt, base_url, trustedmasterservername):
url = base_url + "/config/servers/trusted-master-servers/" + trustedmasterservername
headers = {'Content-Type': content_type, 'Authorization': jwt}
query_params = {
# "page[limit]": 100, #This changes the default page size to 100
# "filter": "jobType eq 'RESTORE'" #This adds a filter to only show RESTORE Jobs
Comment on lines +29 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these parameters valid for this API? If so, then I think there should be a comment explaining that the programmer can uncomment these lines to use them. If they're not valid, then just delete them instead of leaving them commented out.

}

print("performing GET on {}\n".format(url))

resp = requests.get(url, headers=headers, params=query_params, verify=False)

if resp.status_code != 200:
raise Exception('GET Trusted master server with specific name failed with status code {} and {}'.format(resp.status_code, resp.json()))

return resp.json()

def delete_trust(jwt, base_url, trustedmasterservername):
url = base_url + "/config/servers/trusted-master-servers/" +trustedmasterservername
headers = {'Content-Type': content_type, 'Authorization': jwt}
query_params = {
# "page[limit]": 100, #This changes the default page size to 100
# "filter": "jobType eq 'RESTORE'" #This adds a filter to only show RESTORE Jobs
}

print("performing DELETE on {}\n".format(url))

resp = requests.delete(url, headers=headers, verify=False)
if resp.status_code != 204:
raise Exception('DELETE trust with specific trusted master failed with status code {} and {}'.format(resp.status_code, resp.json()))

print("\nThe Trust is deleted with status code: {}\n".format(resp.status_code))

def create_trusted_master_server(jwt, base_url, file_name):
url = base_url + "/config/servers/trusted-master-servers"
headers = {'Content-Type': content_type, 'Authorization': jwt}

path = file_name

req_body = open(path, 'r').read()

print("performing POST on {}\n".format(url))

resp = requests.post(url, headers=headers, data=req_body, verify=False)

if resp.status_code != 201:
raise Exception('Create trust between master servers API failed with status code {} and {}'.format(resp.status_code, resp.json()))

return resp.json()
Loading