Skip to content

Commit 677e2dc

Browse files
committed
Updated *.get_project and *.get_categories. Added prism.Vm.get_metadata and prism.Host.get_metadata.
1 parent 1ac31da commit 677e2dc

File tree

3 files changed

+92
-42
lines changed

3 files changed

+92
-42
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A log of changes by version and date.
77
:header: "Version", "Date", "Notes"
88
:widths: 10, 10, 60
99

10+
"1.1.19", "3/30/2021", "Updated *.get_project and *.get_categories. Added prism.Vm.get_metadata and prism.Host.get_metadata."
1011
"1.1.18", "3/26/2021", "Resolved issue payload dict config with prism.Config.*_categories and prism.Config.*_projects"
1112
"1.1.17", "3/26/2021", "Resolved issue with the returned value from prism.Cluster.get"
1213
"1.1.16", "3/26/2021", "Changed version import in __init.py__ to be absolute. Modified Vm.get to allow for conditional return of both VM disks and VM nics."

ntnx_api/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# 2) we can import it in setup.py for the same reason
44
# 3) we can import it into your module module
55

6-
__version_info__ = ('1', '1', '18')
6+
__version_info__ = ('1', '1', '19')
77
__version__ = '.'.join(__version_info__)

ntnx_api/prism.py

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,7 @@ def __init__(self, api_client):
24332433
logger = logging.getLogger('ntnx_api.prism.Hosts.__init__')
24342434
self.api_client = api_client
24352435
self.hosts = {}
2436+
self.hosts_metadata = {}
24362437

24372438
def get(self, clusteruuid=None):
24382439
"""Retrieve data for each host in a specific cluster
@@ -2457,10 +2458,35 @@ def get(self, clusteruuid=None):
24572458
if clusteruuid:
24582459
params['proxyClusterUuid'] = clusteruuid
24592460

2460-
self.hosts[clusteruuid] = self.api_client.request(uri=uri, api_version='v2.0', payload=payload, params=params).get(
2461-
'entities')
2461+
self.hosts[clusteruuid] = self.api_client.request(uri=uri, api_version='v2.0', payload=payload, params=params).get('entities')
24622462
return self.hosts[clusteruuid]
24632463

2464+
def get_metadata(self, refresh=False):
2465+
"""Retrieve metadata for each host from the connected PC instance
2466+
2467+
:returns: A list of dictionaries describing each vm from the specified cluster.
2468+
:rtype: ResponseList
2469+
"""
2470+
params = {}
2471+
payload = {
2472+
"kind": "host",
2473+
"offset": 0,
2474+
"length": 2147483647
2475+
}
2476+
uri = '/hosts/list'
2477+
2478+
if self.api_client.connection_type == "pc":
2479+
# Remove existing data for this cluster if it exists
2480+
if self.hosts_metadata or refresh:
2481+
self.hosts_metadata = {}
2482+
logger.info('removing existing data from class dict hosts_metadata')
2483+
2484+
hosts = self.api_client.request(uri=uri, api_version='v3', payload=payload, params=params).get('entities')
2485+
for host in hosts:
2486+
self.hosts_metadata[host.get('metadata').get('uuid')] = host.get('metadata')
2487+
2488+
return self.hosts_metadata
2489+
24642490
def search_uuid(self, uuid, clusteruuid=None, refresh=False):
24652491
"""Retrieve data for a specific host, in a specific cluster by host uuid
24662492
@@ -2537,12 +2563,9 @@ def search_ip(self, ip_address, clusteruuid=None, refresh=False):
25372563

25382564
return found
25392565

2540-
def get_project(self, uuid, clusteruuid=None, refresh=False):
2566+
def get_project(self, uuid, refresh=False):
25412567
"""Retrieve the project assigned to the specified host if connected to a prism central
25422568
2543-
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2544-
`connection_type` is set to `pc`.
2545-
:type clusteruuid: str, optional
25462569
:param uuid: The UUID of a host.
25472570
:type uuid: str
25482571
:param refresh: Whether to refresh the class dataset (default=False).
@@ -2553,33 +2576,34 @@ def get_project(self, uuid, clusteruuid=None, refresh=False):
25532576
"""
25542577
logger = logging.getLogger('ntnx_api.prism.Hosts.get_project')
25552578
project = ''
2556-
if self.api_client.connection_type == 'pc':
2557-
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
2558-
if metadata.get('project_reference').get('kind') == 'project':
2559-
project = metadata.get('project_reference').get('name')
2579+
if self.api_client.connection_type == "pc":
2580+
metadata = self.get_metadata(refresh=refresh)
2581+
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
2582+
if vm_metadata:
2583+
if vm_metadata.get('project_reference').get('kind') == 'project':
2584+
project = vm_metadata.get('project_reference').get('name')
25602585
return project
25612586

2562-
def get_categories(self, uuid, clusteruuid=None, refresh=False):
2587+
def get_categories(self, uuid, refresh=False):
25632588
"""Retrieve the categories assigned to the specified host if connected to a prism central
25642589
2565-
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2566-
`connection_type` is set to `pc`.
2567-
:type clusteruuid: str, optional
25682590
:param uuid: The UUID of a host.
25692591
:type uuid: str
25702592
:param refresh: Whether to refresh the class dataset (default=False).
25712593
:type refresh: bool, optional
25722594
2573-
:returns: A dictionary with all .
2595+
:returns: A dictionary with all categories for the specified host.
25742596
:rtype: ResponseDict
25752597
"""
25762598
logger = logging.getLogger('ntnx_api.prism.Hosts.get_categories')
2577-
categories = []
2578-
if self.api_client.connection_type == 'pc':
2579-
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
2580-
if metadata.get('project_reference').get('kind') == 'project':
2581-
for key, value in metadata.get('categories').items():
2582-
categories[key] = value
2599+
categories = {}
2600+
if self.api_client.connection_type == "pc":
2601+
metadata = self.get_metadata(refresh=refresh)
2602+
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
2603+
if vm_metadata:
2604+
if 'categories' in vm_metadata:
2605+
for key, value in vm_metadata.get('categories').items():
2606+
categories[key] = value
25832607
return categories
25842608

