Exemplo n.º 1
0
    def Run(self, args):
        util.ValidateUpdateArgsSpecified(args, [
            'channel_content', 'channel_content_from_file', 'display_name',
            'enabled', 'type', 'description', 'fields', 'update_user_labels',
            'remove_user_labels', 'clear_user_labels', 'update_channel_labels',
            'remove_channel_labels', 'clear_channel_labels'
        ], 'channel')
        flags.ValidateNotificationChannelUpdateArgs(args)

        client = channels.NotificationChannelsClient()
        messages = client.messages

        channel_ref = args.CONCEPTS.channel.Parse()

        passed_yaml_channel = False
        channel_str = args.channel_content or args.channel_content_from_file
        if channel_str:
            passed_yaml_channel = True
            channel = util.MessageFromString(
                channel_str,
                messages.NotificationChannel,
                'NotificationChannel',
                field_remappings=util.CHANNELS_FIELD_REMAPPINGS)
        else:
            channel = client.Get(channel_ref)

        if not args.fields:
            enabled = args.enabled if args.IsSpecified('enabled') else None

            fields = []
            util.ModifyNotificationChannel(channel,
                                           channel_type=args.type,
                                           display_name=args.display_name,
                                           description=args.description,
                                           enabled=enabled,
                                           field_masks=fields)

            new_user_labels = util.ProcessUpdateLabels(
                args, 'user_labels',
                messages.NotificationChannel.UserLabelsValue,
                channel.userLabels)
            new_channel_labels = util.ProcessUpdateLabels(
                args, 'channel_labels',
                messages.NotificationChannel.LabelsValue, channel.labels)
            # TODO(b/73120276): Use field masks per key for label updates.
            if new_user_labels:
                channel.userLabels = new_user_labels
                fields.append('user_labels')
            if new_channel_labels:
                channel.labels = new_channel_labels
                fields.append('labels')

            # For more robust concurrent updates, use update masks if we're not
            # trying to replace the channel using --channel-content or
            # --channel-content-from-file.
            fields = None if passed_yaml_channel else ','.join(sorted(fields))
        else:
            fields = ','.join(args.fields)

        return client.Update(channel_ref, channel, fields)
Exemplo n.º 2
0
  def Run(self, args):
    util.ValidateUpdateArgsSpecified(
        args,
        ['policy', 'policy_from_file', 'display_name', 'enabled',
         'add_notification_channels', 'remove_notification_channels',
         'set_notification_channels',
         'clear_notification_channels', 'documentation', 'documentation_format',
         'documentation_from_file', 'fields', 'update_user_labels',
         'remove_user_labels', 'clear_user_labels'],
        'policy')
    flags.ValidateAlertPolicyUpdateArgs(args)

    client = policies.AlertPolicyClient()
    messages = client.messages

    passed_yaml_policy = False
    policy_ref = args.CONCEPTS.alert_policy.Parse()
    if args.policy or args.policy_from_file:
      passed_yaml_policy = True
      policy = util.GetBasePolicyMessageFromArgs(args, messages.AlertPolicy)
    else:
      # If a full policy isn't given, we want to do Read-Modify-Write.
      policy = client.Get(policy_ref)

    if not args.fields:
      channels = policy.notificationChannels
      new_channels = repeated.ParseResourceNameArgs(
          args, 'notification_channels', lambda: channels,
          util.ParseNotificationChannel)
      enabled = args.enabled if args.IsSpecified('enabled') else None

      fields = []
      util.ModifyAlertPolicy(
          policy,
          messages,
          display_name=args.display_name,
          documentation_content=
          args.documentation or args.documentation_from_file,
          documentation_format=args.documentation_format,
          enabled=enabled,
          channels=new_channels,
          field_masks=fields)

      new_labels = util.ProcessUpdateLabels(
          args,
          'user_labels',
          messages.AlertPolicy.UserLabelsValue,
          policy.userLabels)
      if new_labels:
        policy.userLabels = new_labels
        # TODO(b/73120276): Use field masks per key for label updates.
        fields.append('user_labels')

      # For more robust concurrent updates, use update masks if we're not
      # trying to replace the policy using --policy or --policy-from-file.
      fields = None if passed_yaml_policy else ','.join(sorted(fields))
    else:
      fields = ','.join(args.fields)

    return client.Update(policy_ref, policy, fields)