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.snapshots'. """ msgs = self.context['pubsub_msgs'] pubsub = self.context['pubsub'] for snapshot_name in args.snapshot: snapshot_path = util.SnapshotFormat(snapshot_name) delete_req = msgs.PubsubProjectsSnapshotsDeleteRequest( snapshot=snapshot_path) try: pubsub.projects_snapshots.Delete(delete_req) failed = None except api_ex.HttpError as error: exc = exceptions.HttpException(error) failed = exc.payload.status_message result = util.SnapshotDisplayDict( msgs.Snapshot(name=snapshot_path), failed) log.DeletedResource(snapshot_path, kind='snapshot', failed=failed) yield 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.snapshots'. Raises: util.RequestFailedError: if any of the requests to the API failed. """ client = snapshots.SnapshotsClient() subscription_ref = util.ParseSubscription(args.subscription, args.subscription_project) labels = labels_util.ParseCreateArgs( args, client.messages.CreateSnapshotRequest.LabelsValue) failed = [] for snapshot_name in args.snapshot: snapshot_ref = util.ParseSnapshot(snapshot_name) try: result = client.Create(snapshot_ref, subscription_ref, labels=labels) except api_ex.HttpError as error: exc = exceptions.HttpException(error) log.CreatedResource(snapshot_ref.RelativeName(), kind='snapshot', failed=exc.payload.status_message) failed.append(snapshot_name) continue result = util.SnapshotDisplayDict(result) log.CreatedResource(snapshot_ref.RelativeName(), kind='snapshot') 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. Yields: A serialized object (dict) describing the results of the operation. This description fits the Resource described in the ResourceRegistry under 'pubsub.projects.snapshots'. """ msgs = self.context['pubsub_msgs'] pubsub = self.context['pubsub'] subscription_project = '' if args.subscription_project: subscription_project = projects_util.ParseProject( args.subscription_project).Name() subscription_name = args.subscription for snapshot_name in args.snapshot: snapshot_path = util.SnapshotFormat(snapshot_name) create_req = msgs.PubsubProjectsSnapshotsCreateRequest( createSnapshotRequest=msgs.CreateSnapshotRequest( subscription=util.SubscriptionFormat( subscription_name, subscription_project)), name=snapshot_path) # TODO(b/32275310): Conform to gcloud error handling guidelines. try: result = pubsub.projects_snapshots.Create(create_req) failed = None except api_ex.HttpError as error: result = msgs.Snapshot(name=snapshot_path) exc = exceptions.HttpException(error) failed = exc.payload.status_message result = util.SnapshotDisplayDict(result, failed) log.CreatedResource(snapshot_path, kind='snapshot', failed=failed) yield result