def zunclient(request): ( username, token_id, project_id, endpoint_override ) = get_auth_params_from_request(request) LOG.debug('zunclient connection created using the token "%s" and url' ' "%s"' % (token_id, endpoint_override)) api_version = API_VERSION if API_VERSION.is_latest(): c = zun_client.Client( username=username, project_id=project_id, auth_token=token_id, endpoint_override=endpoint_override, api_version=api_versions.APIVersion("1.1"), ) api_version = api_versions.discover_version(c, api_version) c = zun_client.Client(username=username, project_id=project_id, auth_token=token_id, endpoint_override=endpoint_override, api_version=api_version) return c
def make_client(instance): """Returns a zun service client""" requested_api_version = instance._api_version[API_NAME] zun_client = utils.get_client_class( API_NAME, requested_api_version, API_VERSIONS) LOG.debug("Instantiating zun client: {0}".format( zun_client)) api_version = api_versions.get_api_version(requested_api_version) if api_version.is_latest(): client = zun_client( region_name=instance._region_name, session=instance.session, service_type='container', api_version=api_versions.APIVersion("1.1"), ) api_version = api_versions.discover_version(client, api_version) client = zun_client( region_name=instance._region_name, session=instance.session, service_type='container', api_version=api_version, ) return client
def zunclient(request): (username, token_id, project_id, endpoint_override) = get_auth_params_from_request(request) insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) LOG.debug('zunclient connection created using the token "%s" and url' ' "%s"' % (token_id, endpoint_override)) api_version = API_VERSION if API_VERSION.is_latest(): c = zun_client.Client( username=username, project_id=project_id, auth_token=token_id, endpoint_override=endpoint_override, insecure=insecure, cacert=cacert, api_version=api_versions.APIVersion("1.1"), ) api_version = api_versions.discover_version(c, api_version) c = zun_client.Client(username=username, project_id=project_id, auth_token=token_id, endpoint_override=endpoint_override, insecure=insecure, cacert=cacert, api_version=api_version) return c
def test_server_without_microversion_and_no_version_field(self): fake_client = mock.MagicMock() fake_client.versions.get_current.return_value = versions.Version( None, {}) api_versions.MAX_API_VERSION = "1.11" api_versions.MIN_API_VERSION = "1.1" self.assertEqual( "1.1", api_versions.discover_version( fake_client, api_versions.APIVersion('1.latest')).get_string())
def test_client_end_version_is_the_latest_one(self): fake_client = mock.MagicMock() fake_client.versions.get_current.return_value = mock.MagicMock( max_version="1.16", min_version="1.4") api_versions.MAX_API_VERSION = "1.11" api_versions.MIN_API_VERSION = "1.1" self.assertEqual( "1.11", api_versions.discover_version( fake_client, api_versions.APIVersion('1.latest')).get_string())
def Client(version='1', username=None, auth_url=None, **kwargs): """Initialize client objects based on given version""" _check_arguments(kwargs, 'Queens', 'api_key', right_name='password') # NOTE: OpenStack projects use 2 vars with one meaning: `endpoint_type` # and `interface`. `endpoint_type` is an old name which was used by # most OpenStack clients. Later it was replaced by `interface` in # keystone and later some other clients switched to new var name too. _check_arguments(kwargs, 'Queens', 'endpoint_type', right_name='interface') _check_arguments(kwargs, 'Queens', 'zun_url', right_name='endpoint_override') _check_arguments(kwargs, 'Queens', 'tenant_name', right_name='project_name') _check_arguments(kwargs, 'Queens', 'tenant_id', right_name='project_id') profile = kwargs.pop('profile', None) if osprofiler_profiler and profile: # Initialize the root of the future trace: the created trace ID # will be used as the very first parent to which all related # traces will be bound to. The given HMAC key must correspond to # the one set in zun-api zun.conf, otherwise the latter # will fail to check the request signature and will skip # initialization of osprofiler on the server side. osprofiler_profiler.init(profile) api_version, client_class = _get_client_class_and_version(version) if api_version.is_latest(): c = client_class(api_version=api_versions.APIVersion("1.1"), auth_url=auth_url, username=username, **kwargs) api_version = api_versions.discover_version(c, api_version) return client_class(api_version=api_version, auth_url=auth_url, username=username, **kwargs)
def Client(version='1', username=None, auth_url=None, **kwargs): """Initialize client objects based on given version""" _check_arguments(kwargs, 'Queens', 'api_key', right_name='password') # NOTE: OpenStack projects use 2 vars with one meaning: `endpoint_type` # and `interface`. `endpoint_type` is an old name which was used by # most OpenStack clients. Later it was replaced by `interface` in # keystone and later some other clients switched to new var name too. _check_arguments(kwargs, 'Queens', 'endpoint_type', right_name='interface') _check_arguments(kwargs, 'Queens', 'zun_url', right_name='endpoint_override') _check_arguments(kwargs, 'Queens', 'tenant_name', right_name='project_name') _check_arguments(kwargs, 'Queens', 'tenant_id', right_name='project_id') profile = kwargs.pop('profile', None) if osprofiler_profiler and profile: # Initialize the root of the future trace: the created trace ID # will be used as the very first parent to which all related # traces will be bound to. The given HMAC key must correspond to # the one set in zun-api zun.conf, otherwise the latter # will fail to check the request signature and will skip # initialization of osprofiler on the server side. osprofiler_profiler.init(profile) api_version, client_class = _get_client_class_and_version(version) if api_version.is_latest(): c = client_class(api_version=api_versions.APIVersion("1.1"), auth_url=auth_url, username=username, **kwargs) api_version = api_versions.discover_version(c, api_version) return client_class(api_version=api_version, auth_url=auth_url, username=username, **kwargs)
def main(self, argv): # NOTE(Christoph Jansen): With Python 3.4 argv somehow becomes a Map. # This hack fixes it. argv = list(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 = api_versions.get_api_version(options.zun_api_version) # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse # thinking usage-list --end is ambiguous; but it # works fine with only --endpoint-type present # Go figure. if '--endpoint_type' in argv: spot = argv.index('--endpoint_type') argv[spot] = '--endpoint-type' do_help = "help" in args subcommand_parser = self.get_subcommand_parser( api_version, do_help=do_help) self.parser = subcommand_parser if options.help or not argv: subcommand_parser.print_help() return 0 args = subcommand_parser.parse_args(argv) # Short-circuit and deal with help right away. # NOTE(jamespage): args.func is not guaranteed with python >= 3.4 if not hasattr(args, 'func') or 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_project_name, os_project_id, os_user_domain_id, os_user_domain_name, os_project_domain_id, os_project_domain_name, os_auth_url, os_auth_system, endpoint_type, service_type, bypass_url, insecure, os_cacert) = ( (args.os_username, args.os_project_name, args.os_project_id, args.os_user_domain_id, args.os_user_domain_name, args.os_project_domain_id, args.os_project_domain_name, args.os_auth_url, args.os_auth_system, args.endpoint_type, args.service_type, args.bypass_url, args.insecure, args.os_cacert) ) if os_auth_system and os_auth_system != "keystone": auth_plugin = auth.load_plugin(os_auth_system) else: auth_plugin = None # Fetched and set later as needed os_password = None if not endpoint_type: endpoint_type = DEFAULT_ENDPOINT_TYPE if not service_type: service_type = DEFAULT_SERVICE_TYPE # NA - there is only one service this CLI accesses # 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 cliutils.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 username " "via either --os-username or " "env[OS_USERNAME]") if not os_project_name and not os_project_id: raise exc.CommandError("You must provide a project name " "or project id via --os-project-name, " "--os-project-id, env[OS_PROJECT_NAME] " "or env[OS_PROJECT_ID]") 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 auth url " "via either --os-auth-url or " "env[OS_AUTH_URL] or specify an " "auth_system which defines a " "default url with --os-auth-system " "or env[OS_AUTH_SYSTEM]") # NOTE: The Zun client authenticates when you create it. So instead of # creating here and authenticating later, which is what the # novaclient does, we just create the client later. # Now check for the password/token of which pieces of the # identifying keyring key can come from the underlying client if not cliutils.isunauthenticated(args.func): # NA - Client can't be used with SecretsHelper if (auth_plugin and auth_plugin.opts and "os_password" not in auth_plugin.opts): use_pw = False else: use_pw = True if use_pw: # Auth using token must have failed or not happened # at all, so now switch to password mode and save # the token when its gotten... using our keyring # saver os_password = args.os_password if not os_password: raise exc.CommandError( 'Expecting a password provided via either ' '--os-password, env[OS_PASSWORD], or ' 'prompted response') client = base_client if not do_help: if api_version.is_latest(): # This client is just used to discover api version. # Version API needn't microversion, so we just pass # version 1.1 at here. self.cs = client.Client( version=api_versions.APIVersion("1.1"), username=os_username, password=os_password, project_id=os_project_id, project_name=os_project_name, user_domain_id=os_user_domain_id, user_domain_name=os_user_domain_name, project_domain_id=os_project_domain_id, project_domain_name=os_project_domain_name, auth_url=os_auth_url, service_type=service_type, region_name=args.os_region_name, endpoint_override=bypass_url, interface=endpoint_type, insecure=insecure, cacert=os_cacert) api_version = api_versions.discover_version(self.cs, api_version) min_version = api_versions.APIVersion(api_versions.MIN_API_VERSION) max_version = api_versions.APIVersion(api_versions.MAX_API_VERSION) if not api_version.matches(min_version, max_version): raise exc.CommandError( _("The specified version isn't supported by " "client. The valid version range is '%(min)s' " "to '%(max)s'") % { "min": min_version.get_string(), "max": max_version.get_string()} ) kwargs = {} if profiler: kwargs["profile"] = args.profile self.cs = client.Client(version=api_version, username=os_username, password=os_password, project_id=os_project_id, project_name=os_project_name, user_domain_id=os_user_domain_id, user_domain_name=os_user_domain_name, project_domain_id=os_project_domain_id, project_domain_name=os_project_domain_name, auth_url=os_auth_url, service_type=service_type, region_name=args.os_region_name, endpoint_override=bypass_url, interface=endpoint_type, insecure=insecure, cacert=os_cacert, **kwargs) args.func(self.cs, args) if profiler and args.profile: trace_id = profiler.get().get_base_id() print("To display trace use the command:\n\n" " osprofiler trace show --html %s " % trace_id)
def main(self, argv): # NOTE(Christoph Jansen): With Python 3.4 argv somehow becomes a Map. # This hack fixes it. argv = list(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 = api_versions.get_api_version(options.zun_api_version) # NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse # thinking usage-list --end is ambiguous; but it # works fine with only --endpoint-type present # Go figure. if '--endpoint_type' in argv: spot = argv.index('--endpoint_type') argv[spot] = '--endpoint-type' do_help = "help" in args subcommand_parser = self.get_subcommand_parser( api_version, do_help=do_help) self.parser = subcommand_parser if options.help or not argv: subcommand_parser.print_help() return 0 args = subcommand_parser.parse_args(argv) # Short-circuit and deal with help right away. # NOTE(jamespage): args.func is not guaranteed with python >= 3.4 if not hasattr(args, 'func') or 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_project_name, os_project_id, os_user_domain_id, os_user_domain_name, os_project_domain_id, os_project_domain_name, os_auth_url, os_auth_system, endpoint_type, service_type, bypass_url, insecure, os_cacert, os_cert, os_key) = ( (args.os_username, args.os_project_name, args.os_project_id, args.os_user_domain_id, args.os_user_domain_name, args.os_project_domain_id, args.os_project_domain_name, args.os_auth_url, args.os_auth_system, args.endpoint_type, args.service_type, args.bypass_url, args.insecure, args.os_cacert, args.os_cert, args.os_key) ) if os_auth_system and os_auth_system != "keystone": auth_plugin = auth.load_plugin(os_auth_system) else: auth_plugin = None # Fetched and set later as needed os_password = None if not endpoint_type: endpoint_type = DEFAULT_ENDPOINT_TYPE if not service_type: service_type = DEFAULT_SERVICE_TYPE # NA - there is only one service this CLI accesses # 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 cliutils.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 username " "via either --os-username or " "env[OS_USERNAME]") if not os_project_name and not os_project_id: raise exc.CommandError("You must provide a project name " "or project id via --os-project-name, " "--os-project-id, env[OS_PROJECT_NAME] " "or env[OS_PROJECT_ID]") 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 auth url " "via either --os-auth-url or " "env[OS_AUTH_URL] or specify an " "auth_system which defines a " "default url with --os-auth-system " "or env[OS_AUTH_SYSTEM]") # NOTE: The Zun client authenticates when you create it. So instead of # creating here and authenticating later, which is what the # novaclient does, we just create the client later. # Now check for the password/token of which pieces of the # identifying keyring key can come from the underlying client if not cliutils.isunauthenticated(args.func): # NA - Client can't be used with SecretsHelper if (auth_plugin and auth_plugin.opts and "os_password" not in auth_plugin.opts): use_pw = False else: use_pw = True if use_pw: # Auth using token must have failed or not happened # at all, so now switch to password mode and save # the token when its gotten... using our keyring # saver os_password = args.os_password if not os_password: raise exc.CommandError( 'Expecting a password provided via either ' '--os-password, env[OS_PASSWORD], or ' 'prompted response') client = base_client if not do_help: if api_version.is_latest(): # This client is just used to discover api version. # Version API needn't microversion, so we just pass # version 1.1 at here. self.cs = client.Client( version=api_versions.APIVersion("1.1"), username=os_username, password=os_password, project_id=os_project_id, project_name=os_project_name, user_domain_id=os_user_domain_id, user_domain_name=os_user_domain_name, project_domain_id=os_project_domain_id, project_domain_name=os_project_domain_name, auth_url=os_auth_url, service_type=service_type, region_name=args.os_region_name, endpoint_override=bypass_url, interface=endpoint_type, insecure=insecure, cacert=os_cacert) api_version = api_versions.discover_version(self.cs, api_version) min_version = api_versions.APIVersion(api_versions.MIN_API_VERSION) max_version = api_versions.APIVersion(api_versions.MAX_API_VERSION) if not api_version.matches(min_version, max_version): raise exc.CommandError( _("The specified version isn't supported by " "client. The valid version range is '%(min)s' " "to '%(max)s'") % { "min": min_version.get_string(), "max": max_version.get_string()} ) kwargs = {} if profiler: kwargs["profile"] = args.profile self.cs = client.Client(version=api_version, username=os_username, password=os_password, project_id=os_project_id, project_name=os_project_name, user_domain_id=os_user_domain_id, user_domain_name=os_user_domain_name, project_domain_id=os_project_domain_id, project_domain_name=os_project_domain_name, auth_url=os_auth_url, service_type=service_type, region_name=args.os_region_name, endpoint_override=bypass_url, interface=endpoint_type, insecure=insecure, cacert=os_cacert, cert=os_cert, key=os_key, **kwargs) args.func(self.cs, args) if profiler and args.profile: trace_id = profiler.get().get_base_id() print("To display trace use the command:\n\n" " osprofiler trace show --html %s " % trace_id)