Пример #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
Пример #2
0
 def add_known_arguments(self, parser):
     parser.add_argument(
         'name', metavar='TABLE_NAME',
         help=_('Name of table to delete item from'))
     parser.add_argument(
         '--request-file', metavar='FILE', dest='request_file',
         help=_('File that contains item key description'))
Пример #3
0
 def add_known_arguments(self, parser):
     parser.add_argument(
         'name', metavar='TABLE_NAME',
         help=_('Name of table to put item in'))
     parser.add_argument(
         '--request-file', metavar='FILE',
         help=_('File that contains item description to put in table'))
Пример #4
0
 def add_known_arguments(self, parser):
     parser.add_argument(
         'name', metavar='TABLE_NAME',
         help=_('Name of table to load data'))
     parser.add_argument(
         '--request-file', metavar='FILE',
         help=_('File that contains data to load in table'))
Пример #5
0
 def add_known_arguments(self, parser):
     parser.add_argument(
         '--limit',
         help=_('A maximum number of the items to return'))
     parser.add_argument(
         '--start-table-name',
         help=_('The first table name that this operation will evaluate.'))
Пример #6
0
 def authenticate(self):
     if self.auth_strategy == 'keystone':
         auth_api = self.auth_url.split('/')[-1]
         if auth_api == 'v2.0':
             self._authenticate_keystone()
         elif auth_api == 'v3':
             self._authenticate_keystone_v3()
         else:
             err_msg = _('Unknown Keystone api version: %s') % auth_api
             raise exceptions.Unauthorized(message=err_msg)
     elif self.auth_strategy == 'noauth':
         self._authenticate_noauth()
     else:
         err_msg = _('Unknown auth strategy: %s') % self.auth_strategy
         raise exceptions.Unauthorized(message=err_msg)
Пример #7
0
def make_client(instance):
    """Returns an magnetodb client.
    """
    api_name = instance._api_name
    magnetodb_client = utils.get_client_class(
        instance._api_name,
        instance._api_version[api_name],
        API,
    )
    instance.initialize()
    url = instance._url
    url = url.rstrip("/")
    if '1' == instance._api_version[api_name]:
        client = magnetodb_client(username=instance._username,
                                  tenant_name=instance._tenant_name,
                                  password=instance._password,
                                  region_name=instance._region_name,
                                  auth_url=instance._auth_url,
                                  endpoint_url=url,
                                  token=instance._token,
                                  auth_strategy=instance._auth_strategy,
                                  insecure=instance._insecure,
                                  ca_cert=instance._ca_cert)
        return client
    else:
        raise exceptions.UnsupportedVersion(_("API version %s is not "
                                              "supported") %
                                            instance._api_version[api_name])
Пример #8
0
def safe_encode(text, incoming=None,
                encoding='utf-8', errors='strict'):
    """Encodes incoming str/unicode using `encoding`.

    If incoming is not specified, text is expected to be encoded with
    current python's default encoding. (`sys.getdefaultencoding`)

    :param incoming: Text's current encoding
    :param encoding: Expected encoding for text (Default UTF-8)
    :param errors: Errors handling policy. See here for valid
        values http://docs.python.org/2/library/codecs.html
    :returns: text or a bytestring `encoding` encoded
                representation of it.
    :raises TypeError: If text is not an isntance of str
    """
    if not isinstance(text, six.string_types):
        raise TypeError(_("%s can't be encoded") % type(text).capitalize())

    if not incoming:
        incoming = (sys.stdin.encoding or
                    sys.getdefaultencoding())

    if isinstance(text, six.text_type):
        return text.encode(encoding, errors)
    elif text and encoding != incoming:
        # Decode text before encoding it with `encoding`
        text = safe_decode(text, incoming, errors)
        return text.encode(encoding, errors)

    return text
