added support for custom endpoint type

Change-Id: Ia79bcc5c1acd830c353550af1ec450bb182f63cc
diff --git a/README.rst b/README.rst
index 8726426..9b98a4d 100644
--- a/README.rst
+++ b/README.rst
@@ -701,6 +701,7 @@
         enabled: true
         server:
           identity:
+            endpoint_type: internalURL
             network:
               inet1:
                 tenant: demo
@@ -740,6 +741,7 @@
         enabled: true
         server:
           identity:
+            endpoint_type: internalURL
             router:
               inet1-router:
                 tenant: demo
@@ -764,6 +766,7 @@
         enabled: true
         server:
           identity:
+            endpoint_type: internalURL
             security_group:
               security_group1:
                 tenant: demo
@@ -800,6 +803,7 @@
         enabled: true
         server:
           identity:
+            endpoint_type: internalURL
             floating_ip:
               prx01-instance:
                 server: prx01.mk22-lab-basic.local
diff --git a/_modules/neutronng.py b/_modules/neutronng.py
index e0b6831..652e612 100644
--- a/_modules/neutronng.py
+++ b/_modules/neutronng.py
@@ -43,9 +43,15 @@
                 nkwargs.update({kwarg: kwargs[kwarg]})
         kstone = __salt__['keystone.auth'](**connection_args)
         token = kstone.auth_token
+
+        if kwargs.get('connection_endpoint_type') == None:
+            endpoint_type = 'internalURL'
+        else:
+            endpoint_type = kwargs.get('connection_endpoint_type')
+
         endpoint = kstone.service_catalog.url_for(
             service_type='network',
-            endpoint_type='publicURL')
+            endpoint_type=endpoint_type)
         neutron_interface = client.Client(
             endpoint_url=endpoint, token=token)
         return_data = func_name(neutron_interface, *args, **nkwargs)
diff --git a/_states/neutronng.py b/_states/neutronng.py
index 9d4deaa..90ba36d 100644
--- a/_states/neutronng.py
+++ b/_states/neutronng.py
@@ -42,7 +42,7 @@
     return __salt__['neutronng.{0}'.format(method)](*args, **kwargs)
 
 
-def _auth(profile=None):
+def _auth(profile=None, endpoint_type=None):
     '''
     Set up neutron credentials
     '''
@@ -52,12 +52,12 @@
         password = credentials['keystone.password']
         tenant = credentials['keystone.tenant']
         auth_url = credentials['keystone.auth_url']
-
     kwargs = {
         'connection_user': user,
         'connection_password': password,
         'connection_tenant': tenant,
         'connection_auth_url': auth_url
+        'connection_endpoint_type': endpoint_type
     }
 
     return kwargs
@@ -71,14 +71,15 @@
                     admin_state_up=None,
                     shared=None,
                     provider_segmentation_id=None,
-                    profile=None):
+                    profile=None,
+                    endpoint_type=None):
     '''
     Ensure that the neutron network is present with the specified properties.
     name
         The name of the network to manage
     '''
     tenant_name = tenant
-    connection_args = _auth(profile)
+    connection_args = _auth(profile, endpoint_type)
     try:
         tenant_id = __salt__['keystone.tenant_get'](
             name=tenant_name, **connection_args)[tenant_name]['id']
@@ -86,11 +87,8 @@
         tenant_id = None
         LOG.debug('Cannot get the tenant id. User {0} is not an admin.'.format(
             connection_args['connection_user']))
-    existing_networks = _neutron_module_call(
-        'list_networks', **connection_args)
-    for network in existing_networks:
-        if network.get(name) == name:
-            existing_network = network
+    existing_network = _neutron_module_call(
+        'list_networks', name=name, **connection_args)
     network_arguments = _get_non_null_args(
         name=name,
         provider_network_type=provider_network_type,
@@ -142,8 +140,8 @@
 
 
 @_test_call
-def network_absent(name, profile=None):
-    connection_args = _auth(profile)
+def network_absent(name, profile=None,endpoint_type=None):
+    connection_args = _auth(profile, endpoint_type)
     existing_network = _neutron_module_call(
         'list_networks', name=name, **connection_args)
     if existing_network:
@@ -166,13 +164,14 @@
                    gateway_ip=None,
                    dns_nameservers=None,
                    host_routes=None,
-                   profile=None):
+                   profile=None,
+                   endpoint_type=None):
     '''
     Ensure that the neutron subnet is present with the specified properties.
     name
         The name of the subnet to manage
     '''
