def test_tenant_list(self, mock_get, mock_post):

        client = ContainerPlatformClient(
            username="******",
            password="******",
            api_host="127.0.0.1",
            api_port=8080,
            use_ssl=True,
        )

        # Makes POST Request: https://127.0.0.1:8080/api/v1/login
        client.create_session()

        # Makes GET Request: https://127.0.0.1:8080/api/v1/tenant
        tenants = client.tenant.list()

        # Test that json response is saved in each Tenant object
        self.assertIsNotNone(client.tenant.list()[0].json)

        # Test TenantList subscriptable access and Tenant property setters
        self.assertEqual(tenants[0].id, "/api/v1/tenant/1")
        self.assertEqual(tenants[0].status, "ready")
        self.assertEqual(tenants[0].name, "Site Admin")
        self.assertEqual(tenants[0].description,
                         "Site Admin Tenant for BlueData clusters")

        # Test TenantList iterators
        self.assertEqual(
            [tenant.id for tenant in client.tenant.list()],
            ["/api/v1/tenant/1", "/api/v1/tenant/2"],
        )
    def test_get_k8shosts(self, mock_get, mock_post):

        client = ContainerPlatformClient(
                                username='******', 
                                password='******', 
                                api_host='127.0.0.1', 
                                api_port=8080,
                                use_ssl=True)

        # Makes POST Request: https://127.0.0.1:8080/api/v1/login
        client.create_session()

        # Makes GET Request: https://127.0.0.1:8080/api/v2/worker/k8shost/
        workers = client.k8s_worker.list()

        # Test that json response is saved in each WorkerK8s object
        assert client.k8s_worker.list()[0].json is not None

        # Test WorkerK8sList subscriptable access and property setters
        assert workers[0].worker_id == 4
        assert workers[0].status == WorkerK8sStatus.unlicensed.name
        assert workers[0].hostname == 'ip-10-1-0-238.eu-west-2.compute.internal'
        assert workers[0].ipaddr == '10.1.0.238'
        assert workers[0].href == '/api/v2/worker/k8shost/4'

        # Test WorkerK8sList iterators
        assert [ worker.worker_id for worker in client.k8s_worker.list() ] == [ 4, 5 ]
示例#3
0
    def test_epic_tenant_list(self, mock_post):

        client = ContainerPlatformClient(
            username="******",
            password="******",
            api_host="127.0.0.1",
            api_port=8080,
            use_ssl=True,
        )
        client.create_session()

        client.config.auth(
            {
                "external_identity_server": {
                    "bind_pwd": "5ambaPwd@",
                    "user_attribute": "sAMAccountName",
                    "bind_type": "search_bind",
                    "bind_dn": "cn=Administrator,CN=Users,DC=samdom,DC=example,DC=com",
                    "host": "1.1.1.1",
                    "security_protocol": "ldaps",
                    "base_dn": "CN=Users,DC=samdom,DC=example,DC=com",
                    "verify_peer": False,
                    "type": "Active Directory",
                    "port": 636,
                }
            }
        )
    def test_get_users(self, mock_get, mock_post):
        client = ContainerPlatformClient(
            username="******",
            password="******",
            api_host="127.0.0.1",
            api_port=8080,
            use_ssl=True,
        )

        # Makes POST Request: https://127.0.0.1:8080/api/v1/login
        client.create_session()

        # Makes GET Request: https://127.0.0.1:8080/api/v1/user
        users = client.user.list()

        # Test that json response is saved in each WorkerK8s object
        assert users[0].json is not None

        # Test UserList subscriptable access and property setters
        assert users[0].is_service_account is False
        assert users[0].is_siteadmin is False
        assert users[0].default_tenant == ""
        assert users[0].is_external is False
        assert users[0].is_group_added_user is False

        assert users[0].name == "csnow"
        assert users[0].description == "chris"
        assert users[0]._links == {"self": {"href": "/api/v1/user/16"}}
    def test_auth_ssl(self, mock_post):

        client = ContainerPlatformClient(username='******',
                                         password='******',
                                         api_host='127.0.0.1',
                                         api_port=8080,
                                         use_ssl=True)
        client.create_session()