Пример #9
0
 def get_parser(self, prog_name):
     parser = super(DeleteCommand, self).get_parser(prog_name)
     help_str = _('Name of %s to delete')
     parser.add_argument(
         'name', metavar=self.resource.upper(),
         help=help_str % self.resource)
     return parser
Пример #10
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
Пример #11
0
def add_pagination_argument(parser):
    parser.add_argument(
        '-P', '--page-size',
        dest='page_size', metavar='SIZE', type=int,
        help=_("Specify retrieve unit of each request, then split one request "
               "to several requests"),
        default=None)
 def _add_specific_args(self, parser):
     parser.add_argument(
         '--service-type', metavar='<service-type>',
         default=shell.env('OS_KEYVALUE_SERVICE_TYPE',
                           default='kv-streaming'),
         help=_('Defaults to env[OS_KEYVALUE_SERVICE_TYPE].'))
     return parser
Пример #13
0
def add_sorting_argument(parser):
    parser.add_argument(
        '--sort-key',
        dest='sort_key', metavar='FIELD',
        action='append',
        help=_("Sort list by specified fields (This option can be repeated), "
               "The number of sort_dir and sort_key should match each other, "
               "more sort_dir specified will be omitted, less will be filled "
               "with asc as default direction "),
        default=[])
    parser.add_argument(
        '--sort-dir',
        dest='sort_dir', metavar='{asc,desc}',
        help=_("Sort list in specified directions "
               "(This option can be repeated)"),
        action='append',
        default=[],
        choices=['asc', 'desc'])
def import_class(import_str):
    """Returns a class from a string including module and class."""
    mod_str, _sep, class_str = import_str.rpartition('.')
    try:
        __import__(mod_str)
        return getattr(sys.modules[mod_str], class_str)
    except (ValueError, AttributeError):
        raise ImportError(_('Class %s cannot be found (%s)') %
                          (class_str,
                           traceback.format_exception(*sys.exc_info())))
Пример #15
0
 def run(self, parsed_args):
     self.log.debug('run(%s)', parsed_args)
     self.check_required_args(parsed_args)
     magnetodb_client = self.get_client()
     obj_deleter = getattr(magnetodb_client, self.method)
     _name = parsed_args.name
     obj_deleter(_name)
     print((_('Deleted %(resource)s: %(name)s')
            % {'name': _name,
               'resource': self.resource}),
           file=self.app.stdout)
     return
Пример #16
0
 def _get_info(self, data, parsed_args):
     data = super(ShowIndex, self)._get_info(data, parsed_args)
     index_name = parsed_args.index_name
     for index in data:
         if index['index_name'] == parsed_args.index_name:
             data = index
             break
     else:
         msg = _('Error. Index "%s" is not found in table "%s"')
         msg %= (index_name, parsed_args.name)
         raise exceptions.MagnetoDBClientException(msg)
     return data
Пример #17
0
def add_show_list_common_argument(parser):
    parser.add_argument(
        '-D', '--show-details',
        help=_('Show detailed info'),
        action='store_true',
        default=False, )
    parser.add_argument(
        '--show_details',
        action='store_true',
        help=argparse.SUPPRESS)
    parser.add_argument(
        '--fields',
        help=argparse.SUPPRESS,
        action='append',
        default=[])
    parser.add_argument(
        '-F', '--field',
        dest='fields', metavar='FIELD',
        help=_('Specify the field(s) to be returned by server,'
               ' can be repeated'),
        action='append',
        default=[])
