def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: An HttpException if there was a problem calling the API subscriptions.Patch command. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() try: result = client.Patch( subscription_ref, ack_deadline=args.ack_deadline, push_config=util.ParsePushConfig(args.push_endpoint), retain_acked_messages=args.retain_acked_messages, message_retention_duration=args.message_retention_duration) except subscriptions.NoFieldsSpecifiedError: raise else: log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription') return result
def _Run(args, legacy_output=False): """Deletes one or more subscriptions.""" client = subscriptions.SubscriptionsClient() failed = [] for subscription_ref in args.CONCEPTS.subscription.Parse(): try: result = client.Delete(subscription_ref) except api_ex.HttpError as error: exc = exceptions.HttpException(error) log.DeletedResource(subscription_ref.RelativeName(), kind='subscription', failed=exc.payload.status_message) failed.append(subscription_ref.subscriptionsId) continue subscription = client.messages.Subscription( name=subscription_ref.RelativeName()) if legacy_output: result = util.SubscriptionDisplayDict(subscription) log.DeletedResource(subscription_ref.RelativeName(), kind='subscription') yield result if failed: raise util.RequestsFailedError(failed, 'delete')
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.subscriptions.seek'. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() result = {'subscriptionId': subscription_ref.RelativeName()} snapshot_ref = None time = None if args.snapshot: snapshot_ref = util.ParseSnapshot(args.snapshot, args.snapshot_project) result['snapshotId'] = snapshot_ref.RelativeName() else: time = util.FormatSeekTime(args.time) result['time'] = time client.Seek(subscription_ref, time=time, snapshot_ref=snapshot_ref) return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Display dictionary with information about the new ACK deadline seconds for the given subscription and ackId. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() ack_ids = flags.ParseAckIdsArgs(args) result = client.ModifyAckDeadline(subscription_ref, ack_ids, args.ack_deadline) log.status.Print( 'Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] for subscription [{2}]'.format( args.ack_deadline, ','.join(ack_ids), subscription_ref.RelativeName())) legacy_output = properties.VALUES.pubsub.legacy_output.GetBool() if legacy_output: return { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': ack_ids, 'ackDeadlineSeconds': args.ack_deadline } else: return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Display dictionary with information about the new ACK deadline seconds for the given subscription and ackId. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() ack_ids = flags.ParseAckIdsArgs(args) result = None ack_ids_and_failure_reasons = {} try: result = client.ModifyAckDeadline(subscription_ref, ack_ids, args.ack_deadline) except api_ex.HttpError as error: exc = exceptions.HttpException(error) ack_ids_and_failure_reasons = util.ParseExactlyOnceErrorInfo( exc.payload.details) # If the failure doesn't have more information (specifically for exactly # once related failures), re-raise the exception. if not ack_ids_and_failure_reasons: raise failed_ack_ids = [ack['AckId'] for ack in ack_ids_and_failure_reasons] successfully_processed_ack_ids = [ ack_id for ack_id in ack_ids if ack_id not in failed_ack_ids ] log.status.Print( 'Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] for subscription [{2}]'.format( args.ack_deadline, ','.join(successfully_processed_ack_ids), subscription_ref.RelativeName())) if failed_ack_ids: log.status.Print( 'Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] failed for subscription [{2}]'.format( args.ack_deadline, ','.join(failed_ack_ids), subscription_ref.RelativeName())) if ack_ids_and_failure_reasons: return ack_ids_and_failure_reasons legacy_output = properties.VALUES.pubsub.legacy_output.GetBool() if legacy_output: result = { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': ack_ids, 'ackDeadlineSeconds': args.ack_deadline } return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: util.RequestFailedError: if any of the requests to the API failed. """ client = subscriptions.SubscriptionsClient() topic_ref = util.ParseTopic(args.topic, args.topic_project) push_config = util.ParsePushConfig(args.push_endpoint) retain_acked_messages = getattr(args, 'retain_acked_messages', None) retention_duration = getattr(args, 'message_retention_duration', None) if retention_duration: retention_duration = util.FormatDuration(retention_duration) labels = labels_util.UpdateLabels( None, client.messages.Subscription.LabelsValue, update_labels=labels_util.GetUpdateLabelsDictFromArgs(args)) failed = [] for subscription_name in args.subscription: subscription_ref = util.ParseSubscription(subscription_name) try: result = client.Create(subscription_ref, topic_ref, args.ack_deadline, push_config, retain_acked_messages, retention_duration, labels=labels) except api_ex.HttpError as error: exc = exceptions.HttpException(error) log.CreatedResource(subscription_ref.RelativeName(), kind='subscription', failed=exc.payload.status_message) failed.append(subscription_name) continue result = util.SubscriptionDisplayDict(result) log.CreatedResource(subscription_ref.RelativeName(), kind='subscription') yield result if failed: raise util.RequestsFailedError(failed, 'create')
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: An HttpException if there was a problem calling the API subscriptions.Patch command. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() dead_letter_topic = getattr(args, 'dead_letter_topic', None) max_delivery_attempts = getattr(args, 'max_delivery_attempts', None) clear_dead_letter_policy = getattr(args, 'clear_dead_letter_policy', None) labels_update = labels_util.ProcessUpdateArgsLazy( args, client.messages.Subscription.LabelsValue, orig_labels_thunk=lambda: client.Get(subscription_ref).labels) no_expiration = False expiration_period = getattr(args, 'expiration_period', None) if expiration_period: if expiration_period == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE: no_expiration = True expiration_period = None try: result = client.Patch( subscription_ref, ack_deadline=args.ack_deadline, push_config=util.ParsePushConfig(args), retain_acked_messages=args.retain_acked_messages, labels=labels_update.GetOrNone(), message_retention_duration=args.message_retention_duration, no_expiration=no_expiration, expiration_period=expiration_period, dead_letter_topic=dead_letter_topic, max_delivery_attempts=max_delivery_attempts, clear_dead_letter_policy=clear_dead_letter_policy) except subscriptions.NoFieldsSpecifiedError: if not any(args.IsSpecified(arg) for arg in ('clear_labels', 'update_labels', 'remove_labels')): raise log.status.Print('No update to perform.') result = None else: log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription') return result
def Run(self, args): client = subscriptions.SubscriptionsClient() messages = client.messages subscription_ref = args.CONCEPTS.subscription.Parse() policy = iam_util.ParsePolicyFile(args.policy_file, messages.Policy) response = client.SetIamPolicy(subscription_ref, policy=policy) log.status.Print('Updated IAM policy for subscription [{}].'.format( subscription_ref.Name())) return response
def _Run(args, ack_ids, legacy_output=False, capture_failures=False): """Modifies the ack deadline for one or more messages.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() if not capture_failures: result = client.ModifyAckDeadline(subscription_ref, ack_ids, args.ack_deadline) log.status.Print( 'Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] for subscription [{2}]'.format( args.ack_deadline, ','.join(ack_ids), subscription_ref.RelativeName())) if legacy_output: return { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': ack_ids, 'ackDeadlineSeconds': args.ack_deadline }, {} else: return result, {} result = None ack_ids_and_failure_reasons = {} try: result = client.ModifyAckDeadline(subscription_ref, ack_ids, args.ack_deadline) except api_ex.HttpError as error: ack_ids_and_failure_reasons = util.HandleExactlyOnceDeliveryError( error) failed_ack_ids, successfully_processed_ack_ids = util.ParseExactlyOnceAckIdsAndFailureReasons( ack_ids_and_failure_reasons, ack_ids) log.status.Print('Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] for subscription [{2}]'.format( args.ack_deadline, ','.join(successfully_processed_ack_ids), subscription_ref.RelativeName())) if failed_ack_ids: log.status.Print( 'Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] failed for subscription [{2}]'.format( args.ack_deadline, ','.join(failed_ack_ids), subscription_ref.RelativeName())) if legacy_output: result = { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': ack_ids, 'ackDeadlineSeconds': args.ack_deadline } return result, ack_ids_and_failure_reasons
def _Run(args, enable_labels=False, legacy_output=False): """Creates one or more subscriptions.""" client = subscriptions.SubscriptionsClient() topic_ref = args.CONCEPTS.topic.Parse() push_config = util.ParsePushConfig(args) retain_acked_messages = getattr(args, 'retain_acked_messages', None) retention_duration = getattr(args, 'message_retention_duration', None) if retention_duration: retention_duration = util.FormatDuration(retention_duration) no_expiration = False expiration_period = getattr(args, 'expiration_period', None) if expiration_period: if expiration_period == subscriptions.NEVER_EXPIRATION_PERIOD_VALUE: no_expiration = True expiration_period = None labels = None if enable_labels: labels = labels_util.ParseCreateArgs( args, client.messages.Subscription.LabelsValue) failed = [] for subscription_ref in args.CONCEPTS.subscription.Parse(): try: result = client.Create(subscription_ref, topic_ref, args.ack_deadline, push_config, retain_acked_messages, retention_duration, labels=labels, no_expiration=no_expiration, expiration_period=expiration_period) except api_ex.HttpError as error: exc = exceptions.HttpException(error) log.CreatedResource(subscription_ref.RelativeName(), kind='subscription', failed=exc.payload.status_message) failed.append(subscription_ref.subscriptionsId) continue if legacy_output: result = util.SubscriptionDisplayDict(result) log.CreatedResource(subscription_ref.RelativeName(), kind='subscription') yield result if failed: raise util.RequestsFailedError(failed, 'create')
def _Run(args, max_messages): """Pulls messages from a subscription.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() pull_response = client.Pull(subscription_ref, max_messages) if args.auto_ack and pull_response.receivedMessages: ack_ids = [message.ackId for message in pull_response.receivedMessages] client.Ack(ack_ids, subscription_ref) return pull_response.receivedMessages
def _Run(args, ack_ids, legacy_output=False): client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() result = client.Ack(ack_ids, subscription_ref) log.status.Print('Acked the messages with the following ackIds: [{}]' .format(','.join(ack_ids))) if legacy_output: return {'subscriptionId': subscription_ref.RelativeName(), 'ackIds': ack_ids} else: return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Subscription paths that match the regular expression in args.name_filter. """ client = subscriptions.SubscriptionsClient() for sub in client.List(util.ParseProject(), page_size=args.page_size): yield util.ListSubscriptionDisplayDict(sub)
def _Run(args, max_messages, return_immediately=True, exactly_once_failure_handling=False): """Pulls messages from a subscription.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() pull_response = client.Pull(subscription_ref, max_messages, return_immediately) failed_ack_ids = {} ack_ids_and_failure_reasons = [] if args.auto_ack and pull_response.receivedMessages: ack_ids = [message.ackId for message in pull_response.receivedMessages] try: client.Ack(ack_ids, subscription_ref) except api_ex.HttpError as error: if not exactly_once_failure_handling: raise error exc = util_ex.HttpException(error) ack_ids_and_failure_reasons = util.ParseExactlyOnceErrorInfo( exc.payload.details) # If the failure doesn't have more information (specifically for exactly # once related failures), assume all the ack ids have failed with the # same status. if not ack_ids_and_failure_reasons: for ack_id in ack_ids: failed_ack_ids[ack_id] = 'FAILURE_' + six.text_type( error.status_code) if not failed_ack_ids: for ack_ids_and_failure_reason in ack_ids_and_failure_reasons: failed_ack_ids[ack_ids_and_failure_reason[ 'AckId']] = ack_ids_and_failure_reason['FailureReason'] if not exactly_once_failure_handling: return pull_response.receivedMessages return_val = [] for message in pull_response.receivedMessages: ack_status = 'SUCCESS' if message.ackId in failed_ack_ids: ack_status = failed_ack_ids[message.ackId] return_val.append({ 'received_message': message, 'ack_status': ack_status }) return return_val
def _Run(args, legacy_output=False): """Modifies the push config for a subscription.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() push_config = util.ParsePushConfig(args) result = client.ModifyPushConfig(subscription_ref, push_config) log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription') if legacy_output: return { 'subscriptionId': subscription_ref.RelativeName(), 'pushEndpoint': args.push_endpoint } else: return result
def ParsePushConfig(args, client=None): """Parses configs of push subscription from args.""" push_endpoint = args.push_endpoint if push_endpoint is None: return None client = client or subscriptions.SubscriptionsClient() oidc_token = None service_account_email = getattr(args, 'SERVICE_ACCOUNT_EMAIL', None) # Only set oidc_token when service_account_email is set. if service_account_email is not None: audience = getattr(args, 'OPTIONAL_AUDIENCE_OVERRIDE', None) oidc_token = client.messages.OidcToken( serviceAccountEmail=service_account_email, audience=audience) return client.messages.PushConfig(pushEndpoint=push_endpoint, oidcToken=oidc_token)
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: An HttpException if there was a problem calling the API subscriptions.Patch command. """ client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() labels_update = labels_util.ProcessUpdateArgsLazy( args, client.messages.Subscription.LabelsValue, orig_labels_thunk=lambda: client.Get(subscription_ref).labels) try: result = client.Patch( subscription_ref, ack_deadline=args.ack_deadline, push_config=util.ParsePushConfig(args.push_endpoint), retain_acked_messages=args.retain_acked_messages, labels=labels_update.GetOrNone(), message_retention_duration=args.message_retention_duration) except subscriptions.NoFieldsSpecifiedError: if not any( args.IsSpecified(arg) for arg in ('clear_labels', 'update_labels', 'remove_labels')): raise log.status.Print('No update to perform.') result = None else: log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription') return result
def _Run(args, ack_ids, legacy_output=False, capture_failures=False): """Acks one or more messages.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() if not capture_failures: result = client.Ack(ack_ids, subscription_ref) log.status.Print( 'Acked the messages with the following ackIds: [{}]'.format( ','.join(ack_ids))) if legacy_output: return { 'subscriptionId': subscription_ref.RelativeName(), 'ackIds': ack_ids }, {} else: return result, {} result = None ack_ids_and_failure_reasons = {} try: result = client.Ack(ack_ids, subscription_ref) except api_ex.HttpError as error: ack_ids_and_failure_reasons = util.HandleExactlyOnceDeliveryError( error) failed_ack_ids, successfully_processed_ack_ids = util.ParseExactlyOnceAckIdsAndFailureReasons( ack_ids_and_failure_reasons, ack_ids) log.status.Print( 'Acked the messages with the following ackIds: [{}]'.format( ','.join(successfully_processed_ack_ids))) if failed_ack_ids: log.status.Print( 'Failed to ack the messages with the following ackIds: [{}]'. format(','.join(failed_ack_ids))) if legacy_output: result = { 'subscriptionId': subscription_ref.RelativeName(), 'ackIds': ack_ids } return result, ack_ids_and_failure_reasons
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: An HttpException if there was a problem calling the API subscriptions.Patch command. """ client = subscriptions.SubscriptionsClient() subscription_ref = util.ParseSubscription(args.subscription) update_labels = labels_util.GetUpdateLabelsDictFromArgs(args) remove_labels = labels_util.GetRemoveLabelsListFromArgs(args) if update_labels or remove_labels: original_subscription = client.Get(subscription_ref) labels = labels_util.UpdateLabels( original_subscription.labels, client.messages.Subscription.LabelsValue, update_labels=update_labels, remove_labels=remove_labels) else: labels = None result = client.Patch( subscription_ref, ack_deadline=args.ack_deadline, push_config=util.ParsePushConfig(args.push_endpoint), retain_acked_messages=args.retain_acked_messages, labels=labels, message_retention_duration=args.message_retention_duration) result = util.SubscriptionDisplayDict(result) log.UpdatedResource(subscription_ref.RelativeName(), kind='subscription') return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Ack display dictionary with information about the acknowledged messages and related subscription. """ client = subscriptions.SubscriptionsClient() subscription_ref = util.ParseSubscription(args.subscription) client.Ack(args.ack_id, subscription_ref) # Using this dict, instead of returning the AcknowledgeRequest directly, # to preserve the naming conventions for subscriptionId. return {'subscriptionId': subscription_ref.RelativeName(), 'ackIds': args.ack_id}
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: None """ client = subscriptions.SubscriptionsClient() subscription_ref = util.ParseSubscription(args.subscription) push_config = util.ParsePushConfig(args.push_endpoint) client.ModifyPushConfig(subscription_ref, push_config) return { 'subscriptionId': subscription_ref.RelativeName(), 'pushEndpoint': args.push_endpoint }
def _Run(args, ack_ids, legacy_output=False): """Modifies the ack deadline for one or more messages.""" client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() result = client.ModifyAckDeadline(subscription_ref, ack_ids, args.ack_deadline) log.status.Print('Set ackDeadlineSeconds to [{0}] for messages with ackId ' '[{1}]] for subscription [{2}]'.format( args.ack_deadline, ','.join(ack_ids), subscription_ref.RelativeName())) if legacy_output: return { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': ack_ids, 'ackDeadlineSeconds': args.ack_deadline } else: return result
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.subscriptions'. Raises: util.RequestFailedError: if any of the requests to the API failed. """ client = subscriptions.SubscriptionsClient() failed = [] for subscription_name in args.subscription: subscription_ref = util.ParseSubscription(subscription_name) try: client.Delete(subscription_ref) except api_ex.HttpError as error: exc = exceptions.HttpException(error) log.DeletedResource(subscription_ref.RelativeName(), kind='subscription', failed=exc.payload.status_message) failed.append(subscription_name) continue subscription = client.messages.Subscription( name=subscription_ref.RelativeName()) result = util.SubscriptionDisplayDict(subscription) log.DeletedResource(subscription_ref.RelativeName(), kind='subscription') yield result if failed: raise util.RequestsFailedError(failed, 'delete')
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: A PullResponse message with the response of the Pull operation. """ client = subscriptions.SubscriptionsClient() subscription_ref = util.ParseSubscription(args.subscription) pull_response = client.Pull(subscription_ref, args.max_messages) if args.auto_ack and pull_response.receivedMessages: ack_ids = [ message.ackId for message in pull_response.receivedMessages ] client.Ack(ack_ids, subscription_ref) return pull_response.receivedMessages
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Returns: Display dictionary with information about the new ACK deadline seconds for the given subscription and ackId. """ client = subscriptions.SubscriptionsClient() subscription_ref = util.ParseSubscription(args.subscription) client.ModifyAckDeadline(subscription_ref, args.ack_id, args.ack_deadline) return { 'subscriptionId': subscription_ref.RelativeName(), 'ackId': args.ack_id, 'ackDeadlineSeconds': args.ack_deadline }
def _Run(args, legacy_output=False): client = subscriptions.SubscriptionsClient() for sub in client.List(util.ParseProject(), page_size=args.page_size): if legacy_output: sub = util.ListSubscriptionDisplayDict(sub) yield sub
def ParsePushConfig(push_endpoint, client=None): client = client or subscriptions.SubscriptionsClient() push_config = None if push_endpoint is not None: push_config = client.messages.PushConfig(pushEndpoint=push_endpoint) return push_config
def Run(self, args): client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() return client.Get(subscription_ref)
def SetUp(self): self.subscriptions_client = subscriptions.SubscriptionsClient( self.client, self.msgs) self.subscriptions_service = self.client.projects_subscriptions
def Run(self, args): client = subscriptions.SubscriptionsClient() subscription_ref = args.CONCEPTS.subscription.Parse() return client.AddIamPolicyBinding(subscription_ref, args.member, args.role)