def test_auth_system_success(self, mock_iter_entry_points, mock_request): """Test that we can authenticate using the auth system.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def authenticate(self, cls, auth_url): cls._authenticate(auth_url, {"fake": "me"}) mock_iter_entry_points.side_effect = lambda _t: [ MockEntrypoint("fake", "fake", ["FakePlugin"])] mock_request.side_effect = mock_http_request() auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") cs = client.Client("username", "password", "project_id", "auth_url/v2.0", auth_system="fake", auth_plugin=plugin) cs.client.authenticate() headers = requested_headers(cs) token_url = cs.client.auth_url + "/tokens" mock_request.assert_called_with( "POST", token_url, headers=headers, data='{"fake": "me"}', allow_redirects=True, **self.TEST_REQUEST_BASE)
def test_auth_system_defining_url(self, mock_iter_entry_points): """Test the auth_system defining an url.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self, require=False): return FakePlugin def resolve(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def get_auth_url(self): return "http://faked/v2.0" mock_iter_entry_points.side_effect = lambda _t, name=None: [ MockEntrypoint("fake", "fake", ["FakePlugin"]) ] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") cs = client.Client("username", "password", "project_id", auth_system="fakewithauthurl", auth_plugin=plugin) self.assertEqual("http://faked/v2.0", cs.client.auth_url)
def test_exception_if_no_url(self, mock_iter_entry_points): """Test that no auth_url at all raises exception.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self, require=False): return FakePlugin def resolve(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): pass mock_iter_entry_points.side_effect = lambda _t, name=None: [ MockEntrypoint("fake", "fake", ["FakePlugin"]) ] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") self.assertRaises(exceptions.EndpointNotFound, client.Client, "username", "password", "project_id", auth_system="fake", auth_plugin=plugin)
def test_auth_system_success(self, mock_iter_entry_points, mock_request): """Test that we can authenticate using the auth system.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def authenticate(self, cls, auth_url): cls._authenticate(auth_url, {"fake": "me"}) mock_iter_entry_points.side_effect = lambda _t: [ MockEntrypoint("fake", "fake", ["FakePlugin"]) ] mock_request.side_effect = mock_http_request() auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") cs = client.Client("username", "password", "project_id", "auth_url/v2.0", auth_system="fake", auth_plugin=plugin) cs.client.authenticate() headers = requested_headers(cs) token_url = cs.client.auth_url + "/tokens" mock_request.assert_called_with("POST", token_url, headers=headers, data='{"fake": "me"}', allow_redirects=True, **self.TEST_REQUEST_BASE)
def test_parse_auth_system_options(self, mock_iter_entry_points): """Test that we can parse the auth system options.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self, require=False): return FakePlugin def resolve(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def __init__(self): self.opts = {"fake_argument": True} def parse_opts(self, args): return self.opts mock_iter_entry_points.side_effect = lambda _t, name=None: [ MockEntrypoint("fake", "fake", ["FakePlugin"]) ] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") plugin.parse_opts([]) self.assertIn("fake_argument", plugin.opts)
def connect_to_cloudservers(region=None, **kwargs): """Creates a client for working with cloud servers.""" _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region) mgt_url = _get_service_endpoint("compute", region) cloudservers = None if not mgt_url: # Service is not available return insecure = not get_setting("verify_ssl") cloudservers = _cs_client.Client(identity.username, identity.password, project_id=identity.tenant_id, auth_url=identity.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = identity.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [ image for image in cloudservers.images.list() if not hasattr(image, "server") ] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [ image for image in cloudservers.images.list() if hasattr(image, "server") ] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots return cloudservers
def get_client(environ): rax_env = RaxEnv(**parse_env(environ)) return client.Client( rax_env.username, rax_env.password, rax_env.tenant, rax_env.auth_url, auth_plugin=auth_plugin.load_plugin(rax_env.auth_system), region_name=rax_env.region_name)
def get_clients(): """ Get compute and volume clients from novaclient using envrionment vars. STEAL THIS FUNCTION """ shell = OpenStackComputeShell() parser = shell.get_base_parser() options, args = parser.parse_known_args() kwargs = dict(vars(options)) del kwargs["help"] shell.setup_debugging(kwargs.pop("debug")) # some options can show up under multiple names multi_name_keys = ( ("os_username", "username"), ("os_password", "apikey"), ("os_tenant_name", "projectid"), ("os_auth_url", "url"), ("region_name", "os_region_name"), ) for k, alias in multi_name_keys: val = kwargs.pop(alias, None) if val and not kwargs.get(k): kwargs[k] = val # required args required_args = ("os_compute_api_version", "os_username", "os_password", "os_tenant_name", "os_auth_url") args = [] for arg in required_args: val = kwargs.pop(arg) if not val: raise ClientBuildError("missing required env %s" % arg.upper()) args.append(val) # setup auth systems os_auth_system = kwargs.pop("os_auth_system", None) kwargs["auth_system"] = os_auth_system if os_auth_system and os_auth_system != "keystone": # Discover available auth plugins discover_auth_systems() kwargs["auth_plugin"] = load_plugin(os_auth_system) # invalid args invalid_args = ("os_cacert", "os_user_id", "os_auth_token", "os_tenant_id") for arg in invalid_args: kwargs.pop(arg, None) kwargs["no_cache"] = True kwargs["service_type"] = "compute" compute = Client(*args, **kwargs) # XXX figure out what cinder is doing these days volume = None return compute, volume
def test_plugin(self): # Discover auth plugins auth_plugin.discover_auth_systems() # Make sure rackspace auth was found assert "rackspace" in auth_plugin._discovered_plugins # Get a copy rackspace_auth_plugin = auth_plugin.load_plugin("rackspace") # Make sure the fallback didn't occur assert not isinstance(rackspace_auth_plugin, auth_plugin.DeprecatedAuthPlugin)
def connect_to_cloudservers(region=None, context=None, **kwargs): """Creates a client for working with cloud servers.""" context = context or identity _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region, context=context) mgt_url = _get_service_endpoint(context, "compute", region) cloudservers = None if not mgt_url: # Service is not available return insecure = not get_setting("verify_ssl") cloudservers = _cs_client.Client(context.username, context.password, project_id=context.tenant_id, auth_url=context.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = context.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [image for image in cloudservers.images.list() if not hasattr(image, "server")] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [image for image in cloudservers.images.list() if hasattr(image, "server")] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots cloudservers.identity = identity return cloudservers
def connect_to_cloudservers(region=None): """Creates a client for working with cloud servers.""" _cs_auth_plugin.discover_auth_systems() if default_identity_type and default_identity_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(default_identity_type) else: auth_plugin = None region = safe_region(region) mgt_url = _get_service_endpoint("compute", region) cloudservers = _cs_client.Client(identity.username, identity.api_key, project_id=identity.tenant_name, auth_url=identity.auth_endpoint, auth_system="rackspace", region_name=region, service_type="compute", auth_plugin=auth_plugin, http_log_debug=_http_debug) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = identity.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [ image for image in cloudservers.images.list() if not hasattr(image, "server") ] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [ image for image in cloudservers.images.list() if hasattr(image, "server") ] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots return cloudservers
def test_plugin(self): # Discover auth plugins auth_plugin.discover_auth_systems() # Make sure rackspace auth was found assert 'rackspace' in auth_plugin._discovered_plugins # Get a copy rackspace_auth_plugin = auth_plugin.load_plugin('rackspace') # Make sure the fallback didn't occur assert not isinstance(rackspace_auth_plugin, auth_plugin.DeprecatedAuthPlugin)
def connect_to_cloudservers(region=None): """Creates a client for working with cloud servers.""" _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region) mgt_url = _get_service_endpoint("compute", region) cloudservers = _cs_client.Client(identity.username, identity.password, project_id=identity.tenant_id, auth_url=identity.auth_endpoint, auth_system="rackspace", region_name=region, service_type="compute", auth_plugin=auth_plugin, http_log_debug=_http_debug) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = identity.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [image for image in cloudservers.images.list() if not hasattr(image, "server")] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [image for image in cloudservers.images.list() if hasattr(image, "server")] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots return cloudservers
def test_exception_if_no_url(self, mock_iter_entry_points): """Test that no auth_url at all raises exception.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): pass mock_iter_entry_points.side_effect = lambda _t: [ MockEntrypoint("fake", "fake", ["FakePlugin"])] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") self.assertRaises( exceptions.EndpointNotFound, client.Client, "username", "password", "project_id", auth_system="fake", auth_plugin=plugin)
def test_auth_system_defining_url(self, mock_iter_entry_points): """Test the auth_system defining an url.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def get_auth_url(self): return "http://faked/v2.0" mock_iter_entry_points.side_effect = lambda _t: [ MockEntrypoint("fake", "fake", ["FakePlugin"])] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") cs = client.Client("username", "password", "project_id", auth_system="fakewithauthurl", auth_plugin=plugin) self.assertEquals(cs.client.auth_url, "http://faked/v2.0")
def test_parse_auth_system_options(self, mock_iter_entry_points): """Test that we can parse the auth system options.""" class MockEntrypoint(pkg_resources.EntryPoint): def load(self): return FakePlugin class FakePlugin(auth_plugin.BaseAuthPlugin): def __init__(self): self.opts = {"fake_argument": True} def parse_opts(self, args): return self.opts mock_iter_entry_points.side_effect = lambda _t: [ MockEntrypoint("fake", "fake", ["FakePlugin"])] auth_plugin.discover_auth_systems() plugin = auth_plugin.load_plugin("fake") plugin.parse_opts([]) self.assertIn("fake_argument", plugin.opts)
from novaclient.v1_1 import client os.environ["OS_USERNAME"] os.environ["OS_PASSWORD"] os.environ["OS_NO_CACHE"] os.environ["OS_TENANT_NAME"] os.environ["OS_AUTH_URL"] os.environ["OS_REGION_NAME"] os.environ["OS_AUTH_SYSTEM"] os.environ["NOVA_RAX_AUTH"] os.environ["OS_PROJECT_ID"] from novaclient import auth_plugin as _cs_auth_plugin _cs_auth_plugin.discover_auth_systems() auth_plugin = _cs_auth_plugin.load_plugin("rackspace_uk") cs = client.Client(os.environ["OS_USERNAME"], os.environ["OS_PASSWORD"], os.environ["OS_TENANT_NAME"], auth_url=os.environ["OS_AUTH_URL"], auth_system=os.environ["OS_AUTH_SYSTEM"], region_name=os.environ["OS_REGION_NAME"], service_type="compute", auth_plugin=auth_plugin) help_str=""" the novaclient module has been loaded and INITIATED, try typing one of the commands below to see if it works. #example 1: show all cloud servers under you cloud account l=cs.servers.list()
def connect_to_cloudservers(region=None, context=None, verify_ssl=None, **kwargs): """Creates a client for working with cloud servers.""" context = context or identity _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region, context=context) mgt_url = _get_service_endpoint(context, "compute", region) cloudservers = None if not mgt_url: # Service is not available return if verify_ssl is None: insecure = not get_setting("verify_ssl") else: insecure = not verify_ssl extensions = nc.discover_extensions("1.1") cloudservers = _cs_client.Client(context.username, context.password, project_id=context.tenant_id, auth_url=context.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, extensions=extensions, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = context.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [image for image in cloudservers.images.list() if not hasattr(image, "server")] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [image for image in cloudservers.images.list() if hasattr(image, "server")] def find_images_by_name(expr): """ Returns a list of images whose name contains the specified expression. The value passed is treated as a regular expression, allowing for more specific searches than simple wildcards. The matching is done in a case-insensitive manner. """ return [image for image in cloudservers.images.list() if re.search(expr, image.name, re.I)] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots cloudservers.find_images_by_name = find_images_by_name cloudservers.identity = identity return cloudservers
def connect_to_cloudservers(region=None, context=None, **kwargs): """Creates a client for working with cloud servers.""" context = context or identity _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region, context=context) mgt_url = _get_service_endpoint(context, "compute", region) cloudservers = None if not mgt_url: # Service is not available return insecure = not get_setting("verify_ssl") cs_shell = _cs_shell() extensions = cs_shell._discover_extensions("1.1") cloudservers = _cs_client.Client(context.username, context.password, project_id=context.tenant_id, auth_url=context.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, extensions=extensions, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = context.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [ image for image in cloudservers.images.list() if not hasattr(image, "server") ] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [ image for image in cloudservers.images.list() if hasattr(image, "server") ] def find_images_by_name(expr): """ Returns a list of images whose name contains the specified expression. The value passed is treated as a regular expression, allowing for more specific searches than simple wildcards. The matching is done in a case-insensitive manner. """ return [ image for image in cloudservers.images.list() if re.search(expr, image.name, re.I) ] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots cloudservers.find_images_by_name = find_images_by_name cloudservers.identity = identity return cloudservers