def Run(self, args): """Run 'operations wait'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Raises: HttpException: An http error response was received while executing api request. Raises: OperationError: Operation finished with error(s) or timed out. """ failed_ops = [] for operation_name in args.operation_name: try: dm_v2_util.WaitForOperation(self.client, self.messages, operation_name, self.project, '', OPERATION_TIMEOUT) except exceptions.OperationError: failed_ops.append(operation_name) if failed_ops: if len(failed_ops) == 1: raise exceptions.OperationError( 'Operation %s failed to complete or has errors.' % failed_ops[0]) else: raise exceptions.OperationError( 'Some operations failed to complete without errors:\n' + '\n'.join(failed_ops)) else: log.status.Print('All operations completed successfully.')
def Run(self, args): """Run 'type-providers delete'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Raises: HttpException: An http error response was received while executing api request. """ type_provider_ref = type_providers.GetReference( self.resources, args.provider_name) if not args.quiet: prompt_message = 'Are you sure you want to delete [{0}]?'.format( type_provider_ref.typeProvider) if not console_io.PromptContinue(message=prompt_message, default=False): raise exceptions.OperationError('Deletion aborted by user.') request = self.messages.DeploymentmanagerTypeProvidersDeleteRequest( project=type_provider_ref.project, typeProvider=type_provider_ref.typeProvider) dm_write.Execute(self.client, self.messages, self.resources, request, args. async, self.client.typeProviders.Delete, LogResource)
def Run(self, args): """Run 'types delete'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Raises: HttpException: An http error response was received while executing api request. """ composite_type_ref = composite_types.GetReference(args.name) if not args.quiet: prompt_message = 'Are you sure you want to delete [{0}]?'.format( args.name) if not console_io.PromptContinue(message=prompt_message, default=False): raise exceptions.OperationError('Deletion aborted by user.') request = (dm_beta_base.GetMessages(). DeploymentmanagerCompositeTypesDeleteRequest( project=composite_type_ref.project, compositeType=args.name)) dm_write.Execute(request, args.async, dm_beta_base.GetClient().compositeTypes.Delete, LogResource)
def WaitForOperation(client, messages, operation_name, operation_description=None, project=None, timeout=180): """Wait for an operation to complete. Polls the operation requested approximately every second, showing a progress indicator. Returns when the operation has completed. Args: client: The API client to use. messages: The API message to use. operation_name: The name of the operation to wait on, as returned by operations.list. operation_description: A short description of the operation to wait on, such as 'create' or 'delete'. Will be displayed to the user. project: The name of the project that this operation belongs to. timeout: Number of seconds to wait for. Defaults to 3 minutes. Returns: The operation when it is done. Raises: HttpException: A http error response was received while executing api request. Will be raised if the operation cannot be found. OperationError: The operation finished with error(s). Error: The operation the timeout without completing. """ tick_increment = 1 # every second(s) ticks = 0 message = ('Waiting for {0}[{1}]'.format( operation_description + ' ' if operation_description else '', operation_name)) request = messages.DeploymentmanagerOperationsGetRequest( project=project, operation=operation_name) with progress_tracker.ProgressTracker(message, autotick=False) as ticker: while ticks < timeout: operation = client.operations.Get(request) # Operation status is one of PENDING, RUNNING, DONE if operation.status == 'DONE': if operation.error: raise exceptions.OperationError( 'Error in Operation [{0}]: {1}'.format( operation_name, dm_util.RenderMessageAsYaml(operation.error))) else: # Operation succeeded return operation ticks += tick_increment ticker.Tick() time.sleep(tick_increment) # Timeout exceeded raise exceptions.Error( 'Wait for Operation [{0}] exceeded timeout [{1}].'.format( operation_name, str(timeout)))
def WaitForOperation(client, messages, operation_name, project, 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: client: Object to make requests with messages: Object to build requests with 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. 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. OperationError: The operation finished with error(s). OperationTimeOutError: The operation exceeded the timeout without completing. """ ticks = 0 message = ('Waiting for ' + ('{0} '.format(operation_description) if operation_description else '') + operation_name) with progress_tracker.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 api_exceptions.HttpException(error, HTTP_ERROR_FORMAT) ticker.Tick() # Operation status will be one of PENDING, RUNNING, DONE if operation.status == 'DONE': if operation.error: raise exceptions.OperationError( 'Error in Operation ' + operation_name + ':\n' + GetOperationError(operation.error)) else: # Operation succeeded return time.sleep(1) # wait one second and try again # Timeout exceeded raise exceptions.OperationTimeoutError( 'Wait for Operation {0} exceeded {1} second timeout.'.format( operation_name, timeout))
def Run(self, args): """Run 'deployments delete'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: If --async=true, returns Operation to poll. Else, returns boolean indicating whether insert operation succeeded. Raises: HttpException: An http error response was received while executing api request. """ prompt_message = ('The following deployments will be deleted:\n- ' + '\n- '.join(args.deployment_name)) if not args.quiet: if not console_io.PromptContinue(message=prompt_message, default=False): raise exceptions.OperationError('Deletion aborted by user.') operations = [] errors = [] for deployment_name in args.deployment_name: deployment_ref = self.resources.Parse( deployment_name, params={'project': properties.VALUES.core.project.GetOrFail}, collection='deploymentmanager.deployments') try: operation = self.client.deployments.Delete( self.messages.DeploymentmanagerDeploymentsDeleteRequest( project=dm_base.GetProject(), deployment=deployment_ref.deployment, deletePolicy=(Delete._delete_policy_flag_map. GetEnumForChoice(args.delete_policy)), ) ) if args.async: operations.append(operation) else: op_name = operation.name try: # TODO(b/62720778): Refactor to use waiter.CloudOperationPoller operation = dm_write.WaitForOperation( self.client, self.messages, op_name, 'delete', dm_base.GetProject(), timeout=OPERATION_TIMEOUT) dm_util.LogOperationStatus(operation, 'Delete') except exceptions.OperationError as e: errors.append(exceptions.OperationError( u'Delete operation {0} failed.\n{1}'.format(op_name, e))) completed_operation = self.client.operations.Get( self.messages.DeploymentmanagerOperationsGetRequest( project=dm_base.GetProject(), operation=op_name, ) ) operations.append(completed_operation) except apitools_exceptions.HttpError as error: errors.append(api_exceptions.HttpException( error, dm_api_util.HTTP_ERROR_FORMAT)) if errors: raise core_exceptions.MultiError(errors) return operations
def Run(self, args): """Run 'deployments delete'. Args: args: argparse.Namespace, The arguments that this command was invoked with. Returns: If --async=true, returns Operation to poll. Else, returns boolean indicating whether insert operation succeeded. Raises: HttpException: An http error response was received while executing api request. """ prompt_message = ('The following deployments will be deleted:\n- ' + '\n- '.join(args.deployment_name)) if not args.quiet: if not console_io.PromptContinue(message=prompt_message, default=False): raise exceptions.OperationError('Deletion aborted by user.') operations = [] for deployment_name in args.deployment_name: try: operation = dm_base.GetClient().deployments.Delete( dm_base.GetMessages( ).DeploymentmanagerDeploymentsDeleteRequest( project=dm_base.GetProject(), deployment=deployment_name, deletePolicy=(dm_base.GetMessages( ).DeploymentmanagerDeploymentsDeleteRequest. DeletePolicyValueValuesEnum( args.delete_policy)), )) except apitools_exceptions.HttpError as error: raise api_exceptions.HttpException( error, dm_v2_util.HTTP_ERROR_FORMAT) if args. async: operations.append(operation) else: op_name = operation.name try: dm_write.WaitForOperation(op_name, 'delete', dm_base.GetProject(), timeout=OPERATION_TIMEOUT) log.status.Print('Delete operation ' + op_name + ' completed successfully.') except exceptions.OperationError as e: log.error(u'Delete operation {0} failed.\n{1}'.format( op_name, e)) except apitools_exceptions.HttpError as error: raise api_exceptions.HttpException( error, dm_v2_util.HTTP_ERROR_FORMAT) try: completed_operation = dm_base.GetClient().operations.Get( dm_base.GetMessages( ).DeploymentmanagerOperationsGetRequest( project=dm_base.GetProject(), operation=op_name, )) except apitools_exceptions.HttpError as error: raise api_exceptions.HttpException( error, dm_v2_util.HTTP_ERROR_FORMAT) operations.append(completed_operation) return operations