def test_create(self, post): response = requests.Response() response._content = b'''{ "ssh_key": { "id": 2, "fingerprint": "fingerprint", "public_key": "ssh-rsa example", "name": "Example Key" } }''' post.return_value = response client = ClientV2(token='token') data = client.keys.create(name='Example Key', public_key='ssh-rsa example') post.assert_called_with( 'https://api.digitalocean.com/v2/account/keys', headers={ 'content-type': 'application/json', 'Authorization': 'Bearer token' }, data='{"public_key": "ssh-rsa example", "name": "Example Key"}') assert len(data) == 1 assert data['ssh_key']['id'] == 2 assert data['ssh_key']['fingerprint'] == 'fingerprint' assert data['ssh_key']['public_key'] == 'ssh-rsa example' assert data['ssh_key']['name'] == 'Example Key'
def test_get(self, get): response = requests.Response() response._content = b'''{ "action": { "id": 2, "status": "in-progress", "type": "test", "started_at": "2014-09-05T02:01:58Z", "completed_at": null, "resource_id": null, "resource_type": "backend", "region": "nyc1" } }''' get.return_value = response client = ClientV2(token='token') data = client.actions.get(action_id=2) get.assert_called_with('https://api.digitalocean.com/v2/actions/2', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['action']['id'] == 2 assert data['action']['status'] == 'in-progress' assert data['action']['type'] == 'test' assert data['action']['started_at'] == '2014-09-05T02:01:58Z' assert data['action']['completed_at'] is None assert data['action']['resource_id'] is None assert data['action']['resource_type'] == 'backend' assert data['action']['region'] == 'nyc1'
def test_create(self, post): response = requests.Response() response._content = b'''{ "domain": { "name": "example.com", "ttl": 1800, "zone_file": null } }''' post.return_value = response client = ClientV2(token='token') data = client.domains.create(name='example.com', ip_address='127.0.0.1') post.assert_called_with( 'https://api.digitalocean.com/v2/domains', headers={ 'content-type': 'application/json', 'Authorization': 'Bearer token' }, data='{"ip_address": "127.0.0.1", "name": "example.com"}') assert len(data) == 1 assert data['domain']['name'] == 'example.com' assert data['domain']['ttl'] == 1800 assert data['domain']['zone_file'] is None
def test_list_droplet_kernels(self, get): response = requests.Response() response._content = b'''{ "kernels": [ { "id": 61833229, "name": "Ubuntu 14.04 x32", "version": "3.13.0-24-generic" }, { "id": 485432972, "name": "Ubuntu 14.04 x64", "version": "3.13.0-24-generic" } ], "meta": { "total": 2 } }''' get.return_value = response client = ClientV2(token='token') data = client.droplets.list_droplet_kernels(droplet_id=123) get.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123/kernels', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['kernels']) == 2 assert data['meta']['total'] == 2 assert data['kernels'][0]['id'] == 61833229 assert data['kernels'][0]['name'] == 'Ubuntu 14.04 x32' assert data['kernels'][0]['version'] == '3.13.0-24-generic'
def test_update_domain_record(self, put): response = requests.Response() response._content = b'''{ "domain_record": { "id": 26, "type": "CNAME", "name": "new_name", "data": "@", "priority": null, "port": null, "weight": null } }''' put.return_value = response client = ClientV2(token='token') data = client.domains.update_domain_record(domain_id='example.com', record_id=26, name='new_name') put.assert_called_with( 'https://api.digitalocean.com/v2/domains/example.com/records/26', headers={ 'content-type': 'application/json', 'Authorization': 'Bearer token' }, params={'name': 'new_name'}) assert len(data) == 1 assert data['domain_record']['id'] == 26 assert data['domain_record']['name'] == 'new_name'
def test_enable_private_networking(self, post): response = requests.Response() response._content = b'''{ "action": { "id": 4, "status": "in-progress", "type": "enable_private_networking", "started_at": "2014-09-05T02:02:01Z", "completed_at": null, "resource_id": 123, "resource_type": "droplet", "region": "nyc1" } }''' post.return_value = response client = ClientV2(token='token') data = client.droplets.enable_private_networking(droplet_id=123) post.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123/actions', headers={'content-type': 'application/json', 'Authorization': 'Bearer token'}, data='{"type": "enable_private_networking"}') assert len(data) == 1 assert data['action']['id'] == 4 assert data['action']['type'] == 'enable_private_networking' assert data['action']['resource_id'] == 123 assert data['action']['resource_type'] == 'droplet'
def test_get_domain_record(self, get): response = requests.Response() response._content = b'''{ "domain_record": { "id": 10, "type": "CNAME", "name": "example", "data": "@", "priority": null, "port": null, "weight": null } }''' get.return_value = response client = ClientV2(token='token') data = client.domains.get_domain_record(domain_id='example.com', record_id=10) get.assert_called_with( 'https://api.digitalocean.com/v2/domains/example.com/records/10', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['domain_record']['id'] == 10
def test_get(self, get): response = requests.Response() response._content = b'''{ "ssh_key": { "id": 2, "fingerprint": "fingerprint", "public_key": "ssh-rsa example", "name": "Example Key" } }''' get.return_value = response client = ClientV2(token='token') data = client.keys.get(key_id=2) get.assert_called_with( 'https://api.digitalocean.com/v2/account/keys/2', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['ssh_key']['id'] == 2 assert data['ssh_key']['fingerprint'] == 'fingerprint' assert data['ssh_key']['public_key'] == 'ssh-rsa example' assert data['ssh_key']['name'] == 'Example Key'
def test_update(self, put): response = requests.Response() response._content = b'''{ "ssh_key": { "id": 4, "fingerprint": "fingerprint", "public_key": "ssh-rsa example", "name": "New Name" } }''' put.return_value = response client = ClientV2(token='token') data = client.keys.update(key_id=4, name='New Name') put.assert_called_with( 'https://api.digitalocean.com/v2/account/keys/4', headers={ 'content-type': 'application/json', 'Authorization': 'Bearer token' }, params={'name': 'New Name'}) assert len(data) == 1 assert data['ssh_key']['id'] == 4 assert data['ssh_key']['fingerprint'] == 'fingerprint' assert data['ssh_key']['public_key'] == 'ssh-rsa example' assert data['ssh_key']['name'] == 'New Name'
def test_get_image_action(self, get): response = requests.Response() response._content = b'''{ "action": { "id": 22, "status": "in-progress", "type": "transfer", "started_at": "2014-09-05T02:02:07Z", "completed_at": null, "resource_id": 449676390, "resource_type": "image", "region": "nyc1" } }''' get.return_value = response client = ClientV2(token='token') data = client.images.get_image_action(image_id=449676390, action_id=22) get.assert_called_with( 'https://api.digitalocean.com/v2/images/449676390/actions/22', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['action']['id'] == 22 assert data['action']['status'] == 'in-progress' assert data['action']['type'] == 'transfer' assert data['action']['started_at'] == '2014-09-05T02:02:07Z' assert data['action']['completed_at'] is None assert data['action']['resource_id'] == 449676390 assert data['action']['resource_type'] == 'image' assert data['action']['region'] == 'nyc1'
def test_get_droplet_action(self, get): response = requests.Response() response._content = b'''{ "action": { "id": 3, "status": "in-progress", "type": "create", "started_at": "2014-09-05T02:02:00Z", "completed_at": null, "resource_id": 3, "resource_type": "droplet", "region": "nyc1" } }''' get.return_value = response client = ClientV2(token='token') data = client.droplets.get_droplet_action(droplet_id=123, action_id=3) get.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123/actions/3', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['action']['id'] == 3
def test_transfer(self, post): response = requests.Response() response._content = b'''{ "action": { "id": 23, "status": "in-progress", "type": "transfer", "started_at": "2014-09-05T02:02:08Z", "completed_at": null, "resource_id": 449676391, "resource_type": "image", "region": "nyc1" } }''' post.return_value = response client = ClientV2(token='token') data = client.images.transfer(image_id=449676391, region='nyc1') post.assert_called_with( 'https://api.digitalocean.com/v2/images/449676391/actions', headers={'content-type': 'application/json', 'Authorization': 'Bearer token'}, data='{"region": "nyc1", "type": "transfer"}') assert len(data) == 1 assert data['action']['id'] == 23 assert data['action']['status'] == 'in-progress' assert data['action']['type'] == 'transfer' assert data['action']['started_at'] == '2014-09-05T02:02:08Z' assert data['action']['completed_at'] is None assert data['action']['resource_id'] == 449676391 assert data['action']['resource_type'] == 'image' assert data['action']['region'] == 'nyc1'
def test_update(self, put): response = requests.Response() response._content = b'''{ "image": { "id": 449676394, "name": "New Image Name", "distribution": null, "slug": null, "public": false, "regions": [ "region--3" ], "created_at": "2014-09-05T02:02:09Z" } }''' put.return_value = response client = ClientV2(token='token') data = client.images.update(image_id=449676394, name='New Image Name') put.assert_called_with( 'https://api.digitalocean.com/v2/images/449676394', headers={'content-type': 'application/json', 'Authorization': 'Bearer token'}, params={'name': 'New Image Name'}) assert len(data) == 1 assert data['image']['id'] == 449676394 assert data['image']['name'] == 'New Image Name' assert data['image']['distribution'] is None assert data['image']['slug'] is None assert data['image']['public'] is False assert data['image']['regions'] == ['region--3'] assert data['image']['created_at'] == '2014-09-05T02:02:09Z'
def test_delete(self, delete): response = requests.Response() response._content = b'' response.status_code = 204 delete.return_value = response client = ClientV2(token='token') data = client.keys.delete(key_id=4) delete.assert_called_with( 'https://api.digitalocean.com/v2/account/keys/4', headers={ 'content-type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer token' }, params={}) assert data == '' data = client.keys.delete(key_id='fingerprint') delete.assert_called_with( 'https://api.digitalocean.com/v2/account/keys/fingerprint', headers={ 'content-type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer token' }, params={}) assert data == ''
def test_all(self, get): response = requests.Response() response._content = b'''{ "ssh_keys": [ { "id": 1, "fingerprint": "fingerprint", "public_key": "ssh-rsa public_key", "name": "Example Key" } ], "meta": { "total": 1 } }''' get.return_value = response client = ClientV2(token='token') data = client.keys.all() get.assert_called_with('https://api.digitalocean.com/v2/account/keys', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['ssh_keys']) == 1 assert data['meta']['total'] == 1 assert data['ssh_keys'][0]['id'] == 1 assert data['ssh_keys'][0]['fingerprint'] == 'fingerprint' assert data['ssh_keys'][0]['public_key'] == 'ssh-rsa public_key' assert data['ssh_keys'][0]['name'] == 'Example Key'
def test_all(self, get): response = requests.Response() response._content = b'''{ "domains": [ { "name": "example.com", "ttl": 1800, "zone_file": "Example zone file text..." } ], "meta": { "total": 1 } }''' get.return_value = response client = ClientV2(token='token') data = client.domains.all() get.assert_called_with('https://api.digitalocean.com/v2/domains', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['domains']) == 1 assert data['meta']['total'] == 1 assert data['domains'][0]['name'] == 'example.com' assert data['domains'][0]['ttl'] == 1800 assert data['domains'][0]['zone_file'] == 'Example zone file text...'
def get_droplet_info(ip): client = ClientV2(token=get_api_key()) data = client.droplets.all() for droplet in data["droplets"]: for v4 in droplet["networks"]["v4"]: if v4["ip_address"] == ip: return droplet["id"], droplet["name"] print "[-] Failed to find droplet ID for IP address %s on this account!" % ip sys.exit(1)
def test_all(self, get): response = requests.Response() response._content = b''' { "sizes": [ { "slug": "512mb", "memory": 512, "vcpus": 1, "disk": 20, "transfer": 1, "price_monthly": 5.0, "price_hourly": 0.00744, "regions": [ "nyc1", "sfo1", "ams1" ] }, { "slug": "1gb", "memory": 1024, "vcpus": 2, "disk": 30, "transfer": 2, "price_monthly": 10.0, "price_hourly": 0.01488, "regions": [ "nyc1", "sfo1", "ams1" ] } ], "meta": { "total": 2 } }''' get.return_value = response client = ClientV2(token='token') data = client.sizes.all() get.assert_called_with( 'https://api.digitalocean.com/v2/sizes', headers={'Authorization': 'Bearer token'}, params={}) assert data['sizes'][0]['slug'] == '512mb' assert data['sizes'][0]['memory'] == 512 assert data['sizes'][0]['vcpus'] == 1 assert data['sizes'][0]['disk'] == 20 assert data['sizes'][0]['transfer'] == 1 assert data['sizes'][0]['price_monthly'] == 5.0 assert data['sizes'][0]['price_hourly'] == 0.00744 assert data['sizes'][0]['regions'] == ["nyc1", "sfo1", "ams1"]
def main(): global config if not parse_arguments(): return if not config['record_values']: config['record_values'] = [] public_ip = get_public_ip() for i in range(len(config['records'])): config['record_values'].append(public_ip) client = None try: client = ClientV2(config['api-key']) except: print( "Cannot connect to DigitalOcean API with specified api-key. Exiting..." ) return domain_records = None try: domain_records = client.domains.list_domain_records( config['domain']).get('domain_records') except: print("Cannot retrieve " + config['domain'] + "'s domain records. Exiting...") return for i in range(len(config['records'])): record = config['records'][i] print("Updating " + record + " in " + config['domain'] + " to point to " + config['record_values'][i]) domain_record = None for obj_dmrec in domain_records: if obj_dmrec['type'] == 'A' and obj_dmrec['name'] == record: domain_record = obj_dmrec if not domain_record: print("Cannot find A record " + record + " in " + config['domain'] + ", skipping this record...") continue try: if domain_record['data'] == config['record_values'][i]: print("No need to update") else: client.domains.update_domain_record( config['domain'], domain_record['id'], {'data': config['record_values'][i]}) except: print("Cannot update " + record + " in " + config['domain'] + ", skipping this record...") continue print("done.")
def add_key(): client = ClientV2(token=get_api_key()) data = client.keys.all() for key in data["ssh_keys"]: if get_pub_key() == key["public_key"]: print "[+] SSH key %s is already registered under this account as '%s', using that key" % ( KEY_PATH, key["name"]) return key["id"] key_name = "%s CTF" % CHALLENGE print "[+] Didn't find %s in the list of registered SSH keys, adding as '%s'" % ( KEY_PATH, key_name) data = client.keys.create(name=key_name, public_key=get_pub_key()) return data["id"]
def test_all(self, get): response = requests.Response() response._content = b'''{ "images": [ { "id": 119192817, "name": "Ubuntu 13.04", "distribution": "ubuntu", "slug": "ubuntu1304", "public": true, "regions": [ "nyc1" ], "created_at": "2014-09-05T02:02:08Z" }, { "id": 449676376, "name": "Ubuntu 13.04", "distribution": "ubuntu", "slug": "ubuntu1404", "public": true, "regions": [ "nyc1" ], "created_at": "2014-09-05T02:02:08Z" } ], "meta": { "total": 2 } }''' get.return_value = response client = ClientV2(token='token') data = client.images.all() get.assert_called_with( 'https://api.digitalocean.com/v2/images', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['images']) == 2 assert data['meta']['total'] == 2 assert data['images'][0]['id'] == 119192817 assert data['images'][0]['name'] == 'Ubuntu 13.04' assert data['images'][0]['distribution'] == 'ubuntu' assert data['images'][0]['slug'] == 'ubuntu1304' assert data['images'][0]['public'] is True assert data['images'][0]['regions'] == ['nyc1'] assert data['images'][0]['created_at'] == '2014-09-05T02:02:08Z'
def test_get(self, get): response = requests.Response() response._content = b'''{ "image": { "id": 449676376, "name": "Ubuntu 13.04", "distribution": "ubuntu", "slug": "ubuntu1404", "public": true, "regions": [ "nyc1" ], "created_at": "2014-09-05T02:02:08Z" } }''' get.return_value = response client = ClientV2(token='token') data = client.images.get(image_id=449676376) get.assert_called_with( 'https://api.digitalocean.com/v2/images/449676376', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['image']['id'] == 449676376 assert data['image']['name'] == 'Ubuntu 13.04' assert data['image']['distribution'] == 'ubuntu' assert data['image']['slug'] == 'ubuntu1404' assert data['image']['public'] is True assert data['image']['regions'] == ['nyc1'] assert data['image']['created_at'] == '2014-09-05T02:02:08Z' data = client.images.get(image_id='ubuntu1404') get.assert_called_with( 'https://api.digitalocean.com/v2/images/ubuntu1404', headers={'Authorization': 'Bearer token'}, params={}) assert len(data) == 1 assert data['image']['id'] == 449676376 assert data['image']['name'] == 'Ubuntu 13.04' assert data['image']['distribution'] == 'ubuntu' assert data['image']['slug'] == 'ubuntu1404' assert data['image']['public'] is True assert data['image']['regions'] == ['nyc1'] assert data['image']['created_at'] == '2014-09-05T02:02:08Z'
def staging(): version = prompt("Please specify the website's version: ") client = ClientV2(token=sensitive.DIGITAOCEAN_API_TOKEN) # images = client.images.all() # for image in images["images"]: # print(image['slug'] + ": %s" % image['id']) # sys.exit() ssh_key_id = client.keys.all()['ssh_keys'][0]['id'] droplet = client.droplets.create(name='your.domain.com-v' + version, region='sfo1', size='512mb', image=14169868, ssh_keys=ssh_key_id) droplet_id = droplet['droplet']['id'] ip_address = None timeout = time.time() + 60 * 2 while True: time.sleep(5) droplet_info = client.droplets.get(droplet_id=droplet_id) if droplet_info['droplet']['status'] == 'active': ip_address = droplet_info['droplet']['networks']['v4'][0][ 'ip_address'] break if time.time() > timeout: print red( 'Droplet were not created within two minutes in DigitalOcean.') sys.exit() time.sleep(60) env.user = '******' env.password = None env.host_string = ip_address env.key_filename = '~/.ssh/id_rsa' env.name = 'staging' print(green("Droplet has been created successfully.")) print("It's ip address: " + ip_address)
def new_droplet(name, key_id): client = ClientV2(token=get_api_key()) data = client.droplets.create(name=name, region=IMAGE_REGION, size=IMAGE_SIZE, image=IMAGE_ID, ssh_keys=[key_id]) droplet_id = data["droplet"]["id"] print "[+] Initiated new droplet creation (%s, %s, %s, %s), waiting for completion..." % ( name, IMAGE_ID, IMAGE_SIZE, IMAGE_REGION) while True: time.sleep(5) data = client.droplets.get_droplet_actions(droplet_id=droplet_id) if data["actions"][0]["status"] == "completed": break data = client.droplets.get(droplet_id=droplet_id) return droplet_id, data["droplet"]["networks"]["v4"][0]["ip_address"]
def test_delete(self, delete): response = requests.Response() response._content = b'' response.status_code = 204 delete.return_value = response client = ClientV2(token='token') data = client.droplets.delete(droplet_id=123) delete.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123', headers={'content-type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer token'}, params={}) assert data == ''
def reboot(self, ip_address=None, password=None): if not ip_address or not password: return "Usage: /reboot?ip_address=1.2.3.4&password=password" body = "" for droplet in get_droplets(): if droplet["ip_address"] == ip_address and droplet[ "password"] == password: client = ClientV2(token=get_api_key()) ret = client.droplets.power_cycle(droplet_id=droplet["id"]) body += "Power cycling %s:<br><br><pre>\n" % ip_address body += pformat(ret) body += "</pre>" return body return "Couldn't find that IP address / password combination"
def test_delete_domain_record(self, delete): response = requests.Response() response._content = b'' response.status_code = 204 delete.return_value = response client = ClientV2(token='token') data = client.domains.delete_domain_record(domain_id='example.com', record_id=10) delete.assert_called_with( 'https://api.digitalocean.com/v2/domains/example.com/records/10', headers={ 'content-type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer token' }, params={}) assert data == ''
def test_get_droplet_actions(self, get): response = requests.Response() response._content = b'''{ "actions": [ { "id": 19, "status": "in-progress", "type": "create", "started_at": "2014-09-05T02:02:06Z", "completed_at": null, "resource_id": 123, "resource_type": "droplet", "region": "nyc1" } ], "meta": { "total": 1 } }''' get.return_value = response client = ClientV2(token='token') data = client.droplets.get_droplet_actions(droplet_id=123) get.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123/actions', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['actions']) == 1 assert data['meta']['total'] == 1 assert data['actions'][0]['id'] == 19 assert data['actions'][0]['status'] == 'in-progress' assert data['actions'][0]['type'] == 'create' assert data['actions'][0]['started_at'] == '2014-09-05T02:02:06Z' assert data['actions'][0]['completed_at'] is None assert data['actions'][0]['resource_id'] == 123 assert data['actions'][0]['resource_type'] == 'droplet' assert data['actions'][0]['region'] == 'nyc1'
def test_get_droplet_backups(self, get): response = requests.Response() response._content = b'''{ "backups": [ ], "meta": { "total": 0 } }''' get.return_value = response client = ClientV2(token='token') data = client.droplets.get_droplet_backups(droplet_id=123) get.assert_called_with( 'https://api.digitalocean.com/v2/droplets/123/backups', headers={'Authorization': 'Bearer token'}, params={}) assert len(data['backups']) == 0 assert data['meta']['total'] == 0
def test_create_domain_record(self, post): response = requests.Response() response._content = b'''{ "domain_record": { "id": 16, "type": "AAAA", "name": "subdomain", "data": "2001:db8::ff00:42:8329", "priority": null, "port": null, "weight": null } }''' post.return_value = response client = ClientV2(token='token') data = client.domains.create_domain_record( domain_id=123, name='subdomain', data='2001:db8::ff00:42:8329', rtype='AAAA') post.assert_called_with( 'https://api.digitalocean.com/v2/domains/123/records', headers={ 'content-type': 'application/json', 'Authorization': 'Bearer token' }, data=('{"data": "2001:db8::ff00:42:8329",' ' "type": "AAAA", "name": "subdomain"}')) assert len(data) == 1 assert data['domain_record']['id'] == 16 assert data['domain_record']['type'] == 'AAAA' assert data['domain_record']['name'] == 'subdomain' assert data['domain_record']['data'] == '2001:db8::ff00:42:8329'