def wait_for_volume_resource_status(client, resource_id, statuses): """Waits for a volume resource to reach any of the specified statuses. This function is a common function for volume, snapshot and backup resources. The function extracts the name of the desired resource from the client class name of the resource. """ if not isinstance(statuses, list): statuses = [statuses] resource_name = re.findall( r'(volume|group-snapshot|snapshot|backup|group)', client.resource_type)[-1].replace('-', '_') show_resource = getattr(client, 'show_' + resource_name) resource_status = show_resource(resource_id)[resource_name]['status'] start = int(time.time()) while resource_status not in statuses: time.sleep(client.build_interval) resource_status = show_resource(resource_id)['{}'.format( resource_name)]['status'] if resource_status == 'error' and resource_status not in statuses: raise exceptions.VolumeResourceBuildErrorException( resource_name=resource_name, resource_id=resource_id) if resource_name == 'volume' and resource_status == 'error_restoring': raise exceptions.VolumeRestoreErrorException(volume_id=resource_id) if int(time.time()) - start >= client.build_timeout: message = ('%s %s failed to reach %s status (current %s) ' 'within the required time (%s s).' % (resource_name, resource_id, statuses, resource_status, client.build_timeout)) raise lib_exc.TimeoutException(message) LOG.info('%s %s reached %s after waiting for %f seconds', resource_name, resource_id, statuses, time.time() - start)
def wait_for_volume_resource_status(client, resource_id, status): """Waits for a volume resource to reach a given status. This function is a common function for volume, snapshot and backup resources. The function extracts the name of the desired resource from the client class name of the resource. """ resource_name = re.findall(r'(Volume|Snapshot|Backup)', client.__class__.__name__)[0].lower() show_resource = getattr(client, 'show_' + resource_name) resource_status = show_resource(resource_id)[resource_name]['status'] start = int(time.time()) while resource_status != status: time.sleep(client.build_interval) resource_status = show_resource(resource_id)['{}'.format( resource_name)]['status'] if resource_status == 'error' and resource_status != status: raise exceptions.VolumeResourceBuildErrorException( resource_name=resource_name, resource_id=resource_id) if resource_name == 'volume' and resource_status == 'error_restoring': raise exceptions.VolumeRestoreErrorException(volume_id=resource_id) if int(time.time()) - start >= client.build_timeout: message = ('%s %s failed to reach %s status (current %s) ' 'within the required time (%s s).' % (resource_name, resource_id, status, resource_status, client.build_timeout)) raise lib_exc.TimeoutException(message)
def wait_for_volume_status(client, volume_id, status): """Waits for a Volume to reach a given status.""" body = client.show_volume(volume_id)['volume'] volume_status = body['status'] start = int(time.time()) while volume_status != status: time.sleep(client.build_interval) body = client.show_volume(volume_id)['volume'] volume_status = body['status'] if volume_status == 'error': raise exceptions.VolumeBuildErrorException(volume_id=volume_id) if volume_status == 'error_restoring': raise exceptions.VolumeRestoreErrorException(volume_id=volume_id) if int(time.time()) - start >= client.build_timeout: message = ( 'Volume %s failed to reach %s status (current %s) ' 'within the required time (%s s).' % (volume_id, status, volume_status, client.build_timeout)) raise exceptions.TimeoutException(message)