def Run(self, args):
     """Run the update command."""
     client = triggers.CreateTriggersClient(self.ReleaseTrack())
     trigger_ref = args.CONCEPTS.trigger.Parse()
     event_filters = flags.GetEventFiltersArg(args, self.ReleaseTrack())
     event_filters_path_pattern = flags.GetEventFiltersPathPatternArg(
         args, self.ReleaseTrack())
     update_mask = client.BuildUpdateMask(
         event_filters=event_filters is not None,
         event_filters_path_pattern=event_filters_path_pattern is not None,
         service_account=args.IsSpecified('service_account')
         or args.clear_service_account,
         destination_run_service=args.IsSpecified(
             'destination_run_service'),
         destination_run_job=args.IsSpecified('destination_run_job'),
         destination_run_path=args.IsSpecified('destination_run_path')
         or args.clear_destination_run_path,
         destination_run_region=args.IsSpecified('destination_run_region'),
         destination_gke_namespace=args.IsSpecified(
             'destination_gke_namespace'),
         destination_gke_service=args.IsSpecified(
             'destination_gke_service'),
         destination_gke_path=args.IsSpecified('destination_gke_path')
         or args.clear_destination_gke_path,
         destination_workflow=args.IsSpecified('destination_workflow'),
         destination_workflow_location=args.IsSpecified(
             'destination_workflow_location'))
     old_trigger = client.Get(trigger_ref)
     # The type can't be updated, so it's safe to use the old trigger's type.
     # In the async case, this is the only way to get the type.
     self._event_type = client.GetEventType(old_trigger)
     destination_message = None
     if (args.IsSpecified('destination_run_service')
             or args.IsSpecified('destination_run_job')
             or args.IsSpecified('destination_run_region')
             or args.IsSpecified('destination_run_path')
             or args.clear_destination_run_path):
         destination_message = client.BuildCloudRunDestinationMessage(
             args.destination_run_service, args.destination_run_job,
             args.destination_run_path, args.destination_run_region)
     elif (args.IsSpecified('destination_gke_namespace')
           or args.IsSpecified('destination_gke_service')
           or args.IsSpecified('destination_gke_path')
           or args.clear_destination_gke_path):
         destination_message = client.BuildGKEDestinationMessage(
             None, None, args.destination_gke_namespace,
             args.destination_gke_service, args.destination_gke_path)
     elif (args.IsSpecified('destination_workflow')
           or args.IsSpecified('destination_workflow_location')):
         location = self.GetWorkflowDestinationLocation(args, old_trigger)
         workflow = self.GetWorkflowDestination(args, old_trigger)
         destination_message = client.BuildWorkflowDestinationMessage(
             trigger_ref.Parent().Parent().Name(), workflow, location)
     trigger_message = client.BuildTriggerMessage(
         trigger_ref, event_filters, event_filters_path_pattern,
         args.service_account, destination_message, None, None)
     operation = client.Patch(trigger_ref, trigger_message, update_mask)
     if args.async_:
         return operation
     return client.WaitFor(operation, 'Updating', trigger_ref)
 def Run(self, args):
     """Run the update command."""
     client = triggers.CreateTriggersClient(self.ReleaseTrack())
     trigger_ref = args.CONCEPTS.trigger.Parse()
     event_filters = flags.GetEventFiltersArg(args, self.ReleaseTrack())
     update_mask = client.BuildUpdateMask(
         event_filters=event_filters is not None,
         service_account=args.IsSpecified('service_account')
         or args.clear_service_account,
         destination_run_service=args.IsSpecified(
             'destination_run_service'),
         destination_run_job=
         None,  # Not supported in BETA release track or API
         destination_run_path=args.IsSpecified('destination_run_path')
         or args.clear_destination_run_path,
         destination_run_region=args.IsSpecified('destination_run_region'))
     old_trigger = client.Get(trigger_ref)
     # The type can't be updated, so it's safe to use the old trigger's type.
     # In the async case, this is the only way to get the type.
     self._event_type = client.GetEventType(old_trigger)
     destination_message = client.BuildCloudRunDestinationMessage(
         args.destination_run_service, None, args.destination_run_path,
         args.destination_run_region)
     trigger_message = client.BuildTriggerMessage(trigger_ref,
                                                  event_filters, None,
                                                  args.service_account,
                                                  destination_message, None,
                                                  None)
     operation = client.Patch(trigger_ref, trigger_message, update_mask)
     if args.async_:
         return operation
     return client.WaitFor(operation, 'Updating', trigger_ref)
