Example #1
0
def to_bytes(text, default=0):
    """Converts a string into an integer of bytes.

    Looks at the last characters of the text to determine
    what conversion is needed to turn the input text into a byte number.
    Supports "B, K(B), M(B), G(B), and T(B)". (case insensitive)

    :param text: String input for bytes size conversion.
    :param default: Default return value when text is blank.

    """
    match = BYTE_REGEX.search(text)
    if match:
        magnitude = int(match.group(1))
        mult_key_org = match.group(2)
        if not mult_key_org:
            return magnitude
    elif text:
        msg = _('Invalid string format: %s') % text
        raise TypeError(msg)
    else:
        return default
    mult_key = mult_key_org.lower().replace('b', '', 1)
    multiplier = BYTE_MULTIPLIERS.get(mult_key)
    if multiplier is None:
        msg = _('Unknown byte multiplier: %s') % mult_key_org
        raise TypeError(msg)
    return magnitude * multiplier
    def get_volume_api_version_from_endpoint(self):
        try:
            version = get_volume_api_from_url(self.management_url)
        except exceptions.UnsupportedVersion as e:
            if self.management_url == self.bypass_url:
                msg = (_("Invalid url was specified in --bypass-url or "
                         "environment variable CINDERCLIENT_BYPASS_URL.\n"
                         "%s") % six.text_type(e.message))
            else:
                msg = (_("Service catalog returned invalid url.\n"
                         "%s") % six.text_type(e.message))

            raise exceptions.UnsupportedVersion(msg)

        return version
Example #3
0
    def get_volume_api_version_from_endpoint(self):
        try:
            version = get_volume_api_from_url(self.management_url)
        except exceptions.UnsupportedVersion as e:
            if self.management_url == self.bypass_url:
                msg = (_("Invalid url was specified in --bypass-url or "
                         "environment variable CINDERCLIENT_BYPASS_URL.\n"
                         "%s") % six.text_type(e.message))
            else:
                msg = (_("Service catalog returned invalid url.\n"
                         "%s") % six.text_type(e.message))

            raise exceptions.UnsupportedVersion(msg)

        return version
