Skip to content

Commit

Permalink
add tenant config parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Snow <chsnow123@gmail.com>
  • Loading branch information
snowch committed Jul 22, 2020
1 parent a291177 commit dc1a111
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
38 changes: 37 additions & 1 deletion hpecp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ContainerPlatformClient(object):
#ssl-cert-verification"
warn_ssl : bool
Disable ssl warnings
tenant : str (optional)
The tenant ID, e.g. /api/v1/tenant/2
Returns
-------
Expand Down Expand Up @@ -132,7 +134,10 @@ def create_from_config_file(
[demoserver]
username = admin
password = admin123
tenant = /api/v1/tenant/2
"""
_log = Logger.get_logger()

if profile is None:
profile = "default"

Expand Down Expand Up @@ -196,11 +201,26 @@ def create_from_config_file(
"the default section".format(profile)
)

# tenant parameter is optional

def get_config_value(key, profile):
if key in config[profile]:
_log.debug("Found '{}' in profile '{}'".format(key, profile))
return config[profile][key]
else:
return config["default"][key]
try:
val = config["default"][key]
_log.debug(
"Found '{}' in profile '{}'".format(key, "default")
)
return val
except Exception:
_log.debug(
"Could not find '{}' in profile '{}'".format(
key, profile
)
)
return None

username = str(get_config_value("username", profile))
password = str(get_config_value("password", profile))
Expand All @@ -210,6 +230,9 @@ def get_config_value(key, profile):
verify_ssl = str(get_config_value("verify_ssl", profile))
warn_ssl = str(get_config_value("warn_ssl", profile))

# optional parameter
tenant = get_config_value("tenant", profile)

if use_ssl == "False":
use_ssl = False
else:
Expand All @@ -232,6 +255,7 @@ def get_config_value(key, profile):
use_ssl,
verify_ssl,
warn_ssl,
tenant,
)

@classmethod
Expand All @@ -247,6 +271,7 @@ def create_from_env(cls):
HPECP_USE_SSL
HPECP_VERIFY_SSL
HPECP_WARN_SSL
HPECP_TENANT
See Also
--------
Expand All @@ -263,6 +288,9 @@ def create_from_env(cls):
HPECP_USE_SSL = ast.literal_eval(os.environ["HPECP_USE_SSL"])
HPECP_VERIFY_SSL = ast.literal_eval(os.environ["HPECP_VERIFY_SSL"])
HPECP_WARN_SSL = ast.literal_eval(os.environ["HPECP_WARN_SSL"])

# Optional parameter
HPECP_TENANT = os.getenv("HPECP_TENANT", default=None)
except KeyError as ke:
raise ContainerPlatformClientException(
"Required env var '{}' not found.".format(ke.args[0])
Expand All @@ -279,6 +307,7 @@ def create_from_env(cls):
use_ssl=HPECP_USE_SSL,
verify_ssl=HPECP_VERIFY_SSL,
warn_ssl=HPECP_WARN_SSL,
tenant=HPECP_TENANT,
)

def __init__(
Expand All @@ -290,6 +319,7 @@ def __init__(
use_ssl=True,
verify_ssl=True,
warn_ssl=False,
tenant=None,
):
"""Create a Client object for interacting with HPE Container Platform.
Expand All @@ -310,6 +340,8 @@ def __init__(
#ssl-cert-verification", by default True
warn_ssl : bool, optional
Disable ssl warnings, by default False
tenant : str, optional
The tenant ID, e.g. /api/v1/tenant/2
"""
self._log = Logger.get_logger()

Expand Down Expand Up @@ -359,6 +391,7 @@ def __init__(
self.use_ssl = use_ssl
self.verify_ssl = verify_ssl
self.warn_ssl = warn_ssl
self.tenant_config = tenant

if self.use_ssl:
scheme = "https"
Expand Down Expand Up @@ -400,6 +433,9 @@ def create_session(self):
url = self.base_url + "/api/v1/login"
auth = {"name": self.username, "password": self.password}

if self.tenant_config:
auth["tenant"] = self.tenant_config

if self.warn_ssl is False:
import urllib3

Expand Down
11 changes: 9 additions & 2 deletions tests/library/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):
password = admin123
[tenant1]
api_host = tenant_mock_host
username = tenant-admin
password = tenant-password"""
password = tenant-password
tenant = /api/v1/tenant/2"""
).encode("utf8")

if six.PY2:
Expand All @@ -199,10 +201,15 @@ def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):

hpecp_cli = self.cli.get_client(start_session=False)

self.assertEqual(hpecp_cli.api_host, "mock_host")
# this should be from the [default] section
self.assertEqual(hpecp_cli.api_port, 9999)

# this should be from the [tenant1] section
self.assertEqual(hpecp_cli.api_host, "tenant_mock_host")
self.assertEqual(hpecp_cli.username, "tenant-admin")
self.assertEqual(
hpecp_cli.tenant_config, "/api/v1/tenant/2"
)


class TestBaseProxy(BaseTestCase):
Expand Down

0 comments on commit dc1a111

Please sign in to comment.