示例#1
0
 def run(self, parsed_args):
     self.log.debug('run(%s)', parsed_args)
     apmec_client = self.get_client()
     apmec_client.format = parsed_args.request_format
     _extra_values = parse_args_to_dict(self.values_specs)
     _merge_args(self, parsed_args, _extra_values, self.values_specs)
     body = self.args2body(parsed_args)
     if self.resource in body:
         body[self.resource].update(_extra_values)
     else:
         body[self.resource] = _extra_values
     if not body[self.resource]:
         raise exceptions.CommandError(
             _("Must specify new values to update %s") % self.resource)
     if self.allow_names:
         _id = find_resourceid_by_name_or_id(apmec_client, self.resource,
                                             parsed_args.id)
     else:
         _id = find_resourceid_by_id(apmec_client, self.resource,
                                     parsed_args.id)
     obj_updator = getattr(apmec_client, "update_%s" % self.resource)
     obj_updator(_id, body)
     print((_('Updated %(resource)s: %(id)s') % {
         'id': parsed_args.id,
         'resource': self.resource
     }),
           file=self.app.stdout)
     return
def validate_ip_subnet(parsed_args, attr_name):
    val = getattr(parsed_args, attr_name)
    if not val:
        return
    try:
        netaddr.IPNetwork(val)
    except (netaddr.AddrFormatError, ValueError):
        raise exceptions.CommandError(
            (_('%(attr_name)s "%(val)s" is not a valid CIDR.') % {
                'attr_name': attr_name.replace('_', '-'),
                'val': val
            }))
示例#3
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'})
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)
示例#5
0
 def run(self, parsed_args):
     failure = False
     deleted_ids = []
     failed_items = {}
     apmec_client = self.get_client()
     apmec_client.format = parsed_args.request_format
     obj_deleter = getattr(apmec_client, "delete_%s" % self.resource)
     for resource_id in parsed_args.ids:
         try:
             if self.allow_names:
                 _id = find_resourceid_by_name_or_id(
                     apmec_client, self.resource, resource_id)
             else:
                 _id = resource_id
             obj_deleter(_id)
             deleted_ids.append(resource_id)
         except Exception as e:
             failure = True
             failed_items[resource_id] = e
     if failure:
         msg = ''
         if deleted_ids:
             status_msg = self.deleted_msg.get(self.resource, 'deleted')
             msg = (_('Successfully %(status_msg)s %(resource)s(s):'
                      ' %(deleted_list)s') % {
                          'status_msg': status_msg,
                          'deleted_list': ', '.join(deleted_ids),
                          'resource': self.resource
                      })
         err_msg = _("\n\nUnable to delete the below"
                     " %s(s):") % self.resource
         for failed_id, error in failed_items.iteritems():
             err_msg += (_('\n Cannot delete %(failed_id)s: %(error)s') % {
                 'failed_id': failed_id,
                 'error': error
             })
         msg += err_msg
         raise exceptions.CommandError(msg)
     else:
         print((_('All specified %(resource)s(s) %(msg)s successfully') % {
             'msg': self.deleted_msg.get(self.resource, 'deleted'),
             'resource': self.resource
         }))
     return
示例#6
0
 def _discover_auth_versions(self, session, auth_url):
     # discover the API versions the server is supporting base on the
     # given URL
     try:
         ks_discover = discover.Discover(session=session, auth_url=auth_url)
         return (ks_discover.url_for('2.0'), ks_discover.url_for('3.0'))
     except ks_exc.ClientException:
         # Identity service may not support discover API version.
         # Lets try 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'):
             return (None, auth_url)
         elif path.startswith('/v2'):
             return (auth_url, None)
         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.')
             raise exc.CommandError(msg)