Пример #18
0
 def run_subcommand(self, argv):
     subcommand = self.command_manager.find_command(argv)
     cmd_factory, cmd_name, sub_argv = subcommand
     cmd = cmd_factory(self, self.options)
     err = None
     result = 1
     try:
         self.prepare_to_run_command(cmd)
         full_name = (cmd_name
                      if self.interactive_mode
                      else ' '.join([self.NAME, cmd_name])
                      )
         cmd_parser = cmd.get_parser(full_name)
         return run_command(cmd, cmd_parser, sub_argv)
     except Exception as err:
         if self.options.verbose_level == self.DEBUG_LEVEL:
             self.log.exception(unicode(err))
         else:
             self.log.error(unicode(err))
         try:
             self.clean_up(cmd, result, err)
         except Exception as err2:
             if self.options.verbose_level == self.DEBUG_LEVEL:
                 self.log.exception(unicode(err2))
             else:
                 self.log.error(_('Could not clean up: %s'), unicode(err2))
         if self.options.verbose_level == self.DEBUG_LEVEL:
             raise
     else:
         try:
             self.clean_up(cmd, result, None)
         except Exception as err3:
             if self.options.verbose_level == self.DEBUG_LEVEL:
                 self.log.exception(unicode(err3))
             else:
                 self.log.error(_('Could not clean up: %s'), unicode(err3))
     return result
def validate_int_range(parsed_args, attr_name, min_value=None, max_value=None):
    val = getattr(parsed_args, attr_name, None)
    if val is None:
        return
    try:
        if not isinstance(val, int):
            int_val = int(val, 0)
        else:
            int_val = val
        if ((min_value is None or min_value <= int_val) and
            (max_value is None or int_val <= max_value)):
            return
    except (ValueError, TypeError):
        pass

    if min_value is not None and max_value is not None:
        msg = (_('%(attr_name)s "%(val)s" should be an integer '
                 '[%(min)i:%(max)i].') %
               {'attr_name': attr_name.replace('_', '-'),
                'val': val, 'min': min_value, 'max': max_value})
    elif min_value is not None:
        msg = (_('%(attr_name)s "%(val)s" should be an integer '
                 'greater than or equal to %(min)i.') %
               {'attr_name': attr_name.replace('_', '-'),
                'val': val, 'min': min_value})
    elif max_value is not None:
        msg = (_('%(attr_name)s "%(val)s" should be an integer '
                 'smaller than or equal to %(max)i.') %
               {'attr_name': attr_name.replace('_', '-'),
                'val': val, 'max': max_value})
    else:
        msg = (_('%(attr_name)s "%(val)s" should be an integer.') %
               {'attr_name': attr_name.replace('_', '-'),
                'val': val})

    raise exceptions.CommandError(msg)
Пример #20
0
 def __call__(self, parser, namespace, values, option_string=None):
     outputs = []
     max_len = 0
     app = self.default
     parser.print_help(app.stdout)
     app.stdout.write(_('\nCommands for API v%s:\n') % app.api_version)
     command_manager = app.command_manager
     for name, ep in sorted(command_manager):
         factory = ep.load()
         cmd = factory(self, None)
         one_liner = cmd.get_description().split('\n')[0]
         outputs.append((name, one_liner))
         max_len = max(len(name), max_len)
     for (name, one_liner) in outputs:
         app.stdout.write('  %s  %s\n' % (name.ljust(max_len), one_liner))
     sys.exit(0)
Пример #21
0
def find_resourceid_by_id(client, resource, resource_id):
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % resource_plural)
    # perform search by id only if we are passing a valid UUID
    match = re.match(UUID_PATTERN, resource_id)
    collection = resource_plural
    if match:
        data = obj_lister(id=resource_id, fields='id')
        if data and data[collection]:
            return data[collection][0]['id']
    not_found_message = (_("Unable to find %(resource)s with id "
                           "'%(id)s'") %
                         {'resource': resource, 'id': resource_id})
    # 404 is used to simulate server side behavior
    raise exceptions.MagnetoDBClientException(
        message=not_found_message, status_code=404)
