Skip to content

Commit

Permalink
Updated *.get_project and *.get_categories. Added prism.Vm.get_metada…
Browse files Browse the repository at this point in the history
…ta and prism.Host.get_metadata.
  • Loading branch information
grdavies-ntnx committed Mar 30, 2021
1 parent 1ac31da commit 677e2dc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 42 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A log of changes by version and date.
:header: "Version", "Date", "Notes"
:widths: 10, 10, 60

"1.1.19", "3/30/2021", "Updated *.get_project and *.get_categories. Added prism.Vm.get_metadata and prism.Host.get_metadata."
"1.1.18", "3/26/2021", "Resolved issue payload dict config with prism.Config.*_categories and prism.Config.*_projects"
"1.1.17", "3/26/2021", "Resolved issue with the returned value from prism.Cluster.get"
"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."
Expand Down
2 changes: 1 addition & 1 deletion ntnx_api/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# 2) we can import it in setup.py for the same reason
# 3) we can import it into your module module

__version_info__ = ('1', '1', '18')
__version_info__ = ('1', '1', '19')
__version__ = '.'.join(__version_info__)
131 changes: 90 additions & 41 deletions ntnx_api/prism.py
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,7 @@ def __init__(self, api_client):
logger = logging.getLogger('ntnx_api.prism.Hosts.__init__')
self.api_client = api_client
self.hosts = {}
self.hosts_metadata = {}

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

self.hosts[clusteruuid] = self.api_client.request(uri=uri, api_version='v2.0', payload=payload, params=params).get(
'entities')
self.hosts[clusteruuid] = self.api_client.request(uri=uri, api_version='v2.0', payload=payload, params=params).get('entities')
return self.hosts[clusteruuid]

def get_metadata(self, refresh=False):
"""Retrieve metadata for each host from the connected PC instance
:returns: A list of dictionaries describing each vm from the specified cluster.
:rtype: ResponseList
"""
params = {}
payload = {
"kind": "host",
"offset": 0,
"length": 2147483647
}
uri = '/hosts/list'

if self.api_client.connection_type == "pc":
# Remove existing data for this cluster if it exists
if self.hosts_metadata or refresh:
self.hosts_metadata = {}
logger.info('removing existing data from class dict hosts_metadata')

hosts = self.api_client.request(uri=uri, api_version='v3', payload=payload, params=params).get('entities')
for host in hosts:
self.hosts_metadata[host.get('metadata').get('uuid')] = host.get('metadata')

return self.hosts_metadata

