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)
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)
def Run(self, args): util.ValidateUpdateArgsSpecified( args, ['display_name', 'trigger_count', 'trigger_percent', 'if_value'], 'condition') client = policies.AlertPolicyClient() messages = client.messages condition_ref = args.CONCEPTS.condition.Parse() policy_ref = condition_ref.Parent() policy = client.Get(policy_ref) condition = util.GetConditionFromPolicy(condition_ref.RelativeName(), policy) nested_condition = condition.conditionAbsent or condition.conditionThreshold if args.display_name: condition.displayName = args.display_name if args.trigger_count or args.trigger_percent: nested_condition.trigger = messages.Trigger( count=args.trigger_count, percent=args.trigger_percent) if args.if_value is not None: # Copy existing condition properties into kwargs. kwargs = { 'trigger_count': nested_condition.trigger.count, 'trigger_percent': nested_condition.trigger.percent, 'aggregations': nested_condition.aggregations, 'duration': nested_condition.duration, 'condition_filter': nested_condition.filter, 'if_value': args.if_value } # If any trigger values are specified, overwrite whats in args. if args.trigger_count or args.trigger_percent: kwargs['trigger_count'] = args.trigger_count kwargs['trigger_percent'] = args.trigger_percent # Clear nested condition messages as this can potentially change. condition.conditionAbsent = None condition.conditionThreshold = None # This will change condition in place. util.BuildCondition(messages, condition=condition, **kwargs) return client.Update(policy_ref, policy, fields='conditions')