Ejemplo n.º 1
0
 def testEventTypeFromTypeStringMultipleMatchesNoPrompt(self):
     properties.VALUES.core.disable_prompts.Set(True)
     self._MakeSourceCrds(num_sources=1, num_event_types_per_source=2)
     self._MakeSourceCrds(num_sources=1, num_event_types_per_source=2)
     with self.assertRaises(exceptions.MultipleEventTypesFound):
         util.EventTypeFromTypeString(self.source_crds,
                                      'google.source.0.event.type.0')
Ejemplo n.º 2
0
    def Run(self, args):
        conn_context = connection_context.EventsConnectionContext(args)

        with eventflow_operations.Connect(conn_context) as client:
            source_crds = client.ListSourceCustomResourceDefinitions()
            return util.EventTypeFromTypeString(source_crds, args.event_type,
                                                args.source)
Ejemplo n.º 3
0
 def testEventTypeFromTypeStringFilterSource(self):
     self._MakeSourceCrds(num_sources=2, num_event_types_per_source=2)
     self.assertEqual(
         self.event_types[2],
         util.EventTypeFromTypeString(self.source_crds,
                                      'google.source.1.event.type.0',
                                      'CloudPubSubSource1'))
Ejemplo n.º 4
0
  def Run(self, args):
    conn_context = connection_context.GetConnectionContext(
        args, serverless_flags.Product.EVENTS, self.ReleaseTrack())

    with eventflow_operations.Connect(conn_context) as client:
      source_crds = client.ListSourceCustomResourceDefinitions()
      return util.EventTypeFromTypeString(
          source_crds, args.event_type, args.source)
Ejemplo n.º 5
0
 def testEventTypeFromTypeStringMultipleMatchesWithPrompt(self):
     properties.VALUES.core.disable_prompts.Set(False)
     self._MakeSourceCrds(num_sources=1, num_event_types_per_source=2)
     self._MakeSourceCrds(num_sources=1, num_event_types_per_source=2)
     self.WriteInput('2')
     self.assertEqual(
         self.event_types[1],
         util.EventTypeFromTypeString(self.source_crds,
                                      'google.source.0.event.type.1'))
Ejemplo n.º 6
0
    def Run(self, args):
        conn_context = connection_context.GetConnectionContext(args)
        if conn_context.supports_one_platform:
            raise exceptions.UnsupportedArgumentError(
                'Events are only available with Cloud Run for Anthos.')

        with eventflow_operations.Connect(conn_context) as client:
            source_crds = client.ListSourceCustomResourceDefinitions()
            return util.EventTypeFromTypeString(source_crds, args.event_type)
Ejemplo n.º 7
0
  def Run(self, args):
    conn_context = connection_context.GetConnectionContext(
        args, product=connection_context.Product.EVENTS)
    if conn_context.supports_one_platform:
      raise exceptions.UnsupportedArgumentError(
          'Events are only available with Cloud Run for Anthos.')

    trigger_ref = args.CONCEPTS.trigger.Parse()
    namespace_ref = trigger_ref.Parent()
    with eventflow_operations.Connect(conn_context) as client:
      source_crds = client.ListSourceCustomResourceDefinitions()
      event_type = util.EventTypeFromTypeString(source_crds, args.type)
      source_obj = source.Source.New(client.client, namespace_ref.Name(),
                                     event_type.crd.source_kind,
                                     event_type.crd.source_api_category)
      source_obj.name = _SOURCE_NAME_PATTERN.format(
          trigger=trigger_ref.Name())

      trigger_obj = client.GetTrigger(trigger_ref)
      if trigger_obj is not None:
        # If trigger already exists, validate it has the attributes we're trying
        # to set right now.
        try:
          util.ValidateTrigger(trigger_obj, source_obj, event_type)
        except AssertionError:
          raise exceptions.TriggerCreationError(
              'Trigger [{}] already exists with attributes not '
              'matching this event type.'.format(trigger_obj.name))
        # If the trigger has the right attributes, check if there's already
        # a source that matches the attributes as well.
        source_ref = util.GetSourceRef(
            source_obj.name, source_obj.namespace, event_type.crd)
        if client.GetSource(source_ref, event_type.crd) is not None:
          raise exceptions.TriggerCreationError(
              'Trigger [{}] already exists.'.format(trigger_obj.name))

      parameters = events_flags.GetAndValidateParameters(args, event_type)

      # Create the trigger and source
      with progress_tracker.StagedProgressTracker(
          'Initializing trigger...',
          stages.TriggerSourceStages(),
          failure_message='Trigger creation failed') as tracker:
        client.CreateTriggerAndSource(
            trigger_obj,
            trigger_ref,
            namespace_ref,
            source_obj,
            event_type,
            parameters,
            args.broker,
            args.target_service,
            tracker
        )
