Ejemplo n.º 1
0
    def Run(self, args):
        """Updates settings of a Cloud SQL instance using the patch api method.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      A dict object representing the operations resource describing the patch
      operation if the patch was successful.
    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing the
          command.
    """

        sql_client = self.context['sql_client']
        sql_messages = self.context['sql_messages']
        resources = self.context['registry']

        util.ValidateInstanceName(args.instance)
        instance_ref = resources.Parse(args.instance,
                                       collection='sql.instances')

        try:
            original_instance_resource = sql_client.instances.Get(
                instance_ref.Request())
        except apitools_base.HttpError as error:
            raise exceptions.HttpException(util.GetErrorMessage(error))

        cleared_fields = []

        if args.clear_gae_apps:
            cleared_fields.append('settings.authorizedGaeApplications')
        if args.clear_authorized_networks:
            cleared_fields.append(
                'settings.ipConfiguration.authorizedNetworks')
        if args.clear_database_flags:
            cleared_fields.append('settings.databaseFlags')

        patch_instance = util.ConstructInstanceFromArgs(
            sql_messages, args, original=original_instance_resource)
        patch_instance.project = instance_ref.project
        patch_instance.instance = instance_ref.instance

        log.status.write(
            'The following message will be used for the patch API method.\n')
        log.status.write(
            apitools_base.MessageToJson(patch_instance,
                                        include_fields=cleared_fields) + '\n')

        self.PrintAndConfirmWarningMessage(args)

        with sql_client.IncludeFields(cleared_fields):
            result = sql_client.instances.Patch(patch_instance)

        operation_ref = resources.Create(
            'sql.operations',
            operation=result.operation,
            project=instance_ref.project,
            instance=instance_ref.instance,
        )

        if args. async:
            return sql_client.operations.Get(operation_ref.Request())

        util.WaitForOperation(sql_client, operation_ref,
                              'Patching Cloud SQL instance')

        log.UpdatedResource(instance_ref)

        if args.diff:
            try:
                changed_instance_resource = sql_client.instances.Get(
                    instance_ref.Request())
            except apitools_base.HttpError as error:
                raise exceptions.HttpException(util.GetErrorMessage(error))
            return resource_printer.ResourceDiff(original_instance_resource,
                                                 changed_instance_resource)

        return sql_client.instances.Get(instance_ref.Request())
Ejemplo n.º 2
0
    def Run(self, args):
        """Creates a new Cloud SQL instance.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      A dict object representing the operations resource describing the create
      operation if the create was successful.
    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing the
          command.
    """

        # Added this temporarily for debugging SQL instance creation failures
        log.SetVerbosity(logging.DEBUG)
        sql_client = self.context['sql_client']
        sql_messages = self.context['sql_messages']
        resources = self.context['registry']

        util.ValidateInstanceName(args.instance)
        instance_ref = resources.Parse(args.instance,
                                       collection='sql.instances')

        instance_resource = util.ConstructInstanceFromArgs(sql_messages, args)

        if args.master_instance_name:
            replication = 'ASYNCHRONOUS'
            activation_policy = 'ALWAYS'
        else:
            replication = 'SYNCHRONOUS'
            activation_policy = 'ON_DEMAND'
        if not args.replication:
            instance_resource.settings.replicationType = replication
        if not args.activation_policy:
            instance_resource.settings.activationPolicy = activation_policy

        instance_resource.project = instance_ref.project
        instance_resource.instance = instance_ref.instance
        operation_ref = None

        if args.pricing_plan == 'PACKAGE':
            if not console_io.PromptContinue(
                    'Charges will begin accruing immediately. Really create Cloud '
                    'SQL instance?'):
                raise exceptions.ToolException('canceled by the user.')

        try:
            result = sql_client.instances.Insert(instance_resource)

            operation_ref = resources.Create(
                'sql.operations',
                operation=result.operation,
                project=instance_ref.project,
                instance=instance_ref.instance,
            )

            if args. async:
                return sql_client.operations.Get(operation_ref.Request())

            util.WaitForOperation(sql_client, operation_ref,
                                  'Creating Cloud SQL instance')

            log.CreatedResource(instance_ref)

            rsource = sql_client.instances.Get(instance_ref.Request())
            cache = remote_completion.RemoteCompletion()
            cache.AddToCache(instance_ref.SelfLink())
            return rsource
        except apitools_base.HttpError:
            log.debug('operation : %s', str(operation_ref))
            raise