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: 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)