|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use LWP::UserAgent; |
| 4 | +use JSON; |
| 5 | + |
| 6 | +my $content_type = "application/vnd.netbackup+json; version=4.0"; |
| 7 | + |
| 8 | +# Initialize HTTP client |
| 9 | +my $http = LWP::UserAgent->new( |
| 10 | + ssl_opts => { verify_hostname => 0, verify_peer => 0 }, |
| 11 | + protocols_allowed => ['https'] |
| 12 | +); |
| 13 | + |
| 14 | +# List api keys |
| 15 | +sub list_api_keys { |
| 16 | + my ($base_url, $token) = @_; |
| 17 | + |
| 18 | + my $media_type = "application/vnd.netbackup+json;version=4"; |
| 19 | + my $api_keys_url = "$base_url/security/api-keys"; |
| 20 | + |
| 21 | + # Update http client with default header |
| 22 | + $http->default_header('Accept' => $media_type); |
| 23 | + $http->default_header('Authorization' => $token); |
| 24 | + |
| 25 | + my $response = $http->get($api_keys_url); |
| 26 | + |
| 27 | + unless ($response->is_success) { |
| 28 | + print "HTTP GET error code: ", $response->code, "\n"; |
| 29 | + print "HTTP GET error message: ", $response->message, "\n"; |
| 30 | + |
| 31 | + die "Failed to get api keys.\n"; |
| 32 | + } |
| 33 | + |
| 34 | + my $response_content = decode_json($response->content); |
| 35 | + print "API Keys: ", $response_content, "\n"; |
| 36 | +} |
| 37 | + |
| 38 | +# List rbac roles |
| 39 | +sub list_gen1_roles { |
| 40 | + my ($base_url, $token) = @_; |
| 41 | + |
| 42 | + my $media_type = "application/vnd.netbackup+json;version=1"; |
| 43 | + my $gen1_roles_url = "$base_url/rbac/roles"; |
| 44 | + |
| 45 | + $http->default_header('Accept' => $media_type); |
| 46 | + $http->default_header('Authorization' => $token); |
| 47 | + |
| 48 | + my $response = $http->get($gen1_roles_url); |
| 49 | + |
| 50 | + unless ($response->is_success) { |
| 51 | + print "HTTP GET error code: ", $response->code, "\n"; |
| 52 | + print "HTTP GET error message: ", $response->message, "\n"; |
| 53 | + |
| 54 | + die "Failed to get Gen-1 roles.\n"; |
| 55 | + } |
| 56 | + |
| 57 | + my $response_content = decode_json($response->content); |
| 58 | + print "Gen-1 roles: ", $response_content, "\n"; |
| 59 | +} |
| 60 | + |
| 61 | +# Create access control role |
| 62 | +sub create_access_control_role { |
| 63 | + my ($base_url, $token, $name, $desc) = @_; |
| 64 | + |
| 65 | + my $media_type = "application/vnd.netbackup+json;version=4"; |
| 66 | + my $access_control_roles_url = "$base_url/access-control/roles"; |
| 67 | + |
| 68 | + $http->default_header('Accept' => $media_type); |
| 69 | + $http->default_header('Authorization' => $token); |
| 70 | + |
| 71 | + my %role = ( |
| 72 | + 'data' => { |
| 73 | + 'type' => 'accessControlRole', |
| 74 | + 'attribute' => { |
| 75 | + 'name' => $name, |
| 76 | + 'description' => $desc |
| 77 | + } |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + my $role_body = encode_json(\%role); |
| 82 | + |
| 83 | + my $response = $http->post($access_control_role_url, 'Content-Type' => $media_type, Content => $role_body); |
| 84 | + |
| 85 | + unless ($response->is_success) { |
| 86 | + print "HTTP POST error code: ", $response->code, "\n"; |
| 87 | + print "HTTP POST error message: ", $response->message, "\n"; |
| 88 | + |
| 89 | + die "Failed to create $name role.\n"; |
| 90 | + } |
| 91 | + |
| 92 | + my $response_content = decode_json($response->content); |
| 93 | + my $new_role_id = $response_content->{data}{id}; |
| 94 | + print "$name role created with ID $new_role_id\n"; |
| 95 | +} |
| 96 | + |
| 97 | +1; |
0 commit comments