Set variable types, defined choices, and cleaned up whitespace.

This commit is contained in:
Ethan Devenport 2016-08-31 06:24:55 +00:00
parent e9fd5ad5dc
commit 86285e8824
2 changed files with 25 additions and 18 deletions

View file

@ -580,15 +580,16 @@ def main():
datacenter=dict(), datacenter=dict(),
name=dict(), name=dict(),
image=dict(), image=dict(),
cores=dict(default=2), cores=dict(type='int', default=2),
ram=dict(default=2048), ram=dict(type='int', default=2048),
cpu_family=dict(default='AMD_OPTERON'), cpu_family=dict(choices=['AMD_OPTERON', 'INTEL_XEON'],
volume_size=dict(default=10), default='AMD_OPTERON'),
disk_type=dict(default='HDD'), volume_size=dict(type='int', default=10),
disk_type=dict(choices=['HDD', 'SSD'], default='HDD'),
image_password=dict(default=None), image_password=dict(default=None),
ssh_keys=dict(type='list', default=[]), ssh_keys=dict(type='list', default=[]),
bus=dict(default='VIRTIO'), bus=dict(default='VIRTIO'),
lan=dict(default=1), lan=dict(type='int', default=1),
count=dict(type='int', default=1), count=dict(type='int', default=1),
auto_increment=dict(type='bool', default=True), auto_increment=dict(type='bool', default=True),
instance_ids=dict(type='list', default=[]), instance_ids=dict(type='list', default=[]),

View file

@ -41,7 +41,7 @@ options:
required: false required: false
default: VIRTIO default: VIRTIO
choices: [ "IDE", "VIRTIO"] choices: [ "IDE", "VIRTIO"]
image: image:
description: description:
- The system image ID for the volume, e.g. a3eae284-a2fe-11e4-b187-5f1f641608c8. This can also be a snapshot image ID. - The system image ID for the volume, e.g. a3eae284-a2fe-11e4-b187-5f1f641608c8. This can also be a snapshot image ID.
required: true required: true
@ -63,13 +63,13 @@ options:
choices: [ "HDD", "SSD" ] choices: [ "HDD", "SSD" ]
licence_type: licence_type:
description: description:
- The licence type for the volume. This is used when the image is non-standard. - The licence type for the volume. This is used when the image is non-standard.
required: false required: false
default: UNKNOWN default: UNKNOWN
choices: ["LINUX", "WINDOWS", "UNKNOWN" , "OTHER"] choices: ["LINUX", "WINDOWS", "UNKNOWN" , "OTHER"]
count: count:
description: description:
- The number of volumes you wish to create. - The number of volumes you wish to create.
required: false required: false
default: 1 default: 1
auto_increment: auto_increment:
@ -170,6 +170,7 @@ def _wait_for_completion(profitbricks, promise, wait_timeout, msg):
promise['requestId'] promise['requestId']
) + '" to complete.') ) + '" to complete.')
def _create_volume(module, profitbricks, datacenter, name): def _create_volume(module, profitbricks, datacenter, name):
size = module.params.get('size') size = module.params.get('size')
bus = module.params.get('bus') bus = module.params.get('bus')
@ -201,20 +202,22 @@ def _create_volume(module, profitbricks, datacenter, name):
except Exception as e: except Exception as e:
module.fail_json(msg="failed to create the volume: %s" % str(e)) module.fail_json(msg="failed to create the volume: %s" % str(e))
return volume_response return volume_response
def _delete_volume(module, profitbricks, datacenter, volume): def _delete_volume(module, profitbricks, datacenter, volume):
try: try:
profitbricks.delete_volume(datacenter, volume) profitbricks.delete_volume(datacenter, volume)
except Exception as e: except Exception as e:
module.fail_json(msg="failed to remove the volume: %s" % str(e)) module.fail_json(msg="failed to remove the volume: %s" % str(e))
def create_volume(module, profitbricks): def create_volume(module, profitbricks):
""" """
Creates a volume. Creates a volume.
This will create a volume in a datacenter. This will create a volume in a datacenter.
module : AnsibleModule object module : AnsibleModule object
profitbricks: authenticated profitbricks object. profitbricks: authenticated profitbricks object.
@ -256,7 +259,7 @@ def create_volume(module, profitbricks):
else: else:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)
number_range = xrange(count_offset,count_offset + count + len(numbers)) number_range = xrange(count_offset, count_offset + count + len(numbers))
available_numbers = list(set(number_range).difference(numbers)) available_numbers = list(set(number_range).difference(numbers))
names = [] names = []
numbers_to_use = available_numbers[:count] numbers_to_use = available_numbers[:count]
@ -265,7 +268,7 @@ def create_volume(module, profitbricks):
else: else:
names = [name] * count names = [name] * count
for name in names: for name in names:
create_response = _create_volume(module, profitbricks, str(datacenter), name) create_response = _create_volume(module, profitbricks, str(datacenter), name)
volumes.append(create_response) volumes.append(create_response)
_attach_volume(module, profitbricks, datacenter, create_response['id']) _attach_volume(module, profitbricks, datacenter, create_response['id'])
@ -282,11 +285,12 @@ def create_volume(module, profitbricks):
return results return results
def delete_volume(module, profitbricks): def delete_volume(module, profitbricks):
""" """
Removes a volume. Removes a volume.
This will create a volume in a datacenter. This will create a volume in a datacenter.
module : AnsibleModule object module : AnsibleModule object
profitbricks: authenticated profitbricks object. profitbricks: authenticated profitbricks object.
@ -324,6 +328,7 @@ def delete_volume(module, profitbricks):
return changed return changed
def _attach_volume(module, profitbricks, datacenter, volume): def _attach_volume(module, profitbricks, datacenter, volume):
""" """
Attaches a volume. Attaches a volume.
@ -339,12 +344,12 @@ def _attach_volume(module, profitbricks, datacenter, volume):
server = module.params.get('server') server = module.params.get('server')
# Locate UUID for Server # Locate UUID for Server
if server: if server:
if not (uuid_match.match(server)): if not (uuid_match.match(server)):
server_list = profitbricks.list_servers(datacenter) server_list = profitbricks.list_servers(datacenter)
for s in server_list['items']: for s in server_list['items']:
if server == s['properties']['name']: if server == s['properties']['name']:
server= s['id'] server = s['id']
break break
try: try:
@ -352,18 +357,19 @@ def _attach_volume(module, profitbricks, datacenter, volume):
except Exception as e: except Exception as e:
module.fail_json(msg='failed to attach volume: %s' % str(e)) module.fail_json(msg='failed to attach volume: %s' % str(e))
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
datacenter=dict(), datacenter=dict(),
server=dict(), server=dict(),
name=dict(), name=dict(),
size=dict(default=10), size=dict(type='int', default=10),
bus=dict(default='VIRTIO'), bus=dict(default='VIRTIO'),
image=dict(), image=dict(),
image_password=dict(default=None), image_password=dict(default=None),
ssh_keys=dict(type='list', default=[]), ssh_keys=dict(type='list', default=[]),
disk_type=dict(default='HDD'), disk_type=dict(choices=['HDD', 'SSD'], default='HDD'),
licence_type=dict(default='UNKNOWN'), licence_type=dict(default='UNKNOWN'),
count=dict(type='int', default=1), count=dict(type='int', default=1),
auto_increment=dict(type='bool', default=True), auto_increment=dict(type='bool', default=True),