def Run(self, args):
    """Run 'rolling-updates describe'.

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

    Returns:
      Update, representation of the update if the Get call was
      successful.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
    client = self.context['updater_api']
    messages = self.context['updater_messages']
    resources = self.context['updater_resources']

    ref = resources.Parse(
        args.update,
        collection='replicapoolupdater.rollingUpdates')
    request = messages.ReplicapoolupdaterRollingUpdatesGetRequest(
        project=ref.project,
        zone=ref.zone,
        rollingUpdate=ref.rollingUpdate)

    try:
      return client.rollingUpdates.Get(request)
    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(updater_util.GetError(error))
示例#2
0
    def Run(self, args):
        """Run 'rolling-updates list'.

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

    Yields:
      List of all the updates.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        client = updater_util.GetApiClientInstance()
        messages = updater_util.GetApiMessages()

        request = messages.ReplicapoolupdaterRollingUpdatesListRequest(
            project=properties.VALUES.core.project.Get(required=True),
            zone=properties.VALUES.compute.zone.Get(required=True))
        if args.group:
            request.filter = 'instanceGroup eq %s' % args.group

        try:
            for item in list_pager.YieldFromList(client.rollingUpdates,
                                                 request,
                                                 limit=args.limit):
                # TODO(user): Consider getting rid of instance group manager in api.
                if item.instanceGroup:
                    item.instanceGroupManager = item.instanceGroup
                yield item
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(updater_util.GetError(error))
    def Run(self, args):
        """Run 'rolling-updates list-instance-updates'.

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

    Returns:
      List of all the instance updates.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        client = self.context['updater_api']
        messages = self.context['updater_messages']
        resources = self.context['updater_resources']

        ref = resources.Parse(args.update,
                              collection='replicapoolupdater.rollingUpdates')
        request = (messages.
                   ReplicapoolupdaterRollingUpdatesListInstanceUpdatesRequest(
                       project=ref.project,
                       zone=ref.zone,
                       rollingUpdate=ref.rollingUpdate))

        try:
            return list_pager.YieldFromList(client.rollingUpdates,
                                            request,
                                            method='ListInstanceUpdates')
        except apitools_exceptions.HttpError as error:
            raise exceptions.HttpException(updater_util.GetError(error))
示例#4
0
    def Run(self, args):
        """Run 'replicapool get'.

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

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.

    Returns:
      The get API's response
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        request = client.pools().get(projectName=project,
                                     zone=args.zone,
                                     poolName=args.pool)

        try:
            response = request.execute()
            util.PrettyPrint(response, args.format or 'yaml')
            return response
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#5
0
    def Run(self, args):
        """Run 'replicapool replicas 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.
      ToolException: An error other than http error occurred while executing
          the command.
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        delete_body = {}
        if args.abandon_instance:
            delete_body['abandonInstance'] = args.abandon_instance is True

        request = client.replicas().delete(projectName=project,
                                           zone=args.zone,
                                           poolName=args.pool,
                                           replicaName=args.replica,
                                           body=delete_body)

        try:
            request.execute()
            log.Print('Replica {0} in pool {1} is being deleted.'.format(
                args.replica, args.pool))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#6
0
    def Run(self, args):
        """Run 'replicapool resize'.

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

    Returns:
      An object representing the service response obtained by the Resize
      API if the Resize call 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.
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        request = client.pools().resize(projectName=project,
                                        zone=args.zone,
                                        poolName=args.pool,
                                        numReplicas=args.new_size)

        try:
            request.execute()
            log.Print('Replica pool {0} is being resized.'.format(args.pool))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#7
0
    def Run(self, args):
        """Run 'rolling-updates list'.

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

    Returns:
      List of all the updates.

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        client = self.context['updater_api']
        messages = self.context['updater_messages']

        request = messages.ReplicapoolupdaterRollingUpdatesListRequest(
            project=properties.VALUES.core.project.Get(required=True),
            zone=properties.VALUES.compute.zone.Get(required=True))
        if args.group:
            request.filter = 'instanceGroup eq %s' % args.group
        limit = updater_util.SanitizeLimitFlag(args.limit)

        try:
            return apitools_base.YieldFromList(client.rollingUpdates,
                                               request,
                                               limit=limit)
        except apitools_base.HttpError as error:
            raise exceptions.HttpException(updater_util.GetError(error))
示例#8
0
  def Run(self, args):
    """Run 'resourceviews resources add'.

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

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
    zone_views_client = self.context['zoneViewsClient']
    project = properties.VALUES.core.project.Get(required=True)

    if args.region:
      raise exceptions.ToolException(ValueError(
          'addinstance must be used against a zonal resourceview'))

    instance_urls = []
    for instance in args.resource:
      instance_urls.append(
          'https://www.googleapis.com/compute/v1/projects/' +
          project + '/zones/' + args.zone + '/instances/' +
          instance)
    request_body = {'resources': instance_urls}

    if 'v1beta1' in self.context['api_version']:
      request = zone_views_client.addresources(
          projectName=project,
          zone=args.zone,
          resourceViewName=args.resourceview,
          body=request_body)
    else:
      request = zone_views_client.addResources(
          project=project,
          zone=args.zone,
          resourceView=args.resourceview,
          body=request_body)

    try:
      request.execute()
      log.Print('Instances added to resource view {0}.'.format(
          args.resourceview))
    except errors.HttpError as error:
      raise exceptions.HttpException(util.GetError(error))
    except errors.Error as error:
      raise exceptions.ToolException(error)
