def __wait_task_completion(self, task, timeout): if not task: raise HPOneViewUnknownType(MSG_INVALID_TASK) logger.debug('Waiting for task completion...') # gets current cpu second for timeout start_time = self.get_current_seconds() connection_failure_control = dict( last_success=self.get_current_seconds()) i = 0 while self.is_task_running(task, connection_failure_control): # wait 1 to 10 seconds # the value increases to avoid flooding server with requests i = i + 1 if i < 10 else 10 logger.debug("Waiting for task. Percentage complete: " + str(task.get('computedPercentComplete'))) logger.debug("Waiting for task. Task state: " + str(task.get('taskState'))) time.sleep(i) if (timeout != UNLIMITED_TIMEOUT) and (start_time + timeout < self.get_current_seconds()): raise HPOneViewTimeout(MSG_TIMEOUT % str(timeout))
def make_task_entity_tuple(self, obj): task = {} entity = {} if obj: if obj['category'] == 'tasks' or obj['category'] == 'backups': # it is an error if type is not in obj, so let the except flow uri = '' if obj['type'] == 'TaskResource': task = obj uri = obj['associatedResourceUri'] elif obj['type'] == 'TaskResourceV2': task = obj uri = obj['associatedResource']['resourceUri'] elif obj['type'] == 'BACKUP': task = self._con.get(obj['taskUri']) uri = obj['uri'] else: raise HPOneViewInvalidResource('Task resource is not a' ' recognized version') if uri: try: entity = self._con.get(uri) except HPOneViewException: raise else: entity = obj else: raise HPOneViewUnknownType('Unknown object type') return task, entity
def delete(self, resource, force=False, timeout=-1, custom_headers=None): if not resource: logger.exception(RESOURCE_CLIENT_RESOURCE_WAS_NOT_PROVIDED) raise ValueError(RESOURCE_CLIENT_RESOURCE_WAS_NOT_PROVIDED) if isinstance(resource, dict): if 'uri' in resource and resource['uri']: uri = resource['uri'] else: logger.exception(RESOURCE_CLIENT_UNKNOWN_OBJECT_TYPE) raise HPOneViewUnknownType(RESOURCE_CLIENT_UNKNOWN_OBJECT_TYPE) else: uri = self._uri + "/" + resource if force: uri += '?force=True' logger.debug("Delete resource (uri = %s, resource = %s)" % (self._uri, str(resource))) task, body = self._connection.delete(uri, custom_headers=custom_headers) if not task: # 204 NO CONTENT # Successful return from a synchronous delete operation. return True task = self._task_monitor.wait_for_task(task, timeout=timeout) return task
def test_unknown_type_exception_inheritance_with_string(self): exception = HPOneViewUnknownType("A message string") self.assertIsInstance(exception, HPOneViewException) self.assertEqual(exception.msg, "A message string") self.assertEqual(exception.oneview_response, None) self.assertEqual(exception.args[0], "A message string") self.assertEqual(len(exception.args), 1)
def get_associated_resource(self, task): """ Retrieve a resource associated with a task. Args: task: task dict Returns: tuple: task (updated), the entity found (dict) """ if not task: raise HPOneViewUnknownType(MSG_INVALID_TASK) if task['category'] != 'tasks' and task['category'] != 'backups': # it is an error if type is not in obj, so let the except flow raise HPOneViewUnknownType(MSG_UNKNOWN_OBJECT_TYPE) if task['type'] == 'TaskResourceV2': resource_uri = task['associatedResource']['resourceUri'] if resource_uri and resource_uri.startswith( "/rest/appliance/support-dumps/"): # Specific for support dumps return task, resource_uri elif task['type'] == 'BACKUP': task = self._connection.get(task['taskUri']) resource_uri = task['uri'] else: raise HPOneViewInvalidResource(MSG_TASK_TYPE_UNRECONIZED % task['type']) entity = {} if resource_uri: entity = self._connection.get(resource_uri) return task, entity
def wait_for_task(self, task, timeout=-1): """ Wait for task execution and return associated resource Args: task: task dict timeout: timeout in seconds Returns: associated resource when creating or updating; True when deleting """ if not task: raise HPOneViewUnknownType(MSG_INVALID_TASK) logger.debug('Waiting for task') # gets current cpu second for timeout start_time = self.get_current_seconds() i = 0 while self.is_task_running(task): # wait 1 to 10 seconds # the value increases to avoid flooding server with requests i = i + 1 if i < 10 else 10 logger.debug("Waiting for task. Percentage complete: " + str(task.get('computedPercentComplete'))) logger.debug("Waiting for task. Task state: " + str(task.get('taskState'))) time.sleep(i) if (timeout != UNLIMITED_TIMEOUT) and (start_time + timeout < self.get_current_seconds()): raise HPOneViewTimeout(MSG_TIMEOUT % str(timeout)) task = self.get(task) logger.debug("Waiting for task. Percentage complete: " + str(task.get('computedPercentComplete'))) logger.debug("Waiting for task. Task state: " + str(task.get('taskState'))) task_response = self.__get_task_response(task) logger.debug('Task completed') return task_response
def __validate_resource_uri(self, path): if self._uri not in path: logger.exception('Get by uri : unrecognized uri: (%s)' % path) raise HPOneViewUnknownType(UNRECOGNIZED_URI)