コード例 #1
0
    def GetOperationStatus(sql_client, operation_ref, progress_tracker=None):
        """Helper function for getting the status of an operation for V1Beta4 API.

    Args:
      sql_client: apitools.BaseApiClient, The client used to make requests.
      operation_ref: resources.Resource, A reference for the operation to poll.
      progress_tracker: progress_tracker.ProgressTracker, A reference for the
          progress tracker to tick, in case this function is used in a Retryer.

    Returns:
      True: if the operation succeeded without error.
      False: if the operation is not yet done.
      OperationError: If the operation has an error code or is in UNKNOWN state.
      Exception: Any other exception that can occur when calling Get
    """

        if progress_tracker:
            progress_tracker.Tick()
        try:
            op = sql_client.operations.Get(
                sql_client.MESSAGES_MODULE.SqlOperationsGetRequest(
                    project=operation_ref.project,
                    operation=operation_ref.operation))
        except Exception as e:  # pylint:disable=broad-except
            # Since we use this function in a retryer.RetryOnResult block, where we
            # retry for different exceptions up to different amounts of time, we
            # have to catch all exceptions here and return them.
            return e
        if op.error and op.error.errors:
            return exceptions.OperationError(op.error.errors[0].code)
        if op.status == 'UNKNOWN':
            return exceptions.OperationError(op.status)
        if op.status == 'DONE':
            return True
        return False
コード例 #2
0
 def testUnknownOperation(self):
     operation = self.GetOperation(
         self.client.sql_messages.Operation.OperationTypeValueValuesEnum.
         CREATE, self.client.sql_messages.Operation.StatusValueValuesEnum.
         SQL_OPERATION_STATUS_UNSPECIFIED)
     self.ExpectOperationGet(operation)
     status = _GetOperationStatus(self.mocked_client,
                                  self.GetOperationRef(operation))
     self.AssertErrorsEqual(
         status,
         exceptions.OperationError('SQL_OPERATION_STATUS_UNSPECIFIED'))
コード例 #3
0
    def WaitForOperation(cls, sql_client, operation_ref, message):
        """Wait for a Cloud SQL operation to complete.

    No operation is done instantly. Wait for it to finish following this logic:
    First wait 1s, then query, then retry waiting exponentially more from 2s.
    We want to limit to 20s between retries to maintain some responsiveness.
    Finally, we want to limit the whole process to a conservative 300s. If we
    get to that point it means something is wrong and we can throw an exception.

    Args:
      sql_client: apitools.BaseApiClient, The client used to make requests.
      operation_ref: resources.Resource, A reference for the operation to poll.
      message: str, The string to print while polling.

    Returns:
      True if the operation succeeded without error.

    Raises:
      OperationError: If the operation has an error code, is in UNKNOWN state,
          or if the operation takes more than 300s.
    """
        def ShouldRetryFunc(result, state):
            # In case of HttpError, retry for up to _HTTP_MAX_RETRY_MS at most.
            if isinstance(result, base_exceptions.HttpError):
                if state.time_passed_ms > _BaseOperations._HTTP_MAX_RETRY_MS:
                    raise result
                return True
            # In case of other Exceptions, raise them immediately.
            if isinstance(result, Exception):
                raise result
            # Otherwise let the retryer do it's job until the Operation is done.
            return not result

        with console_progress_tracker.ProgressTracker(message,
                                                      autotick=False) as pt:
            time.sleep(_BaseOperations._PRE_START_SLEEP_SEC)
            retryer = retry.Retryer(
                exponential_sleep_multiplier=2,
                max_wait_ms=_BaseOperations._MAX_WAIT_MS,
                wait_ceiling_ms=_BaseOperations._WAIT_CEILING_MS)
            try:
                retryer.RetryOnResult(
                    cls.GetOperationStatus, [sql_client, operation_ref],
                    {'progress_tracker': pt},
                    should_retry_if=ShouldRetryFunc,
                    sleep_ms=_BaseOperations._INITIAL_SLEEP_MS)
            except retry.WaitException:
                raise exceptions.OperationError((
                    'Operation {0} is taking longer than expected. You can continue '
                    'waiting for the operation by running `{1}`').format(
                        operation_ref,
                        cls.GetOperationWaitCommand(operation_ref)))
コード例 #4
0
ファイル: operations.py プロジェクト: saranraju90/multik8s
    def GetOperation(sql_client, operation_ref, progress_tracker=None):
        """Helper function for getting the status of an operation for V1Beta4 API.

    Args:
      sql_client: apitools.BaseApiClient, The client used to make requests.
      operation_ref: resources.Resource, A reference for the operation to poll.
      progress_tracker: progress_tracker.ProgressTracker, A reference for the
          progress tracker to tick, in case this function is used in a Retryer.

    Returns:
      Operation: if the operation succeeded without error or  is not yet done.
      OperationError: If the operation has an error code or is in UNKNOWN state.
      Exception: Any other exception that can occur when calling Get
    """

        if progress_tracker:
            progress_tracker.Tick()
        try:
            op = sql_client.operations.Get(
                sql_client.MESSAGES_MODULE.SqlOperationsGetRequest(
                    project=operation_ref.project,
                    operation=operation_ref.operation))
        except Exception as e:  # pylint:disable=broad-except
            # Since we use this function in a retryer.RetryOnResult block, where we
            # retry for different exceptions up to different amounts of time, we
            # have to catch all exceptions here and return them.
            return e
        if op.error and op.error.errors:
            error_object = op.error.errors[0]
            # If there's an error message to show, show it in addition to the code.
            error = '[{}]'.format(error_object.code)
            if error_object.message:
                error += ' ' + error_object.message
            return exceptions.OperationError(error)
        if op.status == sql_client.MESSAGES_MODULE.Operation.StatusValueValuesEnum.SQL_OPERATION_STATUS_UNSPECIFIED:
            return exceptions.OperationError(op.status)
        return op
コード例 #5
0
    def testFailedOperationWithoutMessage(self):
        error = self.messages.OperationErrors(
            kind='sql#operationErrors',
            errors=[
                self.messages.OperationError(kind='sql#operationError',
                                             code='bad_thing')
            ])
        operation = self.GetOperation(
            self.client.sql_messages.Operation.OperationTypeValueValuesEnum.
            CREATE,
            self.client.sql_messages.Operation.StatusValueValuesEnum.DONE,
            error)
        self.ExpectOperationGet(operation)
        status = _GetOperationStatus(self.mocked_client,
                                     self.GetOperationRef(operation))

        # The error returned should contain the operation error code.
        self.AssertErrorsEqual(status,
                               exceptions.OperationError('[bad_thing]'))