示例#9
0
    def Run(self, args):
        """Run 'replicapool replicas list'.

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

    Returns:
      An object representing the service response obtained by the List
      API if the List call 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.
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        limit = util.SanitizeLimitFlag(args.limit)

        request = client.replicas().list(projectName=project,
                                         zone=args.zone,
                                         poolName=args.pool)
        results = []
        try:
            response = request.execute()
            self.AppendResults(results, response)
            while response and 'nextPageToken' in response and len(
                    results) < limit:
                request = client.replicas().list(
                    projectName=project,
                    zone=args.zone,
                    poolName=args.pool,
                    pageToken=response['nextPageToken'])
                response = request.execute()
                self.AppendResults(results, response)

            if len(results) > limit:
                results = results[0:limit]

            return results
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#10
0
    def Run(self, args):
        """Run 'resourceviews resources add'.

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

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        zone_views_client = self.context['zoneViewsClient']
        region_views_client = self.context['regionViewsClient']

        project = properties.VALUES.core.project.Get(required=True)

        request_body = {'resources': args.resource}
        if 'v1beta1' in self.context['api_version']:
            if args.region:
                request = region_views_client.addresources(
                    projectName=project,
                    region=args.region,
                    resourceViewName=args.resourceview,
                    body=request_body)
            else:
                request = zone_views_client.addresources(
                    projectName=project,
                    zone=args.zone,
                    resourceViewName=args.resourceview,
                    body=request_body)
        else:
            request = zone_views_client.addResources(
                project=project,
                zone=args.zone,
                resourceView=args.resourceview,
                body=request_body)

        try:
            request.execute()
            log.Print('Resources added to resource view {0}.'.format(
                args.resourceview))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#11
0
    def Run(self, args):
        """Run 'replicapool create'.

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

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        template = replica_template_util.ParseTemplate(
            args.template,
            params=args.param,
            params_from_file=args.param_from_file)

        new_replicapool = {
            'name': args.pool,
            'description': args.description,
            'numReplicas': args.size,
            'autoRestart': True,
            'template': template['template']
        }
        if not args.auto_restart:
            new_replicapool['autoRestart'] = False
        if args.target_pools:
            new_replicapool['targetPools'] = args.target_pools
        if args.resource_views:
            new_replicapool['resourceViews'] = args.resource_views

        request = client.pools().insert(projectName=project,
                                        zone=args.zone,
                                        body=new_replicapool)

        try:
            request.execute()
            log.Print('Replica pool {0} is being created.'.format(args.pool))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#12
0
    def Run(self, args):
        """Run 'resourceviews create'.

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

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        zone_views_client = self.context['zoneViewsClient']
        # remove the regional service client when v1beta1 is deprecated.
        region_views_client = self.context['regionViewsClient']

        project = properties.VALUES.core.project.Get(required=True)

        new_resourceview = {
            'name': args.name,
            'description': args.description,
        }

        if 'v1beta1' in self.context['api_version']:
            if args.region:
                request = region_views_client.insert(projectName=project,
                                                     region=args.region,
                                                     body=new_resourceview)
            else:
                request = zone_views_client.insert(projectName=project,
                                                   zone=args.zone,
                                                   body=new_resourceview)
        else:
            request = zone_views_client.insert(project=project,
                                               zone=args.zone,
                                               body=new_resourceview)

        try:
            request.execute()
            log.Print('Resource view {0} created.'.format(args.name))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#13
