Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
import six
from cliff import show, lister
from osc_lib import utils as osc_utils
from osc_lib.cli import format_columns

from workloadmgrclient import utils
from workloadmgrclient.v1 import WorkloadmgrCommand


class RestoreCommand(WorkloadmgrCommand):
    resource = "restores"


class ListRestore(RestoreCommand, lister.Lister):
    """List all the restores for the snapshot."""

    @staticmethod
    def _add_arguments(parser):
        parser.add_argument(
            "--snapshot_id",
            metavar="<snapshot_id>",
            default=None,
            help="Filter results by snapshot_id",
        )

    def take_action(self, parsed_args):
        client = self.get_client()
        search_opts = {
            "snapshot_id": parsed_args.snapshot_id,
        }
        restores = client.list(search_opts=search_opts) or []
        return (
            ["Created At", "Name", "ID", "Snapshot ID", "Size", "Status"],
            (
                osc_utils.get_item_properties(
                    obj, ["created_at", "name", "id", "snapshot_id", "size", "status"]
                )
                for obj in restores
            ),
        )


class ShowRestore(RestoreCommand, show.ShowOne):
    """Show details about restore of the workload's snapshot"""

    @staticmethod
    def _add_arguments(parser):
        parser.add_argument(
            "restore_id", metavar="<restore_id>", help="ID of the restore."
        )
        parser.add_argument(
            "--output",
            metavar="<output>",
            help="Option to get additional restore details, "
            "Specify --output metadata for restore metadata,"
            "--output networks "
            "--output subnets "
            "--output routers "
            "--output flavors ",
        )

    def take_action(self, parsed_args):
        client = self.get_client()
        restore_obj = utils.find_resource(client, parsed_args.restore_id)
        info = {}
        info.update(restore_obj._info)
        info.pop("links", None)
        info.pop("snapshot_details", None)
        metadata = info.pop("metadata", [])
        networks = info.pop("networks", [])
        subnets = info.pop("subnets", [])
        routers = info.pop("routers", [])
        flavors = info.pop("flavors", [])
        meta = [{str(item["key"]): str(item["value"])} for item in metadata]

        if parsed_args.output == "metadata":
            utils.print_data_vertically(meta, ["Metadata", "Value"])
        if parsed_args.output == "networks":
            utils.print_data_vertically(networks, ["Networks", "Value"])
        if parsed_args.output == "subnets":
            utils.print_data_vertically(subnets, ["Subnets", "Value"])
        if parsed_args.output == "routers":
            utils.print_data_vertically(routers, ["Routers", "Value"])
        if parsed_args.output == "flavors":
            utils.print_data_vertically(flavors, ["Flavors", "Value"])

        columns = list(info.keys())
        data = osc_utils.get_dict_properties(info, columns)
        return columns, data


class CancelRestore(RestoreCommand):
    """Cancel the restore."""

    @staticmethod
    def _add_arguments(parser):
        parser.add_argument(
            "restore_id", metavar="<restore_id>", help="ID of restore to cancel."
        )

    def take_action(self, parsed_args):
        client = self.get_client()
        restore_obj = utils.find_resource(client, parsed_args.restore_id)
        restore_obj.cancel()


class DeleteRestore(RestoreCommand):
    """Delete the restore."""

    @staticmethod
    def _add_arguments(parser):
        parser.add_argument(
            "restore_id", metavar="<restores_id>", help="ID of the restore to delete."
        )

    def take_action(self, parsed_args):
        client = self.get_client()
        restore_obj = utils.find_resource(client, parsed_args.restore_id)
        restore_obj.delete()