25852609

@@ -2594,6 +2618,7 @@ def __init__(self, api_client):
25942618
logger = logging.getLogger('ntnx_api.prism.Vms.__init__')
25952619
self.api_client = api_client
25962620
self.vms = {}
2621+
self.vms_metadata = {}
25972622

25982623
def get(self, clusteruuid=None, include_disks=True, include_nics=True):
25992624
"""Retrieve host data for each virtual machine in a specific cluster
@@ -2636,6 +2661,32 @@ def get(self, clusteruuid=None, include_disks=True, include_nics=True):
26362661
self.vms[clusteruuid] = self.api_client.request(uri=uri, api_version='v2.0', payload=payload, params=params).get('entities')
26372662
return self.vms[clusteruuid]
26382663

2664+
def get_metadata(self, refresh=False):
2665+
"""Retrieve metadata for each virtual machine from the connected PC instance
2666+
2667+
:returns: A list of dictionaries describing each vm from the specified cluster.
2668+
:rtype: ResponseList
2669+
"""
2670+
params = {}
2671+
payload = {
2672+
"kind": "vm",
2673+
"offset": 0,
2674+
"length": 2147483647
2675+
}
2676+
uri = '/vms/list'
2677+
2678+
if self.api_client.connection_type == "pc":
2679+
# Remove existing data for this cluster if it exists
2680+
if self.vms_metadata or refresh:
2681+
self.vms_metadata = {}
2682+
logger.info('removing existing data from class dict vms_metadata')
2683+
2684+
vms = self.api_client.request(uri=uri, api_version='v3', payload=payload, params=params).get('entities')
2685+
for vm in vms:
2686+
self.vms_metadata[vm.get('metadata').get('uuid')] = vm.get('metadata')
2687+
2688+
return self.vms_metadata
2689+
26392690
def search_uuid(self, uuid, clusteruuid=None, refresh=False):
26402691
"""Retrieve data for a specific vm, in a specific cluster by vm uuid
26412692
@@ -2688,49 +2739,47 @@ def search_name(self, name, clusteruuid=None, refresh=False):
26882739

26892740
return found
26902741

2691-
def get_project(self, uuid, clusteruuid=None, refresh=False):
2742+
def get_project(self, uuid, refresh=False):
26922743
"""Retrieve the project assigned to the specified VM if connected to a prism central
26932744
2694-
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2695-
`connection_type` is set to `pc`.
2696-
:type clusteruuid: str, optional
26972745
:param uuid: The UUID of a VM.
26982746
:type uuid: str
2699-
:param refresh: Whether to refresh the class VM dataset (default=False).
2747+
:param refresh: Whether to refresh the class VM Metadata dataset (default=False).
27002748
:type refresh: bool, optional
27012749
27022750
:returns: A string containing the project name.
27032751
:rtype: str
27042752
"""
27052753
logger = logging.getLogger('ntnx_api.prism.Vms.get_project')
27062754
project = ''
2707-
if self.api_client.connection_type == 'pc':
2708-
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
2709-
if metadata.get('project_reference').get('kind') == 'project':
2710-
project = metadata.get('project_reference').get('name')
2755+
if self.api_client.connection_type == "pc":
2756+
metadata = self.get_metadata(refresh=refresh)
2757+
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
2758+
if vm_metadata:
2759+
if vm_metadata.get('project_reference').get('kind') == 'project':
2760+
project = vm_metadata.get('project_reference').get('name')
27112761
return project
27122762

2713-
def get_categories(self, uuid, clusteruuid=None, refresh=False):
2763+
def get_categories(self, uuid, refresh=False):
27142764
"""Retrieve the categories assigned to the specified VM if connected to a prism central
27152765
2716-
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2717-
`connection_type` is set to `pc`.
2718-
:type clusteruuid: str, optional
27192766
:param uuid: The UUID of a VM.
27202767
:type uuid: str
2721-
:param refresh: Whether to refresh the class VM dataset (default=False).
2768+
:param refresh: Whether to refresh the class VM Metadata dataset (default=False).
27222769
:type refresh: bool, optional
27232770
27242771
:returns: A dictionary with all .
27252772
:rtype: ResponseDict
27262773
"""
27272774
logger = logging.getLogger('ntnx_api.prism.Vms.get_categories')
2728-
categories = []
2729-
if self.api_client.connection_type == 'pc':
2730-
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
2731-
if metadata.get('project_reference').get('kind') == 'project':
2732-
for key, value in metadata.get('categories').items():
2733-
categories[key] = value
2775+
categories = {}
2776+
if self.api_client.connection_type == "pc":
2777+
metadata = self.get_metadata(refresh=refresh)
2778+
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
2779+
if vm_metadata:
2780+
if 'categories' in vm_metadata:
2781+
for key, value in vm_metadata.get('categories').items():
2782+
categories[key] = value
27342783
return categories
27352784

27362785

0 commit comments

Comments
 (0)