0
    def Run(self, args):
        """Run 'resourceviews get'.

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

    Returns:
      The object representing the resource views.

    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
        zone_views_client = self.context['zoneViewsClient']
        region_views_client = self.context['regionViewsClient']

        project = properties.VALUES.core.project.Get(required=True)

        if 'v1beta1' in self.context['api_version']:
            if args.region:
                request = region_views_client.get(projectName=project,
                                                  region=args.region,
                                                  resourceViewName=args.name)
            else:
                request = zone_views_client.get(projectName=project,
                                                zone=args.zone,
                                                resourceViewName=args.name)
        else:
            request = zone_views_client.get(project=project,
                                            zone=args.zone,
                                            resourceView=args.name)

        try:
            response = request.execute()
            if not args.format:
                util.PrettyPrint(response, 'yaml')
            return response
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#14
0
  def Run(self, args):
    """Run 'rolling-updates rollback'.

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

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
    client = self.context['updater_api']
    messages = self.context['updater_messages']
    resources = self.context['updater_resources']

    ref = resources.Parse(
        args.update,
        collection='replicapoolupdater.rollingUpdates')
    request = messages.ReplicapoolupdaterRollingUpdatesRollbackRequest(
        project=ref.project,
        zone=ref.zone,
        rollingUpdate=ref.rollingUpdate)

    try:
      operation = client.rollingUpdates.Rollback(request)
      operation_ref = resources.Parse(
          operation.name,
          collection='replicapoolupdater.zoneOperations')
      result = updater_util.WaitForOperation(
          client, operation_ref, 'Initiating rollback of the update')
      if result:
        log.status.write('Initiated rollback of [{0}].\n'.format(ref))
      else:
        raise exceptions.ToolException(
            'could not initiate rollback of [{0}]'.format(ref))

    except apitools_base.HttpError as error:
      raise exceptions.HttpException(updater_util.GetError(error))
示例#15
0
  def Run(self, args):
    """Run 'rolling-updates pause'.

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

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
    client = updater_util.GetApiClientInstance()
    messages = updater_util.GetApiMessages()

    ref = resources.REGISTRY.Parse(
        args.update,
        collection='replicapoolupdater.rollingUpdates')
    request = messages.ReplicapoolupdaterRollingUpdatesPauseRequest(
        project=ref.project,
        zone=ref.zone,
        rollingUpdate=ref.rollingUpdate)

    try:
      operation = client.rollingUpdates.Pause(request)
      operation_ref = resources.REGISTRY.Parse(
          operation.name,
          collection='replicapoolupdater.zoneOperations')
      result = updater_util.WaitForOperation(
          client, operation_ref, 'Pausing the update')
      if result:
        log.status.write('Paused [{0}].\n'.format(ref))
      else:
        raise exceptions.ToolException('could not pause [{0}]'.format(ref))

    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(updater_util.GetError(error))
示例#16
0
    def Run(self, args):
        """Run 'replicapool update-template'.

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

    Returns:
      An object representing the service response obtained by the Get
      API if the Get call 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.
    """
        client = self.context['replicapool']
        project = properties.VALUES.core.project.Get(required=True)

        template = replica_template_util.ParseTemplate(
            args.template,
            params=args.param,
            params_from_file=args.param_from_file)

        request = client.pools().updatetemplate(projectName=project,
                                                zone=args.zone,
                                                poolName=args.pool,
                                                body=template['template'])

        try:
            request.execute()
            log.Print('Template updated for replica pool {0}.'.format(
                args.pool))
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#17
0
    def Run(self, args):
        """Run 'resourceviews resources list'.

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

    Returns:
      A list object representing the resources in a view obtained by the
      ListResources operation if the ListResources API call 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.
    """
        limit = util.SanitizeLimitFlag(args.limit)

        request = self.BuildRequest(args)
        results = []
        try:
            response = request.execute()
            self.AppendResults(results, response)
            while response and 'nextPageToken' in response and len(
                    results) < limit:
                request = self.BuildRequest(args, response['nextPageToken'])
                response = request.execute()
                self.AppendResults(results, response)

            if len(results) > limit:
                results = results[0:limit]

            return results
        except errors.HttpError as error:
            raise exceptions.HttpException(util.GetError(error))
        except errors.Error as error:
            raise exceptions.ToolException(error)
示例#18
0
  def Run(self, args):
    """Run 'rolling-updates start'.

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

    Raises:
      HttpException: An http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing
          the command.
    """
    client = self.context['updater_api']
    messages = self.context['updater_messages']
    resources = self.context['updater_resources']

    request = messages.ReplicapoolupdaterRollingUpdatesInsertRequest(
        project=properties.VALUES.core.project.Get(required=True),
        zone=properties.VALUES.compute.zone.Get(required=True),
        rollingUpdate=self._PrepareUpdate(args))

    try:
      operation = client.rollingUpdates.Insert(request)
      operation_ref = resources.Parse(
          operation.name,
          collection='replicapoolupdater.zoneOperations')
      result = updater_util.WaitForOperation(
          client, operation_ref, 'Starting a new update')
      if result:
        log.status.write('Started [{0}].\n'.format(operation.targetLink))
      else:
        raise exceptions.ToolException(
            'could not start [{0}]'.format(operation.targetLink))

    except apitools_exceptions.HttpError as error:
      raise exceptions.HttpException(updater_util.GetError(error))