Skip to content

Commit

Permalink
Merge pull request ansible#8 from jadunham1/feature/small_fixes
Browse files Browse the repository at this point in the history
Fixing dimensiondata.py, flake8, and working on provisioning module
  • Loading branch information
aimonb committed Mar 10, 2016
2 parents 6c189fc + ca1d3cc commit a0a18cc
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 61 deletions.
129 changes: 78 additions & 51 deletions ansible/dimensiondata/dimensiondata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#!/usr/bin/python
from ansible.module_utils.basic import *
from ansible.module_utils.dimensiondata import *

HAS_LIBCLOUD = True
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
import libcloud.security
except ImportError:
HAS_LIBCLOUD = False

# Get regions early to use in docs etc.
dd_regions = get_dd_regions()

DOCUMENTATION = '''
---
module: didata
Expand All @@ -7,6 +21,11 @@
- Creates, terminates, starts or stops servers in the Dimension Data Cloud
version_added: "1.9"
options:
region:
description:
- The target region.
choices: %s
default: na
state:
description:
- the state you want the hosts to be in
Expand Down Expand Up @@ -94,18 +113,26 @@
aliases: []
unique_names:
description:
- By default Dimension Data allows the same name for multiple servers this will make sure we don't create a new server if the name already exists
- By default Dimension Data allows the same name for multiple servers
this will make sure we don't create a new server if the name
already exists
required: false
default: 'no'
aliases: []
choices: ['yes', 'no']
verify_ssl_cert:
description:
- Check that SSL certificate is valid.
required: false
default: true
author:
- "Jeff Dunham (@jadunham1)"
'''
''' % str(dd_regions)

EXAMPLES = '''
# Note: These examples don't include authorization. You can set these by exporting DIDATA_USER and DIDATA_PASSWORD environment variables like:
# Note: These examples don't include authorization.
# You can set these by exporting DIDATA_USER and DIDATA_PASSWORD var:
# export DIDATA_USER=<username>
# export DIDATA_PASSWORD=<password>
Expand All @@ -118,15 +145,7 @@
name: ansible-test-image
admin_password: fakepass
'''
import os
import json

HAS_LIBCLOUD = True
try:
from libcloud.compute.drivers.dimensiondata import DimensionDataNodeDriver
from libcloud.common.dimensiondata import DEFAULT_REGION
except ImportError:
HAS_LIBCLOUD = False

def module_key_die_if_none(module, key):
v = module.params[key]
Expand All @@ -139,18 +158,20 @@ def get_image_id(client, module, location):
if module.params['image_id'] is not None:
return module.params['image_id']
if module.params['image'] is None:
module.fail_json(msg='Need to specify either an image_id or image to create a node')
module.fail_json(msg='Need to specify either an image_id or'
'image to create a node')

image_match_name = module.params['image']
images = client.list_images(location)
images.extend( client.ex_list_customer_images(location) )
images.extend(client.ex_list_customer_images(location))

matched_images = list(filter(lambda x: x.name == image_match_name, images))

if len(matched_images) < 1:
module.fail_json(msg='No images matched this name')
elif len(matched_images) > 1:
module.fail_json(msg='Multile images matched this please specify one of the image ids')
module.fail_json(msg='Multile images matched this please'
' specify a single unique image id')

return matched_images[0].id

Expand All @@ -160,8 +181,11 @@ def node_to_node_obj(node):
node_obj['id'] = node.id
node_obj['ipv6'] = node.extra['ipv6']
node_obj['os_type'] = node.extra['OS_type']
node_obj['private_ipv4'] = node.private_ips
node_obj['public_ipv4'] = node.public_ips
return node_obj


def create_node(client, module):
changed = False
name = module_key_die_if_none(module, 'name')
Expand All @@ -175,16 +199,17 @@ def create_node(client, module):
network_id = module.params['network_id']
network_domain_id = module.params['network_domain_id']
if not network_domain_id and not network_id:
moduule.fail_json(msg='Need either a network_id (MCP1.0) or network_domain_id (MCP_2.0) to create a server')
module.fail_json(msg='Need either a network_id (MCP1.0) or '
'network_domain_id (MCP_2.0) to create a server')

dd_vlan = client.ex_get_vlan(vlan_id)
image_id = get_image_id(client, module, dd_vlan.location.id)
node = client.create_node(name, image_id, admin_password,
module.params['description'],
ex_network=network_id,
ex_network_domain=network_domain_id,
ex_vlan=vlan_id,
ex_memory_gb=module.params['memory_gb'])
module.params['description'],
ex_network=network_id,
ex_network_domain=network_domain_id,
ex_vlan=vlan_id,
ex_memory_gb=module.params['memory_gb'])
node_obj = node_to_node_obj(node)
return (True, [node_obj])

Expand All @@ -209,22 +234,20 @@ def stoporstart_servers(client, module, desired_state):

return (changed, node_list)

def core(module):
try:
username = os.environ['DIDATA_USER']
password = os.environ['DIDATA_PASSWORD']
except KeyError, e:
module.fail_json(msg='unable to find key %s' % e.message)

if not username or not password:
module.fail_json(msg='here unable to find username %s and password %s')

try:
region = os.environ['REGION']
except KeyError:
region = DEFAULT_REGION
def core(module):
credentials = get_credentials()
if credentials is False:
module.fail_json(msg="User credentials not found")
user_id = credentials['user_id']
key = credentials['key']
region = 'dd-%s' % module.params['region']
verify_ssl_cert = module.params['verify_ssl_cert']

client = DimensionDataNodeDriver(username, password, region)
# Instantiate driver
libcloud.security.VERIFY_SSL_CERT = verify_ssl_cert
DimensionData = get_driver(Provider.DIMENSIONDATA)
client = DimensionData(user_id, key, region=region)
state = module.params['state']
if state == 'stopped' or state == 'running':
return stoporstart_servers(client, module, state)
Expand All @@ -236,22 +259,27 @@ def core(module):

def main():
module = AnsibleModule(
argument_spec = dict(
state = dict(default='present', choices=['present', 'absent', 'running', 'stopped']),
server_ids = dict(type='list', aliases=['server_id']),
name = dict(),
image = dict(),
image_id = dict(),
vlan = dict(),
vlan_id = dict(),
network_id = dict(),
network = dict(),
network_domain_id = dict(),
network_domain = dict(),
admin_password = dict(),
description = dict(),
memory_gb = dict(),
unique_names = dict(type='bool', default='no'),
argument_spec=dict(
state=dict(default='present', choices=['present',
'absent',
'running',
'stopped']),
server_ids=dict(type='list', aliases=['server_id']),
name=dict(),
image=dict(),
image_id=dict(),
vlan=dict(),
vlan_id=dict(),
network_id=dict(),
network=dict(),
network_domain_id=dict(),
network_domain=dict(),
admin_password=dict(),
description=dict(),
memory_gb=dict(),
unique_names=dict(type='bool', default='no'),
region=dict(default='na', choices=dd_regions),
verify_ssl_cert=dict(required=False, default=True, type='bool')
)
)
if not HAS_LIBCLOUD:
Expand All @@ -264,7 +292,6 @@ def main():

module.exit_json(changed=changed, instances=data)

from ansible.module_utils.basic import *

if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion ansible/dimensiondata/dimensiondata_backup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python

from ansible.module_utils.basic import *
from ansible.module_utils.dimensiondatacloud import *
from ansible.module_utils.dimensiondata import *
try:
from libcloud.common.dimensiondata import DimensionDataAPIException
from libcloud.backup.drivers.dimensiondata import DimensionDataBackupDriver
Expand Down Expand Up @@ -103,6 +103,7 @@

POLLING_INTERVAL = 2


def handle_backups(module, client):
changed = False
state = module.params['state']
Expand Down
2 changes: 1 addition & 1 deletion ansible/dimensiondata/dimensiondata_backup_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
from ansible.module_utils.basic import *
from ansible.module_utils.dimensiondatacloud import *
from ansible.module_utils.dimensiondata import *
try:
from libcloud.common.dimensiondata import DimensionDataAPIException
from libcloud.backup.drivers.dimensiondata import DimensionDataBackupDriver
Expand Down
2 changes: 1 addition & 1 deletion ansible/dimensiondata/dimensiondata_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# - Aimon Bustardo <aimon.bustardo@dimensiondata.com>
#
from ansible.module_utils.basic import *
from ansible.module_utils.dimensiondatacloud import *
from ansible.module_utils.dimensiondata import *
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
Expand Down
2 changes: 1 addition & 1 deletion demo/demo-create-networks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
- name: Create MCP 2.0 network
dimensiondata_network:
region: na
location: NA12
location: NA9
name: ansible-test2
2 changes: 1 addition & 1 deletion demo/demo-create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
admin_password: fakepass
tasks:
- name: Create host
didata:
dimensiondata:
state: present
vlan_id: '{{ vlan_id }}'
network_domain_id: '{{ network_domain_id }}'
Expand Down
6 changes: 3 additions & 3 deletions demo/demo-start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
hosts: localhost
vars:
server_ids:
- '7ee719e9-7ae9-480b-9f16-c6b5de03463c'
- '9f5973cd-ad8b-4e2c-b1fa-d406290dc1f5'
tasks:
- name: Shutdown host
didata:
- name: Start a host
dimensiondata:
state: running
server_ids: '{{ server_ids }}'
4 changes: 2 additions & 2 deletions demo/demo-stop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
hosts: localhost
vars:
server_ids:
- '7ee719e9-7ae9-480b-9f16-c6b5de03463c'
- '9f5973cd-ad8b-4e2c-b1fa-d406290dc1f5'
tasks:
- name: Shutdown host
didata:
dimensiondata:
state: stopped
server_ids: '{{ server_ids }}'

0 comments on commit a0a18cc

Please sign in to comment.