def main(): """ Main function :returns: VIP support function information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), type=dict(default='health_monitor', choices=['health_monitor', 'persistence_profile', 'irule'], type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) function_type = module.params.get('type') network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') return_data = return_object('vip_function') network_domain_id = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) try: return_data['vip_function'] = client.list_vip_function( network_domain_id=network_domain_id, function_type=function_type) return_data['count'] = len(return_data.get('vip_function')) module.exit_json(data=return_data) except (KeyError, IndexError, AttributeError, TypeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not find any VIP Functions of type {0}: {1}'.format( function_type, e.msg))
def main(): """ Main function :returns: VIP Listener Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') object_id = module.params.get('id') name = module.params.get('name') return_data = return_object('vip_listener') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.format(network_domain_name)) if object_id: try: return_data['vip_listener'] = client.get_vip_listener(object_id) module.exit_json(data=return_data) except NTTMCPAPIException as exc: module.fail_json(msg='Could not find the Virtual Listener {0}: {1}'.format(object_id, exc)) else: return_data['vip_listener'] = client.list_vip_listener(network_domain_id, name) return_data['count'] = len(return_data.get('vip_listener')) module.exit_json(data=return_data)
def main(): """ Main function :returns: Port List Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), name=dict(required=False, type='str'), network_domain=dict(required=True, type='str') ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('name') network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') return_data = return_object('port_list') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params['region']) # Get a list of existing CNDs and check if the name already exists try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except NTTMCPAPIException as e: module.fail_json(msg='Failed to get a list of Cloud Network Domains - {0}'.format(e)) try: if name: result = client.get_port_list_by_name(network_domain_id, name) if result: return_data['port_list'].append(result) else: return_data['port_list'] = client.list_port_list(network_domain_id) except (KeyError, IndexError, NTTMCPAPIException) as e: module.fail_json(msg='Could not retrieve a list of the Port Lists - {0}'.format(e)) return_data['count'] = len(return_data.get('port_list')) module.exit_json(data=return_data)
def main(): """ Main function :returns: NAT Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), internal_ip=dict(required=False, default=None, type='str'), external_ip=dict(required=False, default=None, type='str'), id=dict(default=None, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') object_id = module.params.get('id') internal_ip = module.params.get('internal_ip') external_ip = module.params.get('external_ip') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) if object_id or internal_ip or external_ip: get_nat_rule(module, client, network_domain_id, object_id, internal_ip, external_ip) else: list_nat_rule(module, client, network_domain_id)
def main(): """ Main function :returns: Server Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), type=dict(default='SCSI', required=False, choices=['SCSI', 'SATA', 'IDE']), controller_number=dict(default=0, required=False, type='int'), disk_number=dict(default=None, required=False, type='int'), size=dict(required=False, type='int'), speed=dict(default='STANDARD', required=False, choices=DISK_SPEEDS), iops=dict(default=None, required=False, type='int'), state=dict(default='present', choices=['present', 'absent']), stop=dict(default=True, type='bool'), start=dict(default=True, type='bool'), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=1200, type='int'), wait_poll_interval=dict(required=False, default=30, type='int'), wait_for_vmtools=dict(required=False, default=False, type='bool') ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('server') datacenter = module.params.get('datacenter') state = module.params.get('state') network_domain_name = module.params.get('network_domain') CORE['datacenter'] = module.params.get('datacenter') CORE['region'] = module.params.get('region') # CORE['wait_for_vmtools'] = module.params.get('wait_for_vmtools') server_running = True stop_server = module.params.get('stop') start = module.params.get('start') server = {} disk_number = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND object based on the supplied name try: if network_domain_name is None: module.fail_json(msg='No network_domain or network_info.network_domain was provided') network = client.get_network_domain_by_name(datacenter=datacenter, name=network_domain_name) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Failed to find the Cloud Network Domain: {0}'.format(network_domain_name)) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) if server: server_running = server.get('started') else: module.fail_json(msg='Failed to find the server - {0}'.format(name)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed attempting to locate any existing server - {0}'.format(e)) # Setup the rest of the CORE dictionary to save passing data around CORE['network_domain_id'] = network_domain_id CORE['module'] = module CORE['client'] = client CORE['name'] = server.get('name') if state == 'present': disk = get_disk(module, server) if not disk: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new {0} disk of size {1} will be added to the server {2}'.format( module.params.get('type'), module.params.get('size'), server.get('name'))) if server_running and stop_server: server_command(module, client, server, 'stop') server_running = False elif server_running and not stop_server: module.fail_json(msg='Server disks cannot be added while the server is running') disk_number = add_disk(module, client, network_domain_id, server) if start and not server_running: server_command(module, client, server, 'start') server = client.get_server_by_name(datacenter, network_domain_id, None, name) module.exit_json(changed=True, disk_number=disk_number, data=server) else: try: if compare_disk(module, disk): if server_running and stop_server: server_command(module, client, server, 'stop') server_running = False elif server_running and not stop_server: module.fail_json(msg='Server disks cannot be added while the server is running') update_disk(module, client, network_domain_id, server, disk) else: module.exit_json(changed=False, data=server) if start and not server_running: server_command(module, client, server, 'start') server = client.get_server_by_name(datacenter, network_domain_id, None, name) module.exit_json(changed=True, data=server) except NTTMCPAPIException as e: module.fail_json(msg='Failed to update the disk - {0}'.format(e)) elif state == 'absent': try: disk = get_disk(module, server) if not disk: module.fail_json(msg='Controller {0} has no disk {1}'.format(module.params.get('controller_number'), module.params.get('disk_number'))) # Implement Check Mode if module.check_mode: module.exit_json(msg='The disk with ID {0} will be removed from the server {1}'.format( disk.get('id'), server.get('name'))) if server_running and stop_server: server_command(module, client, server, 'stop') server_running = False elif server_running and not stop_server: module.fail_json(msg='Disks cannot be removed while the server is running') remove_disk(module, client, network_domain_id, server, disk) # Introduce a pause to allow the API to catch up in more remote MCP locations sleep(10) server = client.get_server_by_name(datacenter, network_domain_id, None, name) if server: server_running = server.get('started') else: module.fail_json(msg='Failed to find the server to determine the final running state - {0}'.format(name)) if start and not server_running: server_command(module, client, server, 'start') server = client.get_server_by_name(datacenter, network_domain_id, None, name) module.exit_json(changed=True, data=server) except (KeyError, IndexError, NTTMCPAPIException) as e: module.fail_json(msg='Could not delete the disk - {0}'.format(e))
def main(): """ Main function :returns: SNAT exclusion Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), description=dict(default=None, required=False, type='str'), cidr=dict(default=None, required=False, type='str'), new_cidr=dict(default=None, required=False, type='str'), state=dict(default='present', choices=['present', 'absent', 'restore'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') snat = new_network_cidr = network_cidr = None snats = [] # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params['region']) # Check to see the CIDR provided is valid if module.params.get('cidr'): try: network_cidr = ip_net(unicode(module.params.get('cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format( module.params.get('cidr'), e)) if network_cidr.version != 4: module.fail_json( msg='The cidr argument must be in valid IPv4 CIDR notation') if module.params.get('new_cidr'): try: new_network_cidr = ip_net(unicode(module.params.get('new_cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format( module.params.get('new_cidr'), e)) if new_network_cidr.version != 4: module.fail_json( msg='The new_cidr argument must be in valid IPv4 CIDR notation' ) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Check if a SNAT exclusion already exists for this ID if not state == 'restore': try: if module.params.get('id'): snat = client.get_snat_exclusion(module.params.get('id')) if not snat and network_cidr: # If no matching routes were found for the name check to see if the supplied network parameters match a rule with a different name snats = client.list_snat_exclusion( network_domain_id=network_domain_id, network=str(network_cidr.network_address), prefix=network_cidr.prefixlen) if len(snats) == 1: snat = snats[0] except (IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Failed to get a list of existing SNAT exclusions - {0}'. format(e)) try: if state == 'present': if not snat: # Implement Check Mode if module.check_mode: module.exit_json( msg='A new SNAT exclusion will be created for {0}'. format(str(network_cidr))) create_snat_exclusion(module, client, network_domain_id, network_cidr) else: # SNAT exclusions cannot be updated. The old one must be removed and a new one created with the new parameters if compare_snat_exclusion(module, new_network_cidr, snat): delete_snat_exclusion(module, client, snat.get('id')) create_snat_exclusion(module, client, network_domain_id, new_network_cidr) module.exit_json(data=snat) elif state == 'restore': client.restore_snat_exclusion(network_domain_id) module.exit_json( changed=True, msg='Successfully restored the default SNAT exclusions') elif state == 'absent': if snat: # Implement Check Mode if module.check_mode: module.exit_json( msg= 'An existing SNAT exclusion was found for {0} and will be removed' .format(snat.get('id'))) delete_snat_exclusion(module, client, snat.get('id')) module.exit_json(changed=True, msg='Successfully deleted the SNAT exclusion') else: module.exit_json(msg='No existing SNAT exclusion was matched') except (KeyError, IndexError, NTTMCPAPIException) as e: module.fail_json( msg='Could not update the SNAT exclusion - {0}'.format(e))
def main(): """ Main function :returns: Cloud Network Domain Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), name=dict(required=True, type='str'), description=dict(required=False, type='str'), network_type=dict(default='ADVANCED', type='str'), new_name=dict(required=False, default=None, type='str'), state=dict(default='present', choices=['present', 'absent']), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=600, type='int'), wait_poll_interval=dict(required=False, default=10, type='int') ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('name') datacenter = module.params.get('datacenter') state = module.params.get('state') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get a list of existing CNDs and check if the name already exists try: network = client.get_network_domain_by_name(name=name, datacenter=datacenter) except NTTMCPAPIException as e: module.fail_json(msg='Failed to get a list of Cloud Network Domains - {0}'.format(e)) # Create the Cloud Network Domain if state == 'present': # Handle case where CND name already exists if not network: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new Cloud Network Domain will be created in {0}'.format(datacenter)) create_network_domain(module, client) else: try: compare_result = compare_network_domain(module, network) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) if compare_result: if compare_result.get('changes'): update_network_domain(module, client, network) module.exit_json(changed=False, data=network) except (KeyError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to update the Cloud Network Domain - {0}'.format(e)) # Delete the Cloud Network Domain elif state == 'absent': if not network: module.exit_json(msg='Cloud Network Domain not found') # Implement Check Mode if module.check_mode: module.exit_json(msg='An existing Cloud Network Domain was found with UUID {0} and will be removed'.format( network.get('id'))) delete_network_domain(module, client, network)
def main(): """ Main function :returns: VIP Pool Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), description=dict(required=False, default=None, type='str'), health_monitor_1=dict(required=False, default=None, type='str'), health_monitor_2=dict(required=False, default=None, type='str'), no_health_monitor=dict(required=False, default=False, type='bool'), load_balancing=dict(required=False, default='ROUND_ROBIN', type='str'), service_down_action=dict(required=False, default='NONE', type='str'), slow_ramp_time=dict(required=False, default=10, type='int'), members=dict(default=None, required=False, type='list', elements='dict'), state=dict(default='present', required=False, choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') pool = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) # Check the load balancing method and service down action supplied is valid if module.params.get('load_balancing') not in LOAD_BALANCING_METHODS: module.fail_json( msg= 'Invalid load_balancing value. Load Balancing method must be one of {0}' .format(LOAD_BALANCING_METHODS)) if module.params.get( 'service_down_action') not in VIP_POOL_SERVICE_DOWN_ACTIONS: module.fail_json( msg= 'Invalid service_down_action value. Service Down action must be one of {0}' .format(LOAD_BALANCING_METHODS)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Verifying the supplied arguments if module.params.get('health_monitor_1') or module.params.get( 'health_monitor_2'): module.params['health_monitor'] = verify_health_monitor( module, client, network_domain_id) if module.params.get('members'): module.params['members'] = verify_member_schema( module, client, network_domain_id) # Check if the Pool already exists try: if module.params.get('id'): pool = client.get_vip_pool(module.params.get('id')) else: pools = client.list_vip_pool(network_domain_id=network_domain_id, name=module.params.get('name')) if len(pools) == 1: pool = pools[0] # Get the members of the pool if it exists try: if pool: pool['members'] = client.list_vip_pool_members(pool.get('id')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json(msg='{0}'.format(exc)) except (KeyError, IndexError, NTTMCPAPIException) as exc: module.fail_json( msg='Could not get a list of existing pools to check against - {0}' .format(exc)) if state == 'present': if not pool: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new VIP pool will be created') create_vip_pool(module, client, network_domain_id) if compare_vip_pool(module, pool): update_vip_pool(module, client, pool) module.exit_json(data=pool.get('id')) elif state == 'absent': if not pool: module.exit_json(msg='No VIP Pool not found. Nothing to remove.') # Implement Check Mode if module.check_mode: module.exit_json( msg='The VIP pool with ID {0} will be deleted'.format( pool.get('id'))) delete_vip_pool(module, client, pool.get('id'))
def main(): """ Main function :returns: VIP Listener Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), description=dict(required=False, default=None, type='str'), type=dict(required=False, default='STANDARD', choices=VIP_LISTENER_TYPES), protocol=dict(required=False, default='ANY', type='str'), ip_address=dict(required=False, default=None, type='str'), port=dict(required=False, default=None, type='int'), enabled=dict(required=False, default=True, type='bool'), preservation=dict(required=False, default='PRESERVE', choices=VIP_LISTENER_PRESERVATION), connection_limit=dict(required=False, default=100000, type='int'), connection_rate_limit=dict(required=False, default=4000, type='int'), pool=dict(required=False, default=None, type='str'), client_pool=dict(required=False, default=None, type='str'), persistence_profile=dict(required=False, default=None, type='str'), fallback_persistence_profile=dict(required=False, default=None, type='str'), optimization_profile=dict(required=False, default=None, type='str'), ssl_offload_profile=dict(required=False, default=None, type='str'), irules=dict(required=False, default=None, type='list', elements='str'), state=dict(default='present', required=False, choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') v_listener = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) # Check port input value if state == 'present': if module.params.get('port') not in range(1, 65535): module.fail_json(msg='port must be between 1 and 65535') # Check connection input values if module.params.get('connection_limit') not in range(1, 100001): module.fail_json( msg='connection_limit must be between 1 and 100000') if module.params.get('connection_rate_limit') not in range(1, 4001): module.fail_json( msg='connection_rate_limit must be between 1 and 4000') if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Check if the Listener already exists try: if module.params.get('id'): v_listener = client.get_vip_listener(module.params.get('id')) else: v_listeners = client.list_vip_listener( network_domain_id=network_domain_id, name=module.params.get('name')) if len(v_listeners) == 1: v_listener = v_listeners[0] except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json( msg= 'Could not get a list of existing Virtual Listeners to check against - {0}' .format(exc)) if state == 'present': if not v_listener: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new Virtual Listener will be created') create_vip_listener(module, client, network_domain_id) else: if compare_vip_listener(module, client, network_domain_id, v_listener): update_vip_listener(module, client, network_domain_id, v_listener) module.exit_json(data=v_listener.get('id')) elif state == 'absent': if not v_listener: module.exit_json( msg='No Virtual Listener found. Nothing to remove.') # Implement Check Mode if module.check_mode: module.exit_json( msg='The Virtual Listener with ID {0} will be deleted'.format( v_listener.get('id'))) delete_vip_listener(module, client, v_listener.get('id'))
def main(): """ Main function :returns: Public IP address reservation information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), description=dict(required=False, type='str'), network_domain=dict(required=True, type='str'), vlan=dict(required=True, type='str'), ip_address=dict(required=True, type='str'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) vlan = {} network_domain_name = module.params.get('network_domain') vlan_name = module.params.get('vlan') datacenter = module.params.get('datacenter') description = module.params.get('description') state = module.params.get('state') ip_address = module.params.get('ip_address') # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Check the IP address is valid try: ip_address_obj = ip_addr(unicode(ip_address)) except AddressValueError: module.fail_json( msg='Invalid IPv4 or IPv6 address: {0}'.format(ip_address)) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Get a list of existing VLANs and check if the new name already exists try: vlan = client.get_vlan_by_name(name=vlan_name, datacenter=datacenter, network_domain_id=network_domain_id) if not vlan: module.fail_json(msg='A valid VLAN is required') except NTTMCPAPIException as e: module.fail_json(msg='Failed to get a list of VLANs - {0}'.format(e)) # Check if the IP address is already reserved is_reserved = get_reservation(module, client, vlan.get('id'), ip_address_obj) if not is_reserved and state == 'absent': module.exit_json(msg='No IPv{0} reservation found for {1}'.format( ip_address_obj.version, str(ip_address_obj))) elif is_reserved and state == 'present': module.exit_json( msg='An existing IPv{0} reservation was found for {1}'.format( ip_address_obj.version, str(ip_address_obj))) if state == 'present': # Implement Check Mode if module.check_mode: module.exit_json( msg='The IPv{0} address {1} will be reserved'.format( ip_address_obj.version, str(ip_address_obj), )) reserve_ip(module, client, vlan.get('id'), ip_address_obj, description) elif state == 'absent': # Implement Check Mode if module.check_mode: module.exit_json( msg='The IPv{0} address {1} will be unreserved'.format( ip_address_obj.version, str(ip_address_obj), )) unreserve_ip(module, client, vlan.get('id'), ip_address_obj)
def main(): """ Main function :returns: Server Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), iso=dict(required=False, default=False, type='bool'), vmtools=dict(required=False, default=True, type='bool'), vapp=dict(required=False, default=None, type='list', elements='dict'), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=600, type='int'), wait_poll_interval=dict(required=False, default=5, type='int'), state=dict(default='present', choices=['present', 'absent']), ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('server') datacenter = module.params.get('datacenter') state = module.params.get('state') network_domain_name = module.params.get('network_domain') server = vapp = dict() server_id = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND object based on the supplied name try: if network_domain_name is None: module.fail_json(msg='No network_domain or network_info.network_domain was provided') network = client.get_network_domain_by_name(datacenter=datacenter, name=network_domain_name) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Failed to find the Cloud Network Domain: {0}'.format(network_domain_name)) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) server_id = server.get('id') if server.get('started'): module.fail_json(msg='vApp properties cannot be modified while the server is running') if not server_id: module.fail_json(msg='Failed to locate the server') # vapp = ast.literal_eval(str(server.get('vAppProperties')).encode('ascii')) or dict() vapp = ast.literal_eval(str(server.get('vAppProperties'))) or dict() except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to locate the server - {0}'.format(e)) # Check vApp dictionary is valid if not (state == 'absent' and module.params.get('vapp') is None): validate_vapp_args(module, client) if state == 'present': if compare_vapp(module, vapp): configure_vapp(module, client, server_id) else: module.exit_json(msg='No update required', data=server.get('vAppProperties')) try: # Pause to allow the API/DB to catch up sleep(2) server = client.get_server_by_name(datacenter, network_domain_id, None, name) except NTTMCPAPIException: module.warn(warning='The update was successfull but there was an issue getting the updated server') pass module.exit_json(changed=True, data=server.get('vAppProperties')) elif state == 'absent': try: if not vapp: module.exit_json(msg='Server {0} does not currently have vApp enabled'.format(server.get('name'))) # Implement Check Mode if module.check_mode: if not module.params.get('vapp'): module.exit_json(msg='vApp will be disabled on the server with ID {0}'.format(server.get('id'))) module.exit_json(msg='The following vApp keys will be removed from the server with ID {0}'.format( server.get('id')), data=module.params.get('vapp')) if module.params.get('vapp'): remove_vapp(module, client, server.get('id'), vapp) try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) except NTTMCPAPIException: module.warn(warning='The update was successfull but there was an issue getting the updated server') pass module.exit_json(changed=True, data=server.get('vAppProperties')) else: disable_vapp(module, client, server.get('id')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not remove vApp from server {0} - {1}'.format(server.get('id'), e))
def main(): """ Main function :returns: SSL Certificate Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(required=False, default=None, type='str'), name=dict(required=False, default=None, type='str'), description=dict(required=False, default=None, type='str'), type=dict(required=False, default='certificate', choices=['certificate', 'chain']), path=dict(required=False, default=None, type='str'), key_path=dict(required=False, default=None, type='str'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') cert = None associated_ssl_profiles = [] # Check Imports if not HAS_OPENSSL: module.fail_json(msg='Missing Python module: pyOpenSSL') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Check if the SSL certificate already exists try: if module.params.get('id'): if module.params.get('type') == 'certificate': cert = client.get_vip_ssl('sslDomainCertificate', module.params.get('id')) elif module.params.get('type') == 'chain': cert = client.get_vip_ssl('sslCertificateChain', module.params.get('id')) else: if module.params.get('type') == 'certificate': certs = client.list_vip_ssl( network_domain_id=network_domain_id, name=module.params.get('name'), ssl_type='sslDomainCertificate') elif module.params.get('type') == 'chain': certs = client.list_vip_ssl( network_domain_id=network_domain_id, name=module.params.get('name'), ssl_type='sslCertificateChain') if len(certs) == 1: cert = certs[0] except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json( msg= 'Could not get a list of existing SSL certificates/chains to check against - {0}' .format(exc)) # Check if the cert is associated with any SSL Offload profiles. SSL certs cannot be updated or removed while still associated with an Offload Profile try: ssl_profiles = client.list_vip_ssl( network_domain_id=network_domain_id, ssl_type='sslOffloadProfile') associated_ssl_profiles = is_used(module.params.get('type'), module.params.get('name'), ssl_profiles) if associated_ssl_profiles: module.fail_json( msg= 'Cannot operate on the SSL {0} {1} as it is still associated with the following' 'SSL Offload profiles: {2}'.format( module.params.get('type'), module.params.get('name'), associated_ssl_profiles)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json( msg= 'Failed getting a list of SSL Offload Profiles to check against - {0}' .format(exc)) if state == 'present': if not cert: # Implement Check Mode if module.check_mode: module.exit_json( msg='The new SSL certificate will be imported') import_ssl_cert(module, client, network_domain_id) else: # Implement Check Mode if module.check_mode: module.exit_json( msg= 'An SSL certificate already exists, the old certificate will be removed and the new one imported' ) delete_ssl_cert(module, client, cert.get('id')) import_ssl_cert(module, client, network_domain_id) elif state == 'absent': if not cert: module.exit_json( msg= 'The SSL certificate/chain was not found. Nothing to remove.') # Implement Check Mode if module.check_mode: module.exit_json( msg='The SSL certificate with ID {0} will be removed'.format( cert.get('id'))) delete_ssl_cert(module, client, cert.get('id')) module.exit_json( changed=True, msg='The SSL certificate/chain was successfully removed.')
def main(): """ Main function :returns: Server Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), vlan=dict(default=None, required=False, type='str'), vlan_2=dict(default=None, required=False, type='str'), ipv4_address=dict(default=None, required=False, type='str'), ipv4_address_2=dict(default=None, required=False, type='str'), type=dict(default='VMXNET3', required=False, choices=NIC_ADAPTER_TYPES), connected=dict(default=True, required=False, type='bool'), state=dict(default='present', choices=['present', 'absent', 'exchange']), stop=dict(default=True, type='bool'), start=dict(default=True, type='bool'), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=1200, type='int'), wait_poll_interval=dict(required=False, default=30, type='int'), wait_for_vmtools=dict(required=False, default=False, type='bool') ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('server') datacenter = module.params.get('datacenter') state = module.params.get('state') network_domain_name = module.params.get('network_domain') server_running = True changed = False vlan_name = module.params.get('vlan') vlan_name_2 = module.params.get('vlan_2') ipv4_address = module.params.get('ipv4_address') ipv4_address_2 = module.params.get('ipv4_address_2') start = module.params.get('start') server = {} # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Get the CND object based on the supplied name try: if network_domain_name is None: module.fail_json(msg='No network_domain or network_info.network_domain was provided') network = client.get_network_domain_by_name(datacenter=datacenter, name=network_domain_name) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Failed to find the Cloud Network Domain: {0}'.format(network_domain_name)) # Get a list of existing VLANs if vlan_name: try: vlan = client.get_vlan_by_name(name=vlan_name, datacenter=datacenter, network_domain_id=network_domain_id) except NTTMCPAPIException as e: module.fail_json(msg='Failed to get a list of VLANs - {0}'.format(e)) else: vlan = False if not vlan: module.fail_json(msg='A valid VLAN name must be supplied') if state != 'absent' and not ipv4_address: module.fail_json(msg='A valid IPv4 address must be supplied') # Get the secondary VLAN and or IPv4 address if vlan_name_2: try: vlan_2 = client.get_vlan_by_name(name=vlan_name_2, datacenter=datacenter, network_domain_id=network_domain_id) except NTTMCPAPIException as e: module.fail_json(msg='Failed to get a list of VLANs for the second VLAN - {0}'.format(e)) else: vlan_2 = False if state == 'exchange' and not vlan_2: module.fail_json(msg='A valid secondary VLAN name or secondary IPv4 address is required when exchanging NIC VLANs') if state == 'exchange' and not ipv4_address_2: module.fail_json(msg='A valid secondary IPv4 address is required if no secondary VLAN is supplied when exchanging NIC VLANs') # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) if server: server_running = server.get('started') else: module.fail_json(msg='Failed to find the server - {0}'.format(name)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed attempting to locate any existing server - {0}'.format(e)) # Setup the rest of the CORE dictionary to save passing data around CORE['network_domain_id'] = network_domain_id CORE['module'] = module CORE['client'] = client CORE['name'] = server.get('name') if state == 'present': try: nic = get_nic(module, server, vlan, ipv4_address) if not nic: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new NIC in VLAN {0} will be added to the server {1}'.format( vlan.get('name'), server.get('name'))) server_running = check_and_stop_server(module, client, server, server_running) add_nic(module, client, network_domain_id, server, vlan) changed = True else: if nic.get('networkAdapter') != module.params.get('type'): # Implement Check Mode if module.check_mode: module.exit_json(msg='NIC with the IP {0} in VLAN {1} will be changed from {2} to {3}'.format( nic.get('privateIpv4'), nic.get('vlanName'), nic.get('networkAdapter'), module.params.get('type'))) server_running = check_and_stop_server(module, client, server, server_running) change_nic_type(module, client, network_domain_id, server, nic.get('id'), module.params.get('type')) changed = True if (not module.params.get('connected') and nic.get('connected')) or ( (module.params.get('connected') and not nic.get('connected'))): operation = 'connected' if module.params.get('connected') else 'disconnected' if module.check_mode: module.exit_json(msg='NIC with the IP {0} in VLAN {1} will be {2}'.format( nic.get('privateIpv4'), nic.get('vlanName'), operation)) change_nic_state(module, client, network_domain_id, server, nic.get('id')) changed = True except NTTMCPAPIException as e: module.fail_json(msg='Could not add the NIC - {0}'.format(e)) elif state == 'absent': try: nic = get_nic(module, server, vlan, ipv4_address) if nic: # Implement Check Mode if module.check_mode: module.exit_json(msg='The NIC with IP {0} in VLAN {1} will be removed'.format( nic.get('privateIpv4'), nic.get('vlanName'))) server_running = check_and_stop_server(module, client, server, server_running) remove_nic(module, client, network_domain_id, server, nic) changed = True else: module.fail_json(msg='Server {0} has not matching NIC'.format(module.params.get('name'))) except NTTMCPAPIException as e: module.fail_json(msg='Could not remove the NIC - {0}'.format(e)) elif state == 'exchange': try: nic_1 = get_nic(module, server, vlan, ipv4_address) nic_2 = get_nic(module, server, vlan_2, ipv4_address_2) if nic_1 and nic_2: # Implement Check Mode if module.check_mode: module.exit_json(msg='The NIC with IP {0} in VLAN {1} will be exchanged with the NIC with IP {2} in VLAN {3}'.format( nic_1.get('privateIpv4'), nic_1.get('vlanName'), nic_2.get('privateIpv4'), nic_2.get('vlanName'),)) server_running = check_and_stop_server(module, client, server, server_running) exchange_nic(module, client, network_domain_id, server, nic_1, nic_2) changed = True else: module.fail_json(msg='Server {0} has no matching NICs'.format(module.params.get('server'))) except NTTMCPAPIException as e: module.fail_json(msg='Could not exchange the server {0} has no matching NICs - {1}'.format(module.params.get('name'), e)) # Introduce a pause to allow the API to catch up in more remote MCP locations sleep(10) try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) if server: server_running = server.get('started') else: module.fail_json(msg='Failed to find the server to determine the final running state - {0}'.format(name)) if start and not server_running: server_command(module, client, server, 'start') server = client.get_server_by_name(datacenter, network_domain_id, None, name) module.exit_json(changed=changed, data=server) except NTTMCPAPIException as e: module.fail_json(changed=changed, msg='Could not verify the server status - {0}'.format(e))
def main(): """ Main function :returns: SNAT Information """ module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict( required=True, type='str'), id=dict(default=None, required=False, type='str'), cidr=dict(default=None, required=False, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') snat_id = module.params.get('id') network_cidr = None # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Check to see the CIDR provided is valid if module.params.get('cidr'): try: network_cidr = ip_net(unicode(module.params.get('cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format( module.params.get('cidr'), e)) if network_cidr.version != 4: module.fail_json( msg='The cidr argument must be in valid IPv4 CIDR notation') # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) if snat_id or network_cidr: get_snat_exclusion(module, client, network_domain_id, snat_id, network_cidr) else: list_snat_exclusion(module, client, network_domain_id)
def main(): """ Main function :returns: Server Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), plan=dict(required=False, type='str'), state=dict(default='present', choices=['present', 'absent']), ), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('server') datacenter = module.params.get('datacenter') state = module.params.get('state') network_domain_name = module.params.get('network_domain') server = {} # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND object based on the supplied name try: if network_domain_name is None: module.fail_json( msg= 'No network_domain or network_info.network_domain was provided' ) network = client.get_network_domain_by_name(datacenter=datacenter, name=network_domain_name) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Failed to find the Cloud Network Domain: {0}'. format(network_domain_name)) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Failed attempting to locate any existing server - {0}'.format( e)) monitoring = server.get('monitoring', {}) if state == 'present': # Implement Check Mode if module.check_mode: if not monitoring.get('servicePlan') == module.params.get('plan'): module.exit_json( msg= 'Monitoring of type {0} will be applied to server with ID {1}' .format(module.params.get('plan'), server.get('id'))) else: module.exit_json(msg='No changes are required') if not monitoring: add_monitoring(module, client, False, server.get('id')) else: if monitoring.get('servicePlan') != module.params.get('plan'): add_monitoring(module, client, True, server.get('id')) else: module.exit_json(changed=False, data=server) try: server = client.get_server_by_name(datacenter, None, name) except NTTMCPAPIException: pass module.exit_json(changed=True, data=server) elif state == 'absent': try: if not monitoring: module.exit_json( msg='Server {0} does not currently have monitoring enabled' .format(server.get('name'))) # Implement Check Mode if module.check_mode: module.exit_json( msg= 'Monitoring of type {0} will be removed from the server with ID {1}' .format(module.params.get('plan'), server.get('id'))) remove_monitoring(module, client, server.get('id')) try: server = client.get_server_by_name(datacenter, network_domain_id, None, name) except NTTMCPAPIException: pass module.exit_json(changed=True, data=server) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not remove the monitoring from server {0} - {1}'. format(module.params.get('name'), e))
def main(): """ Main function :returns: Server Anti-Affinity Group information or a message """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), server=dict(required=True, type='str'), vlan=dict(default=None, required=False, type='str'), state=dict(default='present', required=False, choices=['present', 'absent'])), supports_check_mode=True) network_domain_name = module.params.get('network_domain') network_domain_id = group_type = member_id = None vlan = sec_group = server = nic = dict() datacenter = module.params.get('datacenter') state = module.params.get('state') try: credentials = get_credentials(module) if credentials is False: module.fail_json(msg='Could not load the user credentials') except ImportError as e: module.fail_json(msg='{0}'.format(e)) # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(network_domain_name, datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Try and find any existing Security Group try: if module.params.get('name'): sec_groups = client.list_security_groups( network_domain_id=None if module.params.get('vlan') else network_domain_id, name=None, group_type=None, server_id=None, vlan_id=vlan.get('id', None)) sec_group = [ x for x in sec_groups if x.get('name') == module.params.get('name') ][0] if module.params.get('id'): sec_group = client.get_security_group_by_id( group_id=module.params.get('id')) if sec_group: group_type = sec_group.get('type').lower() else: module.fail_json( msg='Could not find the Security Group {0}'.format( module.params.get('name'))) except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Security Group {0}'.format( module.params.get('name'))) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter, network_domain_id, None, module.params.get('server')) if not server: module.fail_json(msg='Failed to find the server - {0}'.format( module.params.get('server'))) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to find the server - {0}'.format(e)) # Search for any NICs that match any supplied VLAN if module.params.get('vlan'): try: nics = [server.get('networkInfo', {}).get('primaryNic') ] + server.get('networkInfo', {}).get('additionalNic') nic = [ x for x in nics if x.get('vlanName') == module.params.get('vlan') ][0] except (KeyError, IndexError, AttributeError): module.fail_json( msg='Failed to find the NIC for server {0} in VLAN {1}'.format( module.params.get('server'), module.params.get('vlan'))) # Check if the NIC already exists in the Security Group try: if [ x for x in sec_group.get('nics', {}).get('nic', []) if x.get('id') == nic.get('id') ][0]: if state == 'present': module.exit_json( msg= 'NIC with ID {0} is already a member of the Security Group {1}' .format(nic.get('id'), sec_group.get('id'))) except IndexError: if state == 'absent': module.exit_json( msg= 'The NIC with ID {0} is not a member of the Security Group {1}' .format(nic.get('id'), sec_group.get('id'))) pass if module.check_mode: module.exit_json( msg= 'The NIC ID {0} will be added to the Security Group with ID {1}' .format(nic.get('id'), sec_group.get('id'))) member_id = nic.get('id') else: member_id = server.get('id') # Check if the server is already a member of the Security Group try: if [ x for x in sec_group.get('servers', {}).get('server', []) if x.get('id') == server.get('id') ][0]: if state == 'present': module.exit_json( msg= 'Server with ID {0} is already a member of the Security Group {1}' .format(server.get('id'), sec_group.get('id'))) except IndexError: if state == 'absent': module.exit_json( msg= 'The Server with ID {0} is not a member of the Security Group {1}' .format(server.get('id'), sec_group.get('id'))) pass if module.check_mode: module.exit_json( msg= 'The Server ID {0} will be added to the Security Group with ID {1}' .format(server.get('id'), sec_group.get('id'))) try: if state == 'present': try: client.add_security_group_member(group_id=sec_group.get('id'), group_type=group_type, member_id=member_id) sec_group = client.get_security_group_by_id( group_id=sec_group.get('id')) if not sec_group: module.warn( warning= 'Could not verify the update of the Security Group with ID {0}' .format(sec_group.get('id'))) module.exit_json(changed=True, data=sec_group) except (NTTMCPAPIException) as e: module.fail_json( msg='Failed to update the Security Group - {0}'.format(e)) # Delete the Security Group elif state == 'absent': if not sec_group: module.exit_json(msg='Security Group not found') # Implement Check Mode if module.check_mode: module.exit_json( msg= 'An existing Security Group was found for {0} and will be removed' .format(sec_group.get('id'))) result = client.delete_security_group_member( group_id=sec_group.get('id'), member_id=member_id, group_type=group_type) if result.get('responseCode') == 'OK': module.exit_json( changed=True, msg='The Security Group member was successfully removed') module.fail_json( msg='Could not remove the Security Group member - {0}'.format( result.content)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not remove the Security Group member - {0}'.format(e))
def main(): """ Main function :returns: IP Address List Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), name=dict(required=False, type='str'), version=dict(required=False, default='IPV4', type='str', choices=['IPV4', 'IPV6']), network_domain=dict(required=True, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('name') network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') version = module.params.get('version') return_data = return_object('ip_list') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) try: if name: result = client.get_ip_list_by_name(network_domain_id, name, version) if result: return_data['ip_list'].append(result) else: return_data['ip_list'] = client.list_ip_list( network_domain_id, version) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve a list of the IP Address Lists - {0}'. format(e)) return_data['count'] = len(return_data.get('ip_list')) module.exit_json(data=return_data)
def main(): """ Main function :returns: Port list Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), name=dict(required=True, type='str'), description=dict(required=False, type='str'), ports=dict(required=False, type='list', elements='dict'), ports_nil=dict(required=False, default=False, type='bool'), child_port_lists=dict(required=False, type='list', elements='str'), child_port_lists_nil=dict(required=False, default=False, type='bool'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('name') network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Get a list of existing CNDs and check if the name already exists try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Get a list of existing port lists try: port_list = client.get_port_list_by_name( name=name, network_domain_id=network_domain_id) except NTTMCPAPIException as e: module.fail_json( msg='Failed to get a list of Port Lists - {0}'.format(e), exception=traceback.format_exc()) if state == 'present': if not port_list: # Implement check_mode if module.check_mode: module.exit_json( msg= 'A new port list will be created with the parameters: {0}'. format(module.params)) create_port_list(module, client, network_domain_id) else: if compare_port_list(module, client, network_domain_id, deepcopy(port_list), False): update_port_list(module, client, network_domain_id, port_list) module.exit_json(data=port_list) elif state == 'absent': if not port_list: module.exit_json(msg='Port List {0} was not found'.format(name)) # Implement check_mode if module.check_mode: module.exit_json(msg='This port list will be removed', data=port_list) delete_port_list(module, client, port_list)
def main(): """ Main function :returns: Updated VIP node Information """ module = AnsibleModule( argument_spec=dict(auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), id=dict(required=False, default=None, type='str'), name=dict(required=False, default=None, type='str'), description=dict(required=False, default=None, type='str'), ip_address=dict(required=False, default=None, type='str'), status=dict(required=False, default='ENABLED', choices=VIP_NODE_STATES), connection_limit=dict(required=False, default=100000, type='int'), connection_rate_limit=dict(required=False, default=4000, type='int'), health_monitor=dict(required=False, default=None, type='str'), no_health_monitor=dict(required=False, default=False, type='bool'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') node = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) # Check connection input values if module.params.get('connection_limit') not in range(1, 100001): module.fail_json(msg='connection_limit must be between 1 and 100000') if module.params.get('connection_rate_limit') not in range(1, 4001): module.fail_json( msg='connection_rate_limit must be between 1 and 4000') if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except NTTMCPAPIException as e: module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Check if the Node already exists try: if module.params.get('id'): node = client.get_vip_node(module.params.get('id')) else: nodes = client.list_vip_node( network_domain_id=network_domain_id, name=module.params.get('name'), ip_address=module.params.get('ip_address')) if len(nodes) == 1: node = nodes[0] except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not get a list of existing nodes to check against - {0}' .format(e)) # Check if a Health Monitoring profile was provided and update module.params with the UUID instead of the name # Since list_vip_health_monitor returns both Node and Pool profiles, we need to check that the name provided is node compatible try: health_monitors = client.list_vip_health_monitor( network_domain_id=network_domain_id) for profile in health_monitors: if module.params.get('health_monitor') == profile.get( 'name') and profile.get('nodeCompatible'): module.params['health_monitor'] = profile.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg= 'Could not get a list of existing Health Monitoring profiles to check against - {0}' .format(e)) if state == 'present': if not node: # Implement Check Mode if module.check_mode: module.exit_json(msg='The VIP Node will be created') create_vip_node(module, client, network_domain_id) else: if compare_vip_node(module, node): update_vip_node(module, client, node) module.exit_json(data=node) elif state == 'absent': if not node: module.exit_json(msg='No VIP node not found. Nothing to remove.') # Implement Check Mode if module.check_mode: module.exit_json( msg='The VIP Node with ID {0} will be deleted'.format( node.get('id'))) delete_vip_node(module, client, node.get('id'))
def main(): """ Main function :returns: Firewall rule Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), name=dict(required=False, type='str'), network_domain=dict(required=True, type='str'), action=dict(default='ACCEPT_DECISIVELY', choices=['ACCEPT_DECISIVELY', 'DROP']), version=dict(required=False, default='IPV4', choices=['IPV4', 'IPV6']), protocol=dict(default='TCP', choices=['TCP', 'UDP', 'IP', 'ICMP']), src_cidr=dict(required=False, type='str'), src_ip_list=dict(required=False, type='str'), dst_cidr=dict(required=False, type='str'), dst_ip_list=dict(required=False, type='str'), src_port_start=dict(required=False, default=None, type='str'), src_port_end=dict(required=False, default=None, type='str'), src_port_list=dict(required=False, default=None, type='str'), dst_port_start=dict(required=False, default=None, type='str'), dst_port_end=dict(required=False, default=None, type='str'), dst_port_list=dict(required=False, default=None, type='str'), enabled=dict(default=True, type='bool'), position=dict(default='LAST', choices=['FIRST', 'LAST', 'BEFORE', 'AFTER']), position_to=dict(required=False, default=None, type='str'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) fw_rule_exists = None name = module.params['name'] network_domain_name = module.params['network_domain'] datacenter = module.params['datacenter'] state = module.params['state'] src_cidr = dst_cidr = None # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Check to see the CIDR provided is valid if module.params.get('src_cidr'): try: src_cidr = ip_net(unicode(module.params.get('src_cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid source CIDR format {0}: {1}'.format( module.params.get('src_cidr'), e)) if module.params.get('dst_cidr'): try: dst_cidr = ip_net(unicode(module.params.get('dst_cidr'))) except (AddressValueError, ValueError) as e: module.fail_json( msg='Invalid destination CIDR format {0}: {1}'.format( module.params.get('dst_cidr'), e)) # Get the CND object based on the supplied name try: network = client.get_network_domain_by_name(datacenter=datacenter, name=network_domain_name) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Failed to find the Cloud Network Domains - {0}'.format(e), exception=traceback.format_exc()) # If a firewall rule name is provided try and locate the rule if name: try: fw_rule_exists = client.get_fw_rule_by_name( network_domain_id, name) except NTTMCPAPIException as e: module.fail_json( msg='Could not locate the existing firewall rule - {0}'.format( e), exception=traceback.format_exc()) try: if state == 'present': if fw_rule_exists: update_fw_rule(module, client, network_domain_id, fw_rule_exists, src_cidr, dst_cidr) else: # Implement check_mode if module.check_mode: module.exit_json(msg='This firewall rule will be created', data=module.params) create_fw_rule(module, client, network_domain_id, src_cidr, dst_cidr) elif state == 'absent': if not fw_rule_exists: module.exit_json( msg='The firewall rule {0} was not found'.format(name)) # Implement check_mode if module.check_mode: module.exit_json(msg='This firewall rule will be removed', data=fw_rule_exists) delete_fw_rule(module, client, network_domain_id, name) except NTTMCPAPIException as e: module.fail_json( msg='Could not operate on the firewall rule - {0}'.format(e), exception=traceback.format_exc())
def main(): """ Main function :returns: Public IPv4 address information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), next_free_public_ipv4=dict(required=False, default=True, type='bool'), ip_address=dict(required=False, default=None, type='str'), id=dict(default=None, type='str'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') next_free_public_ipv4 = module.params.get('next_free_public_ipv4') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) if state == 'present' and next_free_public_ipv4: # Implement Check Mode if module.check_mode: module.exit_json( msg='A public IPv4 address will be consumed in ' 'the Network Domains with ID {0}'.format(network_domain_id)) get_next_free_public_ipv4(module, client, network_domain_id) elif state == 'present' and not next_free_public_ipv4: # Implement Check Mode if module.check_mode: module.exit_json( msg='A new /31 public IPv4 block will be added to' ' the Network Domains with ID {0}'.format(network_domain_id)) add_public_ipv4(module, client, network_domain_id) elif state == 'absent': delete_public_ipv4(module, client, network_domain_id)
def main(): """ Main function :returns: IP Address Management Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), name=dict(required=False, type='str'), description=dict(required=False, type='str'), network_domain=dict(required=True, type='str'), vlan=dict(required=False, default=None, type='str'), version=dict(required=False, default=4, choices=[4, 6], type='int'), reserved=dict(required=False, default=False, type='bool'), id=dict(default=None, type='str'), public_ip_address=dict(required=False, default=None, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) vlan = {} network_domain_name = module.params.get('network_domain') vlan_name = module.params.get('vlan') datacenter = module.params.get('datacenter') object_id = module.params.get('id') version = module.params.get('version') reserved = module.params.get('reserved') public_ip_address = module.params.get('public_ip_address') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if not credentials: module.fail_json(msg='Error: Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # find the provided VLAN if vlan_name: try: vlan = client.get_vlan_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=vlan_name) except NTTMCPAPIException: pass except KeyError: module.fail_json(msg='A valid VLAN is required') except IndexError: pass if version == 4 and not object_id and not reserved: list_public_ipv4(module, client, network_domain_id) elif (version == 4 and object_id and not reserved) or public_ip_address: get_public_ipv4(module, client, network_domain_id, object_id, public_ip_address) elif reserved: list_reserved_ip(module, client, datacenter, vlan, version) else: module.fail_json(msg='Ambiguous arguments supplied')
def main(): """ Main function :returns: Static route Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), name=dict(default=None, required=False, type='str'), description=dict(default=None, required=False, type='str'), cidr=dict(default=None, required=False, type='str'), next_hop=dict(default=None, required=False, type='str'), state=dict(default='present', choices=['present', 'absent', 'restore']) ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) name = module.params.get('name') network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') state = module.params.get('state') route = network_cidr = None routes = [] # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Error: Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Check to see the CIDR provided is valid if module.params.get('cidr'): try: network_cidr = ip_net(unicode(module.params.get('cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format(module.params.get('cidr'), e)) # Get a list of existing CNDs and check if the name already exists try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to get a list of Cloud Network Domains - {0}'.format(e)) # Check if a route already exists for this name if state != 'restore': try: if name: routes = client.list_static_routes(network_domain_id=network_domain_id, name=name) if not routes and network_cidr: # If no matching routes were found for the name check to see if the supplied # network parameters match a rule with a different name routes = client.list_static_routes(network_domain_id=network_domain_id, name=None, network=str(network_cidr.network_address), prefix=network_cidr.prefixlen, next_hop=module.params.get('next_hop')) if len(routes) == 1: route = routes[0] elif len(routes) == 1: route = routes[0] except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to get a list of existing Static Routes - {0}'.format(e)) try: if state == 'present': if not route: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new static route will be created for {0}'.format(str(network_cidr))) create_static_route(module, client, network_domain_id, network_cidr) else: # Static Routes cannot be updated. The old one must be removed and a new one created with the new parameters if compare_static_route(module, network_cidr, route): delete_static_route(module, client, route.get('id')) create_static_route(module, client, network_domain_id, network_cidr) module.exit_json(data=route) elif state == 'restore': # Implement Check Mode if module.check_mode: module.exit_json(msg='The routes for Network Domain {0} will be restored to defaults'.format( module.params.get('network_domain'))) client.restore_static_routes(network_domain_id) module.exit_json(changed=True, msg='Successfully restored the default Static Routes') elif state == 'absent': if route: # Implement Check Mode if module.check_mode: module.exit_json(msg='An existing static route was found for {0} and will be removed'.format( route.get('name'))) delete_static_route(module, client, route.get('id')) module.exit_json(changed=True, msg='Successfully deleted the Static Route rule') else: module.exit_json(msg='No existing static route was matched') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not update the static route - {0}'.format(e))
def main(): """ Main function :returns: A message """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=False, default=None, type='str'), name=dict(required=True, type='str'), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=1800, type='int'), wait_poll_interval=dict(required=False, default=30, type='int'), ), supports_check_mode=True) result = None CORE['name'] = module.params.get('name') try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name( name=module.params.get('network_domain'), datacenter=module.params.get('datacenter')) CORE['network_domain_id'] = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json( msg='Could not find the Cloud Network Domain: {0}'.format( module.params.get('network_domain'))) try: server = client.get_server_by_name( datacenter=module.params.get('datacenter'), network_domain_id=network.get('id'), name=CORE.get('name')) if not server: module.fail_json(msg='Could not find the server - {0} in {1}'. format(module.params.get('name'), module.params.get('datacenter'))) except (KeyError, IndexError, AttributeError): module.fail_json(msg='Could not find the server - {0} in {1}'.format( module.params.get('name'), module.params.get('datacenter'))) try: if module.check_mode: module.exit_json( 'Server with ID {0} will be migrated from preview mode'.format( server.get('id'))) result = client.migrate_snapshot_preview(server.get('id')) if result.get('responseCode') != 'IN_PROGRESS': module.fail_json( msg='The Snapshot server migration failed with reason - {0}'. format(result.get('responseCode'))) wait_for_migration(module, client, server.get('id')) module.exit_json( msg='The Snapshot Preview server migration was successful') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not retrieve the Snapshot - {0}'.format(e))
def main(): """ Main function :returns: Server Anti-Affinity Group information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), servers=dict(required=True, type='list', elements='str'), state=dict(default='present', required=False, choices=['present', 'absent'])), supports_check_mode=True) network_domain_name = module.params.get('network_domain') network_domain_id = aa_group = None server_ids = list() datacenter = module.params.get('datacenter') state = module.params.get('state') try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(network_domain_name, datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) # Get the servers for server in module.params.get('servers'): try: server = client.get_server_by_name( datacenter=datacenter, network_domain_id=network_domain_id, name=server) if server: server_ids.append(server.get('id')) else: module.fail_json( msg='Could not find the server - {0} in {1}'.format( server, datacenter)) except (KeyError, IndexError, AttributeError): module.fail_json( msg='Could not find the server - {0} in {1}'.format( server, datacenter)) # Attempt to find any existing AntiAffinity Group for this server combination try: aa_group = client.get_anti_affinity_group_by_servers( server_ids[0], server_ids[1]) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve any Server Anti Affinity Groups - {0}'. format(e)) try: if state == 'present': if aa_group: module.exit_json( msg='The server anti-affinity group already exists', data=aa_group) if module.check_mode: module.exit_json( msg='A new server anti-affinity group will be created') client.create_anti_affinity_group(server_ids[0], server_ids[1]) aa_group = client.get_anti_affinity_group_by_servers( server_ids[0], server_ids[1]) module.exit_json(changed=True, data=aa_group) elif state == 'absent': if not aa_group: module.exit_json( msg= 'No server anti-affinity group exists for this server combination' ) if module.check_mode: module.exit_json( msg='The server anti-affinity group {0} will be removed'. format(aa_group.get('id'))) client.delete_anti_affinity_group(aa_group.get('id')) module.exit_json( changed=True, msg='The Anti-Affinity Group {0} was successfully removed'. format(aa_group.get('id'))) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='{0}'.format(e).replace('"', '\''))
def main(): """ Main function :returns: Server Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), description=dict(required=False, type='str'), image=dict(required=True, type='str'), cluster=dict(required=False, type='str'), goc=dict(required=False, default=True, type='bool'), wait=dict(required=False, default=True, type='bool'), wait_time=dict(required=False, default=3600, type='int'), wait_poll_interval=dict(required=False, default=30, type='int') ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) server_name = module.params.get('server') datacenter = module.params.get('datacenter') network_domain_name = module.params.get('network_domain') server = dict() # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(network_domain_name, datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.format(network_domain_name)) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=server_name) if not server: module.fail_json(msg='Failed attempting to locate any existing server') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed attempting to locate any existing server - {0}'.format(e)) if module.check_mode: module.exit_json(msg='The server with ID {0} will be cloned to the image {1}'.format(server.get('id'), module.params.get('image'))) try: result = client.clone_server_to_image(server_id=server.get('id'), image_name=module.params.get('image'), description=module.params.get('description'), cluster_id=module.params.get('cluster'), goc=module.params.get('goc')) if module.params.get('wait'): wait_for_server(module, client, server.get('id')) module.exit_json(changed=True, msg='The server with ID {0} was successfully cloned to the new image with ID {1}'.format(server.get('id'), result)) except NTTMCPAPIException as e: module.fail_json(msg='Could not clone the server with ID {0} - {1}'.format(server.get('id'), e))
def main(): """ Main function :returns: Static Route Information """ module = AnsibleModule( argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), name=dict(default=None, required=False, type='str'), cidr=dict(default=None, required=False, type='str'), next_hop=dict(default=None, required=False, type='str'), version=dict(default=4, required=False, type='int', choices=[4, 6]) ), supports_check_mode=True ) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') name = module.params.get('name') routes = [] route = network_cidr = None # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json(msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') client = NTTMCPClient(credentials, module.params.get('region')) # Check to see the CIDR provided is valid if module.params.get('cidr'): try: network_cidr = ip_net(unicode(module.params.get('cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid network CIDR format {0}: {1}'.format(module.params.get('cidr'), e)) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.format(network_domain_name)) # Check if a route already exists for this name try: if name: routes = client.list_static_routes(network_domain_id=network_domain_id, name=name) if not routes and module.params.get('cidr'): # If no matching routes were found for the name check to see if the supplied # network parameters match a rule with a different name routes = client.list_static_routes(network_domain_id=network_domain_id, name=None, network=str(network_cidr.network_address), prefix=network_cidr.prefixlen, next_hop=module.params.get('next_hop')) if len(routes) == 1: route = routes[0] elif len(routes) == 1: route = routes[0] except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Failed to get a list of existing Static Routes - {0}'.format(e)) if route: return_data = return_object('route') return_data = return_object('route') return_data['count'] = len(return_data.get('route')) module.exit_json(data=route) list_static_routes(module, client, network_domain_id, network_cidr)
def main(): """ Main function :returns: VIP Node Information """ module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict( required=True, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), ip_address=dict(default=None, required=False, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) network_domain_name = module.params.get('network_domain') datacenter = module.params.get('datacenter') object_id = module.params.get('id') ip_address = module.params.get('ip_address') name = module.params.get('name') # Check Imports if not HAS_IPADDRESS: module.fail_json(msg='Missing Python module: ipaddress') # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, NTTMCPAPIException): module.fail_json(msg='Could not find the Cloud Network Domain: {0}'. format(network_domain_name)) if object_id: get_vip_node(module, client, object_id) else: list_vip_node(module, client, network_domain_id, name, ip_address)
def main(): """ Main function :returns: Server Information """ module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict( required=False, type='str'), vlan=dict(default=None, required=False, type='str'), name=dict(required=False, type='str'), id=dict(required=False, type='str')), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) return_data = return_object('server') name = module.params.get('name') server_id = module.params.get('id') datacenter = module.params.get('datacenter') network_domain_name = module.params.get('network_domain') vlan_name = module.params.get('vlan') network_domain_id = vlan_id = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND object based on the supplied name try: if network_domain_name: network_domain = client.get_network_domain_by_name( name=network_domain_name, datacenter=datacenter) network_domain_id = network_domain.get('id') else: network_domain_id = None if network_domain_name and not network_domain: module.fail_json( msg='Failed to locate the Cloud Network Domain - {0}'.format( network_domain_name)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json(msg='Failed to locate the Cloud Network Domain - {0}'. format(network_domain_name)) # Get the VLAN object based on the supplied name try: if vlan_name: vlan = client.get_vlan_by_name(name=vlan_name, datacenter=datacenter, network_domain_id=network_domain_id) vlan_id = vlan.get('id') else: vlan_id = None except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json( msg='Failed to locate the VLAN - {0}'.format(vlan_name)) try: if server_id: server = client.get_server_by_id(server_id=server_id) if server: return_data['server'].append(server) elif name: server = client.get_server_by_name( datacenter=datacenter, network_domain_id=network_domain_id, name=name) if server: return_data['server'].append(server) else: servers = client.list_servers(datacenter, network_domain_id, vlan_id, name) return_data['server'] = servers except (KeyError, IndexError, AttributeError): module.fail_json(msg='Could not find the server - {0} in {1}'.format( name, datacenter)) return_data['count'] = len(return_data.get('server')) module.exit_json(data=return_data)
def main(): """ Main function :returns: Snapshot Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), datacenter=dict(required=True, type='str'), network_domain=dict(required=True, type='str'), server=dict(required=True, type='str'), pre_path=dict(required=False, default=None, type='str'), pre_description=dict(required=False, default=None, type='str'), pre_timeout=dict(required=False, default=300, type='int'), on_failure=dict(required=False, default='CONTINUE', choices=['CONTINUE', 'ABORT']), post_path=dict(required=False, default=None, type='str'), post_description=dict(required=False, default=None, type='str'), username=dict(required=False, default=None, type='str'), password=dict(required=False, default=None, type='str'), state=dict(default='present', choices=['present', 'absent'])), supports_check_mode=True) try: credentials = get_credentials(module) except ImportError as e: module.fail_json(msg='{0}'.format(e)) state = module.params.get('state') datacenter = module.params.get('datacenter') network_domain_name = module.params.get('network_domain') server_name = module.params.get('server') network_domain_id = None server = None # Check the region supplied is valid regions = get_regions() if module.params.get('region') not in regions: module.fail_json( msg='Invalid region. Regions must be one of {0}'.format(regions)) if credentials is False: module.fail_json(msg='Could not load the user credentials') try: client = NTTMCPClient(credentials, module.params.get('region')) except NTTMCPAPIException as e: module.fail_json(msg=e.msg) # Get the CND try: network = client.get_network_domain_by_name(name=network_domain_name, datacenter=datacenter) network_domain_id = network.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not find the Cloud Network Domain: {0}'.format(e)) # Check if the Server exists based on the supplied name try: server = client.get_server_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=server_name) if not server.get('guest', {}).get( 'vmTools', {}).get('runningStatus') == 'RUNNING': raise NTTMCPAPIException( 'VMWare Tools is required to be running on server {0}'.format( server_name)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not locate any existing server - {0}'.format(e)) if state == 'present': if module.params.get('pre_path') is not None or module.params.get( 'post_path') is not None: if None in [ module.params.get('username'), module.params.get('password') ]: module.fail_json( msg= 'A username and password is required when configuring a snapshot script(s)' ) if not server.get('snapshotService'): module.fail_json( msg= 'This server does not currently have the Snapshot Service enabled' ) else: if compare_scripts(module, server.get('snapshotService')): update_scripts(module, client, server) module.exit_json( changed=True, msg='Snapshot scripts updated for server {0}'.format( server.get('id'))) module.exit_json(msg='No changes required') elif state == 'absent': if not server.get('snapshotService'): module.exit_json( msg='Snapshots are not currently configured for this server') if compare_scripts(module, server.get('snapshotService')): delete_scripts(module, client, server) module.exit_json(changed=True, msg="Server snapshot script(s) have been deleted") module.exit_json(msg='No changes required')