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 = super(GnocchiShell, self).build_option_parser( description, version, argparse_kwargs={'allow_abbrev': False}) parser.add_argument('--gnocchi-api-version', default=os.environ.get('GNOCCHI_API_VERSION', '1'), help='Defaults to env[GNOCCHI_API_VERSION] or 1.') # NOTE(jd) This is a workaroun for people using Keystone auth with the # CLI. A lot of rc files do not export OS_AUTH_TYPE=password and # assumes it is the default. It's not in that case, but since we can't # fix all the rc files of the world, workaround it here. if ("OS_AUTH_PASSWORD" in os.environ and "OS_AUTH_TYPE" not in os.environ): os.environ.set("OS_AUTH_TYPE", "password") loading.register_session_argparse_arguments(parser=parser) plugin = loading.register_auth_argparse_arguments( parser=parser, argv=sys.argv, default="gnocchi-basic") if not isinstance(plugin, (auth.GnocchiNoAuthLoader, auth.GnocchiBasicLoader)): adapter.register_adapter_argparse_arguments(parser=parser, service_type="metric") adapter.register_service_adapter_argparse_arguments( parser=parser, service_type="metric") parser.add_argument( '--endpoint', default=os.environ.get('GNOCCHI_ENDPOINT'), help='Gnocchi endpoint (Env: GNOCCHI_ENDPOINT). ' 'Deprecated, use --os-endpoint-override and ' 'OS_ENDPOINT_OVERRIDE instead') return parser
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 = super(GnocchiShell, self).build_option_parser( description, version, argparse_kwargs={'allow_abbrev': False}) parser.add_argument( '--gnocchi-api-version', default=os.environ.get('GNOCCHI_API_VERSION', '1'), help='Defaults to env[GNOCCHI_API_VERSION] or 1.') loading.register_session_argparse_arguments(parser=parser) plugin = loading.register_auth_argparse_arguments( parser=parser, argv=sys.argv, default="gnocchi-basic") if not isinstance(plugin, (auth.GnocchiNoAuthLoader, auth.GnocchiBasicLoader)): adapter.register_adapter_argparse_arguments( parser=parser, service_type="metric") adapter.register_service_adapter_argparse_arguments( parser=parser, service_type="metric") parser.add_argument( '--endpoint', default=os.environ.get('GNOCCHI_ENDPOINT'), help='Gnocchi endpoint (Env: GNOCCHI_ENDPOINT). ' 'Deprecated, use --os-endpoint-override and ' 'OS_ENDPOINT_OVERRIDE instead') return parser
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 = super(GnocchiShell, self).build_option_parser( description, version, argparse_kwargs={'allow_abbrev': False}) parser.add_argument( '--gnocchi-api-version', default=os.environ.get('GNOCCHI_API_VERSION', '1'), help='Defaults to env[GNOCCHI_API_VERSION] or 1.') loading.register_session_argparse_arguments(parser=parser) plugin = loading.register_auth_argparse_arguments( parser=parser, argv=sys.argv, default="gnocchi-basic") if not isinstance(plugin, (auth.GnocchiNoAuthLoader, auth.GnocchiBasicLoader)): adapter.register_adapter_argparse_arguments( parser=parser, service_type="metric") adapter.register_service_adapter_argparse_arguments( parser=parser, service_type="metric") parser.add_argument( '--endpoint', default=os.environ.get('GNOCCHI_ENDPOINT'), help='Gnocchi endpoint (Env: GNOCCHI_ENDPOINT). ' 'Deprecated, use --os-endpoint-override and ' 'OS_ENDPOINT_OVERRIDE instead') return parser
def register_service_argparse_arguments(*args, **kwargs): return adapter.register_service_adapter_argparse_arguments(*args, **kwargs)
def register_argparse_arguments(self, parser, argv, service_keys=None): """Register all of the common argparse options needed. Given an argparse parser, register the keystoneauth Session arguments, the keystoneauth Auth Plugin Options and os-cloud. Also, peek in the argv to see if all of the auth plugin options should be registered or merely the ones already configured. :param argparse.ArgumentParser: parser to attach argparse options to :param list argv: the arguments provided to the application :param string service_keys: Service or list of services this argparse should be specialized for, if known. The first item in the list will be used as the default value for service_type (optional) :raises exceptions.OpenStackConfigException if an invalid auth-type is requested """ if service_keys is None: service_keys = [] # Fix argv in place - mapping any keys with embedded _ in them to - _fix_argv(argv) local_parser = argparse_mod.ArgumentParser(add_help=False) for p in (parser, local_parser): p.add_argument('--os-cloud', metavar='<name>', default=os.environ.get('OS_CLOUD', None), help='Named cloud to connect to') # we need to peek to see if timeout was actually passed, since # the keystoneauth declaration of it has a default, which means # we have no clue if the value we get is from the ksa default # for from the user passing it explicitly. We'll stash it for later local_parser.add_argument('--timeout', metavar='<timeout>') # We need for get_one_cloud to be able to peek at whether a token # was passed so that we can swap the default from password to # token if it was. And we need to also peek for --os-auth-token # for novaclient backwards compat local_parser.add_argument('--os-token') local_parser.add_argument('--os-auth-token') # Peek into the future and see if we have an auth-type set in # config AND a cloud set, so that we know which command line # arguments to register and show to the user (the user may want # to say something like: # openstack --os-cloud=foo --os-oidctoken=bar # although I think that user is the cause of my personal pain options, _args = local_parser.parse_known_args(argv) if options.timeout: self._argv_timeout = True # validate = False because we're not _actually_ loading here # we're only peeking, so it's the wrong time to assert that # the rest of the arguments given are invalid for the plugin # chosen (for instance, --help may be requested, so that the # user can see what options he may want to give cloud = self.get_one_cloud(argparse=options, validate=False) default_auth_type = cloud.config['auth_type'] try: loading.register_auth_argparse_arguments(parser, argv, default=default_auth_type) except Exception: # Hidiing the keystoneauth exception because we're not actually # loading the auth plugin at this point, so the error message # from it doesn't actually make sense to os-client-config users options, _args = parser.parse_known_args(argv) plugin_names = loading.get_available_plugin_names() raise exceptions.OpenStackConfigException( "An invalid auth-type was specified: {auth_type}." " Valid choices are: {plugin_names}.".format( auth_type=options.os_auth_type, plugin_names=",".join(plugin_names))) if service_keys: primary_service = service_keys[0] else: primary_service = None loading.register_session_argparse_arguments(parser) adapter.register_adapter_argparse_arguments( parser, service_type=primary_service) for service_key in service_keys: # legacy clients have un-prefixed api-version options parser.add_argument('--{service_key}-api-version'.format( service_key=service_key.replace('_', '-'), help=argparse_mod.SUPPRESS)) adapter.register_service_adapter_argparse_arguments( parser, service_type=service_key) # Backwards compat options for legacy clients parser.add_argument('--http-timeout', help=argparse_mod.SUPPRESS) parser.add_argument('--os-endpoint-type', help=argparse_mod.SUPPRESS) parser.add_argument('--endpoint-type', help=argparse_mod.SUPPRESS)
def register_argparse_arguments(self, parser, argv, service_keys=[]): """Register all of the common argparse options needed. Given an argparse parser, register the keystoneauth Session arguments, the keystoneauth Auth Plugin Options and os-cloud. Also, peek in the argv to see if all of the auth plugin options should be registered or merely the ones already configured. :param argparse.ArgumentParser: parser to attach argparse options to :param list argv: the arguments provided to the application :param string service_keys: Service or list of services this argparse should be specialized for, if known. The first item in the list will be used as the default value for service_type (optional) :raises exceptions.OpenStackConfigException if an invalid auth-type is requested """ local_parser = argparse_mod.ArgumentParser(add_help=False) for p in (parser, local_parser): p.add_argument( '--os-cloud', metavar='<name>', default=os.environ.get('OS_CLOUD', None), help='Named cloud to connect to') # we need to peek to see if timeout was actually passed, since # the keystoneauth declaration of it has a default, which means # we have no clue if the value we get is from the ksa default # for from the user passing it explicitly. We'll stash it for later local_parser.add_argument('--timeout', metavar='<timeout>') # Peek into the future and see if we have an auth-type set in # config AND a cloud set, so that we know which command line # arguments to register and show to the user (the user may want # to say something like: # openstack --os-cloud=foo --os-oidctoken=bar # although I think that user is the cause of my personal pain options, _args = local_parser.parse_known_args(argv) if options.timeout: self._argv_timeout = True # validate = False because we're not _actually_ loading here # we're only peeking, so it's the wrong time to assert that # the rest of the arguments given are invalid for the plugin # chosen (for instance, --help may be requested, so that the # user can see what options he may want to give cloud = self.get_one_cloud(argparse=options, validate=False) default_auth_type = cloud.config['auth_type'] try: loading.register_auth_argparse_arguments( parser, argv, default=default_auth_type) except Exception: # Hidiing the keystoneauth exception because we're not actually # loading the auth plugin at this point, so the error message # from it doesn't actually make sense to os-client-config users options, _args = parser.parse_known_args(argv) plugin_names = loading.get_available_plugin_names() raise exceptions.OpenStackConfigException( "An invalid auth-type was specified: {auth_type}." " Valid choices are: {plugin_names}.".format( auth_type=options.os_auth_type, plugin_names=",".join(plugin_names))) if service_keys: primary_service = service_keys[0] else: primary_service = None loading.register_session_argparse_arguments(parser) adapter.register_adapter_argparse_arguments( parser, service_type=primary_service) for service_key in service_keys: # legacy clients have un-prefixed api-version options parser.add_argument( '--{service_key}-api-version'.format( service_key=service_key.replace('_', '-'), help=argparse_mod.SUPPRESS)) adapter.register_service_adapter_argparse_arguments( parser, service_type=service_key) # Backwards compat options for legacy clients parser.add_argument('--http-timeout', help=argparse_mod.SUPPRESS) parser.add_argument('--os-endpoint-type', help=argparse_mod.SUPPRESS) parser.add_argument('--endpoint-type', help=argparse_mod.SUPPRESS)