Example #3
0
 def Run(self, args):
     """Run the create command."""
     client = triggers.CreateTriggersClient(self.ReleaseTrack())
     trigger_ref = args.CONCEPTS.trigger.Parse()
     transport_topic_ref = args.CONCEPTS.transport_topic.Parse()
     event_filters = flags.GetEventFiltersArg(args, self.ReleaseTrack())
     operation = client.Create(trigger_ref, event_filters,
                               args.service_account,
                               args.destination_run_service,
                               args.destination_run_path,
                               args.destination_run_region,
                               transport_topic_ref)
     self._event_type = event_filters['type']
     if args.async_:
         return operation
     response = client.WaitFor(operation, 'Creating', trigger_ref)
     trigger_dict = encoding.MessageToPyValue(response)
     if types.IsPubsubType(self._event_type):
         topic = trigger_dict['transport']['pubsub']['topic']
         if args.IsSpecified('transport_topic'):
             log.status.Print(
                 'Publish to Pub/Sub topic [{}] to receive events '
                 'in Cloud Run service [{}].'.format(
                     topic, args.destination_run_service))
         else:
             log.status.Print('Created Pub/Sub topic [{}].'.format(topic))
             log.status.Print(
                 'Publish to this topic to receive events in Cloud Run service [{}].'
                 .format(args.destination_run_service))
     return response
    def Run(self, args):
        """Run the create command."""
        client = triggers.CreateTriggersClient(self.ReleaseTrack())
        trigger_ref = args.CONCEPTS.trigger.Parse()
        channel_ref = flags.GetChannelArg(args, self.ReleaseTrack())
        transport_topic_ref = args.CONCEPTS.transport_topic.Parse()
        event_filters = flags.GetEventFiltersArg(args, self.ReleaseTrack())
        event_filters_path_pattern = flags.GetEventFiltersPathPatternArg(
            args, self.ReleaseTrack())

        destination_message = None
        # destination Cloud Run
        if args.IsSpecified(
                'destination_run_service') or args.IsKnownAndSpecified(
                    'destination_run_job'):
            resource_type = 'service' if args.IsSpecified(
                'destination_run_service') else 'job'
            destination_run_region = self.GetDestinationLocation(
                args, trigger_ref, 'destination_run_region',
                'Cloud Run {}'.format(resource_type))

            # Jobs only exist in the v1 API and thus only in the GA track.
            # This extra check is needed so we don't throw trying to access a flag
            # which doesn't exist.
            run_job = args.destination_run_job if 'destination_run_job' in args else None
            destination_message = client.BuildCloudRunDestinationMessage(
                args.destination_run_service, run_job,
                args.destination_run_path, destination_run_region)
            dest_str = 'Cloud Run {} [{}]'.format(
                resource_type, args.destination_run_service or run_job)
            loading_msg = ''
        # destination GKE service
        elif args.IsSpecified('destination_gke_service'):
            destination_gke_location = self.GetDestinationLocation(
                args, trigger_ref, 'destination_gke_location', 'GKE service')
            destination_gke_namespace = args.destination_gke_namespace or 'default'

            destination_message = client.BuildGKEDestinationMessage(
                args.destination_gke_cluster, destination_gke_location,
                destination_gke_namespace, args.destination_gke_service,
                args.destination_gke_path)
            dest_str = 'GKE service [{}] in cluster [{}]'.format(
                args.destination_gke_service, args.destination_gke_cluster)
            loading_msg = 'this operation may take several minutes'
        # destination Workflow
        elif args.IsSpecified('destination_workflow'):
            destination_workflow_location = self.GetDestinationLocation(
                args, trigger_ref, 'destination_workflow_location', 'Workflow')

            destination_message = client.BuildWorkflowDestinationMessage(
                trigger_ref.Parent().Parent().Name(),
                args.destination_workflow, destination_workflow_location)
            dest_str = 'Workflow [{}]'.format(args.destination_workflow)
            loading_msg = ''
        else:
            raise UnsupportedDestinationError(
                'Must specify a valid destination.')
        trigger_message = client.BuildTriggerMessage(
            trigger_ref, event_filters, event_filters_path_pattern,
            args.service_account, destination_message, transport_topic_ref,
            channel_ref)
        operation = client.Create(trigger_ref, trigger_message)
        self._event_type = event_filters['type']
        if args.async_:
            return operation

        response = client.WaitFor(operation, 'Creating', trigger_ref,
                                  loading_msg)
        trigger_dict = encoding.MessageToPyValue(response)
        if types.IsPubsubType(self._event_type):
            topic = trigger_dict['transport']['pubsub']['topic']
            if args.IsSpecified('transport_topic'):
                log.status.Print(
                    'Publish to Pub/Sub topic [{}] to receive events '
                    'in {}.'.format(topic, dest_str))
            else:
                log.status.Print('Created Pub/Sub topic [{}].'.format(topic))
                log.status.Print(
                    'Publish to this topic to receive events in {}.'.format(
                        dest_str))
        return response