示例#7
0
    def authenticate_user(self):
        """Authentication validation.

        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]"
                          " when providing a service URL"))

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

            else:
                # Validate password flow auth
                project_info = (self.options.os_tenant_name
                                or self.options.os_tenant_id
                                or (self.options.os_project_name and
                                    (self.options.os_project_domain_name
                                     or self.options.os_project_domain_id))
                                or self.options.os_project_id)

                if (not self.options.os_username
                        and not self.options.os_user_id):
                    raise exc.CommandError(
                        _("You must provide a username or user ID via"
                          "  --os-username, env[OS_USERNAME] or"
                          "  --os-user-id, env[OS_USER_ID]"))

                if not self.options.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:
                            self.options.os_password = getpass.getpass(
                                'OS Password: '******'t have a tty or the
                    # user Ctl-D when prompted.
                    if not self.options.os_password:
                        raise exc.CommandError(
                            _("You must provide a password via"
                              " either --os-password or env[OS_PASSWORD]"))

                if (not project_info):
                    # tenent is deprecated in Keystone v3. Use the latest
                    # terminology instead.
                    raise exc.CommandError(
                        _("You must provide a project_id or project_name ("
                          "with project_domain_name or project_domain_id) "
                          "via "
                          "  --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 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]"))
            auth_session = self._get_keystone_session()
            auth = auth_session.auth
        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]"))
            auth_session = None
            auth = None

        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,
            user_id=self.options.os_user_id,
            password=self.options.os_password,
            region_name=self.options.os_region_name,
            api_version=self.api_version,
            auth_strategy=self.options.os_auth_strategy,
            # FIXME (bklei) honor deprecated service_type and
            # endpoint type until they are removed
            service_type=self.options.os_service_type
            or self.options.service_type,
            endpoint_type=self.options.os_endpoint_type or self.endpoint_type,
            insecure=self.options.insecure,
            ca_cert=self.options.os_cacert,
            timeout=self.options.http_timeout,
            retries=self.options.retries,
            raise_errors=False,
            session=auth_session,
            auth=auth,
            log_credentials=True)
        return
示例#8
0
def parse_args_to_dict(values_specs):
    '''It is used to analyze the extra command options to command.

    Besides known options and arguments, our commands also support user to
    put more options to the end of command line. For example,
    list_nets -- --tag x y --key1 value1, where '-- --tag x y --key1 value1'
    is extra options to our list_nets. This feature can support V1.0 API's
    fields selection and filters. For example, to list networks which has name
    'test4', we can have list_nets -- --name=test4.

    value spec is: --key type=int|bool|... value. Type is one of Python
    built-in types. By default, type is string. The key without value is
    a bool option. Key with two values will be a list option.

    '''

    # values_specs for example: '-- --tag x y --key1 type=int value1'
    # -- is a pseudo argument
    values_specs_copy = values_specs[:]
    if values_specs_copy and values_specs_copy[0] == '--':
        del values_specs_copy[0]
    # converted ArgumentParser arguments for each of the options
    _options = {}
    # the argument part for current option in _options
    current_arg = None
    # the string after remove meta info in values_specs
    # for example, '--tag x y --key1 value1'
    _values_specs = []
    # record the count of values for an option
    # for example: for '--tag x y', it is 2, while for '--key1 value1', it is 1
    _value_number = 0
    # list=true
    _list_flag = False
    # action=clear
    _clear_flag = False
    # the current item in values_specs
    current_item = None
    # the str after 'type='
    current_type_str = None
    for _item in values_specs_copy:
        if _item.startswith('--'):
            # Deal with previous argument if any
            _process_previous_argument(current_arg, _value_number,
                                       current_type_str, _list_flag,
                                       _values_specs, _clear_flag,
                                       values_specs)

            # Init variables for current argument
            current_item = _item
            _list_flag = False
            _clear_flag = False
            current_type_str = None
            if "=" in _item:
                _value_number = 1
                _item = _item.split('=')[0]
            else:
                _value_number = 0
            if _item in _options:
                raise exceptions.CommandError(
                    _("Duplicated options %s") % ' '.join(values_specs))
            else:
                _options.update({_item: {}})
            current_arg = _options[_item]
            _item = current_item
        elif _item.startswith('type='):
            if current_arg is None:
                raise exceptions.CommandError(
                    _("Invalid values_specs %s") % ' '.join(values_specs))
            if 'type' not in current_arg:
                current_type_str = _item.split('=', 2)[1]
                current_arg.update({'type': eval(current_type_str)})
                if current_type_str == 'bool':
                    current_arg.update({'type': utils.str2bool})
                elif current_type_str == 'dict':
                    current_arg.update({'type': utils.str2dict})
                continue
        elif _item == 'list=true':
            _list_flag = True
            continue
        elif _item == 'action=clear':
            _clear_flag = True
            continue

        if not _item.startswith('--'):
            # All others are value items
            # Make sure '--' occurs first and allow minus value
            if (not current_item or '=' in current_item
                    or _item.startswith('-') and not is_number(_item)):
                raise exceptions.CommandError(
                    _("Invalid values_specs %s") % ' '.join(values_specs))
            _value_number += 1

        _values_specs.append(_item)

    # Deal with last one argument
    _process_previous_argument(current_arg, _value_number, current_type_str,
                               _list_flag, _values_specs, _clear_flag,
                               values_specs)

    # populate the parser with arguments
    _parser = argparse.ArgumentParser(add_help=False)
    for opt, optspec in _options.items():
        _parser.add_argument(opt, **optspec)
    _args = _parser.parse_args(_values_specs)

    result_dict = {}
    for opt in _options.keys():
        _opt = opt.split('--', 2)[1]
        _opt = _opt.replace('-', '_')
        _value = getattr(_args, _opt)
        result_dict.update({_opt: _value})
    return result_dict