-    connection_args = _auth(profile)
+    connection_args = _auth(profile, endpoint_type)
     tenant_name = tenant
     try:
         tenant_id = __salt__['keystone.tenant_get'](
@@ -235,8 +234,8 @@
 
 
 @_test_call
-def subnet_absent(name, profile=None):
-    connection_args = _auth(profile)
+def subnet_absent(name, profile=None, endpoint_type=None):
+    connection_args = _auth(profile, endpoint_type)
     existing_subnet = _neutron_module_call(
         'list_subnets', tenant_id=tenant_id, name=name, **connection_args)
     if existing_subnet:
@@ -255,7 +254,8 @@
                    gateway_network=None,
                    interfaces=None,
                    admin_state_up=True,
-                   profile=None):
+                   profile=None,
+                   endpoint_type=None):
     '''
     Ensure that the neutron router is present with the specified properties.
     name
@@ -265,7 +265,7 @@
     interfaces
         list of subnets the router attaches to
     '''
-    connection_args = _auth(profile)
+    connection_args = _auth(profile, endpoint_type)
     tenant_name = tenant
     try:
         tenant_id = __salt__['keystone.tenant_get'](
@@ -341,13 +341,14 @@
                        network=None,
                        port_id=None,
                        fip_exists=False,
-                       profile=None):
+                       profile=None,
+                       endpoint_type=None):
     '''
     Ensure that the floating ip address is present for an instance
     '''
     instance_id = __salt__['novang.server_get'](name=name, tenant_name=tenant_name, profile=profile)
     subnet_name = subnet
-    connection_args = _auth(profile)
+    connection_args = _auth(profile, endpoint_type)
     existing_subnet = _neutron_module_call(
         'list_subnets', name=subnet_name, **connection_args)
     subnet_id = existing_subnet[subnet_name]['id']
@@ -393,7 +394,8 @@
                            tenant=None,
                            description=None,
                            rules=[],
-                           profile=None):
+                           profile=None,
+                           endpoint_type=None):
     '''
     Ensure that the security group is present with the specified properties.
     name
@@ -406,7 +408,7 @@
     # If the user is an admin, he's able to see the security groups from
     # other tenants. In this case, we'll use the tenant id to get an existing
     # security group.
-    connection_args = _auth(profile)
+    connection_args = _auth(profile, endpoint_type)
     tenant_name = tenant
     try:
         tenant_id = __salt__['keystone.tenant_get'](
diff --git a/neutron/client.sls b/neutron/client.sls
index fda72c0..91f81e8 100644
--- a/neutron/client.sls
+++ b/neutron/client.sls
@@ -7,7 +7,6 @@
 
 
 {%- for identity_name, identity in client.server.iteritems() %}
-
 {%- if identity.network is defined %}
 
 {%- for network_name, network in identity.network.iteritems() %}
@@ -17,6 +16,9 @@
     - name: {{ network_name }}
     - profile: {{ identity_name }}
     - tenant: {{ network.tenant }}
+    {%- if identity.endpoint_type is defined %}
+    - endpoint_type: {{ identity.endpoint_type }}
+    {%- endif %}
 
     {%- if network.provider_network_type is defined %}
     - provider_network_type: {{ network.provider_network_type }}
@@ -46,6 +48,9 @@
     - network: {{ network_name }}
     - profile: {{ identity_name }}
     - tenant: {{ network.tenant }}
+    {%- if identity.endpoint_type is defined %}
+    - endpoint_type: {{ identity.endpoint_type }}
+    {%- endif %}
 
     {%- if subnet.cidr is defined %}
     - cidr: {{ subnet.cidr  }}
@@ -90,6 +95,9 @@
     - profile: {{ identity_name }}
     - tenant: {{ router.tenant }}
     - admin_state_up: {{ router.admin_state_up }}
+    {%- if identity.endpoint_type is defined %}
+    - endpoint_type: {{ identity.endpoint_type }}
+    {%- endif %}
 {%- endfor %}
 
 {%- endif %}
@@ -104,6 +112,9 @@
     - rules: {{ security_group.rules }}
     - profile: {{ identity_name }}
     - tenant: {{ security_group.tenant }}
+    {%- if identity.endpoint_type is defined %}
+    - endpoint_type: {{ identity.endpoint_type }}
+    {%- endif %}
 {%- endfor %}
 
 {%- endif %}
@@ -118,6 +129,9 @@
     - name: {{ instance.server }}
     - network:  {{ instance.network }}
     - profile: {{ identity_name }}
+    {%- if identity.endpoint_type is defined %}
+    - endpoint_type: {{ identity.endpoint_type }}
+    {%- endif %}
 
 {%- endfor %}