Пример #22
0
def _process_previous_argument(current_arg, _value_number, current_type_str,
                               _list_flag, _values_specs, _clear_flag,
                               values_specs):
    if current_arg is not None:
        if _value_number == 0 and (current_type_str or _list_flag):
                # This kind of argument should have value
                raise exceptions.CommandError(
                    _("Invalid values_specs %s") % ' '.join(values_specs))
        if _value_number > 1 or _list_flag or current_type_str == 'list':
            current_arg.update({'nargs': '+'})
        elif _value_number == 0:
            if _clear_flag:
                # if we have action=clear, we use argument's default
                # value None for argument
                _values_specs.pop()
            else:
                # We assume non value argument as bool one
                current_arg.update({'action': 'store_true'})
Пример #23
0
def _find_resourceid_by_name(client, resource, name):
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % resource_plural)
    data = obj_lister(name=name, fields='id')
    collection = resource_plural
    info = data[collection]
    if len(info) > 1:
        raise exceptions.MagnetoDBClientNoUniqueMatch(resource=resource,
                                                      name=name)
    elif len(info) == 0:
        not_found_message = (_("Unable to find %(resource)s with name "
                               "'%(name)s'") %
                             {'resource': resource, 'name': name})
        # 404 is used to simulate server side behavior
        raise exceptions.MagnetoDBClientException(
            message=not_found_message, status_code=404)
    else:
        return info[0]['id']
Пример #24
0
 def clean_up(self, cmd, result, err):
     self.log.debug('clean_up %s', cmd.__class__.__name__)
     if err:
         self.log.debug(_('Got an error: %s'), unicode(err))
Пример #25
0
    def authenticate_user(self):
        """Make sure the user has provided all of the authentication
        info we need.
        """
        if self.options.os_auth_strategy == 'keystone':
            if self.options.os_token or self.options.os_url:
                # Token flow auth takes priority
                if not self.options.os_token:
                    raise exc.CommandError(
                        _("You must provide a token via"
                          " either --os-token or env[OS_TOKEN]"))

                if not self.options.os_url:
                    raise exc.CommandError(
                        _("You must provide a service URL via"
                          " either --os-url or env[OS_URL]"))

            else:
                # Validate password flow auth
                if not self.options.os_username:
                    raise exc.CommandError(
                        _("You must provide a username via"
                          " either --os-username or env[OS_USERNAME]"))

                if not self.options.os_password:
                    raise exc.CommandError(
                        _("You must provide a password via"
                          " either --os-password or env[OS_PASSWORD]"))

                if (not self.options.os_tenant_name
                    and not self.options.os_tenant_id):
                    raise exc.CommandError(
                        _("You must provide a tenant_name or tenant_id via"
                          "  --os-tenant-name, env[OS_TENANT_NAME]"
                          "  --os-tenant-id, or via env[OS_TENANT_ID]"))

                if not self.options.os_auth_url:
                    raise exc.CommandError(
                        _("You must provide an auth url via"
                          " either --os-auth-url or via env[OS_AUTH_URL]"))
        else:   # not keystone
            if not self.options.os_url:
                raise exc.CommandError(
                    _("You must provide a service URL via"
                      " either --os-url or env[OS_URL]"))

        self.client_manager = clientmanager.ClientManager(
            token=self.options.os_token,
            url=self.options.os_url,
            auth_url=self.options.os_auth_url,
            tenant_name=self.options.os_tenant_name,
            tenant_id=self.options.os_tenant_id,
            username=self.options.os_username,
            password=self.options.os_password,
            region_name=self.options.os_region_name,
            api_version=self.api_version,
            auth_strategy=self.options.os_auth_strategy,
            service_type=self.options.service_type,
            endpoint_type=self.options.endpoint_type,
            insecure=self.options.insecure,
            ca_cert=self.options.os_cacert,
            log_credentials=True)
        return