Example #4
0
def bool_from_string(subject, strict=False):
    """Interpret a string as a boolean.

    A case-insensitive match is performed such that strings matching 't',
    'true', 'on', 'y', 'yes', or '1' are considered True and, when
    `strict=False`, anything else is considered False.

    Useful for JSON-decoded stuff and config file parsing.

    If `strict=True`, unrecognized values, including None, will raise a
    ValueError which is useful when parsing values passed in from an API call.
    Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'.
    """
    if not isinstance(subject, six.string_types):
        subject = str(subject)

    lowered = subject.strip().lower()

    if lowered in TRUE_STRINGS:
        return True
    elif lowered in FALSE_STRINGS:
        return False
    elif strict:
        acceptable = ', '.join(
            "'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS))
        msg = _("Unrecognized value '%(val)s', acceptable values are:"
                " %(acceptable)s") % {'val': subject,
                                      'acceptable': acceptable}
        raise ValueError(msg)
    else:
        return False
Example #5
0
    def get_volume_api_version_from_endpoint(self):
        try:
            version = get_volume_api_from_url(self.get_endpoint())
        except exceptions.UnsupportedVersion as e:
            msg = (_("Service catalog returned invalid url.\n"
                     "%s") % six.text_type(e.message))
            raise exceptions.UnsupportedVersion(msg)

        return version
Example #6
0
 def get_volume_api_version_from_endpoint(self):
     endpoint = self.get_endpoint()
     if not endpoint:
         msg = _('The Cinder server does not support %s. Check your '
                 'providers supported versions and try again with '
                 'setting --os-volume-api-version or the environment '
                 'variable OS_VOLUME_API_VERSION.') % self.version
         raise exceptions.InvalidAPIVersion(msg)
     return get_volume_api_from_url(endpoint)
    def get_volume_api_version_from_endpoint(self):
        try:
            version = get_volume_api_from_url(self.get_endpoint())
        except exceptions.UnsupportedVersion as e:
            msg = (_("Service catalog returned invalid url.\n"
                     "%s") % six.text_type(e.message))
            raise exceptions.UnsupportedVersion(msg)

        return version
Example #8
0
 def get_volume_api_version_from_endpoint(self):
     endpoint = self.get_endpoint()
     if not endpoint:
         msg = _('The Cinder server does not support %s. Check your '
                 'providers supported versions and try again with '
                 'setting --os-volume-api-version or the environment '
                 'variable OS_VOLUME_API_VERSION.') % self.version
         raise exceptions.InvalidAPIVersion(msg)
     return get_volume_api_from_url(endpoint)
Example #9
0
def string_to_bytes(text, unit_system='IEC', return_int=False):
    """Converts a string into an float representation of bytes.

    The units supported for IEC ::

        Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it)
        KB, KiB, MB, MiB, GB, GiB, TB, TiB

    The units supported for SI ::

        kb(it), Mb(it), Gb(it), Tb(it)
        kB, MB, GB, TB

    Note that the SI unit system does not support capital letter 'K'

    :param text: String input for bytes size conversion.
    :param unit_system: Unit system for byte size conversion.
    :param return_int: If True, returns integer representation of text
                       in bytes. (default: decimal)
    :returns: Numerical representation of text in bytes.
    :raises ValueError: If text has an invalid value.

    """
    try:
        base, reg_ex = UNIT_SYSTEM_INFO[unit_system]
    except KeyError:
        msg = _('Invalid unit system: "%s"') % unit_system
        raise ValueError(msg)
    match = reg_ex.match(text)
    if match:
        magnitude = float(match.group(1))
        unit_prefix = match.group(2)
        if match.group(3) in ['b', 'bit']:
            magnitude /= 8
    else:
        msg = _('Invalid string format: %s') % text
        raise ValueError(msg)
    if not unit_prefix:
        res = magnitude
    else:
        res = magnitude * pow(base, UNIT_PREFIX_EXPONENT[unit_prefix])
    if return_int:
        return int(math.ceil(res))
    return res
def get_volume_api_from_url(url):
    scheme, netloc, path, query, frag = urlparse.urlsplit(url)
    components = path.split("/")

    for version in _VALID_VERSIONS:
        if version in components:
            return version[1:]

    msg = (_("Invalid url: '%(url)s'. It must include one of: %(version)s.")
        % {'url': url, 'version': ', '.join(_VALID_VERSIONS)})
    raise exceptions.UnsupportedVersion(msg)
def get_volume_api_from_url(url):
    scheme, netloc, path, query, frag = urlparse.urlsplit(url)
    components = path.split("/")

    for version in _VALID_VERSIONS:
        if version in components:
            return version[1:]

    msg = (_("Invalid url: '%(url)s'. It must include one of: %(version)s.")
        % {'url': url, 'version': ', '.join(_VALID_VERSIONS)})
    raise exceptions.UnsupportedVersion(msg)
Example #12
0
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)
        api_version_input = True
        self.options = options

        if not options.os_volume_api_version:
            # Environment variable OS_VOLUME_API_VERSION was
            # not set and '--os-volume-api-version' option doesn't
            # specify a value.  Fall back to default.
            options.os_volume_api_version = DEFAULT_OS_VOLUME_API_VERSION
            api_version_input = False

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
            options.os_volume_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        subcommand_parser = self.get_subcommand_parser(
            options.os_volume_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        argv = self._delimit_metadata_args(argv)
        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help 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

        (os_username, os_password, os_tenant_name, os_auth_url, os_region_name,
         os_tenant_id, endpoint_type, service_type, service_name,
         volume_service_name, bypass_url, cacert,
         os_auth_system) = (args.os_username, args.os_password,
                            args.os_tenant_name, args.os_auth_url,
                            args.os_region_name, args.os_tenant_id,
                            args.os_endpoint_type, args.service_type,
                            args.service_name, args.volume_service_name,
                            args.bypass_url, args.os_cacert,
                            args.os_auth_system)
        if os_auth_system and os_auth_system != "keystone":
            auth_plugin = cinderclient.auth_plugin.load_plugin(os_auth_system)
        else:
            auth_plugin = None

        if not service_type:
            service_type = DEFAULT_CINDER_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        # FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if auth_plugin:
                auth_plugin.parse_opts(args)

            if not auth_plugin or not auth_plugin.opts:
                if not os_username:
                    raise exc.CommandError("You must provide a user name "
                                           "through --os-username or "
                                           "env[OS_USERNAME].")

            if not os_password:
                # No password, If we've got a tty, try prompting for it
                if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
                    # Check for Ctl-D
                    try:
                        os_password = getpass.getpass('OS Password: '******'t have a tty or the
                # user Ctl-D when prompted.
                if not os_password:
                    raise exc.CommandError("You must provide a password "
                                           "through --os-password, "
                                           "env[OS_PASSWORD] "
                                           "or, prompted response.")

            if not (os_tenant_name or os_tenant_id):
                raise exc.CommandError("You must provide a tenant ID "
                                       "through --os-tenant-id or "
                                       "env[OS_TENANT_ID].")

            # V3 stuff
            project_info_provided = self.options.os_tenant_name or \
                self.options.os_tenant_id or \
                (self.options.os_project_name and
                 (self.options.project_domain_name or
                  self.options.project_domain_id)) or \
                self.options.os_project_id

            if (not project_info_provided):
                raise exc.CommandError(
                    _("You must provide a tenant_name, tenant_id, "
                      "project_id or project_name (with "
                      "project_domain_name or project_domain_id) via "
                      "  --os-tenant-name (env[OS_TENANT_NAME]),"
                      "  --os-tenant-id (env[OS_TENANT_ID]),"
                      "  --os-project-id (env[OS_PROJECT_ID])"
                      "  --os-project-name (env[OS_PROJECT_NAME]),"
                      "  --os-project-domain-id "
                      "(env[OS_PROJECT_DOMAIN_ID])"
                      "  --os-project-domain-name "
                      "(env[OS_PROJECT_DOMAIN_NAME])"))

            if not os_auth_url:
                if os_auth_system and os_auth_system != 'keystone':
                    os_auth_url = auth_plugin.get_auth_url()

            if not os_auth_url:
                raise exc.CommandError(
                    "You must provide an authentication URL "
                    "through --os-auth-url or env[OS_AUTH_URL].")

        if not (os_tenant_name or os_tenant_id):
            raise exc.CommandError(
                "You must provide a tenant ID "
                "through --os-tenant-id or env[OS_TENANT_ID].")

        if not os_auth_url:
            raise exc.CommandError(
                "You must provide an authentication URL "
                "through --os-auth-url or env[OS_AUTH_URL].")

        auth_session = None
        if not auth_plugin:
            auth_session = self._get_keystone_session()

        self.cs = client.Client(options.os_volume_api_version,
                                os_username,
                                os_password,
                                os_tenant_name,
                                os_auth_url,
                                region_name=os_region_name,
                                tenant_id=os_tenant_id,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                volume_service_name=volume_service_name,
                                bypass_url=bypass_url,
                                retries=options.retries,
                                http_log_debug=args.debug,
                                cacert=cacert,
                                auth_system=os_auth_system,
                                auth_plugin=auth_plugin,
                                session=auth_session)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("OpenStack credentials are not valid.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user.")

        endpoint_api_version = None
        # Try to get the API version from the endpoint URL.  If that fails fall
        # back to trying to use what the user specified via
        # --os-volume-api-version or with the OS_VOLUME_API_VERSION environment
        # variable.  Fail safe is to use the default API setting.
        try:
            endpoint_api_version = \
                self.cs.get_volume_api_version_from_endpoint()
            if endpoint_api_version != options.os_volume_api_version:
                msg = (("OpenStack Block Storage API version is set to %s "
                        "but you are accessing a %s endpoint. "
                        "Change its value through --os-volume-api-version "
                        "or env[OS_VOLUME_API_VERSION].") %
                       (options.os_volume_api_version, endpoint_api_version))
                raise exc.InvalidAPIVersion(msg)
        except exc.UnsupportedVersion:
            endpoint_api_version = options.os_volume_api_version
            if api_version_input:
                logger.warning("Cannot determine the API version from "
                               "the endpoint URL. Falling back to the "
                               "user-specified version: %s" %
                               endpoint_api_version)
            else:
                logger.warning("Cannot determine the API version from the "
                               "endpoint URL or user input. Falling back "
                               "to the default API version: %s" %
                               endpoint_api_version)

        profile = osprofiler_profiler and options.profile
        if profile:
            osprofiler_profiler.init(options.profile)

        args.func(self.cs, args)

        if profile:
            trace_id = osprofiler_profiler.get().get_base_id()
            print("Trace ID: %s" % trace_id)
            print("To display trace use next command:\n"
                  "osprofiler trace show --html %s " % trace_id)
    def main(self, argv):

        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)
        api_version_input = True
        self.options = options

        if not options.os_volume_api_version:
            # Environment variable OS_VOLUME_API_VERSION was
            # not set and '--os-volume-api-version' option doesn't
            # specify a value.  Fall back to default.
            options.os_volume_api_version = DEFAULT_OS_VOLUME_API_VERSION
            api_version_input = False

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
            options.os_volume_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        subcommand_parser = self.get_subcommand_parser(
            options.os_volume_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help 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

        (os_username, os_password, os_tenant_name, os_auth_url,
         os_region_name, os_tenant_id, endpoint_type, insecure,
         service_type, service_name, volume_service_name,
         cacert, os_auth_system) = (
             args.os_username, args.os_password,
             args.os_tenant_name, args.os_auth_url,
             args.os_region_name, args.os_tenant_id,
             args.endpoint_type, args.insecure,
             args.service_type, args.service_name,
             args.volume_service_name, args.os_cacert,
             args.os_auth_system)

        if os_auth_system and os_auth_system != "keystone":
            auth_plugin = cinderclient.auth_plugin.load_plugin(os_auth_system)
        else:
            auth_plugin = None

        if not endpoint_type:
            endpoint_type = DEFAULT_CINDER_ENDPOINT_TYPE

        if not service_type:
            service_type = DEFAULT_CINDER_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type

        # FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if auth_plugin:
                auth_plugin.parse_opts(args)

            if not auth_plugin or not auth_plugin.opts:
                if not os_username:
                    raise exc.CommandError("You must provide a user name "
                                           "through --os-username or "
                                           "env[OS_USERNAME].")

            if not os_password:
                raise exc.CommandError("You must provide a password "
                                       "through --os-password or "
                                       "env[OS_PASSWORD].")

            if not (os_tenant_name or os_tenant_id):
                raise exc.CommandError("You must provide a tenant ID "
                                       "through --os-tenant-id or "
                                       "env[OS_TENANT_ID].")

            # V3 stuff
            project_info_provided = self.options.os_tenant_name or \
                self.options.os_tenant_id or \
                (self.options.os_project_name and
                 (self.options.project_domain_name or
                  self.options.project_domain_id)) or \
                self.options.os_project_id

            if (not project_info_provided):
                raise exc.CommandError(
                    _("You must provide a tenant_name, tenant_id, "
                      "project_id or project_name (with "
                      "project_domain_name or project_domain_id) via "
                      "  --os-tenant-name (env[OS_TENANT_NAME]),"
                      "  --os-tenant-id (env[OS_TENANT_ID]),"
                      "  --os-project-id (env[OS_PROJECT_ID])"
                      "  --os-project-name (env[OS_PROJECT_NAME]),"
                      "  --os-project-domain-id "
                      "(env[OS_PROJECT_DOMAIN_ID])"
                      "  --os-project-domain-name "
                      "(env[OS_PROJECT_DOMAIN_NAME])"))

            if not os_auth_url:
                if os_auth_system and os_auth_system != 'keystone':
                    os_auth_url = auth_plugin.get_auth_url()

            if not os_auth_url:
                raise exc.CommandError(
                    "You must provide an authentication URL "
                    "through --os-auth-url or env[OS_AUTH_URL].")

        if not (os_tenant_name or os_tenant_id):
            raise exc.CommandError(
                "You must provide a tenant ID "
                "through --os-tenant-id or env[OS_TENANT_ID].")

        if not os_auth_url:
            raise exc.CommandError(
                "You must provide an authentication URL "
                "through --os-auth-url or env[OS_AUTH_URL].")

        auth_session = self._get_keystone_session()

        self.cs = client.Client(options.os_volume_api_version, os_username,
                                os_password, os_tenant_name, os_auth_url,
                                insecure, region_name=os_region_name,
                                tenant_id=os_tenant_id,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                volume_service_name=volume_service_name,
                                retries=options.retries,
                                http_log_debug=args.debug,
                                cacert=cacert, auth_system=os_auth_system,
                                auth_plugin=auth_plugin,
                                session=auth_session)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("OpenStack credentials are not valid.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user.")

        endpoint_api_version = None
        # Try to get the API version from the endpoint URL.  If that fails fall
        # back to trying to use what the user specified via
        # --os-volume-api-version or with the OS_VOLUME_API_VERSION environment
        # variable.  Fail safe is to use the default API setting.
        try:
            endpoint_api_version = \
                self.cs.get_volume_api_version_from_endpoint()
            if endpoint_api_version != options.os_volume_api_version:
                msg = (("OpenStack Block Storage API version is set to %s "
                        "but you are accessing a %s endpoint. "
                        "Change its value through --os-volume-api-version "
                        "or env[OS_VOLUME_API_VERSION].")
                       % (options.os_volume_api_version, endpoint_api_version))
                raise exc.InvalidAPIVersion(msg)
        except exc.UnsupportedVersion:
            endpoint_api_version = options.os_volume_api_version
            if api_version_input:
                logger.warning("Cannot determine the API version from "
                               "the endpoint URL. Falling back to the "
                               "user-specified version: %s" %
                               endpoint_api_version)
            else:
                logger.warning("Cannot determine the API version from the "
                               "endpoint URL or user input. Falling back "
                               "to the default API version: %s" %
                               endpoint_api_version)

        args.func(self.cs, args)
Example #14
0
    def main(self, argv):
        # Parse args once to find version and debug settings
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self.setup_debugging(options.debug)
        api_version_input = True
        service_type_input = True
        self.options = options

        if not options.os_volume_api_version:
            # Environment variable OS_VOLUME_API_VERSION was
            # not set and '--os-volume-api-version' option doesn't
            # specify a value.  Fall back to default.
            options.os_volume_api_version = DEFAULT_OS_VOLUME_API_VERSION
            api_version_input = False

        version = (options.os_volume_api_version,)

        # build available subcommands based on version
        self.extensions = self._discover_extensions(
            options.os_volume_api_version)
        self._run_extension_hooks('__pre_parse_args__')

        subcommand_parser = self.get_subcommand_parser(
            options.os_volume_api_version)
        self.parser = subcommand_parser

        if options.help or not argv:
            subcommand_parser.print_help()
            return 0

        argv = self._delimit_metadata_args(argv)
        args = subcommand_parser.parse_args(argv)
        self._run_extension_hooks('__post_parse_args__', args)

        # Short-circuit and deal with help 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

        (os_username, os_password, os_tenant_name, os_auth_url,
         os_region_name, os_tenant_id, endpoint_type,
         service_type, service_name, volume_service_name, bypass_url,
         cacert, os_auth_system) = (
             args.os_username, args.os_password,
             args.os_tenant_name, args.os_auth_url,
             args.os_region_name, args.os_tenant_id,
             args.os_endpoint_type,
             args.service_type, args.service_name,
             args.volume_service_name,
             args.bypass_url, args.os_cacert,
             args.os_auth_system)
        if os_auth_system and os_auth_system != "keystone":
            auth_plugin = cinderclient.auth_plugin.load_plugin(os_auth_system)
        else:
            auth_plugin = None

        if not service_type:
            service_type = DEFAULT_CINDER_SERVICE_TYPE
            service_type = utils.get_service_type(args.func) or service_type
            service_type_input = False

        # FIXME(usrleon): Here should be restrict for project id same as
        # for os_username or os_password but for compatibility it is not.

        if not utils.isunauthenticated(args.func):
            if auth_plugin:
                auth_plugin.parse_opts(args)

            if not auth_plugin or not auth_plugin.opts:
                if not os_username:
                    raise exc.CommandError("You must provide a user name "
                                           "through --os-username or "
                                           "env[OS_USERNAME].")

            if not os_password:
                # No password, If we've got a tty, try prompting for it
                if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
                    # Check for Ctl-D
                    try:
                        os_password = getpass.getpass('OS Password: '******'t have a tty or the
                # user Ctl-D when prompted.
                if not os_password:
                    raise exc.CommandError("You must provide a password "
                                           "through --os-password, "
                                           "env[OS_PASSWORD] "
                                           "or, prompted response.")

            if not (os_tenant_name or os_tenant_id):
                raise exc.CommandError("You must provide a tenant ID "
                                       "through --os-tenant-id or "
                                       "env[OS_TENANT_ID].")

            # V3 stuff
            project_info_provided = self.options.os_tenant_name or \
                self.options.os_tenant_id or \
                (self.options.os_project_name and
                 (self.options.project_domain_name or
                  self.options.project_domain_id)) or \
                self.options.os_project_id

            if (not project_info_provided):
                raise exc.CommandError(
                    _("You must provide a tenant_name, tenant_id, "
                      "project_id or project_name (with "
                      "project_domain_name or project_domain_id) via "
                      "  --os-tenant-name (env[OS_TENANT_NAME]),"
                      "  --os-tenant-id (env[OS_TENANT_ID]),"
                      "  --os-project-id (env[OS_PROJECT_ID])"
                      "  --os-project-name (env[OS_PROJECT_NAME]),"
                      "  --os-project-domain-id "
                      "(env[OS_PROJECT_DOMAIN_ID])"
                      "  --os-project-domain-name "
                      "(env[OS_PROJECT_DOMAIN_NAME])"))

            if not os_auth_url:
                if os_auth_system and os_auth_system != 'keystone':
                    os_auth_url = auth_plugin.get_auth_url()

            if not os_auth_url:
                raise exc.CommandError(
                    "You must provide an authentication URL "
                    "through --os-auth-url or env[OS_AUTH_URL].")

        if not (os_tenant_name or os_tenant_id):
            raise exc.CommandError(
                "You must provide a tenant ID "
                "through --os-tenant-id or env[OS_TENANT_ID].")

        if not os_auth_url:
            raise exc.CommandError(
                "You must provide an authentication URL "
                "through --os-auth-url or env[OS_AUTH_URL].")

        auth_session = None
        if not auth_plugin:
            auth_session = self._get_keystone_session()

        if auth_session and (not service_type_input or not api_version_input):
            # NOTE(thingee): Unfortunately the v2 shell is tied to volumev2
            # service_type. If the service_catalog just contains service_type
            # volume with x.x.x.x:8776 for discovery, and the user sets version
            # 2 for the client, it'll default to volumev2 and raise
            # EndpointNotFound. This is a workaround until we feel comfortable
            # with removing the volumev2 assumption.
            keystone_adapter = adapter.Adapter(auth_session)
            try:
                # Try just the client's defaults
                endpoint = keystone_adapter.get_endpoint(
                    service_type=service_type,
                    version=version,
                    interface=endpoint_type)

                # Service was found, but wrong version. Lets try a different
                # version, if the user did not specify one.
                if not endpoint and not api_version_input:
                    if version == ('1',):
                        version = ('2',)
                    else:
                        version = ('1',)

                    endpoint = keystone_adapter.get_endpoint(
                        service_type=service_type, version=version,
                        interface=endpoint_type)

            except keystoneclient_exc.EndpointNotFound as e:
                # No endpoint found with that service_type, lets fall back to
                # other service_types if the user did not specify one.
                if not service_type_input:
                    if service_type == 'volume':
                        service_type = 'volumev2'
                    else:
                        service_type = 'volume'

                try:
                    endpoint = keystone_adapter.get_endpoint(
                        version=version,
                        service_type=service_type, interface=endpoint_type)

                    # Service was found, but wrong version. Lets try
                    # a different version, if the user did not specify one.
                    if not endpoint and not api_version_input:
                        if version == ('1',):
                            version = ('2',)
                        else:
                            version = ('1',)

                        endpoint = keystone_adapter.get_endpoint(
                            service_type=service_type, version=version,
                            interface=endpoint_type)

                except keystoneclient_exc.EndpointNotFound:
                    raise e

        self.cs = client.Client(version[0], os_username, os_password,
                                os_tenant_name, os_auth_url,
                                region_name=os_region_name,
                                tenant_id=os_tenant_id,
                                endpoint_type=endpoint_type,
                                extensions=self.extensions,
                                service_type=service_type,
                                service_name=service_name,
                                volume_service_name=volume_service_name,
                                bypass_url=bypass_url, retries=options.retries,
                                http_log_debug=args.debug, cacert=cacert,
                                auth_system=os_auth_system,
                                auth_plugin=auth_plugin, session=auth_session)

        try:
            if not utils.isunauthenticated(args.func):
                self.cs.authenticate()
        except exc.Unauthorized:
            raise exc.CommandError("OpenStack credentials are not valid.")
        except exc.AuthorizationFailure:
            raise exc.CommandError("Unable to authorize user.")

        endpoint_api_version = None
        # Try to get the API version from the endpoint URL.  If that fails fall
        # back to trying to use what the user specified via
        # --os-volume-api-version or with the OS_VOLUME_API_VERSION environment
        # variable.  Fail safe is to use the default API setting.
        try:
            endpoint_api_version = \
                self.cs.get_volume_api_version_from_endpoint()
            if (endpoint_api_version != options.os_volume_api_version
                    and api_version_input):
                msg = (("OpenStack Block Storage API version is set to %s "
                        "but you are accessing a %s endpoint. "
                        "Change its value through --os-volume-api-version "
                        "or env[OS_VOLUME_API_VERSION].")
                       % (options.os_volume_api_version, endpoint_api_version))
                raise exc.InvalidAPIVersion(msg)
        except exc.UnsupportedVersion:
            endpoint_api_version = options.os_volume_api_version
            if api_version_input:
                logger.warning("Cannot determine the API version from "
                               "the endpoint URL. Falling back to the "
                               "user-specified version: %s" %
                               endpoint_api_version)
            else:
                logger.warning("Cannot determine the API version from the "
                               "endpoint URL or user input. Falling back "
                               "to the default API version: %s" %
                               endpoint_api_version)

        profile = osprofiler_profiler and options.profile
        if profile:
            osprofiler_profiler.init(options.profile)

        args.func(self.cs, args)

        if profile:
            trace_id = osprofiler_profiler.get().get_base_id()
            print("Trace ID: %s" % trace_id)
            print("To display trace use next command:\n"
                  "osprofiler trace show --html %s " % trace_id)
Example #15
0
    def _append_global_identity_args(self, parser):
        # FIXME(bklei): these are global identity (Keystone) arguments which
        # should be consistent and shared by all service clients. Therefore,
        # they should be provided by python-keystoneclient. We will need to
        # refactor this code once this functionality is available in
        # python-keystoneclient.

        parser.add_argument(
            '--os-auth-strategy', metavar='<auth-strategy>',
            default=utils.env('OS_AUTH_STRATEGY', default='keystone'),
            help=_('Authentication strategy (Env: OS_AUTH_STRATEGY'
                   ', default keystone). For now, any other value will'
                   ' disable the authentication'))
        parser.add_argument(
            '--os_auth_strategy',
            help=argparse.SUPPRESS)

        parser.add_argument('--os-username',
                            metavar='<auth-user-name>',
                            default=utils.env('OS_USERNAME',
                                              'CINDER_USERNAME'),
                            help='OpenStack user name. '
                            'Default=env[OS_USERNAME].')
        parser.add_argument('--os_username',
                            help=argparse.SUPPRESS)

        parser.add_argument('--os-password',
                            metavar='<auth-password>',
                            default=utils.env('OS_PASSWORD',
                                              'CINDER_PASSWORD'),
                            help='Password for OpenStack user. '
                            'Default=env[OS_PASSWORD].')
        parser.add_argument('--os_password',
                            help=argparse.SUPPRESS)

        parser.add_argument('--os-tenant-name',
                            metavar='<auth-tenant-name>',
                            default=utils.env('OS_TENANT_NAME',
                                              'CINDER_PROJECT_ID'),
                            help='Tenant name. '
                            'Default=env[OS_TENANT_NAME].')
        parser.add_argument('--os_tenant_name',
                            help=argparse.SUPPRESS)

        parser.add_argument('--os-tenant-id',
                            metavar='<auth-tenant-id>',
                            default=utils.env('OS_TENANT_ID',
                                              'CINDER_TENANT_ID'),
                            help='ID for the tenant. '
                            'Default=env[OS_TENANT_ID].')
        parser.add_argument('--os_tenant_id',
                            help=argparse.SUPPRESS)

        parser.add_argument('--os-auth-url',
                            metavar='<auth-url>',
                            default=utils.env('OS_AUTH_URL',
                                              'CINDER_URL'),
                            help='URL for the authentication service. '
                            'Default=env[OS_AUTH_URL].')
        parser.add_argument('--os_auth_url',
                            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-user-id', metavar='<auth-user-id>',
            default=utils.env('OS_USER_ID'),
            help=_('Authentication user ID (Env: OS_USER_ID)'))

        parser.add_argument(
            '--os_user_id',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-user-domain-id',
            metavar='<auth-user-domain-id>',
            default=utils.env('OS_USER_DOMAIN_ID'),
            help='OpenStack user domain ID. '
            'Defaults to env[OS_USER_DOMAIN_ID].')

        parser.add_argument(
            '--os_user_domain_id',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-user-domain-name',
            metavar='<auth-user-domain-name>',
            default=utils.env('OS_USER_DOMAIN_NAME'),
            help='OpenStack user domain name. '
                 'Defaults to env[OS_USER_DOMAIN_NAME].')

        parser.add_argument(
            '--os_user_domain_name',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-project-id',
            metavar='<auth-project-id>',
            default=utils.env('OS_PROJECT_ID'),
            help='Another way to specify tenant ID. '
            'This option is mutually exclusive with '
            ' --os-tenant-id. '
            'Defaults to env[OS_PROJECT_ID].')

        parser.add_argument(
            '--os_project_id',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-project-name',
            metavar='<auth-project-name>',
            default=utils.env('OS_PROJECT_NAME'),
            help='Another way to specify tenant name. '
                 'This option is mutually exclusive with '
                 ' --os-tenant-name. '
                 'Defaults to env[OS_PROJECT_NAME].')

        parser.add_argument(
            '--os_project_name',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-project-domain-id',
            metavar='<auth-project-domain-id>',
            default=utils.env('OS_PROJECT_DOMAIN_ID'),
            help='Defaults to env[OS_PROJECT_DOMAIN_ID].')

        parser.add_argument(
            '--os-project-domain-name',
            metavar='<auth-project-domain-name>',
            default=utils.env('OS_PROJECT_DOMAIN_NAME'),
            help='Defaults to env[OS_PROJECT_DOMAIN_NAME].')

        parser.add_argument(
            '--os-cert',
            metavar='<certificate>',
            default=utils.env('OS_CERT'),
            help='Defaults to env[OS_CERT].')

        parser.add_argument(
            '--os-key',
            metavar='<key>',
            default=utils.env('OS_KEY'),
            help='Defaults to env[OS_KEY].')

        parser.add_argument('--os-region-name',
                            metavar='<region-name>',
                            default=utils.env('OS_REGION_NAME',
                                              'CINDER_REGION_NAME'),
                            help='Region name. '
                            'Default=env[OS_REGION_NAME].')
        parser.add_argument('--os_region_name',
                            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-token', metavar='<token>',
            default=utils.env('OS_TOKEN'),
            help=_('Defaults to env[OS_TOKEN]'))
        parser.add_argument(
            '--os_token',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-url', metavar='<url>',
            default=utils.env('OS_URL'),
            help=_('Defaults to env[OS_URL]'))
        parser.add_argument(
            '--os_url',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-cacert',
            metavar='<ca-certificate>',
            default=utils.env('OS_CACERT', default=None),
            help=_("Specify a CA bundle file to use in "
                   "verifying a TLS (https) server certificate. "
                   "Defaults to env[OS_CACERT]"))

        parser.add_argument('--insecure',
                            default=utils.env('CINDERCLIENT_INSECURE',
                                              default=False),
                            action='store_true',
                            help=argparse.SUPPRESS)
Example #16
0
    def _append_global_identity_args(self, parser):
        # FIXME(bklei): these are global identity (Keystone) arguments which
        # should be consistent and shared by all service clients. Therefore,
        # they should be provided by python-keystoneclient. We will need to
        # refactor this code once this functionality is available in
        # python-keystoneclient.

        parser.add_argument(
            '--os-auth-strategy',
            metavar='<auth-strategy>',
            default=utils.env('OS_AUTH_STRATEGY', default='keystone'),
            help=_('Authentication strategy (Env: OS_AUTH_STRATEGY'
                   ', default keystone). For now, any other value will'
                   ' disable the authentication'))
        parser.add_argument('--os_auth_strategy', help=argparse.SUPPRESS)

        parser.add_argument('--os-username',
                            metavar='<auth-user-name>',
                            default=utils.env('OS_USERNAME',
                                              'CINDER_USERNAME'),
                            help='OpenStack user name. '
                            'Default=env[OS_USERNAME].')
        parser.add_argument('--os_username', help=argparse.SUPPRESS)

        parser.add_argument('--os-password',
                            metavar='<auth-password>',
                            default=utils.env('OS_PASSWORD',
                                              'CINDER_PASSWORD'),
                            help='Password for OpenStack user. '
                            'Default=env[OS_PASSWORD].')
        parser.add_argument('--os_password', help=argparse.SUPPRESS)

        parser.add_argument('--os-tenant-name',
                            metavar='<auth-tenant-name>',
                            default=utils.env('OS_TENANT_NAME',
                                              'CINDER_PROJECT_ID'),
                            help='Tenant name. '
                            'Default=env[OS_TENANT_NAME].')
        parser.add_argument('--os_tenant_name', help=argparse.SUPPRESS)

        parser.add_argument('--os-tenant-id',
                            metavar='<auth-tenant-id>',
                            default=utils.env('OS_TENANT_ID',
                                              'CINDER_TENANT_ID'),
                            help='ID for the tenant. '
                            'Default=env[OS_TENANT_ID].')
        parser.add_argument('--os_tenant_id', help=argparse.SUPPRESS)

        parser.add_argument('--os-auth-url',
                            metavar='<auth-url>',
                            default=utils.env('OS_AUTH_URL', 'CINDER_URL'),
                            help='URL for the authentication service. '
                            'Default=env[OS_AUTH_URL].')
        parser.add_argument('--os_auth_url', help=argparse.SUPPRESS)

        parser.add_argument('--os-user-id',
                            metavar='<auth-user-id>',
                            default=utils.env('OS_USER_ID'),
                            help=_('Authentication user ID (Env: OS_USER_ID)'))

        parser.add_argument('--os_user_id', help=argparse.SUPPRESS)

        parser.add_argument('--os-user-domain-id',
                            metavar='<auth-user-domain-id>',
                            default=utils.env('OS_USER_DOMAIN_ID'),
                            help='OpenStack user domain ID. '
                            'Defaults to env[OS_USER_DOMAIN_ID].')

        parser.add_argument('--os_user_domain_id', help=argparse.SUPPRESS)

        parser.add_argument('--os-user-domain-name',
                            metavar='<auth-user-domain-name>',
                            default=utils.env('OS_USER_DOMAIN_NAME'),
                            help='OpenStack user domain name. '
                            'Defaults to env[OS_USER_DOMAIN_NAME].')

        parser.add_argument('--os_user_domain_name', help=argparse.SUPPRESS)

        parser.add_argument('--os-project-id',
                            metavar='<auth-project-id>',
                            default=utils.env('OS_PROJECT_ID'),
                            help='Another way to specify tenant ID. '
                            'This option is mutually exclusive with '
                            ' --os-tenant-id. '
                            'Defaults to env[OS_PROJECT_ID].')

        parser.add_argument('--os_project_id', help=argparse.SUPPRESS)

        parser.add_argument('--os-project-name',
                            metavar='<auth-project-name>',
                            default=utils.env('OS_PROJECT_NAME'),
                            help='Another way to specify tenant name. '
                            'This option is mutually exclusive with '
                            ' --os-tenant-name. '
                            'Defaults to env[OS_PROJECT_NAME].')

        parser.add_argument('--os_project_name', help=argparse.SUPPRESS)

        parser.add_argument('--os-project-domain-id',
                            metavar='<auth-project-domain-id>',
                            default=utils.env('OS_PROJECT_DOMAIN_ID'),
                            help='Defaults to env[OS_PROJECT_DOMAIN_ID].')

        parser.add_argument('--os-project-domain-name',
                            metavar='<auth-project-domain-name>',
                            default=utils.env('OS_PROJECT_DOMAIN_NAME'),
                            help='Defaults to env[OS_PROJECT_DOMAIN_NAME].')

        parser.add_argument('--os-region-name',
                            metavar='<region-name>',
                            default=utils.env('OS_REGION_NAME',
                                              'CINDER_REGION_NAME'),
                            help='Region name. '
                            'Default=env[OS_REGION_NAME].')
        parser.add_argument('--os_region_name', help=argparse.SUPPRESS)

        parser.add_argument('--os-token',
                            metavar='<token>',
                            default=utils.env('OS_TOKEN'),
                            help=_('Defaults to env[OS_TOKEN]'))
        parser.add_argument('--os_token', help=argparse.SUPPRESS)

        parser.add_argument('--os-url',
                            metavar='<url>',
                            default=utils.env('OS_URL'),
                            help=_('Defaults to env[OS_URL]'))
        parser.add_argument('--os_url', help=argparse.SUPPRESS)

        # Register the CLI arguments that have moved to the session object.
        session.Session.register_cli_options(parser)
        parser.set_defaults(
            insecure=utils.env('CINDERCLIENT_INSECURE', default=False))
    def _append_global_identity_args(self, parser):
        # FIXME(bklei): these are global identity (Keystone) arguments which
        # should be consistent and shared by all service clients. Therefore,
        # they should be provided by python-keystoneclient. We will need to
        # refactor this code once this functionality is available in
        # python-keystoneclient.

        parser.add_argument(
            "--os-auth-strategy",
            metavar="<auth-strategy>",
            default=utils.env("OS_AUTH_STRATEGY", default="keystone"),
            help=_(
                "Authentication strategy (Env: OS_AUTH_STRATEGY"
                ", default keystone). For now, any other value will"
                " disable the authentication."
            ),
        )
        parser.add_argument("--os_auth_strategy", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-username",
            metavar="<auth-user-name>",
            default=utils.env("OS_USERNAME", "CINDER_USERNAME"),
            help="OpenStack user name. " "Default=env[OS_USERNAME].",
        )
        parser.add_argument("--os_username", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-password",
            metavar="<auth-password>",
            default=utils.env("OS_PASSWORD", "CINDER_PASSWORD"),
            help="Password for OpenStack user. " "Default=env[OS_PASSWORD].",
        )
        parser.add_argument("--os_password", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-tenant-name",
            metavar="<auth-tenant-name>",
            default=utils.env("OS_TENANT_NAME", "CINDER_PROJECT_ID"),
            help="Tenant name. " "Default=env[OS_TENANT_NAME].",
        )
        parser.add_argument("--os_tenant_name", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-tenant-id",
            metavar="<auth-tenant-id>",
            default=utils.env("OS_TENANT_ID", "CINDER_TENANT_ID"),
            help="ID for the tenant. " "Default=env[OS_TENANT_ID].",
        )
        parser.add_argument("--os_tenant_id", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-auth-url",
            metavar="<auth-url>",
            default=utils.env("OS_AUTH_URL", "CINDER_URL"),
            help="URL for the authentication service. " "Default=env[OS_AUTH_URL].",
        )
        parser.add_argument("--os_auth_url", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-user-id",
            metavar="<auth-user-id>",
            default=utils.env("OS_USER_ID"),
            help=_("Authentication user ID (Env: OS_USER_ID)."),
        )

        parser.add_argument("--os_user_id", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-user-domain-id",
            metavar="<auth-user-domain-id>",
            default=utils.env("OS_USER_DOMAIN_ID"),
            help="OpenStack user domain ID. " "Defaults to env[OS_USER_DOMAIN_ID].",
        )

        parser.add_argument("--os_user_domain_id", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-user-domain-name",
            metavar="<auth-user-domain-name>",
            default=utils.env("OS_USER_DOMAIN_NAME"),
            help="OpenStack user domain name. " "Defaults to env[OS_USER_DOMAIN_NAME].",
        )

        parser.add_argument("--os_user_domain_name", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-project-id",
            metavar="<auth-project-id>",
            default=utils.env("OS_PROJECT_ID"),
            help="Another way to specify tenant ID. "
            "This option is mutually exclusive with "
            " --os-tenant-id. "
            "Defaults to env[OS_PROJECT_ID].",
        )

        parser.add_argument("--os_project_id", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-project-name",
            metavar="<auth-project-name>",
            default=utils.env("OS_PROJECT_NAME"),
            help="Another way to specify tenant name. "
            "This option is mutually exclusive with "
            " --os-tenant-name. "
            "Defaults to env[OS_PROJECT_NAME].",
        )

        parser.add_argument("--os_project_name", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-project-domain-id",
            metavar="<auth-project-domain-id>",
            default=utils.env("OS_PROJECT_DOMAIN_ID"),
            help="Defaults to env[OS_PROJECT_DOMAIN_ID].",
        )

        parser.add_argument(
            "--os-project-domain-name",
            metavar="<auth-project-domain-name>",
            default=utils.env("OS_PROJECT_DOMAIN_NAME"),
            help="Defaults to env[OS_PROJECT_DOMAIN_NAME].",
        )

        parser.add_argument(
            "--os-region-name",
            metavar="<region-name>",
            default=utils.env("OS_REGION_NAME", "CINDER_REGION_NAME"),
            help="Region name. " "Default=env[OS_REGION_NAME].",
        )
        parser.add_argument("--os_region_name", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-token", metavar="<token>", default=utils.env("OS_TOKEN"), help=_("Defaults to env[OS_TOKEN].")
        )
        parser.add_argument("--os_token", help=argparse.SUPPRESS)

        parser.add_argument(
            "--os-url", metavar="<url>", default=utils.env("OS_URL"), help=_("Defaults to env[OS_URL].")
        )
        parser.add_argument("--os_url", help=argparse.SUPPRESS)

        # Register the CLI arguments that have moved to the session object.
        session.Session.register_cli_options(parser)
        parser.set_defaults(insecure=utils.env("CINDERCLIENT_INSECURE", default=False))