Merge pull request #998 from linefeedse/digitalocean-user-data

DigitalOcean API version 2 with user_data option
This commit is contained in:
Sam Kottler 2015-04-05 15:26:54 -04:00
commit 7e1d63136f

View file

@ -33,12 +33,9 @@ options:
- Indicate desired state of the target.
default: present
choices: ['present', 'active', 'absent', 'deleted']
client_id:
api_token:
description:
- DigitalOcean manager id.
api_key:
description:
- DigitalOcean api key.
- DigitalOcean api token.
id:
description:
- Numeric, the droplet id you want to operate on.
@ -53,16 +50,16 @@ options:
choices: [ "yes", "no" ]
size_id:
description:
- Numeric, this is the id of the size you would like the droplet created with.
- This is the slug of the size you would like the droplet created with.
image_id:
description:
- Numeric, this is the id of the image you would like the droplet created with.
- This is the slug of the image you would like the droplet created with.
region_id:
description:
- "Numeric, this is the id of the region you would like your server to be created in."
- This is the slug of the region you would like your server to be created in.
ssh_key_ids:
description:
- Optional, comma separated list of ssh_key_ids that you would like to be added to the server.
- Optional, array of of ssh_key_ids that you would like to be added to the server.
virtio:
description:
- "Bool, turn on virtio driver in droplet for improved network and storage I/O."
@ -81,6 +78,12 @@ options:
version_added: "1.6"
default: "no"
choices: [ "yes", "no" ]
user_data:
description:
- opaque blob of data which is made available to the droplet
version_added: "1.10"
required: false
default: None
wait:
description:
- Wait for the droplet to be in state 'running' before returning. If wait is "no" an ip_address may not be returned.
@ -95,8 +98,8 @@ options:
- The public SSH key you want to add to your account.
notes:
- Two environment variables can be used, DO_CLIENT_ID and DO_API_KEY.
- Version 1 of DigitalOcean API is used.
- Two environment variables can be used, DO_API_KEY and DO_API_TOKEN. They both refer to the v2 token.
- Version 2 of DigitalOcean API is used.
requirements: [ dopy ]
'''
@ -111,8 +114,7 @@ EXAMPLES = '''
command=ssh
name=my_ssh_key
ssh_pub_key='ssh-rsa AAAA...'
client_id=XXX
api_key=XXX
api_token=XXX
# Create a new Droplet
# Will return the droplet details including the droplet id (used for idempotence)
@ -121,11 +123,10 @@ EXAMPLES = '''
state=present
command=droplet
name=mydroplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
api_token=XXX
size_id=2gb
region_id=ams2
image_id=fedora-19-x64
wait_timeout=500
register: my_droplet
- debug: msg="ID is {{ my_droplet.droplet.id }}"
@ -140,11 +141,10 @@ EXAMPLES = '''
command=droplet
id=123
name=mydroplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
api_token=XXX
size_id=2gb
region_id=ams2
image_id=fedora-19-x64
wait_timeout=500
# Create a droplet with ssh key
@ -154,13 +154,12 @@ EXAMPLES = '''
- digital_ocean: >
state=present
ssh_key_ids=id1,id2
ssh_key_ids=[id1,id2]
name=mydroplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
api_token=XXX
size_id=2gb
region_id=ams2
image_id=fedora-19-x64
'''
import sys
@ -171,11 +170,11 @@ try:
import dopy
from dopy.manager import DoError, DoManager
except ImportError, e:
print "failed=True msg='dopy >= 0.2.3 required for this module'"
print "failed=True msg='dopy >= 0.3.2 required for this module'"
sys.exit(1)
if dopy.__version__ < '0.2.3':
print "failed=True msg='dopy >= 0.2.3 required for this module'"
if dopy.__version__ < '0.3.2':
print "failed=True msg='dopy >= 0.3.2 required for this module'"
sys.exit(1)
class TimeoutError(DoError):
@ -232,14 +231,14 @@ class Droplet(JsonfyMixIn):
return self.manager.destroy_droplet(self.id, scrub_data=True)
@classmethod
def setup(cls, client_id, api_key):
cls.manager = DoManager(client_id, api_key)
def setup(cls, api_token):
cls.manager = DoManager(None, api_token, api_version=2)
@classmethod
def add(cls, name, size_id, image_id, region_id, ssh_key_ids=None, virtio=True, private_networking=False, backups_enabled=False):
def add(cls, name, size_id, image_id, region_id, ssh_key_ids=None, virtio=True, private_networking=False, backups_enabled=False, user_data=None):
private_networking_lower = str(private_networking).lower()
backups_enabled_lower = str(backups_enabled).lower()
json = cls.manager.new_droplet(name, size_id, image_id, region_id, ssh_key_ids, virtio, private_networking_lower, backups_enabled_lower)
json = cls.manager.new_droplet(name, size_id, image_id, region_id, ssh_key_ids, virtio, private_networking_lower, backups_enabled_lower,user_data)
droplet = cls(json)
return droplet
@ -279,8 +278,8 @@ class SSH(JsonfyMixIn):
return True
@classmethod
def setup(cls, client_id, api_key):
cls.manager = DoManager(client_id, api_key)
def setup(cls, api_token):
cls.manager = DoManager(None, api_token, api_version=2)
@classmethod
def find(cls, name):
@ -310,9 +309,7 @@ def core(module):
return v
try:
# params['client_id'] will be None even if client_id is not passed in
client_id = module.params['client_id'] or os.environ['DO_CLIENT_ID']
api_key = module.params['api_key'] or os.environ['DO_API_KEY']
api_token = module.params['api_token'] or os.environ['DO_API_TOKEN'] or os.environ['DO_API_KEY']
except KeyError, e:
module.fail_json(msg='Unable to load %s' % e.message)
@ -321,7 +318,7 @@ def core(module):
state = module.params['state']
if command == 'droplet':
Droplet.setup(client_id, api_key)
Droplet.setup(api_token)
if state in ('active', 'present'):
# First, try to find a droplet by id.
@ -344,6 +341,7 @@ def core(module):
virtio=module.params['virtio'],
private_networking=module.params['private_networking'],
backups_enabled=module.params['backups_enabled'],
user_data=module.params.get('user_data'),
)
if droplet.is_powered_on():
@ -370,10 +368,10 @@ def core(module):
module.exit_json(changed=False, msg='The droplet is not found.')
event_json = droplet.destroy()
module.exit_json(changed=True, event_id=event_json['event_id'])
module.exit_json(changed=True)
elif command == 'ssh':
SSH.setup(client_id, api_key)
SSH.setup(api_token)
name = getkeyordie('name')
if state in ('active', 'present'):
key = SSH.find(name)
@ -395,18 +393,18 @@ def main():
argument_spec = dict(
command = dict(choices=['droplet', 'ssh'], default='droplet'),
state = dict(choices=['active', 'present', 'absent', 'deleted'], default='present'),
client_id = dict(aliases=['CLIENT_ID'], no_log=True),
api_key = dict(aliases=['API_KEY'], no_log=True),
api_token = dict(aliases=['API_TOKEN'], no_log=True),
name = dict(type='str'),
size_id = dict(type='int'),
image_id = dict(type='int'),
region_id = dict(type='int'),
size_id = dict(),
image_id = dict(),
region_id = dict(),
ssh_key_ids = dict(default=''),
virtio = dict(type='bool', default='yes'),
private_networking = dict(type='bool', default='no'),
backups_enabled = dict(type='bool', default='no'),
id = dict(aliases=['droplet_id'], type='int'),
unique_name = dict(type='bool', default='no'),
user_data = dict(default=None),
wait = dict(type='bool', default=True),
wait_timeout = dict(default=300, type='int'),
ssh_pub_key = dict(type='str'),