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