Skip to content

Commit 0228419

Browse files
committed
Added ability to search for vm/host projects & categories if connected to prism central
1 parent fc77d19 commit 0228419

File tree

3 files changed

+112
-8
lines changed

3 files changed

+112
-8
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.14", "3/25/2021", "Added the ability to search for and return host/vm project & categories."
1011
"1.1.13", "3/23/2021", "Resolved code quality issues in client.py"
1112
"1.1.12", "3/23/2021", "Updated docsctings within for client.py"
1213
"1.1.11", "3/22/2021", "Added docstring for prism.Config.change_ui_admin_password"

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', '13')
6+
__version_info__ = ('1', '1', '14')
77
__version__ = '.'.join(__version_info__)

ntnx_api/prism.py

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ def get_all_uuids(self):
22932293
logger.info('found cluster uuids: {0}'.format(self.clusters))
22942294
return self.clusters
22952295

2296-
def get(self, clusteruuid=None):
2296+
def get(self, clusteruuid=None, refresh=False):
22972297
"""Retrieve data for a specific cluster
22982298
22992299
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
@@ -2307,6 +2307,11 @@ def get(self, clusteruuid=None):
23072307
payload = None
23082308
uri = '/cluster'
23092309

2310+
# Remove existing data for this cluster if it exists
2311+
if self.cluster.get(clusteruuid):
2312+
self.cluster.pop(clusteruuid)
2313+
logger.info('removing existing data from class dict cluster for cluster {0}'.format(clusteruuid))
2314+
23102315
if clusteruuid:
23112316
params['proxyClusterUuid'] = clusteruuid
23122317

@@ -2429,7 +2434,7 @@ def get(self, clusteruuid=None):
24292434
'entities')
24302435
return self.hosts[clusteruuid]
24312436

2432-
def search_uuid(self, uuid, clusteruuid=None):
2437+
def search_uuid(self, uuid, clusteruuid=None, refresh=False):
24332438
"""Retrieve data for a specific host, in a specific cluster by host uuid
24342439
24352440
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
@@ -2443,7 +2448,7 @@ def search_uuid(self, uuid, clusteruuid=None):
24432448
"""
24442449
logger = logging.getLogger('ntnx_api.prism.Hosts.search_uuid')
24452450
found = {}
2446-
if not self.hosts.get(clusteruuid):
2451+
if not self.hosts.get(clusteruuid) or refresh:
24472452
self.get(clusteruuid)
24482453

24492454
for entity in self.hosts.get(clusteruuid):
@@ -2453,21 +2458,23 @@ def search_uuid(self, uuid, clusteruuid=None):
24532458

24542459
return found
24552460

2456-
def search_name(self, name, clusteruuid=None):
2461+
def search_name(self, name, clusteruuid=None, refresh=False):
24572462
"""Retrieve data for a specific host, in a specific cluster by host name
24582463
24592464
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
24602465
`connection_type` is set to `pc`.
24612466
:type clusteruuid: str, optional
24622467
:param name: A host name to search for.
24632468
:type name: str, optional
2469+
:param refresh: Whether to refresh the class dataset (default=False).
2470+
:type refresh: bool, optional
24642471
24652472
:returns: A dictionary describing the found host.
24662473
:rtype: ResponseDict
24672474
"""
24682475
logger = logging.getLogger('ntnx_api.prism.Hosts.search_name')
24692476
found = {}
2470-
if not self.hosts.get(clusteruuid):
2477+
if not self.hosts.get(clusteruuid) or refresh:
24712478
self.get(clusteruuid)
24722479

24732480
for entity in self.hosts.get(clusteruuid):
@@ -2477,21 +2484,23 @@ def search_name(self, name, clusteruuid=None):
24772484

24782485
return found
24792486

2480-
def search_ip(self, ip_address, clusteruuid=None):
2487+
def search_ip(self, ip_address, clusteruuid=None, refresh=False):
24812488
"""Retrieve data for a specific host, in a specific cluster by ip_address. The CVM, Hypervisor and IPMI IP addresses will be tested
24822489
24832490
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
24842491
`connection_type` is set to `pc`.
24852492
:type clusteruuid: str, optional
24862493
:param ip_address: A host name to search for.
24872494
:type ip_address: str, optional
2495+
:param refresh: Whether to refresh the class dataset (default=False).
2496+
:type refresh: bool, optional
24882497
24892498
:returns: A dictionary describing the found host.
24902499
:rtype: ResponseDict
24912500
"""
24922501
logger = logging.getLogger('ntnx_api.prism.Hosts.search_ip')
24932502
found = {}
2494-
if not self.hosts.get(clusteruuid):
2503+
if not self.hosts.get(clusteruuid) or refresh:
24952504
self.get(clusteruuid)
24962505

24972506
for entity in self.hosts.get(clusteruuid):
@@ -2501,6 +2510,51 @@ def search_ip(self, ip_address, clusteruuid=None):
25012510

25022511
return found
25032512

2513+
def get_project(self, uuid, clusteruuid=None, refresh=False):
2514+
"""Retrieve the project assigned to the specified host if connected to a prism central
2515+
2516+
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2517+
`connection_type` is set to `pc`.
2518+
:type clusteruuid: str, optional
2519+
:param uuid: The UUID of a host.
2520+
:type uuid: str
2521+
:param refresh: Whether to refresh the class dataset (default=False).
2522+
:type refresh: bool, optional
2523+
2524+
:returns: A string containing the project name.
2525+
:rtype: str
2526+
"""
2527+
logger = logging.getLogger('ntnx_api.prism.Hosts.get_project')
2528+
project = ''
2529+
if self.api_client.connection_type == 'pc':
2530+
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
2531+
if metadata.get('project_reference').get('kind') == 'project':
2532+
project = metadata.get('project_reference').get('name')
2533+
return project
2534+
2535+
def get_categories(self, uuid, clusteruuid=None, refresh=False):
2536+
"""Retrieve the categories assigned to the specified host if connected to a prism central
2537+
2538+
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2539+
`connection_type` is set to `pc`.
2540+
:type clusteruuid: str, optional
2541+
:param uuid: The UUID of a host.
2542+
:type uuid: str
2543+
:param refresh: Whether to refresh the class dataset (default=False).
2544+
:type refresh: bool, optional
2545+
2546+
:returns: A dictionary with all .
2547+
:rtype: ResponseDict
2548+
"""
2549+
logger = logging.getLogger('ntnx_api.prism.Hosts.get_categories')
2550+
categories = []
2551+
if self.api_client.connection_type == 'pc':
2552+
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
2553+
if metadata.get('project_reference').get('kind') == 'project':
2554+
for key, value in metadata.get('categories').items():
2555+
categories[key] = value
2556+
return categories
2557+
25042558

25052559
class Vms(object):
25062560
"""A class to represent a Nutanix Clusters Virtual Machines
@@ -2548,6 +2602,8 @@ def search_uuid(self, uuid, clusteruuid=None, refresh=False):
25482602
:type clusteruuid: str, optional
25492603
:param uuid: A vm uuid to search for.
25502604
:type uuid: str, optional
2605+
:param refresh: Whether to refresh the class VM dataset (default=False).
2606+
:type refresh: bool, optional
25512607
25522608
:returns: A dictionary describing the found vm.
25532609
:rtype: ResponseDict
@@ -2572,6 +2628,8 @@ def search_name(self, name, clusteruuid=None, refresh=False):
25722628
:type clusteruuid: str, optional
25732629
:param name: A vm name to search for.
25742630
:type name: str, optional
2631+
:param refresh: Whether to refresh the class VM dataset (default=False).
2632+
:type refresh: bool, optional
25752633
25762634
:returns: A dictionary describing the found vm.
25772635
:rtype: ResponseDict
@@ -2588,6 +2646,51 @@ def search_name(self, name, clusteruuid=None, refresh=False):
25882646