def search_uuid(self, uuid, clusteruuid=None, refresh=False):
"""Retrieve data for a specific host, in a specific cluster by host uuid
Expand Down Expand Up @@ -2537,12 +2563,9 @@ def search_ip(self, ip_address, clusteruuid=None, refresh=False):

return found

def get_project(self, uuid, clusteruuid=None, refresh=False):
def get_project(self, uuid, refresh=False):
"""Retrieve the project assigned to the specified host if connected to a prism central
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
`connection_type` is set to `pc`.
:type clusteruuid: str, optional
:param uuid: The UUID of a host.
:type uuid: str
:param refresh: Whether to refresh the class dataset (default=False).
Expand All @@ -2553,33 +2576,34 @@ def get_project(self, uuid, clusteruuid=None, refresh=False):
"""
logger = logging.getLogger('ntnx_api.prism.Hosts.get_project')
project = ''
if self.api_client.connection_type == 'pc':
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
if metadata.get('project_reference').get('kind') == 'project':
project = metadata.get('project_reference').get('name')
if self.api_client.connection_type == "pc":
metadata = self.get_metadata(refresh=refresh)
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
if vm_metadata:
if vm_metadata.get('project_reference').get('kind') == 'project':
project = vm_metadata.get('project_reference').get('name')
return project

def get_categories(self, uuid, clusteruuid=None, refresh=False):
def get_categories(self, uuid, refresh=False):
"""Retrieve the categories assigned to the specified host if connected to a prism central
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
`connection_type` is set to `pc`.
:type clusteruuid: str, optional
:param uuid: The UUID of a host.
:type uuid: str
:param refresh: Whether to refresh the class dataset (default=False).
:type refresh: bool, optional
:returns: A dictionary with all .
:returns: A dictionary with all categories for the specified host.
:rtype: ResponseDict
"""
logger = logging.getLogger('ntnx_api.prism.Hosts.get_categories')
categories = []
if self.api_client.connection_type == 'pc':
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
if metadata.get('project_reference').get('kind') == 'project':
for key, value in metadata.get('categories').items():
categories[key] = value
categories = {}
if self.api_client.connection_type == "pc":
metadata = self.get_metadata(refresh=refresh)
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
if vm_metadata:
if 'categories' in vm_metadata:
for key, value in vm_metadata.get('categories').items():
categories[key] = value
return categories


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

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

def get_metadata(self, refresh=False):
"""Retrieve metadata for each virtual machine from the connected PC instance
:returns: A list of dictionaries describing each vm from the specified cluster.
:rtype: ResponseList
"""
params = {}
payload = {
"kind": "vm",
"offset": 0,
"length": 2147483647
}
uri = '/vms/list'

if self.api_client.connection_type == "pc":
# Remove existing data for this cluster if it exists
if self.vms_metadata or refresh:
self.vms_metadata = {}
logger.info('removing existing data from class dict vms_metadata')

vms = self.api_client.request(uri=uri, api_version='v3', payload=payload, params=params).get('entities')
for vm in vms:
self.vms_metadata[vm.get('metadata').get('uuid')] = vm.get('metadata')

return self.vms_metadata

def search_uuid(self, uuid, clusteruuid=None, refresh=False):
"""Retrieve data for a specific vm, in a specific cluster by vm uuid
Expand Down Expand Up @@ -2688,49 +2739,47 @@ def search_name(self, name, clusteruuid=None, refresh=False):

return found

def get_project(self, uuid, clusteruuid=None, refresh=False):
def get_project(self, uuid, refresh=False):
"""Retrieve the project assigned to the specified VM if connected to a prism central
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
`connection_type` is set to `pc`.
:type clusteruuid: str, optional
:param uuid: The UUID of a VM.
:type uuid: str
:param refresh: Whether to refresh the class VM dataset (default=False).
:param refresh: Whether to refresh the class VM Metadata dataset (default=False).
:type refresh: bool, optional
:returns: A string containing the project name.
:rtype: str
"""
logger = logging.getLogger('ntnx_api.prism.Vms.get_project')
project = ''
if self.api_client.connection_type == 'pc':
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('vm_metadata')
if metadata.get('project_reference').get('kind') == 'project':
project = metadata.get('project_reference').get('name')
if self.api_client.connection_type == "pc":
metadata = self.get_metadata(refresh=refresh)
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
if vm_metadata:
if vm_metadata.get('project_reference').get('kind') == 'project':
project = vm_metadata.get('project_reference').get('name')
return project

def get_categories(self, uuid, clusteruuid=None, refresh=False):
def get_categories(self, uuid, refresh=False):
"""Retrieve the categories assigned to the specified VM if connected to a prism central
:param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
`connection_type` is set to `pc`.
:type clusteruuid: str, optional
:param uuid: The UUID of a VM.
:type uuid: str
:param refresh: Whether to refresh the class VM dataset (default=False).
:param refresh: Whether to refresh the class VM Metadata dataset (default=False).
:type refresh: bool, optional
:returns: A dictionary with all .
:rtype: ResponseDict
"""
logger = logging.getLogger('ntnx_api.prism.Vms.get_categories')
categories = []
if self.api_client.connection_type == 'pc':
metadata = self.search_uuid(uuid=uuid, clusteruuid=clusteruuid, refresh=refresh).get('categories')
if metadata.get('project_reference').get('kind') == 'project':
for key, value in metadata.get('categories').items():
categories[key] = value
categories = {}
if self.api_client.connection_type == "pc":
metadata = self.get_metadata(refresh=refresh)
vm_metadata = next((item for item in metadata if item['uuid'] == uuid), None)
if vm_metadata:
if 'categories' in vm_metadata:
for key, value in vm_metadata.get('categories').items():
categories[key] = value
return categories


Expand Down

0 comments on commit 677e2dc

Please sign in to comment.