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: 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: 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'), 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: VLAN 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'), ipv4_cidr=dict(default=None, required=False, type='str'), vlan_type=dict(default='attachedVlan', choices=['attachedVlan', 'detachedVlan']), attached_vlan_gw=dict(required=False, default='LOW', choices=['LOW', 'HIGH']), detached_vlan_gw=dict(required=False, type='str'), detached_vlan_gw_ipv6=dict(required=False, 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') network_domain_name = module.params.get('network_domain') # 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, 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=name, datacenter=datacenter, network_domain_id=network_domain_id) except NTTMCPAPIException as exc: module.fail_json(msg='Failed to get a list of VLANs - {0}'.format(exc)) # Create the VLAN if state == 'present': # Handle case where VLAN name already exists if not vlan: # Implement Check Mode if module.check_mode: module.exit_json(msg='A new VLAN will be created in the Cloud Network Domain with the UUID {0}'.format( network_domain_id)) create_vlan(module, client, network_domain_id) else: try: if compare_vlan(module, vlan): update_vlan(module, client, vlan) module.exit_json(data=vlan) except NTTMCPAPIException as exc: module.fail_json(msg='Failed to update the VLAN - {0}'.format(exc)) # Delete the VLAN elif state == 'absent': if not vlan: module.exit_json(msg='VLAN not found') # Implement Check Mode if module.check_mode: module.exit_json(msg='An existing VLAN was found for {0} and will be removed'.format( vlan.get('id'))) delete_vlan(module, client, datacenter, network_domain_id, vlan.get('id'))
def main(): """ Main function :returns: Ansible Gateway Host 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'), vlan=dict(required=True, type='str'), name=dict(required=False, default='ansible_gw', type='str'), password=dict(default=None, required=False, type='str'), image=dict(required=False, type='str'), ipv4=dict(default=None, required=False, type='str'), src_ip=dict(default='ANY', required=False, type='str'), src_prefix=dict(default=None, required=False, type='str'), state=dict(default='present', choices=['present', 'absent']), 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') ) ) 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') network_domain_name = module.params.get('network_domain') vlan_name = module.params.get('vlan') vlan_id = ansible_gw = None return_data = {} changed = False # 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 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 exc: module.fail_json(msg='Failed to find the Cloud Network Domain - {0}'.format(exc)) # Get the VLAN object based on the supplied name try: vlan = client.get_vlan_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=vlan_name) vlan_id = vlan.get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json(msg='Failed to get a list of VLANs - {0}'.format(exc), exception=traceback.format_exc()) # Check if the server exists based on the supplied name try: ansible_gw = client.get_server_by_name(datacenter, network_domain_id, vlan_id, name) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json(msg='Failed attempting to locate any existing server - {0}'.format(exc)) # Handle the case where the gateway already exists. This could mean a playbook re-run # If the server exists then we need to check: # a public IP is allocated and if not get the next one # the NAT rule is present and correct and if not update/create it # the Firewall rule is present and correct and if not update/create it if state == 'present' and ansible_gw: try: ansible_gw_private_ipv4 = ansible_gw.get('networkInfo').get('primaryNic').get('privateIpv4') # Check if the NAT rule exists and if not create it nat_result = client.get_nat_by_private_ip(network_domain_id, ansible_gw.get('networkInfo').get('primaryNic').get('privateIpv4')) if nat_result: public_ipv4 = nat_result.get('externalIp') else: public_ipv4 = client.get_next_public_ipv4(network_domain_id).get('ipAddress') create_nat_rule(module, client, network_domain_id, ansible_gw_private_ipv4, public_ipv4) changed = True # Check if the Firewall rule exists and if not create it fw_result = client.get_fw_rule_by_name(network_domain_id, ACL_RULE_NAME) if fw_result: update_fw_rule(module, client, fw_result, network_domain_id, public_ipv4) else: create_fw_rule(module, client, network_domain_id, public_ipv4) changed = True return_data['server_id'] = ansible_gw.get('id') return_data['password'] = ansible_gw.get('password', None) return_data['internal_ipv4'] = ansible_gw.get('networkInfo').get('primaryNic').get('privateIpv4') return_data['ipv6'] = ansible_gw.get('networkInfo').get('primaryNic').get('ipv6') return_data['public_ipv4'] = public_ipv4 module.exit_json(changed=changed, data=return_data) except (KeyError, IndexError, AttributeError) as e: module.fail_json(msg='Could not ascertain the current server state: {0}'.format(e)) elif state == 'present' and not ansible_gw: try: ansible_gw = create_server(module, client, network_domain_id, vlan_id) changed = True ansible_gw_private_ipv4 = ansible_gw.get('networkInfo').get('primaryNic').get('privateIpv4') # Check if the NAT rule exists and if not create it nat_result = client.get_nat_by_private_ip(network_domain_id, ansible_gw_private_ipv4) if nat_result: public_ipv4 = nat_result.get('externalIp') else: public_ipv4 = client.get_next_public_ipv4(network_domain_id).get('ipAddress') create_nat_rule(module, client, network_domain_id, ansible_gw_private_ipv4, public_ipv4) changed = True # Check if the Firewall rule exists and if not create it fw_result = client.get_fw_rule_by_name(network_domain_id, ACL_RULE_NAME) if fw_result: update_fw_rule(module, client, fw_result, network_domain_id, public_ipv4) else: create_fw_rule(module, client, network_domain_id, public_ipv4) changed = True return_data['server_id'] = ansible_gw.get('id') return_data['password'] = ansible_gw.get('password') return_data['internal_ipv4'] = ansible_gw_private_ipv4 return_data['ipv6'] = ansible_gw.get('networkInfo').get('primaryNic').get('ipv6') return_data['public_ipv4'] = public_ipv4 sleep(10) module.exit_json(changed=changed, data=return_data) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json(changed=changed, msg='Failed to create/update the Ansible gateway - {0}'.format(exc), exception=traceback.format_exc()) elif state == 'absent': nat_result = public_ipv4 = None try: # Check if the server exists and remove it if ansible_gw: delete_server(module, client, ansible_gw) # Check if the Firewall rule exists and if not create it fw_result = client.get_fw_rule_by_name(network_domain_id, ACL_RULE_NAME) if fw_result: delete_fw_rule(module, client, network_domain_id, fw_result.get('name')) ''' Cases may exist where the only either the NAT private or public address maybe know so two ensure we've checked all possible options we will search by both The private address can be found from the server/ansible_gw object but the public address can only be determined by the firewall rule desintation value ''' if ansible_gw: ansible_gw_private_ipv4 = ansible_gw.get('networkInfo').get('primaryNic').get('privateIpv4') nat_result = client.get_nat_by_private_ip(network_domain_id, ansible_gw_private_ipv4) if nat_result: public_ipv4 = nat_result.get('externalIp') elif fw_result: public_ipv4 = fw_result.get('destination').get('ip').get('address') nat_result = client.get_nat_by_public_ip(network_domain_id, public_ipv4) if nat_result: delete_nat_rule(module, client, nat_result.get('id')) if public_ipv4: public_ip_block = client.get_public_ipv4_by_ip(network_domain_id, public_ipv4) if public_ip_block: if not client.check_public_block_in_use(network_domain_id, public_ip_block.get('baseIp')): delete_public_ipv4(module, client, public_ip_block.get('id')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(changed=changed, msg='Failed to remove the Ansible gateway configuration - {0}'.format(e)) module.exit_json(changed=True, msg='The Ansible gateway and associated NAT and firewall rules have been removed') else: module.exit_json(changed=False, msg='Nothing to remove')
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'), type=dict(default='vlan', required=False, choices=['vlan', 'server']), name=dict(default=None, required=False, type='str'), id=dict(default=None, required=False, type='str'), server=dict(default=None, required=False, type='str'), vlan=dict(default=None, required=False, type='str')), supports_check_mode=True) network_domain_name = module.params.get('network_domain') network_domain_id = None server = vlan = dict() datacenter = module.params.get('datacenter') return_data = return_object('security_group') 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)) # If a server name was provided get the server object if module.params.get('server'): try: server = client.get_server_by_name( datacenter=datacenter, network_domain_id=network_domain_id, name=module.params.get('server')) if not server: module.fail_json( msg='Could not find the server - {0} in {1}'.format( module.params.get('server'), datacenter)) except (KeyError, IndexError, AttributeError): module.fail_json( msg='Could not find the server - {0} in {1}'.format( module.params.get('server'), datacenter)) # If a vlan name was provided get the vlan object if module.params.get('vlan'): try: vlan = client.get_vlan_by_name(datacenter=datacenter, network_domain_id=network_domain_id, name=module.params.get('vlan')) if not vlan: module.fail_json( msg='Could not find the VLAN - {0} in {1}'.format( module.params.get('vlan'), datacenter)) except (KeyError, IndexError, AttributeError): module.fail_json(msg='Could not find the VLAN - {0} in {1}'.format( module.params.get('vlan'), datacenter)) try: if module.params.get('id'): return_data['security_group'] = client.get_security_group_by_id( group_id=module.params.get('id')) else: return_data['security_group'] = client.list_security_groups( network_domain_id=network_domain_id, name=module.params.get('name'), group_type=module.params.get('type'), server_id=server.get('id', None), vlan_id=vlan.get('id', None)) return_data['count'] = len(return_data['security_group']) module.exit_json(data=return_data) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve any Security Groups - {0}'.format(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(default=None, required=False, type='str'), id=dict(default=None, required=False, type='str'), name=dict(default=None, required=False, type='str'), new_name=dict(default=None, required=False, type='str'), description=dict(default=None, required=False, type='str'), vlan=dict(default=None, required=False, type='str'), force=dict(default=False, required=False, type='bool'), 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 = None vlan = sec_group = 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 if state == 'present': 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)) # If a vlan name was provided get the vlan object if module.params.get('vlan'): try: vlan = client.get_vlan_by_name( datacenter=datacenter, network_domain_id=network_domain_id, name=module.params.get('vlan')) if not vlan: module.fail_json( msg='Could not find the VLAN - {0} in {1}'.format( module.params.get('vlan'), datacenter)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException): module.fail_json( msg='Could not find the VLAN - {0} in {1}'.format( module.params.get('vlan'), datacenter)) # Try and find any existing Security Group try: if module.params.get('name'): sec_group = client.list_security_groups( network_domain_id=network_domain_id, name=module.params.get('name'), group_type=None, server_id=None, vlan_id=vlan.get('id', None))[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() except (KeyError, IndexError, AttributeError, NTTMCPAPIException): pass try: if state == 'present': # Handle case where security Group already exists if not sec_group: # Implement Check Mode if module.check_mode: module.exit_json( msg='A new {0} Security Group will be created'.format( 'VLAN' if module.params.get('vlan') else 'Server')) sec_group = create_security_group(module, client, network_domain_id, vlan.get('id')) else: try: if not compare_security_group(module, sec_group): module.exit_json(data=sec_group) sec_group = update_security_group(module, client, sec_group) except (NTTMCPAPIException) as e: module.fail_json( msg='Failed to update the Security Group - {0}'.format( e)) module.exit_json(changed=True, data=sec_group) # 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'))) if module.params.get('force'): delete_security_group_members(module, client, sec_group, group_type) result = client.delete_security_group(sec_group.get('id')) if result.get('responseCode') == 'OK': module.exit_json( changed=True, msg='The Security Group was successfully removed') module.fail_json(msg='Could not remove the Security Group - {0}'. format(result.content)) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve any Security Groups - {0}'.format(e))