Example #1
0
def ParseUpdateLabels(client, version_ref, args):
    def GetLabels():
        return client.Get(version_ref).labels

    return labels_util.ProcessUpdateArgsLazy(args,
                                             client.version_class.LabelsValue,
                                             GetLabels)
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      A serialized object (dict) describing the results of the operation.

    Raises:
      An HttpException if there was a problem calling the
      API topics.Patch command.
    """
        client = topics.TopicsClient()
        topic_ref = args.CONCEPTS.topic.Parse()

        labels_update = labels_util.ProcessUpdateArgsLazy(
            args,
            client.messages.Topic.LabelsValue,
            orig_labels_thunk=lambda: client.Get(topic_ref).labels)
        try:
            result = client.Patch(topic_ref, labels=labels_update.GetOrNone())
        except topics.NoFieldsSpecifiedError:
            if not any(
                    args.IsSpecified(arg)
                    for arg in ('clear_labels', 'update_labels',
                                'remove_labels')):
                raise
            log.status.Print('No update to perform.')
            result = None
        else:
            log.UpdatedResource(topic_ref.RelativeName(), kind='topic')
        return result
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      A serialized object (dict) describing the results of the operation.
      This description fits the Resource described in the ResourceRegistry under
      'pubsub.projects.subscriptions'.

    Raises:
      An HttpException if there was a problem calling the
      API subscriptions.Patch command.
    """
    client = subscriptions.SubscriptionsClient()
    subscription_ref = args.CONCEPTS.subscription.Parse()

    labels_update = labels_util.ProcessUpdateArgsLazy(
        args, client.messages.Subscription.LabelsValue,
        orig_labels_thunk=lambda: client.Get(subscription_ref).labels)
    try:
      result = client.Patch(
          subscription_ref,
          ack_deadline=args.ack_deadline,
          push_config=util.ParsePushConfig(args.push_endpoint),
          retain_acked_messages=args.retain_acked_messages,
          labels=labels_update.GetOrNone(),
          message_retention_duration=args.message_retention_duration)
    except subscriptions.NoFieldsSpecifiedError:
      if not any(args.IsSpecified(arg) for arg in ('clear_labels',
                                                   'update_labels',
                                                   'remove_labels')):
        raise
      log.status.Print('No update to perform.')
      result = None
    else:
      log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription')
    return result
Example #4
0
  def Run(self, args):
    dataproc = dp.Dataproc(self.ReleaseTrack())

    cluster_ref = util.ParseCluster(args.name, dataproc)

    cluster_config = dataproc.messages.ClusterConfig()
    changed_fields = []

    has_changes = False

    if args.num_workers is not None:
      worker_config = dataproc.messages.InstanceGroupConfig(
          numInstances=args.num_workers)
      cluster_config.workerConfig = worker_config
      changed_fields.append('config.worker_config.num_instances')
      has_changes = True

    if args.num_preemptible_workers is not None:
      worker_config = dataproc.messages.InstanceGroupConfig(
          numInstances=args.num_preemptible_workers)
      cluster_config.secondaryWorkerConfig = worker_config
      changed_fields.append(
          'config.secondary_worker_config.num_instances')
      has_changes = True

    if self.ReleaseTrack() == base.ReleaseTrack.BETA:
      lifecycle_config = dataproc.messages.LifecycleConfig()
      changed_config = False
      if args.max_age is not None:
        lifecycle_config.autoDeleteTtl = str(args.max_age) + 's'
        changed_fields.append('config.lifecycle_config.auto_delete_ttl')
        changed_config = True
      if args.expiration_time is not None:
        lifecycle_config.autoDeleteTime = times.FormatDateTime(
            args.expiration_time)
        changed_fields.append('config.lifecycle_config.auto_delete_time')
        changed_config = True
      if args.max_idle is not None:
        lifecycle_config.idleDeleteTtl = str(args.max_idle) + 's'
        changed_fields.append('config.lifecycle_config.idle_delete_ttl')
        changed_config = True
      if args.no_max_age:
        lifecycle_config.autoDeleteTtl = None
        changed_fields.append('config.lifecycle_config.auto_delete_ttl')
        changed_config = True
      if args.no_max_idle:
        lifecycle_config.idleDeleteTtl = None
        changed_fields.append('config.lifecycle_config.idle_delete_ttl')
        changed_config = True
      if changed_config:
        cluster_config.lifecycleConfig = lifecycle_config
        has_changes = True

    # Put in a thunk so we only make this call if needed
    def _GetCurrentLabels():
      # We need to fetch cluster first so we know what the labels look like. The
      # labels_util will fill out the proto for us with all the updates and
      # removals, but first we need to provide the current state of the labels
      get_cluster_request = (
          dataproc.messages.DataprocProjectsRegionsClustersGetRequest(
              projectId=cluster_ref.projectId,
              region=cluster_ref.region,
              clusterName=cluster_ref.clusterName))
      current_cluster = dataproc.client.projects_regions_clusters.Get(
          get_cluster_request)
      return current_cluster.labels
    labels_update = labels_util.ProcessUpdateArgsLazy(
        args, dataproc.messages.Cluster.LabelsValue,
        orig_labels_thunk=_GetCurrentLabels)
    if labels_update.needs_update:
      has_changes = True
      changed_fields.append('labels')
    labels = labels_update.GetOrNone()

    if not has_changes:
      raise exceptions.ArgumentError(
          'Must specify at least one cluster parameter to update.')

    cluster = dataproc.messages.Cluster(
        config=cluster_config,
        clusterName=cluster_ref.clusterName,
        labels=labels,
        projectId=cluster_ref.projectId)

    request = dataproc.messages.DataprocProjectsRegionsClustersPatchRequest(
        clusterName=cluster_ref.clusterName,
        region=cluster_ref.region,
        projectId=cluster_ref.projectId,
        cluster=cluster,
        updateMask=','.join(changed_fields))

    if (self.ReleaseTrack() == base.ReleaseTrack.BETA and
        args.graceful_decommission_timeout):
      request.gracefulDecommissionTimeout = (
          str(args.graceful_decommission_timeout) + 's')

    operation = dataproc.client.projects_regions_clusters.Patch(request)

    if args.async:
      log.status.write(
          'Updating [{0}] with operation [{1}].'.format(
              cluster_ref, operation.name))
      return

    util.WaitForOperation(
        dataproc,
        operation,
        message='Waiting for cluster update operation',
        timeout_s=args.timeout)

    request = dataproc.messages.DataprocProjectsRegionsClustersGetRequest(
        projectId=cluster_ref.projectId,
        region=cluster_ref.region,
        clusterName=cluster_ref.clusterName)
    cluster = dataproc.client.projects_regions_clusters.Get(request)
    log.UpdatedResource(cluster_ref)
    return cluster
Example #5
0
def ParseUpdateLabels(models_client, args):
  def GetLabels():
    return models_client.Get(args.model).labels
  return labels_util.ProcessUpdateArgsLazy(
      args, models_client.messages.GoogleCloudMlV1Model.LabelsValue, GetLabels)