TIPCommon.adapters
class TIPCommon.adapters.pubsub.pubsub.PubSubAdapter
class TIPCommon.adapters.pubsub.pubsub.PubSubAdapter(session, project_id=None, logger=None, region=None)
Bases: object
Adapter class for managing Google Cloud project Pub/Sub topics and subscriptions.
ack
ack(sub_name, ack_ids)
Acknowledges the messages associated with the ackIds in the
AcknowledgeRequest response returned from PubSubAdapter.pull().
| Parameters | |
|---|---|
sub_name |
strThe subscription name. |
ack_ids |
list[str]
A list of acknowledgment IDs for the messages being
acknowledged. The list is returned by the Pub/Sub system in
the |
static build_pubsub_message
static build_pubsub_message(message_content, encoding='utf-8', ordering_key=None, **attr)
Creates a PubSubMessage object.
| Parameters | |
|---|---|
message_content |
str
The message content. |
encoding |
str
The encoding type to encode or decode the message text. The default value is |
ordering_key |
str
Optional. If configured, the parameter identifies all related messages that must follow the publish order. |
**attr |
str
Optional. The attributes to pass as message object attributes. |
Returns
A TIPCommon.adapters.pubsub.PubSubMessage object.
Return type
create_subscription
create_subscription(sub_name, topic, **attr)
Creates a Pub/Sub subscription for the specified topic.
| Parameters | |
|---|---|
sub_name |
str
The subscription name. |
topic |
str
A Pub/Sub topic name to create the subscription for. |
**attr |
Additional parameters to pass on to the subscription request. |
Returns
TIPCommon.adapters.pubsub.Subscription object of the created subscription.
Return type
Subscription
create_topic
create_topic(topic_name)
Creates a Pub/Sub topic in a Google Cloud project.
| Parameters | |
|---|---|
topic_name |
str
The name of the topic. The name must correspond to Google Cloud resource name rules. |
Returns
TIPCommon.adapters.pubsub.Topic object of the created topic.
Return type
Topic
delete_subscription
delete_subscription(sub_name)
Deletes a Pub/Sub subscription from a Google Cloud project.
| Parameters | |
|---|---|
sub_name |
str
The subscription name to remove. |
delete_topic
delete_topic(topic_name)
Deletes a Pub/Sub topic from a Google Cloud project.
| Parameters | |
|---|---|
topic_name |
strThe topic to remove. |
static from_credentials
static from_credentials(credentials, project_id=None, verify_ssl=True, quota_project=None, logger=None, region=None)
Creates a PubSubAdapter object from a google.oauth2.credentials.Credentials
object.
| Parameters | |
|---|---|
credentials |
google.oauth2.credentials.Credentials
A |
project_id |
Optional.
The Google Cloud project ID. If not provided, the function attempts to use the project configured in the credentials object by default. |
verify_ssl |
Optional.
Specifies whether SSL certificate verification is enabled for HTTP sessions. |
quota_project |
Optional.
The project to use for quota and billing. |
logger |
Optional.
A |
region |
str
The region for Pub/Sub to work in. |
Returns
A PubSubAdapter object.
Return type
static from_service_account_info
static subscription_name(user_service_account, project_id=None, verify_ssl=True, quota_project=None, logger=None)
Creates a PubSubAdapter object from a service_account JSON.
| Parameters | |
|---|---|
user_service_account |
str
The Google Cloud Service Account JSON in text format. |
project_id |
Optional.
The Google Cloud project ID. If not provided, the function attempts to use the project configured in the credentials object by default. |
verify_ssl |
Optional.
Specifies whether SSL certificate verification is enabled for HTTP sessions. |
quota_project |
Optional.
The project to use for quota and billing. |
logger |
Optional.
A |
Returns
A PubSubAdapter object.
Return type
get_subscription
get_subscription(sub_name, topic=None, create_if_not_exist=False, **attr)
Retrieves a Pub/Sub subscription.
| Parameters | |
|---|---|
sub_name |
str
The subscription name. |
topic |
str
A Pub/Sub topic name to create a subscription for. Mandatory if |
create_if_not_exist |
Creates the Pub/Sub subscription in Google Cloud if it doesn't exist. |
**attr |
Additional parameters to pass to the subscription creation request. |
Returns
The TIPCommon.adapters.pubsub.Subscription object of the retrieved
subscription.
Return type
Subscription
get_topic
get_topic(topic_name, create_if_not_exist=False)
Retrieves a Pub/Sub topic object from the configured Google Cloud project.
| Parameters | |
|---|---|
topic_name |
strName of the topic (simplified, without the
|
create_if_not_exist |
boolCreates the Pub/Sub topic in Google Cloud if it doesn't exist. |
Returns
The TIPCommon.adapters.pubsub.Topic object of the received topic.
Return type
Topic
patch_subscription
patch_subscription(sub_name, topic_name, push_config=None, bigquery_config=None, cloud_storage_config=None, ack_deadline_seconds=None, retain_acked_messages=None, retention_duration=None, labels=None, enable_message_ordering=None, expiration_policy=None, query_filter=None, dead_letter_policy=None, return_policy=None, detached=None, enable_once_delivery=None)
Updates an existing subscription.
| Parameters | |
|---|---|
sub_name |
strThe subscription name. |
topic_name |
strThe name of the topic from which the subscription receives messages. |
push_config |
dictOptional. Configuration for a push delivery endpoint. |
bigquery_config |
dictOptional. Configuration for a BigQuery subscription. |
cloud_storage_config |
dictOptional. Configuration for a Cloud Storage subscription. |
ack_deadline_seconds |
intOptional. The approximate amount of time Pub/Sub waits for the subscriber to acknowledge a received message before the message is redelivered. |
retain_acked_messages |
boolOptional. Indicates whether to retain acknowledged messages. |
retention_duration |
strOptional. The amount of time to retain unacknowledged messages in the subscription's backlog, from the moment a message is published. |
labels |
dictOptional. A set of key-value pairs used to organize the subscription. |
enable_message_ordering |
boolOptional. If |
expiration_policy |
dictOptional. Policy specifying the conditions for this subscription's expiration. |
query_filter |
strOptional. An expression written in the Pub/Sub filter language that specifies which messages are delivered to the subscription. |
dead_letter_policy |
dictOptional. A policy that specifies the conditions for dead-lettering messages in this subscription. |
return_policy |
dictOptional. Policy specifying the conditions for returning messages. |
detached |
boolOptional. Indicates whether the subscription is detached from the topic. |
enable_once_delivery |
boolOptional. If |
Returns
The TIPCommon.adapters.pubsub.Subscription object of the received subscription.
Return type
Subscription
patch_topic
patch_topic(topic_name, labels=None, message_storage_policy=None, kms_key_name=None, schema_settings=None, satisfies_pzs=None, retention_duration=None)
Updates an existing topic.
| Parameters | |
|---|---|
topic_name |
strThe topic name. |
labels |
dictOptional. A set of key-value pairs used to organize the topic. |
message_storage_policy |
dictOptional. Policy constraining the regions in which messages are stored at rest. |
kms_key_name |
strOptional. The resource name of the Cloud Key Management Service CryptoKey to be used to protect messages published to this topic. |
schema_settings |
dictOptional. Settings for validating messages published against a schema. |
satisfies_pzs |
boolOptional. Reserved for future use. This field is
set to |
retention_duration |
strOptional. The amount of time a published message is retained by the topic. If configured, the message will be retained for this duration, regardless of any subscription configurations. |
Returns
The TIPCommon.adapters.pubsub.Topic object of the received topic.
Return type
Topic
publish
publish(topic_name, messages)
Publishes a list of PubSubMessage objects to a topic.
| Parameters | |
|---|---|
topic_name |
strThe name of the target topic for the message publication. |
messages |
listA list containing The |
Returns
A list of message IDs.
Return type
list[str]
pull
pull(sub_name, limit, timeout=60, encoding='utf-8')
Pulls messages from Pub/Sub subscriptions.
| Parameters | |
|---|---|
sub_name |
strThe subscription name. |
limit |
intThe maximum number of messages to return for this request. |
timeout |
intThe HTTP request timeout, in seconds. The default value is |
encoding |
strA Pub/Sub message encoding. The default value is |
Returns
A list of TIPCommon.adapters.pubsub.ReceivedMessage objects.
Return type
ReceivedMessage
static subscription_name
static subscription_name(project_id, sub_name)
Retrieves the full subscription name in the following format:
projects/project_id/topics/topic_name.
| Parameters | |
|---|---|
project_id |
strThe project name that contains the resource. |
sub_name |
strA Pub/Sub subscription name. |
Returns
The full subscription name in the following format:
projects/project_id/topics/topic_name.
Return type
str
static topic_name
static topic_name(project_id, topic)
Retrieves a full topic name in the following format:
projects/project_id/topics/topic_name.
| Parameters | |
|---|---|
project_id |
strThe project name containing the resource. |
topic |
strA Pub/Sub topic name. |
Returns
A full topic name:
projects/project_id/topics/topic_name.
Return type
str
Need more help? Get answers from Community members and Google SecOps professionals.