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 ast
import time
from osc_lib import exceptions

from workloadmgrclient import utils
from workloadmgrclient.v1 import WorkloadmgrCommand
from workloadmgrclient.v1.validators import validate_int_value, validate_date_value


class FilePathSearch(WorkloadmgrCommand):
    """File path search"""

    resource = "file_search"

    @staticmethod
    def _add_arguments(parser):
        parser.add_argument("vm_id", metavar="<vm_id>", help="ID of the VM")
        parser.add_argument("file_path", metavar="<file_path>", help="File path")
        parser.add_argument(
            "--snapshotids",
            metavar="<snapshotid>",
            action="append",
            dest="snapshotids",
            default=[],
            help="Search only in specified snapshot ids "
            "snapshot-id: include the instance with this UUID ",
        )
        parser.add_argument(
            "--end_filter",
            metavar="end_filter",
            default=0,
            help="Displays last snapshots, example , last 10 snapshots, "
            "default 0 means displays all snapshots",
        )
        parser.add_argument(
            "--start_filter",
            metavar="start_filter",
            default=0,
            help="Displays snapshots starting from , example , snapshot starting from 5, "
            "default 0 means starts from last snapshot",
        )
        parser.add_argument(
            "--date_from",
            metavar="<date_from>",
            default=None,
            help="From date in format 'YYYY-MM-DDTHH:MM:SS' eg 2016-10-10T00:00:00,"
            "If don't specify time then it takes 00:00 by default",
        )
        parser.add_argument(
            "--date_to",
            metavar="<date_to>",
            default=None,
            help="To date in format 'YYYY-MM-DDTHH:MM:SS'(default is current day),"
            "Specify HH:MM:SS to get snapshots within same day"
            " inclusive/exclusive results for date_from and date_to",
        )

    def take_action(self, parsed_args):
        start_filter_value = validate_int_value(parsed_args.start_filter, 0)
        if not start_filter_value:
            err_msg = ("Invalid value provided for --start_filter '{}'"
                       " check help for more information")
            raise exceptions.CommandError(err_msg.format(parsed_args.start_filter))

        end_filter_value = validate_int_value(parsed_args.end_filter, 0)
        if not end_filter_value:
            err_msg = ("Invalid value provided for --end_filter '{}'"
                       " check help for more information")
            raise exceptions.CommandError(err_msg.format(parsed_args.end_filter))

        client = self.get_client()
        valid_value = validate_int_value(parsed_args.start_filter, 0)
        if not valid_value:
            err_msg = (
                "Invalid value provided for policy_field 'start_filter'"
                " check help for more information"
            )
            raise exceptions.CommandError(err_msg)

        valid_value = validate_int_value(parsed_args.end_filter, 0)
        if not valid_value:
            err_msg = (
                "Invalid value provided for policy_field 'end_filter'"
                " check help for more information"
            )
            raise exceptions.CommandError(err_msg)

        if parsed_args.date_to:
            date_formats = ['%Y-%m-%dT%H:%M:%S', '%H:%M:%S']
            valid_value = validate_date_value(parsed_args.date_to, date_formats)
            if not valid_value:
                err_msg = (
                    "Invalid value provided for policy_field 'date_to'"
                    " check help for more information"
                )
                raise exceptions.CommandError(err_msg)

        if parsed_args.date_from:
            date_formats = ['%Y-%m-%dT%H:%M:%S', '%Y-%m-%d']
            valid_value = validate_date_value(parsed_args.date_from, date_formats)
            if not valid_value:
                err_msg = (
                    "Invalid value provided for policy_field 'date_from'"
                    " check help for more information"
                )
                raise exceptions.CommandError(err_msg)

        search = client.search(
            parsed_args.vm_id,
            parsed_args.file_path,
            parsed_args.snapshotids,
            parsed_args.start_filter,
            parsed_args.end_filter,
            parsed_args.date_from,
            parsed_args.date_to,
        )

        print("Please wait for few seconds to get results")
        print("Your search ID: %s " % search.id)
        while 1:
            search = client.get(search.id)
            if search.status == "error":
                err_msg = "Error:" + search.error_msg
                raise exceptions.CommandError(err_msg)
            if search.status == "completed":
                columns = ["ID", "Status", "Filepath", "Vm_id"]
                utils.print_object(search, columns)
                snapshots = ast.literal_eval(search.json_resp)
                for snapshot in snapshots:
                    utils.print_file_search_dict(snapshot)
                break
            time.sleep(10)
        return