Python包装Digital Ocean API V2
项目描述
Python包装Digital Ocean API V2。
安装
pip install dosa
用法
import dosa
API_KEY = 'Your API Key'
dosa.set_debug() # enables debug logs
client = dosa.Client(api_key=API_KEY)
# Droplets
client.droplets.list()
status, result = client.droplets.create(name='terminator', region='nyc2',\
size='512mb', image='ubuntu-14-04-x32', ssh_keys=[12345])
new_droplet_id = result['id']
# Droplet
new_droplet = client.Droplet(new_droplet_id)
print(new_droplet.info())
## shortcuts
new_droplet.status()
new_droplet.ip_addresses()
client.droplets.delete(new_droplet_id)
# Get all available size configs
client.sizes.list()
# SSH Keys
pub_key = open('~/.ssh/id_rsa.pub').read()
client.keys.create(name='RSA key', public_key=pub_key)
client.keys.list()
# Images
client.images.list()
client.images.all()
client.images.search('ubuntu', 'sgp1', show_op=True)
# Domains
client.domains.list()
client.domains.all()
client.domains.create(name='example.com', ip_address='1.2.3.4')
client.domains.delete(id='example.com')
### Get specific domain
domain = client.Domain(domain='example.com')
domain.info()
# Domain Records
dr = client.DomainRecords(domain='example.com')
dr.list()
dr.create(type='A', name='example.com', data='162.10.66.0')
### Get specific domain record for a domain
record = dr.Record(record_id='7976006')
record.info()
record.update(name='new.example.com')
# Firewalls
## Create a firewall
params = {
'inbound_rules': [{'ports': '22',
'protocol': 'tcp',
'sources': {'addresses': ['0.0.0.0/0', '::/0']}},
{'ports': '80',
'protocol': 'tcp',
'sources': {'addresses': ['0.0.0.0/0', '::/0']}}],
'name': 'firewall',
'outbound_rules': [{'destinations': {'addresses': ['0.0.0.0/0', '::/0']},
'ports': 'all',
'protocol': 'tcp'}],
'tags': []}
firewall = client.firewalls.create(**params)
# search firewall by name
firewall = client.firewalls.get_by_name('firewall')
## add a droplet to a firewall
firewall.add_droplet(new_droplet_id)
## remove a droplet from a firewall
firewall.remove_droplet(new_droplet_id)
## delete a firewall
client.firewalls.delete(id=firewall.id)
# Extras
# $ ls keys/
# rsa_pub1.id rsa_pub2.key rsa_pub3.key
keys_dir = 'keys'
client.sync_ssh_keys(keys_dir)
注意
图像搜索
>>> client.images.search('ubuntu', region='sgp1', show_op=True)
上面的代码片段搜索包含描述或slug中的ubuntu的图像。由于指定了区域(sgp1),因此只考虑sgp1区域的图像。如果没有指定区域,则包括所有区域。
测试
>>> tox
要运行测试,请在存储库的根目录中运行tox
。它将在tox.ini
中创建虚拟环境,安装依赖项,并运行pytest。
致谢
在Scroll.in的项目中创建。