Beispiel #1
0
def do_restore_create(cs, args):
    """Creates a restore."""
    if not uuidutils.is_uuid_like(args.provider_id):
        raise exceptions.CommandError("Invalid provider id provided.")

    if not uuidutils.is_uuid_like(args.checkpoint_id):
        raise exceptions.CommandError("Invalid checkpoint id provided.")

    restore_parameters = _extract_parameters(args)
    restore_auth = None
    if args.restore_target is not None:
        if args.restore_username is None:
            raise exceptions.CommandError(
                "Must specify username for restore_target.")
        if args.restore_password is None:
            raise exceptions.CommandError(
                "Must specify password for restore_target.")
        restore_auth = {
            'type': 'password',
            'username': args.restore_username,
            'password': args.restore_password,
        }
    restore = cs.restores.create(args.provider_id, args.checkpoint_id,
                                 args.restore_target, restore_parameters,
                                 restore_auth)
    dict_format_list = {"parameters"}
    utils.print_dict(restore.to_dict(), dict_format_list=dict_format_list)
Beispiel #2
0
def do_checkpoint_list(cs, args):
    """Lists all checkpoints."""
    if args.plan_id is not None:
        if not uuidutils.is_uuid_like(args.plan_id):
            raise exceptions.CommandError('The plan_id must be a uuid')

    if args.start_date:
        try:
            datetime.strptime(args.start_date, "%Y-%m-%d")
        except (ValueError, SyntaxError):
            raise exceptions.CommandError(
                "The format of start_date should be %Y-%m-%d")

    if args.end_date:
        try:
            datetime.strptime(args.end_date, "%Y-%m-%d")
        except (ValueError, SyntaxError):
            raise exceptions.CommandError(
                "The format of end_date should be %Y-%m-%d")

    search_opts = {
        'plan_id': args.plan_id,
        'start_date': args.start_date,
        'end_date': args.end_date,
        'project_id': args.project_id,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    checkpoints = cs.checkpoints.list(provider_id=args.provider_id,
                                      search_opts=search_opts,
                                      marker=args.marker,
                                      limit=args.limit,
                                      sort_key=args.sort_key,
                                      sort_dir=args.sort_dir,
                                      sort=args.sort)

    key_list = [
        'Id', 'Project id', 'Status', 'Protection plan', 'Metadata',
        'Created at'
    ]

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    formatters = {
        "Protection plan":
        lambda obj: json.dumps(obj.protection_plan, indent=2, sort_keys=True)
    }
    utils.print_list(checkpoints,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index,
                     formatters=formatters)
Beispiel #3
0
def _check_resources(cs, resources):
    # check the resource whether it is available
    for resource in resources:
        try:
            instance = cs.protectables.get_instance(resource["type"],
                                                    resource["id"])
        except exceptions.NotFound:
            raise exceptions.CommandError(
                "The resource: %s can not be found." % resource["id"])
        else:
            if instance is None:
                raise exceptions.CommandError("The resource: %s is invalid." %
                                              resource["id"])
Beispiel #4
0
    def _discover_auth_versions(self, session, auth_url):
        # discover the API versions the server is supporting base on the
        # given URL
        v2_auth_url = None
        v3_auth_url = None
        try:
            ks_discover = discover.Discover(session=session, auth_url=auth_url)
            v2_auth_url = ks_discover.url_for('2.0')
            v3_auth_url = ks_discover.url_for('3.0')
        except ks_exc.ClientException as e:
            # Identity service may not support discover API version.
            # Lets trying to figure out the API version from the original URL.
            url_parts = urlparse.urlparse(auth_url)
            (scheme, netloc, path, params, query, fragment) = url_parts
            path = path.lower()
            if path.startswith('/v3'):
                v3_auth_url = auth_url
            elif path.startswith('/v2'):
                v2_auth_url = auth_url
            else:
                # not enough information to determine the auth version
                msg = ('Unable to determine the Keystone version '
                       'to authenticate with using the given '
                       'auth_url. Identity service may not support API '
                       'version discovery. Please provide a versioned '
                       'auth_url instead. error=%s') % (e)
                raise exc.CommandError(msg)

        return (v2_auth_url, v3_auth_url)
Beispiel #5
0
def do_provider_list(cs, args):
    """Lists all providers."""

    search_opts = {
        'name': args.name,
        'description': args.description,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    providers = cs.providers.list(search_opts=search_opts,
                                  marker=args.marker,
                                  limit=args.limit,
                                  sort_key=args.sort_key,
                                  sort_dir=args.sort_dir,
                                  sort=args.sort)

    key_list = ['Id', 'Name', 'Description']

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(providers,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index)
Beispiel #6
0
def do_plan_list(cs, args):
    """Lists all plans."""

    all_tenants = 1 if args.tenant else \
        int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
        'project_id': args.tenant,
        'name': args.name,
        'description': args.description,
        'status': args.status,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    plans = cs.plans.list(search_opts=search_opts,
                          marker=args.marker,
                          limit=args.limit,
                          sort_key=args.sort_key,
                          sort_dir=args.sort_dir,
                          sort=args.sort)

    key_list = ['Id', 'Name', 'Description', 'Provider id', 'Status']

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(plans,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index)
Beispiel #7
0
def _extract_operation_definition(args):
    operation_definition = {}
    for data in args.operation_definition.split(','):
        if '=' in data:
            (resource_key, resource_value) = data.split('=', 1)
        else:
            raise exceptions.CommandError(
                "Unable to parse parameter operation_definition.")

        operation_definition[resource_key] = resource_value
    return operation_definition
Beispiel #8
0
    def do_help(self, args):
        """Display help about this program or one of its subcommands.

        """
        if getattr(args, 'command', None):
            if args.command in self.subcommands:
                self.subcommands[args.command].print_help()
            else:
                msg = "'%s' is not a valid subcommand"
                raise exc.CommandError(msg % args.command)
        else:
            self.parser.print_help()
Beispiel #9
0
    def _get_keystone_auth(self, session, auth_url, **kwargs):
        auth_token = kwargs.pop('auth_token', None)
        if auth_token:
            return token.Token(
                auth_url,
                auth_token,
                project_id=kwargs.pop('project_id'),
                project_name=kwargs.pop('project_name'),
                project_domain_id=kwargs.pop('project_domain_id'),
                project_domain_name=kwargs.pop('project_domain_name'))

        # NOTE(starodubcevna): this is a workaround for the bug:
        # https://bugs.launchpad.net/python-openstackclient/+bug/1447704
        # Change that fix this error in keystoneclient was abandoned,
        # so we should use workaround until we move to keystoneauth.
        # The idea of the code came from glanceclient.

        (v2_auth_url, v3_auth_url) = self._discover_auth_versions(
            session=session,
            auth_url=auth_url)

        if v3_auth_url:
            # NOTE(starodubcevna): set user_domain_id and project_domain_id
            # to default as it done in other projects.
            return password.Password(auth_url,
                                     username=kwargs.pop('username'),
                                     user_id=kwargs.pop('user_id'),
                                     password=kwargs.pop('password'),
                                     user_domain_id=kwargs.pop(
                                         'user_domain_id') or 'default',
                                     user_domain_name=kwargs.pop(
                                         'user_domain_name'),
                                     project_id=kwargs.pop('project_id'),
                                     project_name=kwargs.pop('project_name'),
                                     project_domain_id=kwargs.pop(
                                         'project_domain_id') or 'default')
        elif v2_auth_url:
            return password.Password(auth_url,
                                     username=kwargs.pop('username'),
                                     user_id=kwargs.pop('user_id'),
                                     password=kwargs.pop('password'),
                                     project_id=kwargs.pop('project_id'),
                                     project_name=kwargs.pop('project_name'))
        else:
            # if we get here it means domain information is provided
            # (caller meant to use Keystone V3) but the auth url is
            # actually Keystone V2. Obviously we can't authenticate a V3
            # user using V2.
            exc.CommandError("Credential and auth_url mismatch. The given "
                             "auth_url is using Keystone V2 endpoint, which "
                             "may not able to handle Keystone V3 credentials. "
                             "Please provide a correct Keystone V3 auth_url.")
Beispiel #10
0
def do_plan_delete(cs, args):
    """Deletes plan."""
    failure_count = 0
    for plan_id in args.plan:
        try:
            plan = utils.find_resource(cs.plans, plan_id)
            cs.plans.delete(plan.id)
        except exceptions.NotFound:
            failure_count += 1
            print("Failed to delete '{0}'; plan not found".format(plan_id))
    if failure_count == len(args.plan):
        raise exceptions.CommandError("Unable to find and delete any of the "
                                      "specified plan.")
Beispiel #11
0
def _extract_properties(args):
    properties = {}
    if args.properties is None:
        return properties
    for data in args.properties.split(','):
        if '=' in data:
            (resource_key, resource_value) = data.split('=', 1)
        else:
            raise exceptions.CommandError(
                "Unable to parse parameter properties.")

        properties[resource_key] = resource_value
    return properties
Beispiel #12
0
def _extract_parameters(args):
    if all((args.parameters, args.parameters_json)):
        raise exceptions.CommandError(
            "Must provide parameters or parameters-json, not both")
    if not any((args.parameters, args.parameters_json)):
        return {}

    if args.parameters_json:
        return jsonutils.loads(args.parameters_json)
    parameters = {}
    for resource_params in args.parameters:
        resource_type = None
        resource_id = None
        parameter = {}
        for param_kv in resource_params.split(','):
            try:
                key, value = param_kv.split('=')
            except Exception:
                raise exceptions.CommandError(
                    'parameters must be in the form: key1=val1,key2=val2,...')
            if key == "resource_type":
                resource_type = value
            elif key == "resource_id":
                if not uuidutils.is_uuid_like(value):
                    raise exceptions.CommandError('resource_id must be a uuid')
                resource_id = value
            else:
                parameter[key] = value
        if resource_type is None:
            raise exceptions.CommandError(
                'Must specify resource_type for parameters')
        if resource_id is None:
            resource_key = resource_type
        else:
            resource_key = "%s#%s" % (resource_type, resource_id)
        parameters[resource_key] = parameter

    return parameters
Beispiel #13
0
def do_checkpoint_delete(cs, args):
    """Deletes checkpoints."""
    failure_count = 0
    for checkpoint_id in args.checkpoint:
        try:
            checkpoint = cs.checkpoints.get(args.provider_id, checkpoint_id)
            cs.checkpoints.delete(args.provider_id, checkpoint.id)
        except exceptions.NotFound:
            failure_count += 1
            print("Failed to delete '{0}'; checkpoint not found".format(
                checkpoint_id))
    if failure_count == len(args.checkpoint):
        raise exceptions.CommandError("Unable to find and delete any of the "
                                      "specified checkpoint.")
Beispiel #14
0
def do_trigger_delete(cs, args):
    """Deletes trigger."""
    failure_count = 0
    for trigger_id in args.trigger:
        try:
            trigger = utils.find_resource(cs.triggers, trigger_id)
            cs.triggers.delete(trigger.id)
        except exceptions.NotFound:
            failure_count += 1
            print(
                "Failed to delete '{0}'; trigger not found".format(trigger_id))
    if failure_count == len(args.trigger):
        raise exceptions.CommandError("Unable to find and delete any of the "
                                      "specified trigger.")
Beispiel #15
0
def _extract_resources(args):
    resources = []
    for data in args.resources.split(','):
        if '=' in data and len(data.split('=')) in [3, 4]:
            resource = dict(
                zip(['id', 'type', 'name', 'extra_info'], data.split('=')))
            if resource.get('extra_info'):
                resource['extra_info'] = jsonutils.loads(
                    resource.get('extra_info'))
        else:
            raise exceptions.CommandError(
                "Unable to parse parameter resources. "
                "The keys of resource are id , type, name and "
                "extra_info. The extra_info field is optional.")
        resources.append(resource)
    return resources
Beispiel #16
0
def do_scheduledoperation_delete(cs, args):
    """Deletes a scheduled operation."""
    failure_count = 0
    for scheduledoperation_id in args.scheduledoperation:
        try:
            scheduledoperation = utils.find_resource(cs.scheduled_operations,
                                                     scheduledoperation_id)
            cs.scheduled_operations.delete(scheduledoperation.id)
        except exceptions.NotFound:
            failure_count += 1
            print(
                "Failed to delete '{0}'; scheduledoperation not found".format(
                    scheduledoperation_id))
    if failure_count == len(args.scheduledoperation):
        raise exceptions.CommandError("Unable to find and delete any of the "
                                      "specified scheduled operation.")
Beispiel #17
0
def do_plan_update(cs, args):
    """Updatas a plan."""
    data = {}
    if args.name is not None:
        data['name'] = args.name
    if args.resources is not None:
        plan_resources = _extract_resources(args)
        data['resources'] = plan_resources
    if args.status is not None:
        data['status'] = args.status
    try:
        plan = utils.find_resource(cs.plans, args.plan_id)
        plan = cs.plans.update(plan.id, data)
    except exceptions.NotFound:
        raise exceptions.CommandError("Plan %s not found" % args.plan_id)
    else:
        utils.print_dict(plan.to_dict())
Beispiel #18
0
def do_trigger_list(cs, args):
    """Lists all triggers."""

    all_tenants = 1 if args.tenant else \
        int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
        'project_id': args.tenant,
        'name': args.name,
        'type': args.type,
        'properties': args.properties,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    triggers = cs.triggers.list(search_opts=search_opts,
                                marker=args.marker,
                                limit=args.limit,
                                sort_key=args.sort_key,
                                sort_dir=args.sort_dir,
                                sort=args.sort)

    key_list = ['Id', 'Name', 'Type', 'Properties']

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0

    formatters = {
        "Properties":
        lambda obj: json.dumps(obj.properties, indent=2, sort_keys=True)
    }
    utils.print_list(triggers,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index,
                     formatters=formatters)
Beispiel #19
0
def do_restore_list(cs, args):
    """Lists all restores."""

    all_tenants = 1 if args.tenant else \
        int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
        'project_id': args.tenant,
        'status': args.status,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    restores = cs.restores.list(search_opts=search_opts,
                                marker=args.marker,
                                limit=args.limit,
                                sort_key=args.sort_key,
                                sort_dir=args.sort_dir,
                                sort=args.sort)

    key_list = [
        'Id', 'Project id', 'Provider id', 'Checkpoint id', 'Restore target',
        'Parameters', 'Status'
    ]

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    formatters = {
        "Parameters":
        lambda obj: json.dumps(obj.parameters, indent=2, sort_keys=True)
    }
    utils.print_list(restores,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index,
                     formatters=formatters)
Beispiel #20
0
def do_protectable_list_instances(cs, args):
    """Lists all protectable instances."""

    search_opts = {
        'type':
        args.type,
        'parameters':
        (_extract_instances_parameters(args) if args.parameters else None),
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    instances = cs.protectables.list_instances(args.protectable_type,
                                               search_opts=search_opts,
                                               marker=args.marker,
                                               limit=args.limit,
                                               sort_key=args.sort_key,
                                               sort_dir=args.sort_dir,
                                               sort=args.sort)

    key_list = ['Id', 'Type', 'Name', 'Dependent resources', 'Extra info']

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0

    formatters = {
        "Dependent resources":
        lambda obj: json.dumps(
            obj.dependent_resources, indent=2, sort_keys=True)
    }
    utils.print_list(instances,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index,
                     formatters=formatters)
Beispiel #21
0
def do_scheduledoperation_list(cs, args):
    """Lists all scheduledoperations."""

    all_tenants = 1 if args.tenant else \
        int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
        'project_id': args.tenant,
        'name': args.name,
        'operation_type': args.operation_type,
        'trigger_id': args.trigger_id,
        'operation_definition': args.operation_definition,
    }

    if args.sort and (args.sort_key or args.sort_dir):
        raise exceptions.CommandError(
            'The --sort_key and --sort_dir arguments are deprecated and are '
            'not supported with --sort.')

    scheduledoperations = cs.scheduled_operations.list(search_opts=search_opts,
                                                       marker=args.marker,
                                                       limit=args.limit,
                                                       sort_key=args.sort_key,
                                                       sort_dir=args.sort_dir,
                                                       sort=args.sort)

    key_list = [
        'Id', 'Name', 'OperationType', 'TriggerId', 'OperationDefinition'
    ]

    if args.sort_key or args.sort_dir or args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(scheduledoperations,
                     key_list,
                     exclude_unavailable=True,
                     sortby_index=sortby_index)
Beispiel #22
0
def find_resource(manager, name_or_id, *args, **kwargs):
    """Helper for the _find_* methods."""
    # first try to get entity as integer id
    try:
        if isinstance(name_or_id, int) or name_or_id.isdigit():
            return manager.get(int(name_or_id), *args, **kwargs)
    except exceptions.NotFound:
        pass

    # now try to get entity as uuid
    try:
        uuid.UUID(str(name_or_id))
        return manager.get(name_or_id, *args, **kwargs)
    except (ValueError, exceptions.NotFound):
        pass

    # finally try to find entity by name
    try:
        return manager.find(name=name_or_id)
    except exceptions.NotFound:
        msg = "No %s with a name or ID of '%s' exists." % \
              (manager.resource_class.__name__.lower(), name_or_id)
        raise exceptions.CommandError(msg)
Beispiel #23
0
    def main(self, argv):
        # Parse args once to find version
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self._setup_logging(options.debug)

        # build available subcommands based on version
        api_version = options.karbor_api_version
        subcommand_parser = self.get_subcommand_parser(api_version)
        self.parser = subcommand_parser

        keystone_session = None
        keystone_auth = None

        # Handle top-level --help/-h before attempting to parse
        # a command off the command line.
        if (not args and options.help) or not argv:
            self.do_help(options)
            return 0

        # Parse args again and call whatever callback was selected.
        args = subcommand_parser.parse_args(argv)

        # Short-circuit and deal with help command right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        if not args.os_username and not args.os_auth_token:
            raise exc.CommandError("You must provide a username via"
                                   " either --os-username or env[OS_USERNAME]"
                                   " or a token via --os-auth-token or"
                                   " env[OS_AUTH_TOKEN]")

        if args.os_no_client_auth:
            if not args.karbor_url:
                raise exc.CommandError(
                    "If you specify --os-no-client-auth"
                    " you must also specify a Karbor API URL"
                    " via either --karbor-url or env[KARBOR_URL]")

        else:
            # Tenant name or ID is needed to make keystoneclient retrieve a
            # service catalog, it's not required if os_no_client_auth is
            # specified, neither is the auth URL.
            if not any([args.os_tenant_name, args.os_tenant_id,
                        args.os_project_id, args.os_project_name]):
                raise exc.CommandError("You must provide a project name or"
                                       " project id via --os-project-name,"
                                       " --os-project-id, env[OS_PROJECT_ID]"
                                       " or env[OS_PROJECT_NAME]. You may"
                                       " use os-project and os-tenant"
                                       " interchangeably.")
            if not args.os_auth_url:
                raise exc.CommandError("You must provide an auth url via"
                                       " either --os-auth-url or via"
                                       " env[OS_AUTH_URL]")

        endpoint = args.karbor_url

        if args.os_no_client_auth:
            # Authenticate through karbor, don't use session
            kwargs = {
                'username': args.os_username,
                'password': args.os_password,
                'auth_token': args.os_auth_token,
                'auth_url': args.os_auth_url,
                'token': args.os_auth_token,
                'insecure': args.insecure,
                'timeout': args.api_timeout
            }

            if args.os_region_name:
                kwargs['region_name'] = args.os_region_name
        else:
            # Create a keystone session and keystone auth
            keystone_session = ksession.Session.load_from_cli_options(args)
            project_id = args.os_project_id or args.os_tenant_id
            project_name = args.os_project_name or args.os_tenant_name

            keystone_auth = self._get_keystone_auth(
                keystone_session,
                args.os_auth_url,
                username=args.os_username,
                user_id=args.os_user_id,
                user_domain_id=args.os_user_domain_id,
                user_domain_name=args.os_user_domain_name,
                password=args.os_password,
                auth_token=args.os_auth_token,
                project_id=project_id,
                project_name=project_name,
                project_domain_id=args.os_project_domain_id,
                project_domain_name=args.os_project_domain_name)

            endpoint_type = args.os_endpoint_type or 'publicURL'
            service_type = args.os_service_type or 'data-protect'

            endpoint = keystone_auth.get_endpoint(
                keystone_session,
                service_type=service_type,
                region_name=args.os_region_name)

            kwargs = {
                'session': keystone_session,
                'auth': keystone_auth,
                'service_type': service_type,
                'endpoint_type': endpoint_type,
                'region_name': args.os_region_name,
            }

        if args.api_timeout:
            kwargs['timeout'] = args.api_timeout

        client = karbor_client.Client(api_version, endpoint, **kwargs)

        args.func(client, args)