def get_snat_exclusion(module, client, network_domain_id, snat_id, network_cidr): """ Get a SNAT by UUID :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg snat_id: The UUID of the SNAT :arg network_cidr: The network CIDR for the SNAT exclusion :returns: SNAT object """ return_data = return_object('snat') if snat_id is None and network_cidr is None: module.fail_json(msg='A value for id and cidr is required') try: if snat_id: result = client.get_snat_exclusion(snat_id) else: # 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: result = snats[0] if result is None: module.fail_json(msg='Could not find the SNAT Exclusion for {0}'. format(snat_id if snat_id else str(network_cidr))) return_data['snat'].append(result) except NTTMCPAPIException as exc: module.fail_json( msg='Could not get the SNAT Exclusion - {0}'.format(exc)) return_data['count'] = len(return_data['snat']) module.exit_json(data=return_data)
def create_ip_list(module, client, network_domain_id): """ Create a IP address list :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: The created IP address list object """ return_data = return_object('ip_list') name = module.params.get('name') description = module.params.get('description') ip_addresses = module.params.get('ip_addresses') child_ip_lists = module.params.get('child_ip_lists') version = module.params.get('version') if name is None: module.fail_json(msg='A valid name is required') if not ip_addresses and not child_ip_lists: module.fail_json( msg= 'ip_addresses or child_ip_lists must have at least one valid entry' ) try: client.create_ip_list(network_domain_id, name, description, ip_addresses, child_ip_lists, version) return_data['ip_list'] = client.get_ip_list_by_name( network_domain_id, name, version) except NTTMCPAPIException as e: module.fail_json( msg='Could not create the IP Address List {0}'.format(e)) module.exit_json(changed=True, data=return_data['ip_list'])
def update_ip_list(module, client, network_domain_id, ip_list): """ Update a IP address list :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg ip_list: The existing IP address list to be updated :returns: The updated IP address list object """ return_data = return_object('ip_list') name = module.params.get('name') try: client.update_ip_list(network_domain_id, ip_list.get('id'), module.params.get('description'), module.params.get('ip_addresses'), module.params.get('ip_addresses_nil'), module.params.get('child_ip_lists'), module.params.get('child_ip_lists_nil')) return_data['ip_list'] = client.get_ip_list_by_name( network_domain_id, name, ip_list.get('ipVersion')) except (KeyError, IndexError, NTTMCPAPIException) as e: module.fail_json( msg='Could not update the IP Address List - {0}'.format(e)) module.exit_json(changed=True, data=return_data['ip_list'])
def get_fw_rule(module, client, network_domain_id, name): """ Gets a specific firewall rule by name :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg name: The name of the firewall rule to search for :returns: a firewall rule """ return_data = return_object('acl') try: if module.params.get('stats'): return_data['acl'] = client.list_fw_rule_stats(network_domain_id, name) else: return_data['acl'].append(client.get_fw_rule_by_name(network_domain_id, name)) if return_data['acl'] is None: module.exit_json(changed=False, msg='Could not find the ACL {0}'.format(name)) except NTTMCPAPIException as e: module.exit_json(msg='Could not get the firewall rule - {0}'.format(e)) except KeyError: module.fail_json(msg='Network Domain is invalid') return_data['count'] = len(return_data.get('acl')) module.exit_json(changed=False, data=return_data)
def create_port_list(module, client, network_domain_id): """ Create a port list :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: The created port list object """ return_data = return_object('port_list') name = module.params.get('name') description = module.params.get('description') ports = module.params.get('ports') child_port_lists = module.params.get('child_port_lists') if name is None: module.fail_json(msg='A valid name is required') if not ports and not child_port_lists: module.fail_json( msg='ports or child_ports_list must have at least one valid entry') try: client.create_port_list(network_domain_id, name, description, ports, child_port_lists) return_data['port_list'] = client.get_port_list_by_name( network_domain_id, name) except (KeyError, IndexError, NTTMCPAPIException) as e: module.fail_json(msg='Could not create the Port List {0}'.format(e)) module.exit_json(changed=True, data=return_data.get('port_list'))
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: 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=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('network_domain') name = module.params.get('name') datacenter = module.params.get('datacenter') # 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=str(e.msg)) # Get a list of existing CNDs and check if the name already exists try: networks = client.list_network_domains(datacenter=datacenter) except NTTMCPAPIException as e: module.fail_json( msg='Failed to get a list of Cloud Network - {0}'.format(e)) try: if name: return_data['network_domain'] = [ x for x in networks if x.get('name') == name ] else: return_data['network_domain'] = networks except (KeyError, IndexError, AttributeError) as e: module.fail_json( msg='Could not find the Cloud Network Domain - {0} in {1}'.format( name, datacenter)) return_data['count'] = len(return_data['network_domain']) 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 get_vip_node(module, client, node_id): """ Get a VIP Node by UUID :arg module: The Ansible module instance :arg client: The CC API client instance :arg pool_id: The UUID of the VIP Node :returns: VIP Node object """ return_data = return_object('node') if node_id is None: module.fail_json(msg='A value for id is required') try: result = client.get_vip_node(node_id) if result: return_data['node'].append(result) except NTTMCPAPIException as exc: module.fail_json(msg='Could not get the Node - {0}'.format(exc)) return_data['count'] = len(return_data['node']) module.exit_json(data=return_data)
def list_vip_node(module, client, network_domain_id, name, ip_address): """ List the VIP Node for a network domain, filter by name if provided :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the VIP Node :arg name: Optional name of the VIP Node :arg ip_address: Optional IP address to filter on :returns: List of VIP Node objects """ return_data = return_object('node') try: return_data['node'] = client.list_vip_node(network_domain_id, name, ip_address) except (KeyError, IndexError, NTTMCPAPIException) as exc: module.fail_json( msg='Could not retrieve a list of Nodes - {0}'.format(exc)) return_data['count'] = len(return_data.get('node')) module.exit_json(data=return_data)
def list_public_ipv4(module, client, network_domain_id): """ List the public IPv4 blocks for the specified network domain :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: List of public IPv4 block objects """ return_data = return_object('ipam') try: return_data['ipam'] = client.list_public_ipv4(network_domain_id) except NTTMCPAPIException as exc: module.fail_json( msg='Could not list the public IPv4 blocks - {0}'.format(exc)) except KeyError: module.fail_json(msg='Network Domain is invalid') return_data['count'] = len(return_data.get('ipam')) module.exit_json(changed=False, data=return_data)
def get_next_free_public_ipv4(module, client, network_domain_id): """ Get the next available public IPv4 address. If no existing IPv4 blocks exist one will allocated as part of this process :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: The allocated public IPv4 address """ return_data = return_object('ipam') return_data['ipam'] = {} try: result = client.get_next_public_ipv4(network_domain_id) return_data['ipam']['ip'] = result.get('ipAddress') except NTTMCPAPIException as e: module.fail_json( msg='Could get the next free public IPv4 address - {0}'.format(e)) module.exit_json(changed=result.get('changed'), data=return_data.get('ipam'))
def list_nat_rule(module, client, network_domain_id): """ Get a NAT rule by UUID :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of a network domain :returns: NAT object """ return_data = return_object('nat') try: return_data['nat'] = client.list_nat_rule(network_domain_id) except NTTMCPAPIException as e: module.fail_json( msg='Could not retrieve a list of NAT(s) - {0}'.format(e)) except KeyError: module.fail_json(msg='Network Domain is invalid') return_data['count'] = len(return_data.get('nat')) module.exit_json(changed=False, data=return_data)
def get_image(module, client): """ List images filtered by optional parameters from the Ansible arguments :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: List of image objects """ return_data = return_object('image') image_id = module.params['id'] image_name = module.params['name'] os_family = module.params['family'] datacenter = module.params['datacenter'] customer_image = module.params['customer_image'] try: if customer_image: result = client.list_customer_image(datacenter_id=datacenter, image_id=image_id, image_name=image_name, os_family=os_family) else: result = client.list_image(datacenter_id=datacenter, image_id=image_id, image_name=image_name, os_family=os_family) except NTTMCPAPIException as exc: module.fail_json( msg='Could not get a list of images - {0}'.format(exc)) try: if customer_image: return_data['count'] = result['totalCount'] return_data['image'] = result['customerImage'] else: return_data['count'] = result['totalCount'] return_data['image'] = result['osImage'] except KeyError: pass module.exit_json(data=return_data)
def get_dc(module, client): """ Gets a the specified DC/MCP :arg module: The Ansible module instance :arg client: The CC API client instance :returns: MCP Information """ return_data = return_object('mcp') dc_id = module.params['id'] try: result = client.get_dc(dc_id=dc_id) except NTTMCPAPIException as exc: module.fail_json(msg='Could not get a list of MCPs - {0}'.format(exc), exception=traceback.format_exc()) try: return_data['count'] = result.get('totalCount') return_data['mcp'] = result.get('datacenter') except (KeyError, AttributeError): pass module.exit_json(data=return_data)
def create_snat_exclusion(module, client, network_domain_id, network_cidr): """ Create a SNAT exclusion :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg network_cidr: The network CIDR for the SNAT exclusion :returns: The created SNAT exclusion """ return_data = return_object('snat') description = module.params.get('description') network = str(network_cidr.network_address) prefix = network_cidr.prefixlen if None in [network_domain_id, network, prefix]: module.fail_json( msg= 'A valid value is required for network_domain, network and prefix') try: client.create_snat_exclusion(network_domain_id, description, network, prefix) except NTTMCPAPIException as e: module.fail_json( msg='Could not create the SNAT exclusion - {0}'.format(e)) try: return_data['snat'] = client.list_snat_exclusion( network_domain_id=network_domain_id, network=network, prefix=prefix)[0] except (KeyError, IndexError, NTTMCPAPIException) as e: module.exit_json( changed=True, msg='Could not verify the SNAT exclusion was created - {0}'.format( e), results=None) module.exit_json(changed=True, data=return_data['snat'])
def list_snat_exclusion(module, client, network_domain_id): """ List the SNAT for a network domain, filter by name if provided :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: List of SNAT objects """ return_data = return_object('snat') snat_id = module.params.get('id') network = module.params.get('network') prefix = module.params.get('prefix') try: return_data['snat'] = client.list_snat_exclusion( network_domain_id, snat_id, network, prefix) except NTTMCPAPIException as exc: module.fail_json( msg='Could not retrieve a list of SNAT Exclusions - {0}'.format( exc)) return_data['count'] = len(return_data['snat']) module.exit_json(data=return_data)
def get_nat_rule(module, client, network_domain_id, nat_rule_id, internal_ip, external_ip): """ Get a NAT rule by UUID :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the CND :arg nat_rule_id: The UUID of the NAT rule to get :arg internal_ip: The internal IPv4 address of the NAT rule :arg external_ip: The external public IPv4 address of the NAT rule :returns: NAT object """ return_data = return_object('nat') if nat_rule_id is None and internal_ip is None and external_ip is None: module.fail_json( msg= 'A value for is required for one of id, internal_ip or external_ip' ) try: if nat_rule_id: result = client.get_nat_rule(nat_rule_id) elif internal_ip: result = client.get_nat_by_private_ip(network_domain_id, internal_ip) elif external_ip: result = client.get_nat_by_public_ip(network_domain_id, external_ip) if result is None: module.exit_json(msg='Could not find a matching NAT rule', data=None) return_data['nat'].append(result) except NTTMCPAPIException as e: module.fail_json(msg='Could not get the NAT rule - {0}'.format(e)) return_data['count'] = len(return_data.get('nat')) module.exit_json(changed=False, data=return_data)
def update_vlan(module, client, vlan): """ Update a VLAN :arg module: The Ansible module instance :arg client: The CC API client instance :arg vlan: The existing VLAN object :returns: the comparison result """ return_data = return_object('vlan') name = vlan['name'] new_name = module.params['new_name'] description = module.params['description'] datacenter = vlan['datacenterId'] vlan_id = vlan['id'] network_domain_id = vlan['networkDomain']['id'] detached_vlan_gw = module.params['detached_vlan_gw'] detached_vlan_gw_ipv6 = module.params['detached_vlan_gw_ipv6'] wait = module.params['wait'] if new_name: name = new_name try: client.update_vlan(vlan_id, name, description, detached_vlan_gw, detached_vlan_gw_ipv6) except NTTMCPAPIException as exc: module.fail_json(msg='Could not update the VLAN - {0}'.format(exc), exception=traceback.format_exc()) if wait: wait_result = wait_for_vlan(module, client, name, datacenter, network_domain_id, 'NORMAL') if wait_result is None: module.fail_json(msg='Could not verify the VLAN update was successful. Check the UI.') return_data['vlan'] = wait_result else: return_data['vlan'] = {'id': vlan['id']} module.exit_json(changed=True, data=return_data['vlan'])
def list_fw_rule(module, client, network_domain_id): """ List all firewall rules for a given network domain :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: List of firewall rules """ return_data = return_object('acl') try: if module.params.get('stats'): return_data['acl'] = client.list_fw_rule_stats(network_domain_id, None, 250) else: return_data['acl'] = client.list_fw_rules(network_domain_id, None, 250) except NTTMCPAPIException as e: module.fail_json(msg='Could not retrieve a list of firewall rules - {0}'.format(e)) except KeyError: module.fail_json(msg='Network Domain is invalid') return_data['count'] = len(return_data.get('acl')) module.exit_json(changed=False, data=return_data)
def update_vip_node(module, client, node): """ Update a VIP node :arg module: The Ansible module instance :arg client: The CC API client instance :arg port_list: The dict containing the existing VIP node to be updated :returns: The updated VIP node dict """ return_data = return_object('node') try: client.update_vip_node(node.get('id'), module.params.get('description'), module.params.get('status'), module.params.get('health_monitor'), module.params.get('no_health_monitor'), module.params.get('connection_limit'), module.params.get('connection_rate_limit')) return_data['node'] = client.get_vip_node(node.get('id')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json(msg='Could not update the VIP Node - {0}'.format(e)) module.exit_json(changed=True, data=return_data['node'])
def create_vlan(module, client, network_domain_id): """ Create a VLAN :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg ipv4_cidr: The IPv4 network address and mask represented as an ipaddress object :returns: VLAN Object """ return_data = return_object('vlan') ipv4_cidr = None # Determine the IPv4 network and mask try: ipv4_cidr = ip_net(unicode(module.params.get('ipv4_cidr'))) except (AddressValueError, ValueError) as e: module.fail_json(msg='Invalid IPv4 CIDR format {0}: {1}'.format(module.params.get('ipv4_cidr'), e)) datacenter = module.params['datacenter'] name = module.params['name'] vlan_type = module.params['vlan_type'] ipv4_network = str(ipv4_cidr.network_address) ipv4_prefix = str(ipv4_cidr.prefixlen) wait = module.params['wait'] if vlan_type == 'attachedVlan': if not module.params.get('attached_vlan_gw'): module.fail_json(msg='An attached_vlan_gw value [LOW/HIGH] is required for a new Attached VLAN.') elif vlan_type == 'detachedVlan': if not module.params.get('detached_vlan_gw'): module.fail_json(msg='A detached_vlan_gw value (e.g. 10.0.0.1) is reuiqred for a new Detached VLAN.') try: if vlan_type == 'attachedVlan': gateway = module.params['attached_vlan_gw'] result = client.create_vlan( networkDomainId=network_domain_id, name=name, description=module.params['description'], privateIpv4NetworkAddress=ipv4_network, privateIpv4PrefixSize=ipv4_prefix, attachedVlan=True, attachedVlan_gatewayAddressing=gateway) new_vlan_id = result['info'][0]['value'] elif vlan_type == 'detachedVlan': gateway = module.params['detached_vlan_gw'] result = client.create_vlan( networkDomainId=network_domain_id, name=name, description=module.params['description'], privateIpv4NetworkAddress=ipv4_network, privateIpv4PrefixSize=ipv4_prefix, detachedVlan=True, detachedVlan_ipv4GatewayAddress=gateway) new_vlan_id = result['info'][0]['value'] except (KeyError, IndexError, NTTMCPAPIException) as exc: module.fail_json(msg='Could not create the VLAN - {0}'.format(exc), exception=traceback.format_exc()) if wait: wait_result = wait_for_vlan(module, client, name, datacenter, network_domain_id, 'NORMAL') if wait_result is None: module.fail_json(msg='Could not verify the VLAN creation was successful. Check the UI.') return_data['vlan'] = wait_result else: return_data['vlan'] = {'id': new_vlan_id} module.exit_json(changed=True, data=return_data['vlan'])
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: IP Address List Information """ module = AnsibleModule(argument_spec=dict( auth=dict(type='dict'), region=dict(default='na', type='str'), my_user=dict(default=False, type='bool'), username=dict(default=None, type='str'), firstname=dict(default=None, type='str'), lastname=dict(default=None, type='str'), email=dict(default=None, type='str'), phone_country_code=dict(default=None, type='str'), phone=dict(default=None, type='str'), state=dict(default=None, type='str'), department=dict(default=None, type='str'), ), supports_check_mode=True) return_data = return_object('user') user = None 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) try: if module.params.get('my_user'): user = client.get_my_user() if user is not None: return_data['user'].append(user) elif module.params.get('username') is not None: user = client.get_user(username=module.params.get('username')) if user is not None: return_data['user'].append(user) else: return_data['user'] = client.list_users( firstname=module.params.get('firstname'), lastname=module.params.get('lastname'), email=module.params.get('email'), phone_country_code=module.params.get('phone_country_code'), phone=module.params.get('phone'), state=module.params.get('state'), department=module.params.get('department')) return_data['count'] = len(return_data.get('user')) module.exit_json(data=return_data) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not retrieve a list of users - {0}'.format(e))
def update_fw_rule(module, client, network_domain_id, existing_fw_rule, src_cidr, dst_cidr): """ Update a firewall rule :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :arg existing_fw_rule: The existing firewall rule to be updated :returns: The updated firewall rule object """ return_data = return_object('acl') return_data['acl'] = {} args = module.params fw_rule_id = existing_fw_rule.get('id') # Build the parameter list to compare to the existing object in prepartion for update if existing_fw_rule.get('ruleType') != 'DEFAULT_RULE': try: if args['src_ip_list']: args['src_ip_list'] = client.get_ip_list_by_name( network_domain_id, args.get('src_ip_list'), args.get('version')).get('id') if args['dst_ip_list']: args['dst_ip_list'] = client.get_ip_list_by_name( network_domain_id, args.get('dst_ip_list'), args.get('version')).get('id') if args['src_port_list']: args['src_port_list'] = client.get_port_list_by_name( network_domain_id, args.get('src_port_list')).get('id') if args['dst_port_list']: args['dst_port_list'] = client.get_port_list_by_name( network_domain_id, args.get('dst_port_list')).get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg= 'update_fw_rule: Could not determine IP address and/or child port lists - {0}' .format(e), exception=traceback.format_exc()) fw_rule = client.fw_args_to_dict( False, fw_rule_id, network_domain_id, args.get('name'), args.get('action'), args.get('version'), args.get('protocol'), str(src_cidr.network_address) if hasattr(src_cidr, 'network_address') else None, str(src_cidr.prefixlen) if hasattr(src_cidr, 'prefixlen') else None, args.get('src_ip_list'), str(dst_cidr.network_address) if hasattr(dst_cidr, 'network_address') else None, str(dst_cidr.prefixlen) if hasattr( dst_cidr, 'prefixlen') else None, args.get('dst_ip_list'), args.get('src_port_start'), args.get('src_port_end'), args.get('src_port_list'), args.get('dst_port_start'), args.get('dst_port_end'), args.get('dst_port_list'), args.get('enabled'), args.get('position'), args.get('position_to')) # Check for any state changes in the fw rule and update if required compare_result = compare_fw_rule(fw_rule, deepcopy(existing_fw_rule)) else: fw_rule = dict() fw_rule['id'] = fw_rule_id fw_rule['enabled'] = args.get('enabled') tmp_fw_rule = deepcopy(existing_fw_rule) tmp_fw_rule['enabled'] = args.get('enabled') # Check for any state changes in the fw rule and update if required compare_result = compare_fw_rule(tmp_fw_rule, deepcopy(existing_fw_rule)) # Implement check_mode if module.check_mode: module.exit_json(data=compare_result) if compare_result: try: if compare_result['changes']: client.update_fw_rule(fw_rule) return_data['acl'] = client.get_fw_rule_by_name( network_domain_id, args.get('name')) module.exit_json(changed=True, data=return_data.get('acl')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg='Could not update the firewall rule - {0}'.format(e), exception=traceback.format_exc()) module.exit_json(changed=False, data=existing_fw_rule)
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 create_fw_rule(module, client, network_domain_id, src_cidr, dst_cidr): """ Create a firewall rule :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of the network domain :returns: The created firewall rule object """ return_data = return_object('acl') args = module.params try: if args['src_ip_list']: args['src_ip_list'] = client.get_ip_list_by_name( network_domain_id, args.get('src_ip_list'), args.get('version')).get('id') if args['dst_ip_list']: args['dst_ip_list'] = client.get_ip_list_by_name( network_domain_id, args.get('dst_ip_list'), args.get('version')).get('id') if args['src_port_list']: args['src_port_list'] = client.get_port_list_by_name( network_domain_id, args.get('src_port_list')).get('id') if args['dst_port_list']: args['dst_port_list'] = client.get_port_list_by_name( network_domain_id, args.get('dst_port_list')).get('id') except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e: module.fail_json( msg= 'create_fw_rule: Could not determine IP address and/or child port lists - {0}' .format(e), exception=traceback.format_exc()) try: fw_rule = client.fw_args_to_dict( True, None, network_domain_id, args.get('name'), args.get('action'), args.get('version'), args.get('protocol'), str(src_cidr.network_address) if hasattr(src_cidr, 'network_address') else None, str(src_cidr.prefixlen) if hasattr(src_cidr, 'prefixlen') else None, args.get('src_ip_list'), str(dst_cidr.network_address) if hasattr(dst_cidr, 'network_address') else None, str(dst_cidr.prefixlen) if hasattr( dst_cidr, 'prefixlen') else None, args.get('dst_ip_list'), args.get('src_port_start'), args.get('src_port_end'), args.get('src_port_list'), args.get('dst_port_start'), args.get('dst_port_end'), args.get('dst_port_list'), args.get('enabled'), args.get('position'), args.get('position_to')) fw_rule_id = client.create_fw_rule(fw_rule) return_data['acl'] = client.get_fw_rule(network_domain_id, fw_rule_id) except (NTTMCPAPIException) as e: module.fail_json( msg='Could not create the firewall rule - {0}'.format(e), exception=traceback.format_exc()) except (KeyError, IndexError, AttributeError) as e: module.fail_json(changed=False, msg='Invalid data - {0}'.format(e), exception=traceback.format_exc()) module.exit_json(changed=True, data=return_data.get('acl'))
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: 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))