25892647
return found
25902648

2649+
def get_project(self, uuid, clusteruuid=None, refresh=False):
2650+
"""Retrieve the project assigned to the specified VM if connected to a prism central
2651+
2652+
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2653+
`connection_type` is set to `pc`.
2654+
:type clusteruuid: str, optional
2655+
:param uuid: The UUID of a VM.
2656+
:type uuid: str
2657+
:param refresh: Whether to refresh the class VM dataset (default=False).
2658+
:type refresh: bool, optional
2659+
2660+
:returns: A string containing the project name.
2661+
:rtype: str
2662+
"""
2663+
logger = logging.getLogger('ntnx_api.prism.Vms.get_project')
2664+
project = ''
2665+
if self.api_client.connection_type == 'pc':
2666+
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
2667+
if metadata.get('project_reference').get('kind') == 'project':
2668+
project = metadata.get('project_reference').get('name')
2669+
return project
2670+
2671+
def get_categories(self, uuid, clusteruuid=None, refresh=False):
2672+
"""Retrieve the categories assigned to the specified VM if connected to a prism central
2673+
2674+
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2675+
`connection_type` is set to `pc`.
2676+
:type clusteruuid: str, optional
2677+
:param uuid: The UUID of a VM.
2678+
:type uuid: str
2679+
:param refresh: Whether to refresh the class VM dataset (default=False).
2680+
:type refresh: bool, optional
2681+
2682+
:returns: A dictionary with all .
2683+
:rtype: ResponseDict
2684+
"""
2685+
logger = logging.getLogger('ntnx_api.prism.Vms.get_categories')
2686+
categories = []
2687+
if self.api_client.connection_type == 'pc':
2688+
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
2689+
if metadata.get('project_reference').get('kind') == 'project':
2690+
for key, value in metadata.get('categories').items():
2691+
categories[key] = value
2692+
return categories
2693+
25912694

25922695
class Images(object):
25932696
"""A class to represent a Nutanix Clusters Images

0 commit comments

Comments
 (0)