class TestRecoveryAppliance(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') @responses.activate def test_list_recovery_appliances(self): with open('mock-api/list-recovery-appliances.json') as f: data = json.load(f) appliance_name = data[0]['name'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/recovery_appliances', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_recovery_images() self.assertEqual(r[0]['name'], appliance_name) @responses.activate def test_get_recovery_appliances(self): with open('mock-api/get-recovery-appliance.json') as f: data = json.load(f) appliance_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/recovery_appliances/%s' % appliance_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_recovery_image(image_id=appliance_id) self.assertEqual(r['id'], appliance_id)
def get_conn(): """ Return a conn object for the passed VM data """ return OneAndOneService( api_token=config.get_cloud_config_value( "api_token", get_configured_provider(), __opts__, search_global=False ) )
class TestImage(unittest.TestCase): def setUp(self): self.client = OneAndOneService("USER-API-KEY") @responses.activate def test_list_server_usages(self): with open("mock-api/list-monitoring-center-usages.json") as f: data = json.load(f) server_id = data[0]["id"] responses.add( responses.GET, "https://cloudpanel-api.1and1.com/v1/monitoring_center", body=json.dumps(data), status=200, content_type="application/json", ) r = self.client.list_server_usages() self.assertEqual(r[0]["id"], server_id) @responses.activate def test_get_usage(self): with open("mock-api/get-usage.json") as f: data = json.load(f) server_id = data["id"] period = "LAST_24H" responses.add( responses.GET, "https://cloudpanel-api.1and1.com/v1/monitoring_center/%s" % server_id, body=json.dumps(data), status=200, content_type="application/json", ) r = self.client.get_usage(server_id=server_id, period=period) self.assertEqual(r["id"], server_id)
class TestServerAppliance(unittest.TestCase): def setUp(self): self.client = OneAndOneService("USER-API-KEY") @responses.activate def test_list_appliances(self): with open("mock-api/list-appliances.json") as f: data = json.load(f) appliance_name = data[0]["name"] responses.add( responses.GET, "https://cloudpanel-api.1and1.com/v1/server_appliances", body=json.dumps(data), status=200, content_type="application/json", ) r = self.client.list_appliances() self.assertEqual(r[0]["name"], appliance_name) @responses.activate def test_get_appliance(self): with open("mock-api/get-appliance.json") as f: data = json.load(f) appliance_id = data["id"] responses.add( responses.GET, "https://cloudpanel-api.1and1.com/v1/server_appliances/%s" % appliance_id, body=json.dumps(data), status=200, content_type="application/json", ) r = self.client.get_appliance(appliance_id=appliance_id) self.assertEqual(r["id"], appliance_id)
class TestImage(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') @responses.activate def test_list_server_usages(self): with open('mock-api/list-monitoring-center-usages.json') as f: data = json.load(f) server_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_center', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_server_usages() self.assertEqual(r[0]['id'], server_id) @responses.activate def test_get_usage(self): with open('mock-api/get-usage.json') as f: data = json.load(f) server_id = data['id'] period = 'LAST_24H' responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_center/%s' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_usage(server_id=server_id, period=period) self.assertEqual(r['id'], server_id)
class TestLogs(unittest.TestCase): def setUp(self): self.client = OneAndOneService('API-USER-KEY') @responses.activate def test_list_logs(self): with open('mock-api/list-logs.json') as f: data = json.load(f) test_id = data[0]['id'] period = 'LAST_24H' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/logs', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_logs(period=period) self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_log(self): with open('mock-api/get-log.json') as f: data = json.load(f) log_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/logs/%s' % log_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_log(log_id=log_id) self.assertEqual(r['id'], log_id)
class TestDVD(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') @responses.activate def test_list_dvds(self): with open('mock-api/list-dvds.json') as f: data = json.load(f) iso_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/dvd_isos', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_dvds() self.assertEqual(r[0]['id'], iso_id) @responses.activate def test_get_dvd(self): with open('mock-api/get-dvd.json') as f: data = json.load(f) iso_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/dvd_isos/%s' % iso_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_dvd(iso_id=iso_id) self.assertEqual(r['id'], iso_id)
def getOneAndOneClient(self): """ initialize 1and1 client :return: OneAndOneService() """ # Try initializing the 1and1 client ant return it try: client = OneAndOneService(self.getConfig(self.configApiToken)) # If initializing failed return nothing except Exception as e: self.logger.warning("Could not establish connection to 1&1 Cloud Site. ERROR: %s" % e) return return client
class TestUsages(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') @responses.activate def test_list_usages(self): with open('mock-api/list-usages.json') as f: data = json.load(f) server_id = data['SERVERS'][0]['id'] period = "LAST_24H" responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/usages', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_usages(period=period) self.assertEqual(r['SERVERS'][0]['id'], server_id)
# List all usages from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') usages = client.list_usages(period='LAST_24H')
# List all firewall policies from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') firewall_policies = client.list_firewall_policies() # Retrieve a single firewall from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') firewall_policy = client.get_firewall(firewall_id='') # List all servers attached to a firewall from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') servers = client.list_firewall_servers(firewall_id='') # Retrieve a server attached to a firewall from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server = client.get_firewall_server(firewall_id='', server_ip_id='') # List all rules of a firewall from oneandone.client import OneAndOneService
import os from oneandone.client import OneAndOneService from oneandone.client import Server, Hdd token = os.getenv('ONEANDONE_TOKEN') client = OneAndOneService(token) print "creating bare metal server" server_appliances = client.list_appliances(q='baremetal') baremetal_model = client.list_baremetal_models(q='BMC_L') # Create a server server1 = Server(name='Python baremetal Server', description='Server Description', server_type='baremetal', baremetal_model_id=baremetal_model[0]['id'], appliance_id=server_appliances[0]['id'] ) hdd1 = Hdd(size=120, is_main=True) hdds = [hdd1] new_server = client.create_server(server=server1) print server1.wait_for() # Add a new IP to a server. print "add a new IP to the server" response = client.add_new_ip(server_id=new_server['id'], ip_type='IPV4') print server1.wait_for()
import os from oneandone.client import OneAndOneService from oneandone.client import Vpn token = os.getenv('ONEANDONE_TOKEN') client = OneAndOneService(token) # List all vpns vpns = client.list_vpns() # Create a vpn vpn1 = Vpn(name="python vpn", description="test vpn") new_vpn = client.create_vpn(vpn1) print vpn1.wait_for() # Retrieve a single vpn vpn = client.get_vpn(vpn_id=new_vpn['id']) # Download vpn configurations vpn = client.download_config(vpn_id=new_vpn['id'], file_path='c:\\' + new_vpn['name']) # Modify a vpn modified_vpn = client.modify_vpn(vpn_id=new_vpn['id'], name='New Name', description='New Description') # Delete a vpn response = client.delete_vpn(vpn_id=new_vpn['id'])
class TestImage(unittest.TestCase): def setUp(self): self.client = OneAndOneService('<USER-API-KEY>') @responses.activate def test_list_images(self): with open('mock-api/list-images.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/images', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_images() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_image(self): with open('mock-api/get-image.json') as f: data = json.load(f) image_id = data["id"] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/images/%s' % image_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_image(image_id=image_id) self.assertEqual(r['id'], image_id) @responses.activate def test_edit_image(self): with open('mock-api/edit-image.json') as f: data = json.load(f) image_id = data['id'] name = data['name'] description = data['description'] frequency = data['frequency'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/images/%s' % image_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_image(image_id=image_id, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) self.assertEqual(r['frequency'], frequency) @responses.activate def test_create_image(self): with open('mock-api/create-image.json') as f: data = json.load(f) image1 = Image(server_id=data['server_id'], name=data['name'], description='Test Description', frequency=data['frequency'], num_images=data['num_images']) responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/images', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_image(image=image1) self.assertEqual(r['server_id'], image1.server_id) self.assertEqual(r['name'], image1.name) self.assertEqual(r['frequency'], image1.frequency) self.assertEqual(r['num_images'], image1.num_images) @responses.activate def test_delete_image(self): with open('mock-api/delete-image.json') as f: data = json.load(f) image_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/images/%s' % image_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_image(image_id=image_id) self.assertEqual(r['state'], 'REMOVING')
# List all servers from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') servers = client.list_servers() # Retrieve a single server from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') server = client.get_server(server_id='') # List fixed server flavors from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') fixed_servers = client.fixed_server_flavors() # Retrieve information about a fixed server flavor from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') fixed_server = client.get_fixed_server(fixed_server_id='') # Retrieve information about a server's hardware from oneandone.client import OneAndOneService
# List all usages and alerts of monitoring servers from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') usages = client.list_server_usages() # Retrieve the usages and alerts for a monitoring server from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') usage = client.get_usage(server_id='', period='LAST_24H')
class TestFirewallPolicy(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_policies(self): with open('mock-api/list-firewalls.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_policies() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_policy(self): with open('mock-api/get-firewall.json') as f: data = json.load(f) firewall_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall(firewall_id=firewall_id) self.assertEqual(r['id'], firewall_id) @responses.activate def test_list_firewall_servers(self): with open('mock-api/list-server-ips-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_servers(firewall_id=firewall_id) self.assertEqual(r[0]['id'], server_ip_id) @responses.activate def test_get_firewall_server(self): with open('mock-api/get-server-ip-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips/%s' % (firewall_id, server_ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall_server(firewall_id=firewall_id, server_ip_id=server_ip_id) self.assertEqual(r['id'], server_ip_id) @responses.activate def test_list_policy_rules(self): with open('mock-api/list-fp-rules.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_policy_rules(firewall_id=firewall_id) self.assertEqual(r[0]['id'], rule_id) @responses.activate def test_get_policy_rule(self): with open('mock-api/get-fp-rule.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules/%s' % (firewall_id, rule_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall_policy_rule(firewall_id=firewall_id, rule_id=rule_id) self.assertEqual(r['id'], rule_id) # 'PUT' Methods @responses.activate def test_modify_firewall(self): with open('mock-api/modify-fp.json') as f: data = json.load(f) firewall_id = data['id'] name = data['name'] description = data['description'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_firewall(firewall_id=firewall_id, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) # 'POST' Methods @responses.activate def test_create_firewall_policy(self): with open('mock-api/create-fp.json') as f: data = json.load(f) fp1 = FirewallPolicy(name=data['name'], description=data['description']) rule1 = FirewallPolicyRule(protocol=data['rules'][0]['protocol'], port_from=data['rules'][0]['port_from'], port_to=data['rules'][0]['port_to'], source=data['rules'][0]['source']) rule2 = FirewallPolicyRule(protocol=data['rules'][1]['protocol'], port_from=data['rules'][1]['port_from'], port_to=data['rules'][1]['port_to'], source=data['rules'][1]['source']) rules = [rule1, rule2] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_firewall_policy(firewall_policy=fp1, firewall_policy_rules=rules) self.assertEqual(r['name'], fp1.specs['name']) self.assertEqual(r['description'], fp1.specs['description']) self.assertEqual(r['rules'][0]['protocol'], rule1.rule_set['protocol']) self.assertEqual(r['rules'][1]['protocol'], rule2.rule_set['protocol']) @responses.activate def test_assign_servers(self): with open('mock-api/assign-ip-fp.json') as f: data = json.load(f) firewall_id = data['id'] server1 = AttachServer(server_ip_id=data['server_ips'][0]['id']) servers = [server1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_server_firewall_policy(firewall_id=firewall_id, server_ips=servers) self.assertEqual(r['id'], firewall_id) self.assertEqual(r['server_ips'][0]['id'], server1.server_ip_id) @responses.activate def test_add_rule(self): with open('mock-api/add-rule-fp.json') as f: data = json.load(f) firewall_id = data['id'] rule1 = FirewallPolicyRule(protocol=data['rules'][2]['protocol'], port_from=data['rules'][2]['port_from'], port_to=data['rules'][2]['port_to'], source=data['rules'][2]['source']) rules = [rule1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_firewall_policy_rule(firewall_id=firewall_id, firewall_policy_rules=rules) self.assertEqual(r['rules'][2]['protocol'], rule1.rule_set['protocol']) self.assertEqual(r['rules'][2]['port_from'], rule1.rule_set['port_from']) self.assertEqual(r['rules'][2]['port_to'], rule1.rule_set['port_to']) self.assertEqual(r['rules'][2]['source'], rule1.rule_set['source']) # 'DELETE' Methods @responses.activate def test_remove_server_firewall(self): with open('mock-api/remove-ip-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = 'SERVER_IP_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips/%s' % (firewall_id, server_ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_firewall_server(firewall_id=firewall_id, server_ip_id=server_ip_id) self.assertEqual(r['state'], 'CONFIGURING') self.assertEqual(r['server_ips'], []) @responses.activate def test_remove_firewall_rule(self): with open('mock-api/delete-rule-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = 'RULE_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules/%s' % (firewall_id, rule_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_firewall_rule(firewall_id=firewall_id, rule_id=rule_id) self.assertNotEqual(len(r['rules']), 3) @responses.activate def test_delete_firewall(self): with open('mock-api/delete-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_firewall(firewall_id=firewall_id) self.assertEqual(r['state'], 'REMOVING')
class TestSharedStorage(unittest.TestCase): def setUp(self): self.client = OneAndOneService('<USER-API-KEY>') # 'GET' Methods @responses.activate def test_list_shared_storages(self): with open('mock-api/list-storages.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_shared_storages() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_shared_storage(self): with open('mock-api/get-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_shared_storage(shared_storage_id=shared_storage_id) self.assertEqual(r['id'], shared_storage_id) @responses.activate def test_servers_attached(self): with open('mock-api/storage-servers.json') as f: data = json.load(f) shared_storage_id = data[0]['id'] server_name = data[0]['name'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers' % shared_storage_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_servers_attached_storage(shared_storage_id=shared_storage_id) self.assertEqual(r[0]['name'], server_name) @responses.activate def test_get_server(self): with open('mock-api/get-server-storage.json') as f: data = json.load(f) shared_storage_id = 'SHARED_STORAGE_ID' server_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers/%s' % (shared_storage_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_shared_storage_server(shared_storage_id=shared_storage_id, server_id=server_id) self.assertEqual(r['id'], server_id) @responses.activate def test_get_credentials(self): with open('mock-api/list-credentials.json') as f: data = json.load(f) kerberos_content_file = data['kerberos_content_file'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/access', body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_credentials() self.assertEqual(r['kerberos_content_file'], kerberos_content_file) # 'POST' Methods @responses.activate def test_create_shared_storage(self): with open('mock-api/create-storage.json') as f: data = json.load(f) storage1 = SharedStorage(name=data['name'], description=data['description'], size=data['size']) responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/shared_storages', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_shared_storage(shared_storage=storage1) self.assertEqual(r['name'], storage1.name) self.assertEqual(r['description'], storage1.description) self.assertEqual(r['size'], storage1.size) @responses.activate def test_attach_servers(self): with open('mock-api/attach-server-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] server1 = AttachServer(server_id=data['servers'][0]['id'], rights=data['servers'][0]['rights']) server2 = AttachServer(server_id=data['servers'][1]['id'], rights=data['servers'][1]['rights']) servers = [server1, server2] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_server_shared_storage(shared_storage_id=shared_storage_id, server_ids=servers) self.assertEqual(r['servers'][0]['id'], server1.server_id) self.assertEqual(r['servers'][1]['id'], server2.server_id) # 'PUT' Methods @responses.activate def test_modify_shared_storage(self): with open('mock-api/modify-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] name = data['name'] description = data['description'] size = data['size'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_shared_storage(shared_storage_id=shared_storage_id, name=name, description=description, size=size) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) self.assertEqual(r['size'], size) @responses.activate def test_change_password(self): with open('mock-api/change-password.json') as f: data = json.load(f) new_password = '******' responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/shared_storages/access', body=json.dumps(data), status=202, content_type="application/json") r = self.client.change_password(password=new_password) self.assertEqual(r['state'], 'CONFIGURING') # 'DELETE' Methods @responses.activate def test_delete_shared_storage(self): with open('mock-api/delete-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_shared_storage(shared_storage_id=shared_storage_id) self.assertEqual(r['state'], 'REMOVING') @responses.activate def test_detach_server_ss(self): with open('mock-api/detach-server-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] server_id = 'SERVER_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers/%s' % (shared_storage_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.detach_server_shared_storage(shared_storage_id=shared_storage_id, server_id=server_id) self.assertNotEqual(r['servers'][0]['id'], server_id)
# List all images from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') images = client.list_images() # Retrieve an image from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') image = client.get_image(image_id='') # Create an image from oneandone.client import OneAndOneService from oneandone.client import Image client = OneAndOneService('API-TOKEN') image1 = Image(server_id='', name='Test Image', description='Test Description', frequency='WEEKLY', num_images=1 ) new_image = client.create_image(image=image1) # Modify an image from oneandone.client import OneAndOneService
def setUp(self): self.client = OneAndOneService('USER-API-KEY')
import os from oneandone.client import OneAndOneService token = os.getenv('ONEANDONE_TOKEN') client = OneAndOneService(token) # List all users users = client.list_users() # Retrieve a user user = client.get_user(user_id='') # Retrieve a user's API privileges api_info = client.api_info(user_id='') # Retrieve a user's API key api_key = client.show_api_key(user_id='') # Retrieve users current permissions user_permissions = client.show_user_permissions() # List IPs from which API access is allowed for a user ips_allowed = client.ips_api_access_allowed(user_id='') # Create user new_user = client.create_user(name='Test User', password='******', email='*****@*****.**', description='Test Description' )
# List all servers from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') servers = client.list_servers() # Retrieve a single server from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') server = client.get_server(server_id='') # List fixed server flavors from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') fixed_servers = client.fixed_server_flavors() # Retrieve information about a fixed server flavor from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') fixed_server = client.get_fixed_server(fixed_server_id='') # List baremetal models from oneandone.client import OneAndOneService
class TestPrivateNetwork(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_private_networks(self): with open('mock-api/list-private-networks.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_private_networks() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_private_network(self): with open('mock-api/get-private-network.json') as f: data = json.load(f) private_network_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_private_network(private_network_id=private_network_id) self.assertEqual(r['id'], private_network_id) @responses.activate def test_list_servers(self): with open('mock-api/list-pn-servers.json') as f: data = json.load(f) private_network_id = 'PRIVATE_NETWORK_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_private_network_servers(private_network_id=private_network_id) self.assertEqual(len(r), 2) @responses.activate def test_get_server(self): private_network_id = 'B944DB7D0575A828A7091CC774069A8B' server_id = 'AAAF4FDE974D6E5564654FF7FAB4C869' with open('mock-api/get-pn-server.json') as f: data = json.load(f) private_network_id = 'PRIVATE_NETWORK_ID' server_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers/%s' % (private_network_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_private_network_server(private_network_id=private_network_id, server_id=server_id) self.assertEqual(r['id'], server_id) # 'PUT' Methods @responses.activate def test_modify_private_network(self): with open('mock-api/modify-pn.json') as f: data = json.load(f) private_network_id = data['id'] name = data['name'] description = data['description'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_private_network(private_network_id=private_network_id, name=name, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) # 'POST' Methods @responses.activate def test_create_private_network(self): with open('mock-api/create-pn.json') as f: data = json.load(f) pn1 = PrivateNetwork(name=data['name']) responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/private_networks', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_private_network(private_network=pn1) self.assertEqual(r['name'], pn1.name) @responses.activate def test_attach_servers(self): with open('mock-api/attach-server-pn.json') as f: data = json.load(f) private_network_id= 'PRIVATE_NETWORK_ID' server1 = AttachServer(server_id=data[2]['id']) servers = [server1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers' % private_network_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_private_network_servers(private_network_id=private_network_id, server_ids=servers) self.assertEqual(r[2]['id'], server1.server_id) # 'DELETE' Methods @responses.activate def test_remove_server_private_network(self): with open('mock-api/remove-server-pn.json') as f: data = json.load(f) private_network_id= data['id'] server_id = 'SERVER_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers/%s' % (private_network_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_private_network_server(private_network_id=private_network_id, server_id=server_id) self.assertEqual(len(r['servers']), 1) @responses.activate def test_delete_private_network(self): with open('mock-api/delete-pn.json') as f: data = json.load(f) private_network_id= data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_private_network(private_network_id=private_network_id) self.assertEqual(r['state'], 'REMOVING')
from oneandone.client import OneAndOneService from oneandone.client import Server, Hdd, LoadBalancer, LoadBalancerRule from oneandone.client import FirewallPolicy, FirewallPolicyRule client = OneAndOneService('<API-Token>') # Create Load Balancer lb1 = LoadBalancer(name='Example App LB', description='Example Description', health_check_test='TCP', health_check_interval=40, persistence=True, persistence_time=1200, method='ROUND_ROBIN') rule1 = LoadBalancerRule(protocol='TCP', port_balancer=80, port_server=80, source='0.0.0.0') rules = [rule1] new_load_balancer = client.create_load_balancer(load_balancer=lb1, load_balancer_rules=rules) ## Wait for Load Balancer to go live print 'Creating load balancer...' print lb1.wait_for() # Create Firewall Policy fp1 = FirewallPolicy(name='Example App FP', description='Test Description') rule1 = FirewallPolicyRule(protocol='TCP', port_from=80, port_to=80, source='0.0.0.0')
class TestImage(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_servers(self): with open('mock-api/list-servers.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_servers() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_fixed_servers(self): with open('mock-api/fixed-server-flavors.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/fixed_instance_sizes', body=json.dumps(data), status=200, content_type="application/json") r = self.client.fixed_server_flavors() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_fixed_server(self): with open('mock-api/get-fixed-server.json') as f: data = json.load(f) fixed_server_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/fixed_instance_sizes/%s' % fixed_server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_fixed_server(fixed_server_id=fixed_server_id) self.assertEqual(r['id'], fixed_server_id) @responses.activate def test_get_server(self): with open('mock-api/get-server.json') as f: data = json.load(f) server_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server(server_id=server_id) self.assertEqual(r['id'], server_id) @responses.activate def test_get_server_hardware(self): with open('mock-api/get-hardware.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_hardware(server_id=server_id) self.assertEqual(r['vcore'], 1) @responses.activate def test_list_server_hdds(self): with open('mock-api/list-hdds.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_server_hdds(server_id=server_id) self.assertEqual(r[0]['size'], 40) @responses.activate def test_get_server_hdd(self): with open('mock-api/get-hdd.json') as f: data = json.load(f) server_id = 'SERVER_ID' hdd_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds/%s' % (server_id, hdd_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_hdd(server_id=server_id, hdd_id=hdd_id) self.assertEqual(r['id'], hdd_id) @responses.activate def test_get_server_image(self): with open('mock-api/get-server-image.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/image' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_image(server_id=server_id) self.assertEqual(r['id'], '76EBF29C1250167C8754B2B3D1C05F68') @responses.activate def test_list_server_ips(self): with open('mock-api/list-server-ips.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_server_ips(server_id=server_id) self.assertTrue(len(r) > 0) @responses.activate def test_get_server_ip(self): with open('mock-api/get-server-ip.json') as f: data = json.load(f) server_id = 'SERVER_ID' ip_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s' % (server_id, ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_ip(server_id=server_id, ip_id=ip_id) self.assertEqual(r['id'], ip_id) @responses.activate def test_list_ip_firewall_policy(self): with open('mock-api/list-server-fps.json') as f: data = json.load(f) server_id = 'SERVER_ID' ip_id = 'IP_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/firewall_policy' % (server_id, ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_ip_firewall_policy(server_id=server_id, ip_id=ip_id) self.assertEqual(r['name'], 'Windows') @responses.activate def test_list_ip_load_balancers(self): with open('mock-api/list-server-lbs.json') as f: data = json.load(f) server_id = 'SERVER_ID' ip_id = 'IP_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/load_balancers' % (server_id, ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_ip_load_balancers(server_id=server_id, ip_id=ip_id) self.assertEqual(r[0]['name'], 'My load balancer') @responses.activate def test_get_server_status(self): with open('mock-api/get-server-status.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/status' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_status(server_id=server_id) self.assertEqual(r['state'], 'POWERED_ON') @responses.activate def test_get_server_dvd(self): with open('mock-api/get-server-dvd.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/dvd' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_server_dvd(server_id=server_id) self.assertEqual(r['name'], 'Windows 2012 - 64 bits') @responses.activate def test_list_server_private_networks(self): with open('mock-api/list-server-pns.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/private_networks' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_server_private_networks(server_id=server_id) self.assertEqual(r[0]['name'], 'New private network 1') @responses.activate def test_private_network_info(self): with open('mock-api/get-server-pn.json') as f: data = json.load(f) server_id = data['servers'][0]['id'] private_network_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/private_networks/%s' % (server_id, private_network_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.private_network_info(server_id=server_id, private_network_id=private_network_id) self.assertEqual(r['id'], private_network_id) @responses.activate def test_list_server_snapshots(self): with open('mock-api/list-snapshots.json') as f: data = json.load(f) server_id = 'SERVER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/servers/%s/snapshots' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_server_snapshots(server_id=server_id) self.assertEqual(r[0]['id'], 'B77E19E062D5818532EFF11C747BD104') # 'PUT' Methods @responses.activate def test_modify_server(self): with open('mock-api/modify-server.json') as f: data = json.load(f) server_id = data['id'] name = data['name'] description = data['description'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s' % server_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_server(server_id=server_id, name=name, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) @responses.activate def test_modify_server_hardware(self): with open('mock-api/modify-server-hardware.json') as f: data = json.load(f) server_id = data['id'] vcore = data['hardware']['vcore'] cores_per_processor = data['hardware']['cores_per_processor'] ram = data['hardware']['ram'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_server_hardware(server_id=server_id, vcore=vcore, ram=ram, test=True) self.assertEqual(r['hardware']['vcore'], vcore) self.assertEqual(r['hardware']['cores_per_processor'], cores_per_processor) self.assertEqual(r['hardware']['ram'], ram) @responses.activate def test_modify_hdd(self): with open('mock-api/modify-server-hdd.json') as f: data = json.load(f) server_id = data['id'] hdd_id = data['hardware']['hdds'][0]['id'] size = data['hardware']['hdds'][0]['size'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds/%s' % (server_id, hdd_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_hdd(server_id=server_id, hdd_id=hdd_id, size=150, test=True) self.assertEqual(r['hardware']['hdds'][0]['size'], size) @responses.activate def test_add_firewall(self): with open('mock-api/add-firewall.json') as f: data = json.load(f) server_id = data['id'] ip_id = data['ips'][0]['id'] firewall_id = data['ips'][0]['firewall_policy']['id'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/firewall_policy' % (server_id, ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_firewall_policy(server_id=server_id, ip_id=ip_id, firewall_id=firewall_id) self.assertEqual(r['ips'][0]['firewall_policy']['id'], firewall_id) @responses.activate def test_modify_server_status(self): with open('mock-api/change-server-status.json') as f: data = json.load(f) server_id = data['id'] action = 'POWER_OFF' method = 'SOFTWARE' responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/status/action' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_server_status(server_id=server_id, action=action, method=method) self.assertEqual(r['status']['state'], 'POWERING_OFF') @responses.activate def test_load_dvd(self): with open('mock-api/load-dvd.json') as f: data = json.load(f) server_id = data['id'] dvd_id = 'DVD_ID' responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/dvd' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.load_dvd(server_id=server_id, dvd_id=dvd_id) self.assertEqual(r['status']['state'], 'CONFIGURING') @responses.activate def test_restore_snapshot(self): with open('mock-api/restore-snapshot.json') as f: data = json.load(f) server_id = data['id'] snapshot_id = data['snapshot']['id'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/snapshots/%s' % (server_id, snapshot_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.restore_snapshot(server_id=server_id, snapshot_id=snapshot_id) self.assertEqual(r['snapshot']['id'], snapshot_id) @responses.activate def test_reinstall_image(self): with open('mock-api/reinstall-image.json') as f: data = json.load(f) server_id = data['id'] image_id = data['image']['id'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/servers/%s/image' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.reinstall_image(server_id=server_id, image_id=image_id) self.assertEqual(r['image']['id'], image_id) # 'DELETE' Methods @responses.activate def test_delete_server(self): with open('mock-api/delete-server.json') as f: data = json.load(f) server_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_server(server_id=server_id) self.assertEqual(r['status']['state'], 'REMOVING') @responses.activate def test_remove_hdd(self): with open('mock-api/remove-hdd.json') as f: data = json.load(f) server_id = data['id'] hdd_id = 'HDD_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds/%s' % (server_id, hdd_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_hdd(server_id=server_id, hdd_id=hdd_id) self.assertEqual(r['status']['state'], 'CONFIGURING') @responses.activate def test_remove_ip(self): with open('mock-api/remove-server-ip.json') as f: data = json.load(f) server_id = data['id'] ip_id = 'IP_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s' % (server_id, ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_ip(server_id=server_id, ip_id=ip_id) self.assertNotIn(ip_id, r['ips']) @responses.activate def test_remove_firewall_policy(self): with open('mock-api/remove-firewall-policy.json') as f: data = json.load(f) server_id = data['id'] ip_id = data['ips'][0]['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/firewall_policy' % (server_id, ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_firewall_policy(server_id=server_id, ip_id=ip_id) self.assertEqual(r['ips'][0]['firewall_policy'], None) @responses.activate def test_remove_load_balancer(self): with open('mock-api/remove-lb.json') as f: data = json.load(f) server_id = data['id'] ip_id = data['ips'][0]['id'] load_balancer_id = 'LOAD_BALANCER_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/load_balancers/%s' % (server_id, ip_id, load_balancer_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_load_balancer(server_id=server_id, ip_id=ip_id, load_balancer_id=load_balancer_id) self.assertEqual(r['ips'][0]['load_balancers'], []) @responses.activate def test_remove_private_network(self): with open('mock-api/remove-pn.json') as f: data = json.load(f) server_id = data['id'] private_network_id = 'PRIVATE_NETWORK_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/private_networks/%s' % (server_id, private_network_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_private_network(server_id=server_id, private_network_id=private_network_id) self.assertNotIn(private_network_id, r['private_networks']) @responses.activate def test_eject_dvd(self): with open('mock-api/eject-dvd.json') as f: data = json.load(f) server_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/dvd' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.eject_dvd(server_id=server_id) self.assertEqual(r['status']['state'], 'CONFIGURING') @responses.activate def test_delete_snapshot(self): with open('mock-api/delete-snapshot.json') as f: data = json.load(f) server_id = data['id'] snapshot_id = 'SNAPSHOT_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/servers/%s/snapshots/%s' % (server_id, snapshot_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_snapshot(server_id=server_id, snapshot_id=snapshot_id) self.assertEqual(r['status']['state'], 'CONFIGURING') # 'POST' Methods @responses.activate def test_create_server(self): with open('mock-api/create-server.json') as f: data = json.load(f) server1 = Server(name=data['name'], description=data['description'], vcore=data['hardware']['vcore'], cores_per_processor=data['hardware']['cores_per_processor'], ram=data['hardware']['ram'], appliance_id='APPLIANCE_ID') hdd1 = Hdd(size=40, is_main=True) hdds = [hdd1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_server(server=server1, hdds=hdds) self.assertEqual(r['name'], server1.specs['name']) self.assertEqual(r['status']['state'], 'DEPLOYING') @responses.activate def test_add_hdd(self): with open('mock-api/add-hdd.json') as f: data = json.load(f) server_id = data['id'] hdd1 = Hdd(size=40, is_main=False) hdds = [hdd1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/hardware/hdds' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_hdd(server_id=server_id, hdds=hdds) self.assertEqual(r['status']['state'], 'CONFIGURING') @responses.activate def test_add_new_ip(self): with open('mock-api/add-server-ip.json') as f: data = json.load(f) server_id = data['id'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_new_ip(server_id=server_id) self.assertTrue(len(r['ips']) > 0) @responses.activate def test_add_load_balancer(self): with open('mock-api/add-lb.json') as f: data = json.load(f) server_id = data['id'] ip_id = data['ips'][0]['id'] load_balancer_id = data['ips'][0]['load_balancers'][0]['id'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/ips/%s/load_balancers' % (server_id, ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_load_balancer(server_id=server_id, ip_id=ip_id, load_balancer_id=load_balancer_id) self.assertEqual(r['ips'][0]['load_balancers'][0]['id'], load_balancer_id) @responses.activate def test_assign_private_network(self): with open('mock-api/add-pn.json') as f: data = json.load(f) server_id = data['id'] private_network_id = 'PRIVATE_NETWORK_ID' responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/private_networks' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.assign_private_network(server_id=server_id, private_network_id=private_network_id) self.assertEqual(r['private_networks'], None) @responses.activate def test_create_snapshot(self): with open('mock-api/create-snapshot.json') as f: data = json.load(f) server_id = data['id'] snapshot_id = data['snapshot']['id'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/snapshots' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_snapshot(server_id=server_id) self.assertEqual(r['snapshot']['id'], snapshot_id) @responses.activate def test_clone_server(self): with open('mock-api/clone-server.json') as f: data = json.load(f) server_id = data['id'] name = data['name'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/servers/%s/clone' % server_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.clone_server(server_id=server_id, name=name) self.assertEqual(r['name'], name) self.assertEqual(r['status']['state'], 'DEPLOYING')
class TestUser(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_users(self): with open('mock-api/list-users.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/users', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_users() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_user(self): with open('mock-api/get-user.json') as f: data = json.load(f) user_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/users/%s' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_user(user_id=user_id) self.assertEqual(r['id'], user_id) @responses.activate def test_api_info(self): with open('mock-api/get-user-api.json') as f: data = json.load(f) user_id = 'USER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/users/%s/api' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.api_info(user_id=user_id) self.assertEqual(r['active'], True) @responses.activate def test_show_api_key(self): with open('mock-api/get-user-api-key.json') as f: data = json.load(f) user_id = 'USER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/users/%s/api/key' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.show_api_key(user_id=user_id) self.assertEqual(r['key'], data['key']) @responses.activate def test_ips_api_access_allowed(self): with open('mock-api/list-user-ips.json') as f: data = json.load(f) user_id = 'USER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/users/%s/api/ips' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.ips_api_access_allowed(user_id=user_id) self.assertEqual(len(r), 3) # 'POST' Methods @responses.activate def test_create_user(self): with open('mock-api/create-user.json') as f: data = json.load(f) name = data['name'] password = '******' description = data['description'] email = data['email'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/users', body=json.dumps(data), status=201, content_type="application/json") r = self.client.create_user(name=name, password=password, description=description, email=email) self.assertEqual(r['state'], 'CREATING') self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) self.assertEqual(r['email'], email) @responses.activate def test_add_user_ip(self): with open('mock-api/add-new-ip.json') as f: data = json.load(f) user_id = data['id'] ip1 = data['api']['allowed_ips'][2] ip2 = data['api']['allowed_ips'][3] ips = [ip1, ip2] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/users/%s/api/ips' % user_id, body=json.dumps(data), status=201, content_type="application/json") r = self.client.add_user_ip(user_id=user_id, user_ips=ips) self.assertEqual(r['api']['allowed_ips'][2], ip1) self.assertEqual(r['api']['allowed_ips'][3], ip2) # 'PUT' Methods @responses.activate def test_modify_user(self): with open('mock-api/modify-user.json') as f: data = json.load(f) user_id = data['id'] description = data['description'] email = data['email'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/users/%s' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_user(user_id=user_id, description=description, email=email) self.assertEqual(r['description'], description) self.assertEqual(r['email'], email) @responses.activate def test_modify_user_api(self): with open('mock-api/modify-user-api.json') as f: data = json.load(f) user_id = data['id'] active = data['api']['active'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/users/%s/api' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_user_api(user_id=user_id, active=active) self.assertEqual(r['api']['active'], active) @responses.activate def test_change_api_key(self): with open('mock-api/change-api-key.json') as f: data = json.load(f) user_id = data['id'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/users/%s/api/key' % user_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.change_api_key(user_id=user_id) self.assertEqual(r['state'], 'CONFIGURING') # 'DELETE' Methods @responses.activate def test_delete_user(self): with open('mock-api/delete-user.json') as f: data = json.load(f) user_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/users/%s' % user_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_user(user_id=user_id) self.assertEqual(r['state'], 'REMOVING') @responses.activate def test_remove_user_ip(self): with open('mock-api/delete-ip.json') as f: data = json.load(f) user_id = data['id'] ip = 'IP_ADDRESS' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/users/%s/api/ips/%s' % (user_id, ip), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_user_ip(user_id=user_id, ip=ip) self.assertEqual(r['api']['allowed_ips'], [])
# List all monitoring policies from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') monitoring_policies = client.list_monitoring_policies() # Retrieve a monitoring policy from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') monitoring_policy = client.get_monitoring_policy(monitoring_policy_id='') # List all ports of a monitoring policy from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') ports = client.list_monitoring_policy_ports(monitoring_policy_id='') # Retrieve a monitoring policy port from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') port = client.get_monitoring_policy_port(monitoring_policy_id='', port_id='') # List all processes of a monitoring policy from oneandone.client import OneAndOneService
# List all private networks from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') private_networks = client.list_private_networks() # Retrieve a private network from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') private_network = client.get_private_network(private_network_id='') # List all servers attached to a private network from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') servers = client.list_private_network_servers(private_network_id='') # Retrieve a private network server from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server = client.get_private_network_server(private_network_id='', server_id='') # Create a private network from oneandone.client import OneAndOneService from oneandone.client import PrivateNetwork
class TestMonitoringPolicy(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_monitoring_policies(self): with open('mock-api/list-mps.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policies() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_monitoring_policy(self): with open('mock-api/get-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy(monitoring_policy_id=monitoring_policy_id) self.assertEqual(r['id'], monitoring_policy_id) @responses.activate def test_list_ports(self): with open('mock-api/list-mp-ports.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_ports(monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_port(self): with open('mock-api/get-mp-port.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' port_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_port(monitoring_policy_id=monitoring_policy_id, port_id=port_id) self.assertEqual(r['id'], port_id) @responses.activate def test_list_processes(self): with open('mock-api/list-mp-processes.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_processes(monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_process(self): with open('mock-api/get-mp-process.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' process_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_process(monitoring_policy_id=monitoring_policy_id, process_id=process_id) self.assertEqual(r['id'], process_id) @responses.activate def test_list_servers(self): with open('mock-api/list-mp-servers.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_servers(monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_server(self): with open('mock-api/get-mp-server.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' server_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers/%s' % (monitoring_policy_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_server(monitoring_policy_id=monitoring_policy_id, server_id=server_id) self.assertEqual(r['id'], server_id) # 'DELETE' Methods @responses.activate def test_delete_port_monitoring_policy(self): with open('mock-api/remove-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port_id = 'PORT_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy_port(monitoring_policy_id=monitoring_policy_id, port_id=port_id) self.assertEqual(r['ports'], []) @responses.activate def test_delete_process(self): with open('mock-api/remove-process-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] process_id = 'PROCESS_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy_process(monitoring_policy_id=monitoring_policy_id, process_id=process_id) self.assertEqual(r['processes'], []) @responses.activate def test_detach_server(self): with open('mock-api/detach-server-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] server_id = 'SERVER_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers/%s' % (monitoring_policy_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.detach_monitoring_policy_server(monitoring_policy_id=monitoring_policy_id, server_id=server_id) self.assertEqual(len(r['servers']), 2) self.assertEqual(r['state'], 'CONFIGURING') @responses.activate def test_delete_monitoring_policy(self): with open('mock-api/delete-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy(monitoring_policy_id=monitoring_policy_id) self.assertEqual(r['state'], 'REMOVING') # 'POST' Methods @responses.activate def test_create_monitoring_policy(self): with open('mock-api/create-mp.json') as f: data = json.load(f) ## MP Object monitoring_policy1 = MonitoringPolicy(name=data['name'], email=data['email'], agent=data['agent']) ## Thresholds cpu = Threshold(entity='cpu', warning_value=data['thresholds']['cpu']['warning']['value'], warning_alert=data['thresholds']['cpu']['warning']['alert'], critical_value=data['thresholds']['cpu']['critical']['value'], critical_alert=data['thresholds']['cpu']['critical']['alert']) ram = Threshold(entity='ram', warning_value=data['thresholds']['ram']['warning']['value'], warning_alert=data['thresholds']['ram']['warning']['alert'], critical_value=data['thresholds']['ram']['critical']['value'], critical_alert=data['thresholds']['ram']['critical']['alert']) disk = Threshold(entity='disk', warning_value=data['thresholds']['disk']['warning']['value'], warning_alert=data['thresholds']['disk']['warning']['alert'], critical_value=data['thresholds']['disk']['critical']['value'], critical_alert=data['thresholds']['disk']['critical']['alert']) transfer = Threshold(entity='transfer', warning_value=data['thresholds']['transfer']['warning']['value'], warning_alert=data['thresholds']['transfer']['warning']['alert'], critical_value=data['thresholds']['transfer']['critical']['value'], critical_alert=data['thresholds']['transfer']['critical']['alert']) internal_ping = Threshold(entity='internal_ping', warning_value=data['thresholds']['internal_ping']['warning']['value'], warning_alert=data['thresholds']['internal_ping']['warning']['alert'], critical_value=data['thresholds']['internal_ping']['critical']['value'], critical_alert=data['thresholds']['internal_ping']['critical']['alert']) thresholds = [cpu, ram, disk, transfer, internal_ping] ## Ports port1 = Port(protocol=data['ports'][0]['protocol'], port=data['ports'][0]['port'], alert_if=data['ports'][0]['alert_if'], email_notification=data['ports'][0]['email_notification']) ports = [port1] ## Processes process1 = Process(process=data['processes'][0]['process'], alert_if=data['processes'][0]['alert_if'], email_notification=data['processes'][0]['email_notification']) processes = [process1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies', body=json.dumps(data), status=201, content_type="application/json") r = self.client.create_monitoring_policy(monitoring_policy=monitoring_policy1, thresholds=thresholds, ports=ports, processes=processes) self.assertEqual(r['name'], monitoring_policy1.specs['name']) self.assertEqual(r['email'], monitoring_policy1.specs['email']) self.assertEqual(r['agent'], monitoring_policy1.specs['agent']) self.assertEqual(r['ports'][0]['protocol'], port1.specs['protocol']) self.assertEqual(r['processes'][0]['process'], process1.process_set['process']) @responses.activate def test_add_port(self): with open('mock-api/add-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port1 = Port(protocol=data['ports'][0]['protocol'], port=data['ports'][0]['port'], alert_if='NOT_RESPONDING', email_notification=True) ports = [port1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_port(monitoring_policy_id=monitoring_policy_id, ports=ports) self.assertEqual(r['ports'][0]['protocol'], port1.specs['protocol']) self.assertEqual(r['ports'][0]['port'], port1.specs['port']) @responses.activate def test_add_process(self): with open('mock-api/add-process-mp.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' process1 = Process(process=data[2]['process'], alert_if=data[2]['alert_if'], email_notification=data[2]['email_notifications']) processes = [process1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_process(monitoring_policy_id=monitoring_policy_id, processes=processes) self.assertEqual(r[2]['process'], process1.process_set['process']) self.assertEqual(r[2]['alert_if'], process1.process_set['alert_if']) self.assertEqual(r[2]['email_notifications'], process1.process_set['email_notification']) @responses.activate def test_attach_server(self): with open('mock-api/add-server-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] server1 = AttachServer(server_id='92AA60BEC8333A21EDB9EAAA61852860') servers = [server1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_monitoring_policy_server(monitoring_policy_id=monitoring_policy_id, servers=servers) self.assertEqual(r['state'], 'CONFIGURING') # 'PUT' Methods @responses.activate def test_modify_port(self): with open('mock-api/modify-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port_id = 'PORT_ID' edit_port = Port(port=data['ports'][0]['port'], alert_if='NOT_RESPONDING', email_notification=False) responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_port(monitoring_policy_id=monitoring_policy_id, port_id=port_id, port=edit_port, test=True) self.assertEqual(r['ports'][0]['port'], edit_port.specs['port']) @responses.activate def test_modify_process(self): with open('mock-api/modify-process-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] process_id = data['processes'][0]['process'] edit_process = Process(alert_if='NOT_RUNNING', email_notification=False) responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_process(monitoring_policy_id=monitoring_policy_id, process_id=process_id, process=edit_process, test=True) self.assertEqual(r['processes'][0]['process'], process_id) @responses.activate def test_modify_monitoring_policy(self): with open('mock-api/modify-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] cpu = Threshold(entity='cpu', warning_value=80, warning_alert=True, critical_value=90, critical_alert=True) ram = Threshold(entity='ram', warning_value=80, warning_alert=True, critical_value=90, critical_alert=True) disk = Threshold(entity='disk', warning_value=70, warning_alert=True, critical_value=80, critical_alert=True) thresholds = [cpu, ram, disk] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_monitoring_policy(monitoring_policy_id=monitoring_policy_id, thresholds=thresholds, test=True) self.assertEqual(r['thresholds']['cpu']['warning']['value'], 90)
class TestPrivateNetwork(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_private_networks(self): with open('mock-api/list-private-networks.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_private_networks() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_private_network(self): with open('mock-api/get-private-network.json') as f: data = json.load(f) private_network_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_private_network( private_network_id=private_network_id) self.assertEqual(r['id'], private_network_id) @responses.activate def test_list_servers(self): with open('mock-api/list-pn-servers.json') as f: data = json.load(f) private_network_id = 'PRIVATE_NETWORK_ID' responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_private_network_servers( private_network_id=private_network_id) self.assertEqual(len(r), 2) @responses.activate def test_get_server(self): private_network_id = 'B944DB7D0575A828A7091CC774069A8B' server_id = 'AAAF4FDE974D6E5564654FF7FAB4C869' with open('mock-api/get-pn-server.json') as f: data = json.load(f) private_network_id = 'PRIVATE_NETWORK_ID' server_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers/%s' % (private_network_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_private_network_server( private_network_id=private_network_id, server_id=server_id) self.assertEqual(r['id'], server_id) # 'PUT' Methods @responses.activate def test_modify_private_network(self): with open('mock-api/modify-pn.json') as f: data = json.load(f) private_network_id = data['id'] name = data['name'] description = data['description'] responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_private_network( private_network_id=private_network_id, name=name, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) # 'POST' Methods @responses.activate def test_create_private_network(self): with open('mock-api/create-pn.json') as f: data = json.load(f) pn1 = PrivateNetwork(name=data['name']) responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/private_networks', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_private_network(private_network=pn1) self.assertEqual(r['name'], pn1.name) @responses.activate def test_attach_servers(self): with open('mock-api/attach-server-pn.json') as f: data = json.load(f) private_network_id = 'PRIVATE_NETWORK_ID' server1 = AttachServer(server_id=data[2]['id']) servers = [server1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers' % private_network_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_private_network_servers( private_network_id=private_network_id, server_ids=servers) self.assertEqual(r[2]['id'], server1.server_id) # 'DELETE' Methods @responses.activate def test_remove_server_private_network(self): with open('mock-api/remove-server-pn.json') as f: data = json.load(f) private_network_id = data['id'] server_id = 'SERVER_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s/servers/%s' % (private_network_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_private_network_server( private_network_id=private_network_id, server_id=server_id) self.assertEqual(len(r['servers']), 1) @responses.activate def test_delete_private_network(self): with open('mock-api/delete-pn.json') as f: data = json.load(f) private_network_id = data['id'] responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/private_networks/%s' % private_network_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_private_network( private_network_id=private_network_id) self.assertEqual(r['state'], 'REMOVING')
# List all shared storages from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') storages = client.list_shared_storages() # Retrieve a single shared storage from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') storage = client.get_shared_storage(shared_storage_id='') # List all servers attached to a shared storage from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') servers = client.list_servers_attached_storage(shared_storage_id='') # Retrieve a server attached to a shared storage from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server = client.get_shared_storage_server(shared_storage_id='', server_id='') # Retrieve shared storage credentials from oneandone.client import OneAndOneService
class TestPublicIP(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_public_ips(self): with open('mock-api/list-public-ips.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/public_ips', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_public_ips() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_public_ip(self): ip_id = '9DB3E4FFEAA6BD8C7007B821C0E868D6' with open('mock-api/get-public-ip.json') as f: data = json.load(f) ip_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/public_ips/%s' % ip_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_public_ip(ip_id=ip_id) self.assertEqual(r['id'], ip_id) # 'POST' Methods @responses.activate def test_create_public_ip(self): with open('mock-api/create-public-ip.json') as f: data = json.load(f) ip_id = data['id'] reverse_dns = data['reverse_dns'] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/public_ips', body=json.dumps(data), status=201, content_type="application/json") r = self.client.create_public_ip(reverse_dns=reverse_dns) self.assertEqual(r['id'], ip_id) self.assertEqual(r['reverse_dns'], reverse_dns) # 'PUT' Methods @responses.activate def test_modify_public_ip(self): with open('mock-api/modify-public-ip.json') as f: data = json.load(f) ip_id = data['id'] reverse_dns = data['reverse_dns'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/public_ips/%s' % ip_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_public_ip(ip_id=ip_id, reverse_dns=reverse_dns) self.assertEqual(r['reverse_dns'], reverse_dns) # 'DELETE' Methods @responses.activate def test_delete_public_ip(self): with open('mock-api/delete-public-ip.json') as f: data = json.load(f) ip_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/public_ips/%s' % ip_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.delete_public_ip(ip_id=ip_id) self.assertEqual(r['state'], 'CONFIGURING')
# List all public IPs from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') public_ips = client.list_public_ips() # Retrieve a public IP from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') public_ip = client.get_public_ip(ip_id='') # Create a public IP from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') new_public_ip = client.create_public_ip(reverse_dns='example.com') # Modify a public IP from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') modified_ip = client.modify_public_ip(ip_id='', reverse_dns='newexample.com') # Delete a public IP from oneandone.client import OneAndOneService
# List all images from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') images = client.list_images() # Retrieve an image from oneandone.client import OneAndOneService client = OneAndOneService('API-TOKEN') image = client.get_image(image_id='') # Create an image from oneandone.client import OneAndOneService from oneandone.client import Image client = OneAndOneService('API-TOKEN') image1 = Image(server_id='', name='Test Image', description='Test Description', frequency='WEEKLY', num_images=1) new_image = client.create_image(image=image1) # Modify an image from oneandone.client import OneAndOneService
# List all load balancers from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') load_balancers = client.list_load_balancers() # Retrieve a single load balancer from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') load_balancer = client.get_load_balancer(load_balancer_id='') # List all servers attached to a load balancer from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') servers = client.list_load_balancer_servers(load_balancer_id='') # Retrieve a load balancer server from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server = client.get_load_balancer_server(load_balancer_id='', server_ip_id='') # List all load balancer rules from oneandone.client import OneAndOneService
# List all logs from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') logs = client.list_logs(period='LAST_24H') # Retrieve a log from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') log = client.get_log(log_id='')
class TestMonitoringPolicy(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_monitoring_policies(self): with open('mock-api/list-mps.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policies() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_monitoring_policy(self): with open('mock-api/get-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy( monitoring_policy_id=monitoring_policy_id) self.assertEqual(r['id'], monitoring_policy_id) @responses.activate def test_list_ports(self): with open('mock-api/list-mp-ports.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_ports( monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_port(self): with open('mock-api/get-mp-port.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' port_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_port( monitoring_policy_id=monitoring_policy_id, port_id=port_id) self.assertEqual(r['id'], port_id) @responses.activate def test_list_processes(self): with open('mock-api/list-mp-processes.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_processes( monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_process(self): with open('mock-api/get-mp-process.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' process_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_process( monitoring_policy_id=monitoring_policy_id, process_id=process_id) self.assertEqual(r['id'], process_id) @responses.activate def test_list_servers(self): with open('mock-api/list-mp-servers.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers' % monitoring_policy_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_monitoring_policy_servers( monitoring_policy_id=monitoring_policy_id) self.assertEqual(len(r), 2) @responses.activate def test_get_server(self): with open('mock-api/get-mp-server.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' server_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers/%s' % (monitoring_policy_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_monitoring_policy_server( monitoring_policy_id=monitoring_policy_id, server_id=server_id) self.assertEqual(r['id'], server_id) # 'DELETE' Methods @responses.activate def test_delete_port_monitoring_policy(self): with open('mock-api/remove-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port_id = 'PORT_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy_port( monitoring_policy_id=monitoring_policy_id, port_id=port_id) self.assertEqual(r['ports'], []) @responses.activate def test_delete_process(self): with open('mock-api/remove-process-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] process_id = 'PROCESS_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy_process( monitoring_policy_id=monitoring_policy_id, process_id=process_id) self.assertEqual(r['processes'], []) @responses.activate def test_detach_server(self): with open('mock-api/detach-server-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] server_id = 'SERVER_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers/%s' % (monitoring_policy_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.detach_monitoring_policy_server( monitoring_policy_id=monitoring_policy_id, server_id=server_id) self.assertEqual(len(r['servers']), 2) self.assertEqual(r['state'], 'CONFIGURING') @responses.activate def test_delete_monitoring_policy(self): with open('mock-api/delete-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_monitoring_policy( monitoring_policy_id=monitoring_policy_id) self.assertEqual(r['state'], 'REMOVING') # 'POST' Methods @responses.activate def test_create_monitoring_policy(self): with open('mock-api/create-mp.json') as f: data = json.load(f) ## MP Object monitoring_policy1 = MonitoringPolicy(name=data['name'], email=data['email'], agent=data['agent']) ## Thresholds cpu = Threshold( entity='cpu', warning_value=data['thresholds']['cpu']['warning']['value'], warning_alert=data['thresholds']['cpu']['warning']['alert'], critical_value=data['thresholds']['cpu']['critical']['value'], critical_alert=data['thresholds']['cpu']['critical']['alert']) ram = Threshold( entity='ram', warning_value=data['thresholds']['ram']['warning']['value'], warning_alert=data['thresholds']['ram']['warning']['alert'], critical_value=data['thresholds']['ram']['critical']['value'], critical_alert=data['thresholds']['ram']['critical']['alert']) disk = Threshold( entity='disk', warning_value=data['thresholds']['disk']['warning']['value'], warning_alert=data['thresholds']['disk']['warning']['alert'], critical_value=data['thresholds']['disk']['critical']['value'], critical_alert=data['thresholds']['disk']['critical']['alert']) transfer = Threshold( entity='transfer', warning_value=data['thresholds']['transfer']['warning']['value'], warning_alert=data['thresholds']['transfer']['warning']['alert'], critical_value=data['thresholds']['transfer']['critical']['value'], critical_alert=data['thresholds']['transfer']['critical']['alert']) internal_ping = Threshold(entity='internal_ping', warning_value=data['thresholds'] ['internal_ping']['warning']['value'], warning_alert=data['thresholds'] ['internal_ping']['warning']['alert'], critical_value=data['thresholds'] ['internal_ping']['critical']['value'], critical_alert=data['thresholds'] ['internal_ping']['critical']['alert']) thresholds = [cpu, ram, disk, transfer, internal_ping] ## Ports port1 = Port(protocol=data['ports'][0]['protocol'], port=data['ports'][0]['port'], alert_if=data['ports'][0]['alert_if'], email_notification=data['ports'][0]['email_notification']) ports = [port1] ## Processes process1 = Process( process=data['processes'][0]['process'], alert_if=data['processes'][0]['alert_if'], email_notification=data['processes'][0]['email_notification']) processes = [process1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies', body=json.dumps(data), status=201, content_type="application/json") r = self.client.create_monitoring_policy( monitoring_policy=monitoring_policy1, thresholds=thresholds, ports=ports, processes=processes) self.assertEqual(r['name'], monitoring_policy1.specs['name']) self.assertEqual(r['email'], monitoring_policy1.specs['email']) self.assertEqual(r['agent'], monitoring_policy1.specs['agent']) self.assertEqual(r['ports'][0]['protocol'], port1.specs['protocol']) self.assertEqual(r['processes'][0]['process'], process1.process_set['process']) @responses.activate def test_add_port(self): with open('mock-api/add-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port1 = Port(protocol=data['ports'][0]['protocol'], port=data['ports'][0]['port'], alert_if='NOT_RESPONDING', email_notification=True) ports = [port1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_port(monitoring_policy_id=monitoring_policy_id, ports=ports) self.assertEqual(r['ports'][0]['protocol'], port1.specs['protocol']) self.assertEqual(r['ports'][0]['port'], port1.specs['port']) @responses.activate def test_add_process(self): with open('mock-api/add-process-mp.json') as f: data = json.load(f) monitoring_policy_id = 'MONITORING_POLICY_ID' process1 = Process(process=data[2]['process'], alert_if=data[2]['alert_if'], email_notification=data[2]['email_notifications']) processes = [process1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_process(monitoring_policy_id=monitoring_policy_id, processes=processes) self.assertEqual(r[2]['process'], process1.process_set['process']) self.assertEqual(r[2]['alert_if'], process1.process_set['alert_if']) self.assertEqual(r[2]['email_notifications'], process1.process_set['email_notification']) @responses.activate def test_attach_server(self): with open('mock-api/add-server-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] server1 = AttachServer(server_id='92AA60BEC8333A21EDB9EAAA61852860') servers = [server1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/servers' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_monitoring_policy_server( monitoring_policy_id=monitoring_policy_id, servers=servers) self.assertEqual(r['state'], 'CONFIGURING') # 'PUT' Methods @responses.activate def test_modify_port(self): with open('mock-api/modify-port-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] port_id = 'PORT_ID' edit_port = Port(port=data['ports'][0]['port'], alert_if='NOT_RESPONDING', email_notification=False) responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/ports/%s' % (monitoring_policy_id, port_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_port(monitoring_policy_id=monitoring_policy_id, port_id=port_id, port=edit_port, test=True) self.assertEqual(r['ports'][0]['port'], edit_port.specs['port']) @responses.activate def test_modify_process(self): with open('mock-api/modify-process-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] process_id = data['processes'][0]['process'] edit_process = Process(alert_if='NOT_RUNNING', email_notification=False) responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s/processes/%s' % (monitoring_policy_id, process_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_process( monitoring_policy_id=monitoring_policy_id, process_id=process_id, process=edit_process, test=True) self.assertEqual(r['processes'][0]['process'], process_id) @responses.activate def test_modify_monitoring_policy(self): with open('mock-api/modify-mp.json') as f: data = json.load(f) monitoring_policy_id = data['id'] cpu = Threshold(entity='cpu', warning_value=80, warning_alert=True, critical_value=90, critical_alert=True) ram = Threshold(entity='ram', warning_value=80, warning_alert=True, critical_value=90, critical_alert=True) disk = Threshold(entity='disk', warning_value=70, warning_alert=True, critical_value=80, critical_alert=True) thresholds = [cpu, ram, disk] responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/monitoring_policies/%s' % monitoring_policy_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_monitoring_policy( monitoring_policy_id=monitoring_policy_id, thresholds=thresholds, test=True) self.assertEqual(r['thresholds']['cpu']['warning']['value'], 90)
# List all DVDs from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') dvds = client.list_dvds() # Retrieve a DVD from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') dvd = client.get_dvd(iso_id='')
# List all of the images available for recovering purposes from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') recovery_appliances = client.list_recovery_images() # Retrieve a specific appliance from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') recovery_appliance = client.get_recovery_image(image_id='')
class TestLoadBalancer(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_load_balancers(self): with open('mock-api/list-load-balancers.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_load_balancers() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_load_balancer(self): with open('mock-api/get-load-balancer.json') as f: data = json.load(f) load_balancer_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s' % load_balancer_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_load_balancer(load_balancer_id=load_balancer_id) self.assertEqual(r['id'], load_balancer_id) @responses.activate def test_list_servers_load_balancer(self): with open('mock-api/list-lb-servers.json') as f: data = json.load(f) load_balancer_id = 'LOAD_BALANCER_ID' responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/server_ips' % load_balancer_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_load_balancer_servers(load_balancer_id=load_balancer_id) self.assertEqual(len(r), 2) @responses.activate def test_get_load_balancer_server(self): with open('mock-api/get-lb-server.json') as f: data = json.load(f) load_balancer_id = 'LOAD_BALANCER_ID' server_ip_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/server_ips/%s' % (load_balancer_id, server_ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_load_balancer_server(load_balancer_id=load_balancer_id, server_ip_id=server_ip_id) self.assertEqual(r['id'], server_ip_id) @responses.activate def test_load_balancer_rules(self): with open('mock-api/list-lb-rules.json') as f: data = json.load(f) load_balancer_id = 'LOAD_BALANCER_ID' first_rule_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/rules' % load_balancer_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.load_balancer_rules(load_balancer_id=load_balancer_id) self.assertEqual(r[0]['id'], first_rule_id) @responses.activate def test_get_load_balancer_rule(self): with open('mock-api/get-lb-rule.json') as f: data = json.load(f) load_balancer_id = 'LOAD_BALANCER_ID' rule_id = data['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/rules/%s' % (load_balancer_id, rule_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_load_balancer_rule(load_balancer_id=load_balancer_id, rule_id=rule_id) self.assertEqual(r['id'], rule_id) # 'PUT' Methods @responses.activate def test_modify_load_balancer(self): with open('mock-api/modify-lb.json') as f: data = json.load(f) load_balancer_id = data['id'] name = data['name'] description = data['description'] method = data['method'] responses.add(responses.PUT, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s' % load_balancer_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_load_balancer(load_balancer_id=load_balancer_id, name=name, description=description, method=method) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) self.assertEqual(r['method'], method) # 'POST' Methods @responses.activate def test_create_load_balancer(self): lb1 = LoadBalancer(name='Unit Test', health_check_test='TCP', health_check_interval=20, persistence=True, persistence_time=200, method='LEAST_CONNECTIONS') rule1 = LoadBalancerRule(protocol='TCP', port_balancer=22, port_server=22) rules = [rule1] with open('mock-api/create-lb.json') as f: data = json.load(f) lb1 = LoadBalancer(name=data['name'], health_check_test=data['health_check_test'], health_check_interval=data['health_check_interval'], persistence=data['persistence'], persistence_time=data['persistence_time'], method=data['method']) rule1 = LoadBalancerRule(protocol=data['rules'][0]['protocol'], port_balancer=data['rules'][0]['port_balancer'], port_server=data['rules'][0]['port_server']) rule2 = LoadBalancerRule(protocol=data['rules'][1]['protocol'], port_balancer=data['rules'][1]['port_balancer'], port_server=data['rules'][1]['port_server']) rules = [rule1, rule2] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/load_balancers', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_load_balancer(load_balancer=lb1, load_balancer_rules=rules) self.assertEqual(r['name'], lb1.specs['name']) self.assertEqual(r['health_check_test'], lb1.specs['health_check_test']) self.assertEqual(r['health_check_interval'], lb1.specs['health_check_interval']) self.assertEqual(r['method'], lb1.specs['method']) @responses.activate def test_attach_server_load_balancer(self): with open('mock-api/assign-server-lb.json') as f: data = json.load(f) load_balancer_id = data['id'] server1 = AttachServer(server_ip_id=data['server_ips'][0]['id']) servers = [server1] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/server_ips' % load_balancer_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_load_balancer_server(load_balancer_id=load_balancer_id, server_ips=servers) self.assertEqual(r['server_ips'][0]['id'], server1.server_ip_id) @responses.activate def test_add_rule_load_balancer(self): with open('mock-api/add-rule-lb.json') as f: data = json.load(f) load_balancer_id = 'LOAD_BALANCER_ID' rule1 = LoadBalancerRule(protocol=data['rules'][0]['protocol'], port_balancer=data['rules'][0]['port_balancer'], port_server=data['rules'][0]['port_server']) rule2 = LoadBalancerRule(protocol=data['rules'][1]['protocol'], port_balancer=data['rules'][1]['port_balancer'], port_server=data['rules'][1]['port_server']) rule3 = LoadBalancerRule(protocol=data['rules'][2]['protocol'], port_balancer=data['rules'][2]['port_balancer'], port_server=data['rules'][2]['port_server']) rules = [rule1, rule2, rule3] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/rules' % load_balancer_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_load_balancer_rule(load_balancer_id=load_balancer_id, load_balancer_rules=rules) self.assertEqual(len(r['rules']), 3) # 'DELETE' Methods @responses.activate def test_remove_load_balancer_server(self): with open('mock-api/detach-server-lb.json') as f: data = json.load(f) load_balancer_id = data['id'] server_ip_id = 'SERVER_IP_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/server_ips/%s' % (load_balancer_id, server_ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_load_balancer_server(load_balancer_id=load_balancer_id, server_ip_id=server_ip_id) self.assertEqual(r['server_ips'], []) @responses.activate def test_remove_rule(self): with open('mock-api/remove-rule-lb.json') as f: data = json.load(f) load_balancer_id = data['id'] rule_id = 'RULE_ID' responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s/rules/%s' % (load_balancer_id, rule_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_load_balancer_rule(load_balancer_id=load_balancer_id, rule_id=rule_id) self.assertEqual(len(r['rules']), 2) @responses.activate def test_delete_load_balancer(self): load_balancer_id = '2652331583F5C01BA803016A60E94CAE' with open('mock-api/delete-lb.json') as f: data = json.load(f) load_balancer_id = data['id'] responses.add(responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/load_balancers/%s' % load_balancer_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_load_balancer(load_balancer_id=load_balancer_id) self.assertEqual(r['state'], 'REMOVING')
class TestSharedStorage(unittest.TestCase): def setUp(self): self.client = OneAndOneService('<USER-API-KEY>') # 'GET' Methods @responses.activate def test_list_shared_storages(self): with open('mock-api/list-storages.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_shared_storages() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_shared_storage(self): with open('mock-api/get-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_shared_storage(shared_storage_id=shared_storage_id) self.assertEqual(r['id'], shared_storage_id) @responses.activate def test_servers_attached(self): with open('mock-api/storage-servers.json') as f: data = json.load(f) shared_storage_id = data[0]['id'] server_name = data[0]['name'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers' % shared_storage_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_servers_attached_storage( shared_storage_id=shared_storage_id) self.assertEqual(r[0]['name'], server_name) @responses.activate def test_get_server(self): with open('mock-api/get-server-storage.json') as f: data = json.load(f) shared_storage_id = 'SHARED_STORAGE_ID' server_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers/%s' % (shared_storage_id, server_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_shared_storage_server( shared_storage_id=shared_storage_id, server_id=server_id) self.assertEqual(r['id'], server_id) @responses.activate def test_get_credentials(self): with open('mock-api/list-credentials.json') as f: data = json.load(f) kerberos_content_file = data['kerberos_content_file'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/shared_storages/access', body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_credentials() self.assertEqual(r['kerberos_content_file'], kerberos_content_file) # 'POST' Methods @responses.activate def test_create_shared_storage(self): with open('mock-api/create-storage.json') as f: data = json.load(f) storage1 = SharedStorage(name=data['name'], description=data['description'], size=data['size']) responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/shared_storages', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_shared_storage(shared_storage=storage1) self.assertEqual(r['name'], storage1.name) self.assertEqual(r['description'], storage1.description) self.assertEqual(r['size'], storage1.size) @responses.activate def test_attach_servers(self): with open('mock-api/attach-server-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] server1 = AttachServer(server_id=data['servers'][0]['id'], rights=data['servers'][0]['rights']) server2 = AttachServer(server_id=data['servers'][1]['id'], rights=data['servers'][1]['rights']) servers = [server1, server2] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_server_shared_storage( shared_storage_id=shared_storage_id, server_ids=servers) self.assertEqual(r['servers'][0]['id'], server1.server_id) self.assertEqual(r['servers'][1]['id'], server2.server_id) # 'PUT' Methods @responses.activate def test_modify_shared_storage(self): with open('mock-api/modify-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] name = data['name'] description = data['description'] size = data['size'] responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.modify_shared_storage( shared_storage_id=shared_storage_id, name=name, description=description, size=size) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) self.assertEqual(r['size'], size) @responses.activate def test_change_password(self): with open('mock-api/change-password.json') as f: data = json.load(f) new_password = '******' responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/shared_storages/access', body=json.dumps(data), status=202, content_type="application/json") r = self.client.change_password(password=new_password) self.assertEqual(r['state'], 'CONFIGURING') # 'DELETE' Methods @responses.activate def test_delete_shared_storage(self): with open('mock-api/delete-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s' % shared_storage_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_shared_storage( shared_storage_id=shared_storage_id) self.assertEqual(r['state'], 'REMOVING') @responses.activate def test_detach_server_ss(self): with open('mock-api/detach-server-storage.json') as f: data = json.load(f) shared_storage_id = data['id'] server_id = 'SERVER_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/shared_storages/%s/servers/%s' % (shared_storage_id, server_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.detach_server_shared_storage( shared_storage_id=shared_storage_id, server_id=server_id) self.assertNotEqual(r['servers'][0]['id'], server_id)
# Create a block storage from oneandone.client import OneAndOneService, BlockStorage client = OneAndOneService('<API-TOKEN>') block_storage = BlockStorage(name='My new block storage', description='My block storage description', size=20, datacenter_id='908DC2072407C94C8054610AD5A53B8C') response = client.create_block_storage(block_storage) # List all block storages from oneandone.client import OneAndOneService, BlockStorage client = OneAndOneService('<API-TOKEN>') response = client.list_block_storages() # Retrieve a single block storage from oneandone.client import OneAndOneService client = OneAndOneService('<API-TOKEN>') response = client.get_block_storage(block_storage_id='') # Modify a block storage from oneandone.client import OneAndOneService client = OneAndOneService('<API-TOKEN>')
class TestFirewallPolicy(unittest.TestCase): def setUp(self): self.client = OneAndOneService('USER-API-KEY') # 'GET' Methods @responses.activate def test_list_policies(self): with open('mock-api/list-firewalls.json') as f: data = json.load(f) test_id = data[0]['id'] responses.add(responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies', body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_policies() self.assertEqual(r[0]['id'], test_id) @responses.activate def test_get_policy(self): with open('mock-api/get-firewall.json') as f: data = json.load(f) firewall_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall(firewall_id=firewall_id) self.assertEqual(r['id'], firewall_id) @responses.activate def test_list_firewall_servers(self): with open('mock-api/list-server-ips-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = data[0]['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_servers(firewall_id=firewall_id) self.assertEqual(r[0]['id'], server_ip_id) @responses.activate def test_get_firewall_server(self): with open('mock-api/get-server-ip-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips/%s' % (firewall_id, server_ip_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall_server(firewall_id=firewall_id, server_ip_id=server_ip_id) self.assertEqual(r['id'], server_ip_id) @responses.activate def test_list_policy_rules(self): with open('mock-api/list-fp-rules.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = data[0]['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.list_firewall_policy_rules(firewall_id=firewall_id) self.assertEqual(r[0]['id'], rule_id) @responses.activate def test_get_policy_rule(self): with open('mock-api/get-fp-rule.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = data['id'] responses.add( responses.GET, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules/%s' % (firewall_id, rule_id), body=json.dumps(data), status=200, content_type="application/json") r = self.client.get_firewall_policy_rule(firewall_id=firewall_id, rule_id=rule_id) self.assertEqual(r['id'], rule_id) # 'PUT' Methods @responses.activate def test_modify_firewall(self): with open('mock-api/modify-fp.json') as f: data = json.load(f) firewall_id = data['id'] name = data['name'] description = data['description'] responses.add( responses.PUT, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=200, content_type="application/json") r = self.client.modify_firewall(firewall_id=firewall_id, description=description) self.assertEqual(r['name'], name) self.assertEqual(r['description'], description) # 'POST' Methods @responses.activate def test_create_firewall_policy(self): with open('mock-api/create-fp.json') as f: data = json.load(f) fp1 = FirewallPolicy(name=data['name'], description=data['description']) rule1 = FirewallPolicyRule(protocol=data['rules'][0]['protocol'], port_from=data['rules'][0]['port_from'], port_to=data['rules'][0]['port_to'], source=data['rules'][0]['source']) rule2 = FirewallPolicyRule(protocol=data['rules'][1]['protocol'], port_from=data['rules'][1]['port_from'], port_to=data['rules'][1]['port_to'], source=data['rules'][1]['source']) rules = [rule1, rule2] responses.add(responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies', body=json.dumps(data), status=202, content_type="application/json") r = self.client.create_firewall_policy(firewall_policy=fp1, firewall_policy_rules=rules) self.assertEqual(r['name'], fp1.specs['name']) self.assertEqual(r['description'], fp1.specs['description']) self.assertEqual(r['rules'][0]['protocol'], rule1.rule_set['protocol']) self.assertEqual(r['rules'][1]['protocol'], rule2.rule_set['protocol']) @responses.activate def test_assign_servers(self): with open('mock-api/assign-ip-fp.json') as f: data = json.load(f) firewall_id = data['id'] server1 = AttachServer(server_ip_id=data['server_ips'][0]['id']) servers = [server1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.attach_server_firewall_policy(firewall_id=firewall_id, server_ips=servers) self.assertEqual(r['id'], firewall_id) self.assertEqual(r['server_ips'][0]['id'], server1.server_ip_id) @responses.activate def test_add_rule(self): with open('mock-api/add-rule-fp.json') as f: data = json.load(f) firewall_id = data['id'] rule1 = FirewallPolicyRule(protocol=data['rules'][2]['protocol'], port_from=data['rules'][2]['port_from'], port_to=data['rules'][2]['port_to'], source=data['rules'][2]['source']) rules = [rule1] responses.add( responses.POST, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.add_firewall_policy_rule(firewall_id=firewall_id, firewall_policy_rules=rules) self.assertEqual(r['rules'][2]['protocol'], rule1.rule_set['protocol']) self.assertEqual(r['rules'][2]['port_from'], rule1.rule_set['port_from']) self.assertEqual(r['rules'][2]['port_to'], rule1.rule_set['port_to']) self.assertEqual(r['rules'][2]['source'], rule1.rule_set['source']) # 'DELETE' Methods @responses.activate def test_remove_server_firewall(self): with open('mock-api/remove-ip-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' server_ip_id = 'SERVER_IP_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/server_ips/%s' % (firewall_id, server_ip_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_firewall_server(firewall_id=firewall_id, server_ip_id=server_ip_id) self.assertEqual(r['state'], 'CONFIGURING') self.assertEqual(r['server_ips'], []) @responses.activate def test_remove_firewall_rule(self): with open('mock-api/delete-rule-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' rule_id = 'RULE_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s/rules/%s' % (firewall_id, rule_id), body=json.dumps(data), status=202, content_type="application/json") r = self.client.remove_firewall_rule(firewall_id=firewall_id, rule_id=rule_id) self.assertNotEqual(len(r['rules']), 3) @responses.activate def test_delete_firewall(self): with open('mock-api/delete-fp.json') as f: data = json.load(f) firewall_id = 'FIREWALL_ID' responses.add( responses.DELETE, 'https://cloudpanel-api.1and1.com/v1/firewall_policies/%s' % firewall_id, body=json.dumps(data), status=202, content_type="application/json") r = self.client.delete_firewall(firewall_id=firewall_id) self.assertEqual(r['state'], 'REMOVING')
# List all the of appliances that you can use for creating a server from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server_appliances = client.list_appliances() # Retrieve a specific appliance from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') server_appliance = client.get_appliance(appliance_id='')
# List all users from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') users = client.list_users() # Retrieve a user from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') user = client.get_user(user_id='') # Retrieve a user's API privileges from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') api_info = client.api_info(user_id='') # Retrieve a user's API key from oneandone.client import OneAndOneService client = OneAndOneService('675fbe491b27896b57e76867604f8255') api_key = client.show_api_key(user_id='') # List IPs from which API access is allowed for a user from oneandone.client import OneAndOneService