Skip to content

Commit 4efd392

Browse files
santhosh-vrtssanthosh-vrts
authored andcommitted
Get VMware assets API py script enhancements. Updated script to get VM and VM group assets.
1 parent 79c8af3 commit 4efd392

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
## The script can be run with Python 3.5 or higher version.
22
## The script requires 'requests' library to make the API calls. The library can be installed using the command: pip install requests.
33

4-
import sys
54
import argparse
65
import requests
76
import login.login_api as login_api
87

9-
def print_usage():
8+
def get_usage():
109
return ("\nThe command should be run from the parent directory of the 'assets' directory:\n"
11-
"\tpython -Wignore -m assets.get_vmware_assets -nbserver <server> -username <username> -password <password> "
12-
"-domainName <domainName> -domainType <domainType> [-assetsFilter <filter criteria>]\n")
10+
"python -Wignore -m assets.get_vmware_assets -nbserver <server> -username <username> -password <password> "
11+
"-domainName <domainName> -domainType <domainType> [-assetType <vm|vmGroup>] [-assetsFilter <filter criteria>]\n"
12+
"Optional arguments:\n"
13+
"assetType - vm or vmGroup. Default is 'vm'.\n"
14+
"assetsFilter - OData filter to filter the returned assets (VMs or VM Groups). If not specified, returns all the assets.\n")
1315

14-
parser = argparse.ArgumentParser(usage = print_usage())
16+
parser = argparse.ArgumentParser(usage = get_usage())
1517
parser.add_argument('-nbserver', required=True)
1618
parser.add_argument('-username', required=True)
1719
parser.add_argument('-password', required=True)
1820
parser.add_argument('-domainName', required=True)
1921
parser.add_argument('-domainType', required=True)
22+
parser.add_argument('-assetType', default="vm", choices=['vm', 'vmGroup'])
2023
parser.add_argument('-assetsFilter', default="")
2124
args = parser.parse_args()
2225

@@ -25,28 +28,33 @@ def print_usage():
2528
password = args.password
2629
domainName = args.domainName
2730
domainType = args.domainType
31+
assetType = args.assetType
2832
assetsFilter = args.assetsFilter
2933

3034
base_url = "https://" + nbserver + "/netbackup"
3135
vm_assets_url = base_url + "/asset-service/workloads/vmware/assets"
3236

3337
default_sort = "commonAssetAttributes.displayName"
3438

35-
assetTypeFilter = "(assetType eq 'vm')";
39+
print("\nExecuting the script...")
40+
jwt = login_api.perform_login(base_url, username, password, domainName, domainType)
41+
42+
print("\nGetting VMware {} assets...".format(assetType))
43+
44+
if assetType == "vm":
45+
assetTypeFilter = "(assetType eq 'vm')";
46+
print("Printing the following VM details: VM Display Name, Instance Id, vCenter, Protection Plan Names\n")
47+
elif assetType == "vmGroup":
48+
assetTypeFilter = "(assetType eq 'vmGroup')";
49+
print("Printing the following VM group details: VM Group Name, VM Server, Filter Criteria, Protection Plan Names\n")
50+
3651
if assetsFilter != "":
3752
assetsFilter = assetsFilter + " and " + assetTypeFilter
3853
else:
3954
assetsFilter = assetTypeFilter
4055

41-
print("\nExecuting the script...")
42-
43-
jwt = login_api.perform_login(base_url, username, password, domainName, domainType)
44-
4556
headers = {'Authorization': jwt}
4657

47-
print("\nGetting VMware assets...")
48-
print("Printing the following asset details: DisplayName, InstanceId, vCenter, ProtectedByPlanNames\n")
49-
5058
def get_vmware_assets():
5159
offset = 0
5260
next = True
@@ -56,8 +64,8 @@ def get_vmware_assets():
5664
assets = assets_response.json()
5765

5866
if assets_response.status_code != 200:
59-
print("VMware Assets API failed with status code {} and {}\n".format(resp.status_code, resp.json()))
60-
raise SystemExit("\n\n")
67+
print("VMware Assets API returned status code: {}, response: {}\n".format(assets_response.status_code, assets_response.json()))
68+
raise SystemExit()
6169

6270
print_assets(assets['data'])
6371

@@ -77,9 +85,12 @@ def print_assets(assets_data):
7785
for asset_protection in asset_protection_list:
7886
asset_protection_plans.append(asset_protection['protectionPlanName'])
7987

80-
print(asset_common_attrs['displayName'], asset_attrs['instanceUuid'], asset_attrs['vCenter'], asset_protection_plans, sep="\t")
88+
if assetType == "vm":
89+
print(asset_common_attrs['displayName'], asset_attrs['instanceUuid'], asset_attrs['vCenter'], asset_protection_plans, sep="\t")
90+
elif assetType == "vmGroup":
91+
print(asset_common_attrs['displayName'], asset_attrs['filterConstraint'], asset_attrs['oDataQueryFilter'], asset_protection_plans, sep="\t")
8192

8293

8394
get_vmware_assets()
8495

85-
print("\nScript completed successfully!\n")
96+
print("\nScript completed!\n")

0 commit comments

Comments
 (0)