class Google::Cloud::PubSub::Subscription::PushConfig

Configuration for a push delivery endpoint.

@example Create a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
topic = pubsub.topic "my-topic"

push_config = Google::Cloud::PubSub::Subscription::PushConfig.new endpoint: "http://example.net/callback"
push_config.set_oidc_token "service-account@example.net", "audience-header-value"

sub = topic.subscribe "my-subscription", push_config: push_config

@example Read a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

sub = pubsub.subscription "my-topic-sub"
sub.push_config.endpoint #=> "http://example.com/callback"
sub.push_config.authentication.email #=> "user@example.com"
sub.push_config.authentication.audience #=> "client-12345"

@example Update a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
sub = pubsub.subscription "my-subscription"

sub.push_config do |pc|
  pc.endpoint = "http://example.net/callback"
  pc.set_oidc_token "user@example.net", "client-67890"
end

Public Class Methods

from_grpc(grpc) click to toggle source

@private

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 192
def self.from_grpc grpc
  new.tap do |pc|
    pc.instance_variable_set :@grpc, grpc.dup if grpc
  end
end
new(endpoint: nil, email: nil, audience: nil) click to toggle source

Creates a new push configuration.

@param [String] endpoint A URL locating the endpoint to which messages should be pushed. For

example, a Webhook endpoint might use `https://example.com/push`.

@param [String] email The service account email to be used for generating the OIDC token.

The caller must have the `iam.serviceAccounts.actAs` permission for the service account.

@param [String] audience The audience to be used when generating OIDC token. The audience claim identifies

the recipients that the JWT is intended for. The audience value is a single case-sensitive string. Having
multiple values (array) for the audience field is not supported. More info about the OIDC JWT token
audience here: https://tools.ietf.org/html/rfc7519#section-4.1.3 Note: if not specified, the `endpoint`
URL will be used.
# File lib/google/cloud/pubsub/subscription/push_config.rb, line 71
def initialize endpoint: nil, email: nil, audience: nil
  @grpc = Google::Cloud::PubSub::V1::PushConfig.new

  self.endpoint = endpoint unless endpoint.nil?

  raise ArgumentError, "audience provided without email. Authentication is invalid" if audience && !email

  set_oidc_token email, audience if email
end

Public Instance Methods

authentication() click to toggle source

The authentication method used by push endpoints to verify the source of push requests.

@return [OidcToken, nil] An OIDC JWT token if specified, `nil`

otherwise.
# File lib/google/cloud/pubsub/subscription/push_config.rb, line 104
def authentication
  return nil unless @grpc.authentication_method == :oidc_token

  OidcToken.from_grpc @grpc.oidc_token
end
authentication=(new_auth) click to toggle source

Sets the authentication method used by push endpoints to verify the source of push requests.

@param [OidcToken, nil] new_auth An authentication value.

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 114
def authentication= new_auth
  if new_auth.nil?
    @grpc.oidc_token = nil
  else
    raise ArgumentError unless new_auth.is_a? OidcToken

    @grpc.oidc_token = new_auth.to_grpc
  end
end
endpoint() click to toggle source

A URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use `example.com/push`.

@return [String]

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 86
def endpoint
  @grpc.push_endpoint
end
endpoint=(new_endpoint) click to toggle source

Sets the URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use `example.com/push`.

@param [String, nil] new_endpoint New URL value

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 95
def endpoint= new_endpoint
  @grpc.push_endpoint = String new_endpoint
end
oidc_token?() click to toggle source

Checks whether authentication is an {OidcToken}.

@return [Boolean]

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 128
def oidc_token?
  authentication.is_a? OidcToken
end
set_oidc_token(email, audience) click to toggle source

Sets the authentication method to use an {OidcToken}.

@param [String] email Service account email. @param [String] audience Audience to be used.

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 137
def set_oidc_token email, audience
  oidc_token = OidcToken.new.tap do |token|
    token.email = email
    token.audience = audience
  end
  self.authentication = oidc_token
end
to_grpc() click to toggle source

@private

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 186
def to_grpc
  @grpc
end
version() click to toggle source

The format of the pushed message. This attribute indicates the version of the data expected by the endpoint. This controls the shape of the pushed message (i.e., its fields and metadata). The endpoint version is based on the version of the Pub/Sub API.

If not present during the Subscription creation, it will default to the version of the API used to make such call.

The possible values for this attribute are:

  • `v1beta1`: uses the push format defined in the v1beta1 Pub/Sub API.

  • `v1` or `v1beta2`: uses the push format defined in the v1 Pub/Sub API.

@return [String]

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 161
def version
  @grpc.attributes["x-goog-version"]
end
version=(new_version) click to toggle source

Sets the format of the pushed message.

The possible values for this attribute are:

  • `v1beta1`: uses the push format defined in the v1beta1 Pub/Sub API.

  • `v1` or `v1beta2`: uses the push format defined in the v1 Pub/Sub API.

@param [String, nil] new_version The new version value.

# File lib/google/cloud/pubsub/subscription/push_config.rb, line 176
def version= new_version
  if new_version.nil?
    @grpc.attributes.delete "x-goog-version"
  else
    @grpc.attributes["x-goog-version"] = new_version
  end
end