def GetSource(self, source_ref, source_crd):
     """Returns the referenced source."""
     request_method = self.SourceGetMethod(source_crd)
     request_message_type = request_method.GetRequestType()
     request = request_message_type(name=source_ref.RelativeName())
     try:
         response = request_method.Call(request, client=self._client)
     except api_exceptions.HttpNotFoundError:
         return None
     return source.Source(response, self.messages, source_crd.source_kind)
 def GetSource(self, source_ref, source_crd):
   """Returns the referenced source."""
   request_message_type = self.SourceGetMessage(source_crd)
   request = request_message_type(
       name=source_ref.RelativeName())
   try:
     client_service = self.SourceClientService(source_crd)
     response = client_service.Get(request)
   except api_exceptions.HttpNotFoundError:
     return None
   return source.Source(response, self.messages, source_crd.source_kind)
Beispiel #3
0
    def CreateSource(self, source_obj, source_crd, owner_trigger,
                     namespace_ref, broker_name, parameters):
        """Create an source with the specified event type and owner trigger.

    Args:
      source_obj: source.Source. The source object being created.
      source_crd: custom_resource_definition.SourceCRD, the source crd for the
        source to create
      owner_trigger: trigger.Trigger, trigger to associate as an owner of the
        source.
      namespace_ref: googlecloudsdk.core.resources.Resource, namespace resource.
      broker_name: str, name of the broker to act as a sink.
      parameters: dict, additional parameters to set on the source spec.

    Returns:
      source.Source of the created source.
    """
        client = self.ClientFromCrd(source_crd)
        messages = client.MESSAGES_MODULE

        source_obj.ce_overrides[trigger.SOURCE_TRIGGER_LINK_FIELD] = (
            owner_trigger.filter_attributes[trigger.SOURCE_TRIGGER_LINK_FIELD])

        source_obj.owners.append(
            messages.OwnerReference(apiVersion=owner_trigger.apiVersion,
                                    kind=owner_trigger.kind,
                                    name=owner_trigger.name,
                                    uid=owner_trigger.uid,
                                    controller=True))
        source_obj.set_sink(broker_name, self._api_version)

        # Parse parameters flags into source's spec
        source.ParseDynamicFieldsIntoMessage(source_obj.spec, parameters)
        source.SourceFix(source_obj)

        request_method = self.SourceCreateMethod(source_crd)
        request_message_type = request_method.GetRequestType()
        request = request_message_type(
            **{
                request_method.request_field: source_obj.Message(),
                'parent': namespace_ref.RelativeName()
            })
        try:
            with metrics.RecordDuration(metric_names.CREATE_SOURCE):
                response = request_method.Call(request, client=client)
        except api_exceptions.HttpConflictError:
            raise exceptions.SourceCreationError(
                'Source [{}] already exists.'.format(source_obj.name))

        return source.Source(response, messages, source_crd.source_kind)
Beispiel #4
0
    def GetSource(self, source_ref, source_crd):
        """Returns the referenced source."""
        client = self.ClientFromCrd(source_crd)
        messages = client.MESSAGES_MODULE

        request_method = self.SourceGetMethod(source_crd)
        request_message_type = request_method.GetRequestType()
        request = request_message_type(name=source_ref.RelativeName())
        try:
            with metrics.RecordDuration(metric_names.GET_SOURCE):
                response = request_method.Call(request, client=client)
        except api_exceptions.HttpNotFoundError:
            return None
        return source.Source(response, messages, source_crd.source_kind)
  def CreateSource(self, source_obj, source_crd, owner_trigger, namespace_ref,
                   broker, parameters):
    """Create an source with the specified event type and owner trigger.

    Args:
      source_obj: source.Source. The source object being created.
      source_crd: custom_resource_definition.SourceCRD, the source crd for the
        source to create
      owner_trigger: trigger.Trigger, trigger to associate as an owner of the
        source.
      namespace_ref: googlecloudsdk.core.resources.Resource, namespace resource.
      broker: str, name of the broker to act as a sink.
      parameters: dict, additional parameters to set on the source spec.

    Returns:
      source.Source of the created source.
    """
    source_obj.ce_overrides[trigger.SOURCE_TRIGGER_LINK_FIELD] = (
        owner_trigger.filter_attributes[trigger.SOURCE_TRIGGER_LINK_FIELD])
    source_obj.owners.append(
        self.messages.OwnerReference(
            apiVersion=owner_trigger.apiVersion,
            kind=owner_trigger.kind,
            name=owner_trigger.name,
            uid=owner_trigger.uid,
            controller=True))
    source_obj.sink = broker
    arg_utils.ParseStaticFieldsIntoMessage(source_obj.spec, parameters)

    request_message_type = self.SourceCreateMessage(source_crd)
    source_field = (
        source_crd.source_kind[0].lower() + source_crd.source_kind[1:])
    request = request_message_type(**{
        source_field: source_obj.Message(),
        'parent': namespace_ref.RelativeName()})
    with metrics.RecordDuration(metric_names.CREATE_SOURCE):
      try:
        client_service = self.SourceClientService(source_crd)
        response = client_service.Create(request)
      except api_exceptions.HttpConflictError:
        raise exceptions.SourceCreationError(
            'Source [{}] already exists.'.format(source_obj.name))

    return source.Source(response, self.messages, source_crd.source_kind)