示例#6
0
def get_client():
        client = ContainerPlatformClient(
                                username='******', 
                                password='******', 
                                api_host='127.0.0.1', 
                                api_port=8080,
                                use_ssl=True)
        client.create_session()
        return client
    def test_create_from_env_var_factory_method_with_missing_env_values(self):

        try:
            ContainerPlatformClient.create_from_env()
        except ContainerPlatformClientException as expected:
            self.assertEqual(
                expected.message,
                "Required env var 'HPECP_PASSWORD' not found.",
            )
    def test_create_from_env_var_factory_method_with_type_error(self):

        try:
            ContainerPlatformClient.create_from_env()
        except ContainerPlatformClientException as expected:
            self.assertEqual(
                expected.message,
                "invalid literal for int() with base 10: 'not_an_int'",
            )
    def test_create_session(self, mock_post):

        client = ContainerPlatformClient(username='******',
                                         password='******',
                                         api_host='127.0.0.1',
                                         api_port=8080,
                                         use_ssl=False)

        self.assertIsInstance(client.create_session(), ContainerPlatformClient)
示例#10
0
    def test_auth_ssl(self, mock_post):

        client = ContainerPlatformClient(
            username="******",
            password="******",
            api_host="127.0.0.1",
            api_port=8080,
            use_ssl=True,
        )
        client.create_session()
示例#11
0
    def test_auth_ssl_with_error(self, mock_post):

        client = ContainerPlatformClient(username='******',
                                         password='******',
                                         api_host='127.0.0.1',
                                         api_port=8080,
                                         use_ssl=True)

        with self.assertRaises(requests.exceptions.HTTPError):
            client.create_session()
def get_client():
    client = ContainerPlatformClient(
        username="******",
        password="******",
        api_host="127.0.0.1",
        api_port=8080,
        use_ssl=True,
    )
    client.create_session()
    return client
示例#13
0
 def mock_get_client():
     client = ContainerPlatformClient(
         username="",
         password="",
         api_host="",
         api_port=9090,
         use_ssl=True,
         verify_ssl=True,
         warn_ssl=True,
     )
     client.session_id = "ABC"
     client.k8s_worker.get = mock_k8s_worker_get
     client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key
     return client
示例#14
0
    def test_create_from_config_file_factory_method(self):
        file_data = dedent("""[default]
                              api_host = 127.0.0.1
                              api_port = 8080
                              use_ssl = True
                              verify_ssl = False
                              warn_ssl = True
                              username = admin
                              password = admin123""")

        tmp = tempfile.NamedTemporaryFile(delete=True)
        try:
            tmp.write(file_data.encode('utf-8'))
            tmp.flush()

            client = ContainerPlatformClient.create_from_config_file(
                config_file=tmp.name)
            self.assertEqual(client.username, 'admin')
            self.assertEqual(client.password, 'admin123')
            self.assertEqual(client.api_host, '127.0.0.1')
            self.assertEqual(client.api_port, 8080)
            self.assertEqual(client.use_ssl, True)
            self.assertEqual(client.verify_ssl, False)
            self.assertEqual(client.warn_ssl, True)
        finally:
            tmp.close()
def get_client(start_session=True):
    """Retrieve a reference to an authenticated client object."""
    client = ContainerPlatformClient.create_from_config_file(
        config_file=get_config_file(),
        profile=get_profile(),
    )
    if start_session:
        client.create_session()
    return client
示例#16
0
def get_client():
    """Utility function to retrieve an authenticated client object"""
    try:
        client = ContainerPlatformClient.create_from_config_file(
            config_file=HPECP_CONFIG_FILE, profile=PROFILE)
        client.create_session()
        return client
    except APIException as e:
        print(e.message)
        sys.exit(1)
    def test_create_from_env_var_factory_method_with_falses(self):

        client = ContainerPlatformClient.create_from_env()

        self.assertEqual(client.username, "test_username")
        self.assertEqual(client.password, "test_password")
        self.assertEqual(client.api_host, "test_apihost")
        self.assertEqual(client.api_port, 8080)
        self.assertEqual(client.use_ssl, False)
        self.assertEqual(client.verify_ssl, False)
        self.assertEqual(client.warn_ssl, False)
示例#18
0
    def test_create_session_chained(self, mock_post):

        client = ContainerPlatformClient(
            username="******",
            password="******",
            api_host="127.0.0.1",
            api_port=8080,
            use_ssl=False,
        ).create_session()

        self.assertIsInstance(client, ContainerPlatformClient)
示例#19
0
def version(debug=False):
    """Display version information."""
    print("HPECP Version:   " + ContainerPlatformClient.version())

    if debug:
        print("HPECP Bin Path:  " + os.path.dirname(os.path.abspath(__file__)))
        print("HPECP Lib Path:  " +
              os.path.dirname(os.path.abspath(hpecp.__file__)))
        print("Python Version:  " + sys.version.replace("\n", ""))
        print("Python Exe Path: " + sys.executable)
        print("Python Path:     " + os.pathsep.join(sys.path))
        print("System Path:     " + os.environ.get("PATH", ""))