Ejemplo n.º 8
0
    def Run(self, args):
        conn_context = connection_context.GetConnectionContext(
            args, serverless_flags.Product.EVENTS, self.ReleaseTrack())

        trigger_ref = args.CONCEPTS.trigger.Parse()
        namespace_ref = trigger_ref.Parent()
        with eventflow_operations.Connect(conn_context) as client:
            if client.IsCluster():
                trigger_ref = resources.REGISTRY.Parse(
                    trigger_ref.RelativeName(),
                    collection=util.ANTHOS_TRIGGER_COLLECTION_NAME,
                    api_version=client.api_version)

                namespace_ref = trigger_ref.Parent()
            if args.custom_type:
                event_type = args.type
                source_obj = None
                tracker_stages = stages.TriggerStages()
            else:
                source_crds = client.ListSourceCustomResourceDefinitions()
                event_type = util.EventTypeFromTypeString(
                    source_crds, args.type, args.source)
                source_obj = source.Source.New(
                    client.client, namespace_ref.Name(),
                    event_type.crd.source_kind,
                    event_type.crd.source_api_category)
                source_obj.name = _SOURCE_NAME_PATTERN.format(
                    trigger=trigger_ref.Name())
                parameters = flags.GetAndValidateParameters(args, event_type)
                tracker_stages = stages.TriggerAndSourceStages()

            trigger_obj = client.GetTrigger(trigger_ref)
            if trigger_obj is not None:
                if args.custom_type:
                    # If custom type, no need to check idempotency since there's only
                    # a trigger to worry about.
                    raise exceptions.TriggerCreationError(
                        'Trigger [{}] already exists.'.format(
                            trigger_obj.name))
                else:
                    # If trigger already exists, validate it has the attributes we're
                    # trying to set right now to see if this is a case of idempotency.
                    try:
                        util.ValidateTrigger(trigger_obj, source_obj,
                                             event_type)
                    except AssertionError:
                        raise exceptions.TriggerCreationError(
                            'Trigger [{}] already exists with attributes not '
                            'matching this event type.'.format(
                                trigger_obj.name))
                    # If the trigger has the right attributes, check if there's already
                    # a source that matches the attributes as well.
                    source_ref = util.GetSourceRef(source_obj.name,
                                                   source_obj.namespace,
                                                   event_type.crd,
                                                   client.IsCluster())
                    if client.GetSource(source_ref,
                                        event_type.crd) is not None:
                        raise exceptions.TriggerCreationError(
                            'Trigger [{}] already exists.'.format(
                                trigger_obj.name))

            # Create the trigger and source
            with progress_tracker.StagedProgressTracker(
                    'Initializing trigger...',
                    tracker_stages,
                    failure_message='Trigger creation failed') as tracker:
                if trigger_obj is None:
                    trigger_obj = client.CreateTrigger(
                        trigger_ref, source_obj,
                        event_type if args.custom_type else event_type.type,
                        args.trigger_filters, args.target_service, args.broker)
                if not args.custom_type:
                    client.CreateSource(source_obj, event_type.crd,
                                        trigger_obj, namespace_ref,
                                        args.broker, parameters)
                    client.PollSource(source_obj, event_type, tracker)
                client.PollTrigger(trigger_ref, tracker)
Ejemplo n.º 9
0
 def testEventTypeFromTypeStringNoEventTypes(self):
     self._MakeSourceCrds(num_sources=2, num_event_types_per_source=0)
     with self.assertRaises(exceptions.EventTypeNotFound):
         util.EventTypeFromTypeString(self.source_crds,
                                      'google.source.0.event.type.0')
Ejemplo n.º 10
0
 def testEventTypeFromTypeStringNotFound(self):
     self._MakeSourceCrds(num_sources=2, num_event_types_per_source=2)
     with self.assertRaises(exceptions.EventTypeNotFound):
         util.EventTypeFromTypeString(self.source_crds,
                                      'nonexistent.event.type')