1+ # !/usr/bin/env perl
2+ use LWP::UserAgent;
3+ use JSON;
4+
5+ my $content_type_v2 = " application/vnd.netbackup+json; version=2.0" ;
6+
7+ my $json = JSON-> new;
8+ my $ua = LWP::UserAgent-> new(
9+ ssl_opts => { verify_hostname => 0, verify_peer => 0},
10+ );
11+
12+ # create object group to access only VMware policies
13+ my $object_group_id ;
14+ sub create_rbac_object_group_for_VMware_policy {
15+
16+ my @argument_list = @_ ;
17+ my $base_url = $argument_list [0];
18+ my $token = $argument_list [1];
19+
20+ my $url = " $base_url /rbac/object-groups" ;
21+ my $object_group_name = " VMwarePolicy" ;
22+
23+ my $req = HTTP::Request-> new(POST => $url );
24+ $req -> header(' content-type' => $content_type_v2 );
25+ $req -> header(' Authorization' => $token );
26+
27+ my $post_data = qq( { "data": { "type": "object-group", "attributes": {
28+ "name": "$object_group_name ", "criteria": [
29+ { "objectCriterion": "policyType eq 40", "objectType": "NBPolicy" } ]} } }) ;
30+ $req -> content($post_data );
31+
32+ print " \n\n **************************************************************" ;
33+ print " \n\n Making POST Request to create object group to access only VMware policies \n\n " ;
34+
35+ my $resp = $ua -> request($req );
36+ if ($resp -> is_success) {
37+ my $json_message = decode_json($resp -> decoded_content);
38+ $object_group_id = $json_message -> {" data" }{" id" };
39+ print " Object group [$object_group_name ] is created with id [$object_group_id ] to access only VMware policies with status code: " , $resp -> code, " \n " ;
40+ }
41+ else {
42+ print " HTTP POST error code: " , $resp -> code, " \n " ;
43+ print " HTTP POST error message: " , $resp -> message, " \n " ;
44+ }
45+ }
46+
47+ # create access rule for a user with object group
48+ my $access_rule_id ;
49+ sub create_rbac_access_rules {
50+
51+ my @argument_list = @_ ;
52+ my $base_url = $argument_list [0];
53+ my $token = $argument_list [1];
54+ my $user = $argument_list [2];
55+ my $domain = $argument_list [3];
56+ my $domainType = $argument_list [4];
57+
58+ my $url = " $base_url /rbac/access-rules" ;
59+
60+ my $req = HTTP::Request-> new(POST => $url );
61+ $req -> header(' content-type' => $content_type_v2 );
62+ $req -> header(' Authorization' => $token );
63+
64+ my $post_data = qq( { "data": { "type": "access-rule", "attributes": {
65+ "description": "adding VMwarePolicy object group"}, "relationships": {
66+ "userPrincipal": { "data": { "type" : "user-principal", "id": "$domain :$user :$domainType :$user " } },
67+ "objectGroup": { "data": { "type": "object-group", "id": "$object_group_id " } },
68+ "role": { "data": { "type": "role", "id": "3" } } } } }) ;
69+ $req -> content($post_data );
70+
71+ print " \n\n **************************************************************" ;
72+ print " \n\n Making POST Request to create access rule \n\n " ;
73+
74+ my $resp = $ua -> request($req );
75+ if ($resp -> is_success) {
76+ my $json_message = decode_json($resp -> decoded_content);
77+ $access_rule_id = $json_message -> {" data" }{" id" };
78+ print " Access rule is created with id [$access_rule_id ] to access only VMware policies with status code: " , $resp -> code, " \n " ;
79+ }
80+ else {
81+ print " HTTP POST error code: " , $resp -> code, " \n " ;
82+ print " HTTP POST error message: " , $resp -> message, " \n " ;
83+ }
84+ }
85+
86+ # subroutine to delete the object group
87+ sub delete_rbac_object_group_for_VMware_policy {
88+
89+ my @argument_list = @_ ;
90+ my $base_url = $argument_list [0];
91+ my $token = $argument_list [1];
92+
93+ my $url = " $base_url /rbac/object-groups/$object_group_id " ;
94+
95+ my $req = HTTP::Request-> new(DELETE => $url );
96+ $req -> header(' content-type' => $content_type_v2 );
97+ $req -> header(' Authorization' => $token );
98+
99+ print " \n\n **************************************************************" ;
100+ print " \n\n Making DELETE Request to remove the object group \n\n " ;
101+
102+ my $resp = $ua -> request($req );
103+ if ($resp -> is_success) {
104+ print " Policy is deleted with status code: " , $resp -> code, " \n " ;
105+ }
106+ else {
107+ print " HTTP DELETE error code: " , $resp -> code, " \n " ;
108+ print " HTTP DELETE error message: " , $resp -> message, " \n " ;
109+ }
110+ }
111+
112+ # subroutine to delete the object group
113+ sub delete_rbac_access_rule {
114+
115+ my @argument_list = @_ ;
116+ my $base_url = $argument_list [0];
117+ my $token = $argument_list [1];
118+
119+ my $url = " $base_url /rbac/access-rules/$access_rule_id " ;
120+
121+ my $req = HTTP::Request-> new(DELETE => $url );
122+ $req -> header(' content-type' => $content_type_v2 );
123+ $req -> header(' Authorization' => $token );
124+
125+ print " \n\n **************************************************************" ;
126+ print " \n\n Making DELETE Request to remove the object group \n\n " ;
127+
128+ my $resp = $ua -> request($req );
129+ if ($resp -> is_success) {
130+ print " Policy is deleted with status code: " , $resp -> code, " \n " ;
131+ }
132+ else {
133+ print " HTTP DELETE error code: " , $resp -> code, " \n " ;
134+ print " HTTP DELETE error message: " , $resp -> message, " \n " ;
135+ }
136+ }
137+
138+ # create VMWare policy with the name vmware_test_policy with default values
139+ sub create_bpnbat_user {
140+
141+ my @argument_list = @_ ;
142+ my $username = $argument_list [0];
143+ my $domainName = $argument_list [1];
144+ my $password = $argument_list [2];
145+
146+ print " \n\n **************************************************************" ;
147+ print " \n\n Creating user for RBAC filtering using bpnbat \n\n " ;
148+
149+ if ( $^O =~ / MSWin32/ ) {
150+ my $path = ' C:/\"Program Files\"/Veritas/NetBackup/bin/bpnbat.exe' ;
151+ my $rc = system (qq( $path -AddUser $username $domainName $password ) ); # returns exit status values
152+ die " system() failed with status $rc " unless $rc == 0;
153+ } else {
154+ my $path = ' /usr/openv/netbackup/bin/bpnbat' ;
155+ my $rc = system (qq( $path -AddUser $username $domainName $password ) ); # returns exit status values
156+ die " system() failed with status $rc " unless $rc == 0;
157+ }
158+ print " \n\n " ;
159+ }
160+
161+ # create VMWare policy with the name vmware_test_policy with default values
162+ sub create_oracle_policy_with_defaults {
163+
164+ my @argument_list = @_ ;
165+ my $base_url = $argument_list [0];
166+ my $token = $argument_list [1];
167+
168+ my $url = " $base_url /config/policies" ;
169+ my $policy_name = " oracle_test_policy" ;
170+
171+ my $req = HTTP::Request-> new(POST => $url );
172+ $req -> header(' content-type' => $content_type_v2 );
173+ $req -> header(' Authorization' => $token );
174+
175+ my $post_data = qq( { "data": { "type": "policy", "id": "$policy_name ", "attributes": {
176+ "policy": { "policyName": "$policy_name ", "policyType": "Oracle" } } } }) ;
177+ $req -> content($post_data );
178+
179+ print " \n\n **************************************************************" ;
180+ print " \n\n Making POST Request to create Oracle policy with defaults \n\n " ;
181+
182+ my $resp = $ua -> request($req );
183+ if ($resp -> is_success) {
184+ print " Policy [$policy_name ] with default values is create with status code: " , $resp -> code, " \n " ;
185+ }
186+ else {
187+ print " HTTP POST error code: " , $resp -> code, " \n " ;
188+ print " HTTP POST error message: " , $resp -> message, " \n " ;
189+ }
190+ }
191+
192+ # create VMWare policy with the name vmware_test_policy with default values
193+ sub create_vmware_policy_with_defaults {
194+
195+ my @argument_list = @_ ;
196+ my $base_url = $argument_list [0];
197+ my $token = $argument_list [1];
198+
199+ my $url = " $base_url /config/policies" ;
200+ my $policy_name = " vmware_test_policy" ;
201+
202+ my $req = HTTP::Request-> new(POST => $url );
203+ $req -> header(' content-type' => $content_type_v2 );
204+ $req -> header(' Authorization' => $token );
205+
206+ my $post_data = qq( { "data": { "type": "policy", "id": "$policy_name ", "attributes": {
207+ "policy": { "policyName": "$policy_name ", "policyType": "VMware" } } } }) ;
208+ $req -> content($post_data );
209+
210+ print " \n\n **************************************************************" ;
211+ print " \n\n Making POST Request to create VMWare policy with defaults \n\n " ;
212+
213+ my $resp = $ua -> request($req );
214+ if ($resp -> is_success) {
215+ print " Policy [$policy_name ] with default values is create with status code: " , $resp -> code, " \n " ;
216+ }
217+ else {
218+ print " HTTP POST error code: " , $resp -> code, " \n " ;
219+ print " HTTP POST error message: " , $resp -> message, " \n " ;
220+ }
221+ }
222+
223+ 1;
0 commit comments