Пример #26
0
    def build_option_parser(self, description, version):
        """Return an argparse option parser for this application.

        Subclasses may override this method to extend
        the parser with more global options.

        :param description: full description of the application
        :paramtype description: str
        :param version: version number for the application
        :paramtype version: str
        """
        parser = argparse.ArgumentParser(
            description=description,
            add_help=False, )
        parser.add_argument(
            '--version',
            action='version',
            version=__version__, )
        parser.add_argument(
            '-v', '--verbose', '--debug',
            action='count',
            dest='verbose_level',
            default=self.DEFAULT_VERBOSE_LEVEL,
            help=_('Increase verbosity of output and show tracebacks on'
                   ' errors. Can be repeated.'))
        parser.add_argument(
            '-q', '--quiet',
            action='store_const',
            dest='verbose_level',
            const=0,
            help=_('Suppress output except warnings and errors'))
        parser.add_argument(
            '-h', '--help',
            action=HelpAction,
            nargs=0,
            default=self,  # tricky
            help=_("Show this help message and exit"))
        # Global arguments
        parser.add_argument(
            '--os-auth-strategy', metavar='<auth-strategy>',
            default=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-auth-url', metavar='<auth-url>',
            default=env('OS_AUTH_URL'),
            help=_('Authentication URL (Env: OS_AUTH_URL)'))
        parser.add_argument(
            '--os_auth_url',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-tenant-name', metavar='<auth-tenant-name>',
            default=env('OS_TENANT_NAME'),
            help=_('Authentication tenant name (Env: OS_TENANT_NAME)'))
        parser.add_argument(
            '--os_tenant_name',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-tenant-id', metavar='<auth-tenant-id>',
            default=env('OS_TENANT_ID'),
            help=_('Authentication tenant name (Env: OS_TENANT_ID)'))

        parser.add_argument(
            '--os-username', metavar='<auth-username>',
            default=utils.env('OS_USERNAME'),
            help=_('Authentication username (Env: OS_USERNAME)'))
        parser.add_argument(
            '--os_username',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-password', metavar='<auth-password>',
            default=utils.env('OS_PASSWORD'),
            help=_('Authentication password (Env: OS_PASSWORD)'))
        parser.add_argument(
            '--os_password',
            help=argparse.SUPPRESS)

        parser.add_argument(
            '--os-region-name', metavar='<auth-region-name>',
            default=env('OS_REGION_NAME'),
            help=_('Authentication region name (Env: OS_REGION_NAME)'))
        parser.add_argument(
            '--os_region_name',
            help=argparse.SUPPRESS)

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

        parser.add_argument(
            '--service-type', metavar='<service-type>',
            default=env('OS_KEYVALUE_SERVICE_TYPE', default='kv-storage'),
            help=_('Defaults to env[OS_KEYVALUE_SERVICE_TYPE].'))

        parser.add_argument(
            '--endpoint-type', metavar='<endpoint-type>',
            default=env('OS_ENDPOINT_TYPE', default='publicURL'),
            help=_('Defaults to env[OS_ENDPOINT_TYPE] or publicURL.'))

        parser.add_argument(
            '--os-url', metavar='<url>',
            default=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=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',
            action='store_true',
            default=env('MAGNETODBCLIENT_INSECURE', default=False),
            help=_("Explicitly allow magnetodbclient to perform \"insecure\" "
                   "SSL (https) requests. The server's certificate will "
                   "not be verified against any certificate authorities. "
                   "This option should be used with caution."))

        return parser
Пример #27
0
 def add_known_arguments(self, parser):
     help_str = _('Name of table to look up')
     parser.add_argument(
         'name', metavar='TABLE_NAME',
         help=help_str)
 def _from_json(self, datastring):
     try:
         return jsonutils.loads(datastring)
     except ValueError:
         msg = _("Cannot understand JSON")
         raise exception.MalformedResponseBody(reason=msg)
Пример #29
0
 def add_known_arguments(self, parser):
     parser.add_argument(
         '--request-file', metavar='FILE',
         help=_('File that contains table description to create'))
Пример #30
0
 def add_known_arguments(self, parser):
     super(ShowIndex, self).add_known_arguments(parser)
     parser.add_argument(
         'index_name', metavar='INDEX_NAME',
         help=_('Name of index to describe'))