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
54import argparse
65import requests
76import login .login_api as login_api
87
9- def print_usage ():
8+ def get_usage ():
109 return ("\n The command should be run from the parent directory of the 'assets' directory:\n "
11- "\t python -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 ())
1517parser .add_argument ('-nbserver' , required = True )
1618parser .add_argument ('-username' , required = True )
1719parser .add_argument ('-password' , required = True )
1820parser .add_argument ('-domainName' , required = True )
1921parser .add_argument ('-domainType' , required = True )
22+ parser .add_argument ('-assetType' , default = "vm" , choices = ['vm' , 'vmGroup' ])
2023parser .add_argument ('-assetsFilter' , default = "" )
2124args = parser .parse_args ()
2225
@@ -25,28 +28,33 @@ def print_usage():
2528password = args .password
2629domainName = args .domainName
2730domainType = args .domainType
31+ assetType = args .assetType
2832assetsFilter = args .assetsFilter
2933
3034base_url = "https://" + nbserver + "/netbackup"
3135vm_assets_url = base_url + "/asset-service/workloads/vmware/assets"
3236
3337default_sort = "commonAssetAttributes.displayName"
3438
35- assetTypeFilter = "(assetType eq 'vm')" ;
39+ print ("\n Executing the script..." )
40+ jwt = login_api .perform_login (base_url , username , password , domainName , domainType )
41+
42+ print ("\n Getting 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+
3651if assetsFilter != "" :
3752 assetsFilter = assetsFilter + " and " + assetTypeFilter
3853else :
3954 assetsFilter = assetTypeFilter
4055
41- print ("\n Executing the script..." )
42-
43- jwt = login_api .perform_login (base_url , username , password , domainName , domainType )
44-
4556headers = {'Authorization' : jwt }
4657
47- print ("\n Getting VMware assets..." )
48- print ("Printing the following asset details: DisplayName, InstanceId, vCenter, ProtectedByPlanNames\n " )
49-
5058def 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
8394get_vmware_assets ()
8495
85- print ("\n Script completed successfully !\n " )
96+ print ("\n Script completed!\n " )
0 commit comments