Skip to content

Commit 19ad33f

Browse files
authored
Sample perl script for VMware assets API
1 parent c2e48f9 commit 19ad33f

3 files changed

Lines changed: 387 additions & 0 deletions

File tree

recipes/perl/assets/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### NetBackup API Code Samples for perl
2+
3+
This directory contains code samples to invoke NetBackup VMware GET ASSETS APIs using perl.
4+
5+
#### Disclaimer
6+
7+
These scripts are only meant to be used as a reference. If you intend to use them in production, use it at your own risk.
8+
9+
#### Prerequisites:
10+
11+
- NetBackup 8.3 or higher
12+
- Perl 5.20.2 or higher
13+
14+
#### Executing the recipes in perl
15+
16+
Use the following commands to run the perl samples.
17+
18+
- `perl get_vmware_assets.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
19+
20+
The script uses the NetBackup Asset Service API to get the VMware workload assets along with Id's configured in the system.
21+
22+
- `perl create_assetGroup.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
23+
24+
The script creates a VMware Asset Group by using Netabckup Asset Service API.
25+
26+
27+
Examples:
28+
- perl get_vmware_assets.pl -nbmaster localhost -username user -password password -domainName domain -domainType NT
29+
- perl create_assetGroup.pl -nbmaster localhost -username user -password password -domainName domain -domainType NT
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#!/usr/bin/env perl
2+
3+
use LWP::UserAgent;
4+
use LWP::Protocol::https;
5+
print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
6+
print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
7+
use JSON;
8+
use Getopt::Long qw(GetOptions);
9+
10+
11+
#
12+
# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token
13+
# and use this token in your Authorization header for all subsequent requests. Token validity
14+
# is fixed at 24 hours
15+
#
16+
17+
my $content_type = "application/vnd.netbackup+json; version=4.0";
18+
my $protocol = "https";
19+
my $port = "1556";
20+
my $nbmaster;
21+
my $username;
22+
my $password;
23+
my $domainName;
24+
my $domainType;
25+
my $token;
26+
my $base_url;
27+
28+
my $ua = LWP::UserAgent->new(
29+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
30+
);
31+
32+
# subroutines for printing usage and library information required to run the script.
33+
sub print_usage {
34+
print("\n\nUsage:");
35+
print("\nperl get_vmware_assets -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] \n\n\n");
36+
}
37+
38+
sub print_disclaimer {
39+
print("--------------------------------------------------------\n");
40+
print("-- This script requires Perl 5.20.2 or later --\n");
41+
print("--------------------------------------------------------\n");
42+
print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n");
43+
print("You can specify the 'nbmaster', 'username', 'password', 'domainName', 'domainType' as command-line parameters\n");
44+
print_usage();
45+
}
46+
47+
48+
# subroutine to process user input
49+
sub user_input {
50+
GetOptions(
51+
'nbmaster=s' => \$nbmaster,
52+
'username=s' => \$username,
53+
'password=s' => \$password,
54+
'domainName=s' => \$domainName,
55+
'domainType=s' => \$domainType,
56+
) or (print_usage() && exit);
57+
58+
if ($nbmaster eq "") {
59+
print("Please provide the value for 'nbmaster'");
60+
exit;
61+
}
62+
63+
if ($username eq "") {
64+
print("Please provide the value for 'username'");
65+
exit;
66+
}
67+
68+
if ($password eq "") {
69+
print("Please provide the value for 'password'");
70+
exit;
71+
}
72+
73+
$base_url = "$protocol://$nbmaster:$port/netbackup";
74+
}
75+
76+
sub login {
77+
my @argument_list = @_;
78+
$base_url = $argument_list[0];
79+
my $username = $argument_list[1];
80+
my $password = $argument_list[2];
81+
my $domainName = $argument_list[3];
82+
my $domainType = $argument_list[4];
83+
84+
my $url = "$base_url/login";
85+
86+
my $req = HTTP::Request->new(POST => $url);
87+
$req->header('content-type' => 'application/json');
88+
89+
if ($domainName eq "" && $domainType eq "") {
90+
$post_data = qq({ "userName": "$username", "password": "$password" });
91+
}
92+
else {
93+
$post_data = qq({ "domainType": "$domainType", "domainName": "$domainName", "userName": "$username", "password": "$password" });
94+
}
95+
$req->content($post_data);
96+
97+
98+
print "\n\n**************************************************************";
99+
print "\n\n Making POST Request to login to get token \n\n";
100+
101+
my $resp = $ua->request($req);
102+
if ($resp->is_success) {
103+
my $message = decode_json($resp->content);
104+
$token = $message->{"token"};
105+
print "Login succeeded with status code: ", $resp->code, "\n";
106+
}
107+
else {
108+
print "Request failed with status code: ", $resp->code, "\n";
109+
}
110+
return $token;
111+
}
112+
113+
# subroutine to Create Asset Group
114+
sub create_assetGroup {
115+
my $url = "$base_url/asset-service/queries";
116+
117+
my $asset_data = qq({
118+
"data": {
119+
"type": "query",
120+
"attributes": {
121+
"queryName": "create-or-update-assets",
122+
"workloads": ["vmware"],
123+
"parameters": {
124+
"objectList": [
125+
{
126+
"correlationId": "cor-1234",
127+
"type": "vmwareGroupAsset",
128+
"assetGroup": {
129+
"description": "sampleDescription",
130+
"assetType": "vmGroup",
131+
"filterConstraint": "sampleFilterConstaint",
132+
"oDataQueryFilter": "commonAssetAttributes/displayName eq 'testing1234'",
133+
"commonAssetAttributes": {
134+
"displayName": "sampleGroup1234",
135+
"workloadType": "vmware",
136+
"protectionCapabilities": {
137+
"isProtectable": "YES",
138+
"isProtectableReason": "sampleReason",
139+
"isRecoverable": "NO",
140+
"isRecoverableReason": "sampleReason"
141+
},
142+
"detection": {
143+
"detectionMethod": "MANUAL"
144+
}
145+
}
146+
}
147+
}
148+
]
149+
}
150+
}
151+
}
152+
});
153+
154+
my $req = HTTP::Request->new(POST => $url);
155+
$req->header('Content-Type' => $content_type);
156+
$req->header('Authorization' => $token);
157+
$req->content($asset_data);
158+
159+
print "\n\n**************************************************************";
160+
print "\n\n Making POST Request to create assetGroup\n\n";
161+
162+
my $resp = $ua->request($req);
163+
164+
my $message = $resp->decoded_content;
165+
print "Create vmware Asset Group succeeded with status code: ", $resp->code, "\n\n\n";
166+
print "=======================\n";
167+
168+
my $json = decode_json($message);
169+
my $id = $json->{'data'}->{'id'};
170+
print "id: ", $id, "\n\n\n";
171+
172+
my $url = "$base_url/asset-service/queries/$id";
173+
my $req = HTTP::Request->new(GET => $url);
174+
$req->header('Accept' => $content_type);
175+
$req->header('Authorization' => $token);
176+
177+
print "\n\n**************************************************************";
178+
print "\n\n Making GET Request for the QueryID response\n\n";
179+
180+
my $resp = $ua->request($req);
181+
my $message = $resp->decoded_content;
182+
183+
print "Create vmware Asset Group query id : ", $resp->code, "\n\n\n";
184+
185+
my $json = decode_json($message);
186+
my @responses = @{$json->{'data'}};
187+
for my $response (@responses) {
188+
my @workitems = @{$response->{'attributes'}->{'workItemResponses'}};
189+
for my $workItem (@workitems) {
190+
my $status = $workItem->{'statusDetails'}->{'message'};
191+
if ($status eq 'Created') {
192+
print "VMware Asset Group : ", $status ,"\n";
193+
} else {
194+
print "VMware Asset Group Not Created due to: ", $status ,"\n";
195+
}
196+
}
197+
}
198+
}
199+
200+
print_disclaimer();
201+
202+
user_input();
203+
204+
$token = login($base_url, $username, $password, $domain_name, $domain_type);
205+
206+
create_assetGroup();
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env perl
2+
3+
use LWP::UserAgent;
4+
use LWP::Protocol::https;
5+
print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
6+
print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
7+
use JSON;
8+
use Getopt::Long qw(GetOptions);
9+
10+
11+
#
12+
# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token
13+
# and use this token in your Authorization header for all subsequent requests. Token validity
14+
# is fixed at 24 hours
15+
#
16+
17+
my $content_type = "application/vnd.netbackup+json; version=4.0";
18+
my $protocol = "https";
19+
my $port = "1556";
20+
my $nbmaster;
21+
my $username;
22+
my $password;
23+
my $domainName;
24+
my $domainType;
25+
my $token;
26+
my $base_url;
27+
my $assetsFilter;
28+
29+
my $ua = LWP::UserAgent->new(
30+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
31+
);
32+
33+
# subroutines for printing usage and library information required to run the script.
34+
sub print_usage {
35+
print("\n\nUsage:");
36+
print("\nperl get_vmware_assets -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] [-assetsFilter <filter>]\n\n\n");
37+
}
38+
39+
sub print_disclaimer {
40+
print("--------------------------------------------------------\n");
41+
print("-- This script requires Perl 5.20.2 or later --\n");
42+
print("--------------------------------------------------------\n");
43+
print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n");
44+
print("You can specify the 'nbmaster', 'username', 'password', 'domainName', 'domainType' and 'assetsFilter' as command-line parameters\n");
45+
print_usage();
46+
}
47+
48+
# subroutine to process user input
49+
sub user_input {
50+
GetOptions(
51+
'nbmaster=s' => \$nbmaster,
52+
'username=s' => \$username,
53+
'password=s' => \$password,
54+
'domainName=s' => \$domainName,
55+
'domainType=s' => \$domainType,
56+
) or (print_usage() && exit);
57+
58+
if ($nbmaster eq "") {
59+
print("Please provide the value for 'nbmaster'");
60+
exit;
61+
}
62+
63+
if ($username eq "") {
64+
print("Please provide the value for 'username'");
65+
exit;
66+
}
67+
68+
if ($password eq "") {
69+
print("Please provide the value for 'password'");
70+
exit;
71+
}
72+
73+
$base_url = "$protocol://$nbmaster:$port/netbackup";
74+
}
75+
76+
sub login {
77+
my @argument_list = @_;
78+
$base_url = $argument_list[0];
79+
my $username = $argument_list[1];
80+
my $password = $argument_list[2];
81+
my $domainName = $argument_list[3];
82+
my $domainType = $argument_list[4];
83+
84+
my $url = "$base_url/login";
85+
86+
my $req = HTTP::Request->new(POST => $url);
87+
$req->header('content-type' => 'application/json');
88+
89+
if ($domainName eq "" && $domainType eq "") {
90+
$post_data = qq({ "userName": "$username", "password": "$password" });
91+
}
92+
else {
93+
$post_data = qq({ "domainType": "$domainType", "domainName": "$domainName", "userName": "$username", "password": "$password" });
94+
}
95+
$req->content($post_data);
96+
97+
98+
print "\n\n**************************************************************";
99+
print "\n\n Making POST Request to login to get token \n\n";
100+
101+
my $resp = $ua->request($req);
102+
if ($resp->is_success) {
103+
my $message = decode_json($resp->content);
104+
$token = $message->{"token"};
105+
print "Login succeeded with status code: ", $resp->code, "\n";
106+
}
107+
else {
108+
print "Request failed with status code: ", $resp->code, "\n";
109+
}
110+
return $token;
111+
}
112+
113+
# subroutine to get assets
114+
sub get_assets {
115+
my $url = "$base_url/asset-service/workloads/vmware/assets";
116+
117+
my $req = HTTP::Request->new(GET => $url);
118+
$req->header('Accept' => $content_type);
119+
$req->header('Authorization' => $token);
120+
121+
print "\n\n**************************************************************";
122+
print "\n\n Making GET Request to get assets\n\n";
123+
124+
my $resp = $ua->request($req);
125+
if ($resp->is_success) {
126+
my $message = $resp->decoded_content;
127+
print "Get vmware Assets succeeded with status code: ", $resp->code, "\n\n\n";
128+
print "Below is the list of asset IDs:\n";
129+
print "=======================\n";
130+
131+
my $json = decode_json($message);
132+
my @assets = @{$json->{'data'}};
133+
foreach (@assets) {
134+
my $asset = $_;
135+
my $id = $asset->{'id'};
136+
printf("Asset id : %s \n", $id);
137+
138+
}
139+
}
140+
else {
141+
print "HTTP GET error code: ", $resp->code, "\n";
142+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
143+
}
144+
}
145+
146+
print_disclaimer();
147+
148+
user_input();
149+
150+
$token = login($base_url, $username, $password, $domain_name, $domain_type);
151+
152+
get_assets();

0 commit comments

Comments
 (0)