mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Refactor dimensiondata_network module (#21043)
* Refactor dimensiondata_network to use shared base class for common functionality. * Experiment: remove the assignments in the "except ImportError:" block that keep PyCharm happy. If this fixes the build, then I reckon there's a bug in the validate-modules script (https://github.com/ansible/ansible/blob/devel/test/sanity/validate-modules/validate-modules#L322). * Remove unused imports. * Changes based on feedback from @gundalow for ansible/ansible#21043. - Use no_log=True for mcp_password parameter. - Collapse module parameter definitions. * Use shared definitions and doc fragments for common module arguments (ansible/ansible#21043). * Make default network plan "ESSENTIALS", rather than "ADVANCED" (this is consistent with our other tooling). Tidy up module parameter documentation. * Simplify dimensiondata module documentation fragments (didn't know you could include multiple fragments). * Change 'verify_ssl_cert' module parameter to 'validate_certs'.
This commit is contained in:
parent
b5811ccb8a
commit
1a28a48176
4 changed files with 560 additions and 563 deletions
|
@ -22,42 +22,24 @@
|
|||
# - Adam Friedman <tintoy@tintoy.io>
|
||||
#
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'}
|
||||
ANSIBLE_METADATA = {
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'version': '1.0'
|
||||
}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: dimensiondata_network
|
||||
short_description: Create, update, and delete MCP 1.0 & 2.0 networks
|
||||
extends_documentation_fragment:
|
||||
- dimensiondata
|
||||
- dimensiondata_wait
|
||||
description:
|
||||
- Create, update, and delete MCP 1.0 & 2.0 networks
|
||||
version_added: "2.3"
|
||||
author: 'Aimon Bustardo (@aimonb)'
|
||||
options:
|
||||
region:
|
||||
description:
|
||||
- The target region.
|
||||
- Valid regions are defined in Apache libcloud project [libcloud/common/dimensiondata.py]
|
||||
- Regions are also listed in https://libcloud.readthedocs.io/en/latest/compute/drivers/dimensiondata.html
|
||||
- Note that the default value "na" stands for "North America".
|
||||
- The module prepends 'dd-' to the region.
|
||||
default: na
|
||||
mcp_user:
|
||||
description:
|
||||
- The username used to authenticate to the CloudControl API.
|
||||
- If not specified, will fall back to MCP_USER from environment variable or ~/.dimensiondata.
|
||||
required: false
|
||||
mcp_password:
|
||||
description:
|
||||
- The password used to authenticate to the CloudControl API.
|
||||
- If not specified, will fall back to MCP_PASSWORD from environment variable or ~/.dimensiondata.
|
||||
- Required if mcp_user is specified.
|
||||
required: false
|
||||
location:
|
||||
description:
|
||||
- The target datacenter.
|
||||
required: true
|
||||
name:
|
||||
description:
|
||||
- The name of the network domain to create.
|
||||
|
@ -71,27 +53,7 @@ options:
|
|||
- The service plan, either “ESSENTIALS” or “ADVANCED”.
|
||||
- MCP 2.0 Only.
|
||||
choices: [ESSENTIALS, ADVANCED]
|
||||
default: ADVANCED
|
||||
verify_ssl_cert:
|
||||
description:
|
||||
- Check that SSL certificate is valid.
|
||||
required: false
|
||||
default: true
|
||||
wait:
|
||||
description:
|
||||
- Should we wait for the task to complete before moving onto the next.
|
||||
required: false
|
||||
default: false
|
||||
wait_time:
|
||||
description:
|
||||
- Only applicable if wait is true. This is the amount of time in seconds to wait
|
||||
required: false
|
||||
default: 600
|
||||
wait_poll_interval:
|
||||
description:
|
||||
- The amount to time inbetween polling for task completion
|
||||
required: false
|
||||
default: 2
|
||||
default: ESSENTIALS
|
||||
state:
|
||||
description:
|
||||
- Should the resource be present or absent.
|
||||
|
@ -124,7 +86,7 @@ EXAMPLES = '''
|
|||
RETURN = '''
|
||||
network:
|
||||
description: Dictionary describing the network.
|
||||
returned: On success when I(state) is 'present'
|
||||
returned: On success when I(state=present).
|
||||
type: dictionary
|
||||
contains:
|
||||
id:
|
||||
|
@ -159,181 +121,198 @@ network:
|
|||
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.dimensiondata import get_credentials, DimensionDataAPIException, LibcloudNotFound
|
||||
from ansible.module_utils.dimensiondata import DimensionDataModule, DimensionDataAPIException
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
try:
|
||||
from libcloud.compute.types import Provider
|
||||
from libcloud.compute.providers import get_driver
|
||||
from libcloud.compute.base import NodeLocation
|
||||
import libcloud.security
|
||||
|
||||
HAS_LIBCLOUD = True
|
||||
except ImportError:
|
||||
HAS_LIBCLOUD = False
|
||||
|
||||
|
||||
def network_obj_to_dict(network, version):
|
||||
network_dict = dict(id=network.id, name=network.name,
|
||||
description=network.description)
|
||||
if isinstance(network.location, NodeLocation):
|
||||
network_dict['location'] = network.location.id
|
||||
else:
|
||||
network_dict['location'] = network.location
|
||||
class DimensionDataNetworkModule(DimensionDataModule):
|
||||
"""
|
||||
The dimensiondata_network module for Ansible.
|
||||
"""
|
||||
|
||||
if version == '1.0':
|
||||
network_dict['private_net'] = network.private_net
|
||||
network_dict['multicast'] = network.multicast
|
||||
network_dict['status'] = None
|
||||
else:
|
||||
network_dict['private_net'] = None
|
||||
network_dict['multicast'] = None
|
||||
network_dict['status'] = network.status
|
||||
return network_dict
|
||||
def __init__(self):
|
||||
"""
|
||||
Create a new Dimension Data network module.
|
||||
"""
|
||||
|
||||
|
||||
def get_mcp_version(driver, location):
|
||||
# Get location to determine if MCP 1.0 or 2.0
|
||||
location = driver.ex_get_location_by_id(location)
|
||||
if 'MCP 2.0' in location.name:
|
||||
return '2.0'
|
||||
return '1.0'
|
||||
|
||||
|
||||
def create_network(module, driver, mcp_version, location,
|
||||
name, description):
|
||||
|
||||
# Make sure service_plan argument is defined
|
||||
if mcp_version == '2.0' and 'service_plan' not in module.params:
|
||||
module.fail_json('service_plan required when creating network and ' +
|
||||
'location is MCP 2.0')
|
||||
service_plan = module.params['service_plan']
|
||||
|
||||
# Create network
|
||||
try:
|
||||
if mcp_version == '1.0':
|
||||
res = driver.ex_create_network(location, name,
|
||||
description=description)
|
||||
else:
|
||||
res = driver.ex_create_network_domain(location, name,
|
||||
service_plan,
|
||||
description=description)
|
||||
except DimensionDataAPIException:
|
||||
e = get_exception()
|
||||
|
||||
module.fail_json(msg="Failed to create new network: %s" % str(e))
|
||||
|
||||
if module.params['wait'] is True:
|
||||
wait_for_network_state(module, driver, res.id, 'NORMAL')
|
||||
msg = "Created network %s in %s" % (name, location)
|
||||
network = network_obj_to_dict(res, mcp_version)
|
||||
|
||||
module.exit_json(changed=True, msg=msg, network=network)
|
||||
|
||||
|
||||
def delete_network(module, driver, matched_network, mcp_version):
|
||||
try:
|
||||
if mcp_version == '1.0':
|
||||
res = driver.ex_delete_network(matched_network[0])
|
||||
else:
|
||||
res = driver.ex_delete_network_domain(matched_network[0])
|
||||
if res is True:
|
||||
module.exit_json(changed=True,
|
||||
msg="Deleted network with id %s" %
|
||||
matched_network[0].id)
|
||||
|
||||
module.fail_json("Unexpected failure deleting network with " +
|
||||
"id %s", matched_network[0].id)
|
||||
except DimensionDataAPIException:
|
||||
e = get_exception()
|
||||
|
||||
module.fail_json(msg="Failed to delete network: %s" % str(e))
|
||||
|
||||
|
||||
def wait_for_network_state(module, driver, net_id, state_to_wait_for):
|
||||
try:
|
||||
return driver.connection.wait_for_state(
|
||||
state_to_wait_for, driver.ex_get_network_domain,
|
||||
module.params['wait_poll_interval'],
|
||||
module.params['wait_time'], net_id
|
||||
super(DimensionDataNetworkModule, self).__init__(
|
||||
module=AnsibleModule(
|
||||
argument_spec=DimensionDataModule.argument_spec_with_wait(
|
||||
name=dict(type='str', required=True),
|
||||
description=dict(type='str', required=False),
|
||||
service_plan=dict(default='ESSENTIALS', choices=['ADVANCED', 'ESSENTIALS']),
|
||||
state=dict(default='present', choices=['present', 'absent'])
|
||||
),
|
||||
required_together=DimensionDataModule.required_together()
|
||||
)
|
||||
)
|
||||
except DimensionDataAPIException:
|
||||
e = get_exception()
|
||||
|
||||
module.fail_json(msg='Network did not reach % state in time: %s'
|
||||
% (state_to_wait_for, e.msg))
|
||||
self.name = self.module.params['name']
|
||||
self.description = self.module.params['description']
|
||||
self.service_plan = self.module.params['service_plan']
|
||||
self.state = self.module.params['state']
|
||||
|
||||
def state_present(self):
|
||||
network = self._get_network()
|
||||
|
||||
if network:
|
||||
self.module.exit_json(
|
||||
changed=False,
|
||||
msg='Network already exists',
|
||||
network=self._network_to_dict(network)
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
network = self._create_network()
|
||||
|
||||
self.module.exit_json(
|
||||
changed=True,
|
||||
msg='Created network "%s" in datacenter "%s".' % (self.name, self.location),
|
||||
network=self._network_to_dict(network)
|
||||
)
|
||||
|
||||
def state_absent(self):
|
||||
network = self._get_network()
|
||||
|
||||
if not network:
|
||||
self.module.exit_json(
|
||||
changed=False,
|
||||
msg='Network "%s" does not exist' % self.name,
|
||||
network=self._network_to_dict(network)
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
self._delete_network(network)
|
||||
|
||||
def _get_network(self):
|
||||
if self.mcp_version == '1.0':
|
||||
networks = self.driver.list_networks(location=self.location)
|
||||
else:
|
||||
networks = self.driver.ex_list_network_domains(location=self.location)
|
||||
|
||||
matched_network = [network for network in networks if network.name == self.name]
|
||||
if matched_network:
|
||||
return matched_network[0]
|
||||
|
||||
return None
|
||||
|
||||
def _network_to_dict(self, network):
|
||||
network_dict = dict(
|
||||
id=network.id,
|
||||
name=network.name,
|
||||
description=network.description
|
||||
)
|
||||
|
||||
if isinstance(network.location, NodeLocation):
|
||||
network_dict['location'] = network.location.id
|
||||
else:
|
||||
network_dict['location'] = network.location
|
||||
|
||||
if self.mcp_version == '1.0':
|
||||
network_dict['private_net'] = network.private_net
|
||||
network_dict['multicast'] = network.multicast
|
||||
network_dict['status'] = None
|
||||
else:
|
||||
network_dict['private_net'] = None
|
||||
network_dict['multicast'] = None
|
||||
network_dict['status'] = network.status
|
||||
|
||||
return network_dict
|
||||
|
||||
def _create_network(self):
|
||||
|
||||
# Make sure service_plan argument is defined
|
||||
if self.mcp_version == '2.0' and 'service_plan' not in self.module.params:
|
||||
self.module.fail_json(
|
||||
msg='service_plan required when creating network and location is MCP 2.0'
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
# Create network
|
||||
try:
|
||||
if self.mcp_version == '1.0':
|
||||
network = self.driver.ex_create_network(
|
||||
self.location,
|
||||
self.name,
|
||||
description=self.description
|
||||
)
|
||||
else:
|
||||
network = self.driver.ex_create_network_domain(
|
||||
self.location,
|
||||
self.name,
|
||||
self.module.params['service_plan'],
|
||||
description=self.description
|
||||
)
|
||||
except DimensionDataAPIException:
|
||||
api_exception = get_exception()
|
||||
|
||||
self.module.fail_json(
|
||||
msg="Failed to create new network: %s" % str(api_exception)
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
if self.module.params['wait'] is True:
|
||||
network = self._wait_for_network_state(network.id, 'NORMAL')
|
||||
|
||||
return network
|
||||
|
||||
def _delete_network(self, network):
|
||||
try:
|
||||
if self.mcp_version == '1.0':
|
||||
deleted = self.driver.ex_delete_network(network)
|
||||
else:
|
||||
deleted = self.driver.ex_delete_network_domain(network)
|
||||
|
||||
if deleted:
|
||||
self.module.exit_json(
|
||||
changed=True,
|
||||
msg="Deleted network with id %s" % network.id
|
||||
)
|
||||
|
||||
self.module.fail_json(
|
||||
"Unexpected failure deleting network with id %s", network.id
|
||||
)
|
||||
|
||||
except DimensionDataAPIException:
|
||||
api_exception = get_exception()
|
||||
|
||||
self.module.fail_json(
|
||||
msg="Failed to delete network: %s" % str(api_exception)
|
||||
)
|
||||
|
||||
def _wait_for_network_state(self, net_id, state_to_wait_for):
|
||||
try:
|
||||
return self.driver.connection.wait_for_state(
|
||||
state_to_wait_for,
|
||||
self.driver.ex_get_network_domain,
|
||||
self.module.params['wait_poll_interval'],
|
||||
self.module.params['wait_time'],
|
||||
net_id
|
||||
)
|
||||
except DimensionDataAPIException:
|
||||
api_exception = get_exception()
|
||||
|
||||
self.module.fail_json(
|
||||
msg='Network did not reach % state in time: %s' % (state_to_wait_for, api_exception.msg)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
region=dict(default='na'),
|
||||
mcp_user=dict(required=False, type='str'),
|
||||
mcp_password=dict(required=False, type='str'),
|
||||
location=dict(required=True, type='str'),
|
||||
name=dict(required=True, type='str'),
|
||||
description=dict(required=False, type='str'),
|
||||
service_plan=dict(default='ADVANCED', choices=['ADVANCED',
|
||||
'ESSENTIALS']),
|
||||
state=dict(default='present', choices=['present', 'absent']),
|
||||
wait=dict(required=False, default=False, type='bool'),
|
||||
wait_time=dict(required=False, default=600, type='int'),
|
||||
wait_poll_interval=dict(required=False, default=2, type='int'),
|
||||
verify_ssl_cert=dict(required=False, default=True, type='bool')
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
credentials = get_credentials(module)
|
||||
except LibcloudNotFound:
|
||||
module.fail_json(msg='libcloud is required for this module.')
|
||||
|
||||
if not credentials:
|
||||
module.fail_json(msg="User credentials not found")
|
||||
|
||||
# set short vars for readability
|
||||
user_id = credentials['user_id']
|
||||
key = credentials['key']
|
||||
region = 'dd-%s' % module.params['region']
|
||||
location = module.params['location']
|
||||
name = module.params['name']
|
||||
description = module.params['description']
|
||||
verify_ssl_cert = module.params['verify_ssl_cert']
|
||||
state = module.params['state']
|
||||
|
||||
# Instantiate driver
|
||||
libcloud.security.VERIFY_SSL_CERT = verify_ssl_cert
|
||||
DimensionData = get_driver(Provider.DIMENSIONDATA)
|
||||
driver = DimensionData(user_id, key, region=region)
|
||||
|
||||
# Get MCP API Version
|
||||
mcp_version = get_mcp_version(driver, location)
|
||||
|
||||
# Get network list
|
||||
if mcp_version == '1.0':
|
||||
networks = driver.list_networks(location=location)
|
||||
else:
|
||||
networks = driver.ex_list_network_domains(location=location)
|
||||
matched_network = [network for network in networks if network.name == name]
|
||||
|
||||
# Ensure network state
|
||||
if state == 'present':
|
||||
# Network already exists
|
||||
if matched_network:
|
||||
module.exit_json(
|
||||
changed=False,
|
||||
msg="Network already exists",
|
||||
network=network_obj_to_dict(matched_network[0], mcp_version)
|
||||
)
|
||||
create_network(module, driver, mcp_version, location, name,
|
||||
description)
|
||||
elif state == 'absent':
|
||||
# Destroy network
|
||||
if matched_network:
|
||||
delete_network(module, driver, matched_network, mcp_version)
|
||||
else:
|
||||
module.exit_json(changed=False, msg="Network does not exist")
|
||||
else:
|
||||
module.fail_json(msg="Requested state was " +
|
||||
"'%s'. State must be 'absent' or 'present'" % state)
|
||||
module = DimensionDataNetworkModule()
|
||||
if module.state == 'present':
|
||||
module.state_present()
|
||||
elif module.state == 'absent':
|
||||
module.state_absent()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue