def WaitForOperation(operation_name, project, context, operation_description, timeout=None): """Wait for an operation to complete. Polls the operation requested approximately every second, showing a progress indicator. Returns when the operation has completed. Args: operation_name: The name of the operation to wait on, as returned by operations.list. project: The name of the project that this operation belongs to. context: Context object with messages and client to access the deploymentmanager service. operation_description: A short description of the operation to wait on, such as 'create' or 'delete'. Will be displayed to the user. timeout: Optional (approximate) timeout in seconds, after which wait will return failure. Raises: HttpException: A http error response was received while executing api request. Will be raised if the operation cannot be found. DeploymentManagerError: The operation finished with error(s) or exceeded the timeout without completing. """ client = context['deploymentmanager-client'] messages = context['deploymentmanager-messages'] ticks = 0 message = ('Waiting for ' + ('{0} '.format(operation_description) if operation_description else '') + operation_name) with console_io.ProgressTracker(message, autotick=False) as ticker: while timeout is None or ticks < timeout: ticks += 1 try: operation = client.operations.Get( messages.DeploymentmanagerOperationsGetRequest( project=project, operation=operation_name, )) except apitools_exceptions.HttpError as error: raise HttpException(GetError(error)) ticker.Tick() # Operation status will be one of PENDING, RUNNING, DONE if operation.status == 'DONE': if operation.error: raise DeploymentManagerError('Error in Operation ' + operation_name + ': ' + str(operation.error)) else: # Operation succeeded return time.sleep(1) # wait one second and try again # Timeout exceeded raise DeploymentManagerError('Wait for Operation ' + operation_name + ' exceeded timeout.')
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except exceptions.HttpError as error: log.error('Error:\n' + error.content) raise HttpException.FromCurrent()