示例#20
0
#!/usr/bin/env python3

from hpecp import ContainerPlatformClient
import time

client = ContainerPlatformClient(username='******',
                                 password='******',
                                 api_host='127.0.0.1',
                                 api_port=8080,
                                 use_ssl=True,
                                 verify_ssl='/certs/hpecp-ca-cert.pem')

client.create_session()

license = client.license.get_license()
print(license)

print(client.license.delete_license(license['Licenses'][0]['LicenseKey']))
time.sleep(5)

license = client.license.get_license()
print(license)
示例#21
0
def configure_cli():

    controller_api_host = None
    controller_api_port = None
    controller_use_ssl = None
    controller_verify_ssl = None
    controller_warn_ssl = None
    controller_username = None
    controller_password = None

    config_path = os.path.join(os.path.expanduser("~"), ".hpecp.conf",)

    if os.path.exists(config_path):
        config_reader = ContainerPlatformClient.create_from_config_file()
        controller_api_host = config_reader.api_host
        controller_api_port = config_reader.api_port
        controller_use_ssl = config_reader.use_ssl
        controller_verify_ssl = config_reader.verify_ssl
        controller_warn_ssl = config_reader.warn_ssl
        controller_username = config_reader.username
        controller_password = config_reader.password

    sys.stdout.write("Controller API Host [{}]: ".format(controller_api_host))
    tmp = input()
    if tmp != "":
        controller_api_host = tmp

    sys.stdout.write("Controller API Port [{}]: ".format(controller_api_port))
    tmp = input()
    if tmp != "":
        controller_api_port = tmp

    sys.stdout.write(
        "Controller uses ssl (True|False) [{}]: ".format(controller_use_ssl)
    )
    tmp = input()
    if tmp != "":
        controller_use_ssl = tmp

    sys.stdout.write(
        "Controller verify ssl (True|False) [{}]: ".format(
            controller_verify_ssl
        )
    )
    tmp = input()
    if tmp != "":
        controller_verify_ssl = tmp

    sys.stdout.write(
        "Controller warn ssl (True|False) [{}]: ".format(controller_warn_ssl)
    )
    tmp = input()
    if tmp != "":
        controller_warn_ssl = tmp

    sys.stdout.write("Controller Username [{}]: ".format(controller_username))
    tmp = input()
    if tmp != "":
        controller_username = tmp

    sys.stdout.write("Controller Password [{}]: ".format(controller_password))
    tmp = input()
    if tmp != "":
        controller_password = tmp

    config = configparser.ConfigParser()
    config["default"] = OrderedDict()
    config["default"]["api_host"] = controller_api_host
    config["default"]["api_port"] = str(controller_api_port)
    config["default"]["use_ssl"] = str(controller_use_ssl)
    config["default"]["verify_ssl"] = str(controller_verify_ssl)
    config["default"]["warn_ssl"] = str(controller_warn_ssl)
    config["default"]["username"] = controller_username
    config["default"]["password"] = controller_password

    with open(config_path, "w",) as config_file:
        config.write(config_file)
try:
    with open('./generated/output.json') as f:
        j = json.load(f)
except:
    print(80 * "*")
    print("ERROR: Can't parse: './generated/output.json'")
    print(80 * "*")
    sys.exit(1)

controller_public_ip = j["controller_public_ip"]["value"]
ad_server_private_ip = j["ad_server_private_ip"]["value"]

client = ContainerPlatformClient(username='******',
                                 password='******',
                                 api_host=controller_public_ip,
                                 api_port=8080,
                                 use_ssl=True,
                                 verify_ssl=False)

client.create_session()

print("*" * 80)
print("Current License:\n" + str(client.license.get_license()))
print("*" * 80)
print("Platform ID: " + client.license.get_platform_id())
print("*" * 80)
lic = input("Paste License Text: ")

with open('./generated/LICENSE', 'w') as out_file:
    out_file.write(lic)
#!/usr/bin/env python3

from hpecp import ContainerPlatformClient
import os
os.environ["LOG_LEVEL"] = "INFO"

# Disable the SSL warnings - don't do this on productions!  
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

client = ContainerPlatformClient(username='******', 
                                password='******', 
                                api_host='127.0.0.1', 
                                api_port=8080,
                                use_ssl=True,
                                verify_ssl=False)

client.create_session()

host = client.worker.get_gateways()[0]
print("Gateway: {} | {} | {} | {}".format( 
        host['_links']['self']['href'], 
        host['ip'], 
        host['purpose'], 
        host['state'] ))