]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Cisco N1kv: Fix update network profile for add tenants
authorAbhishek Raut <abhraut@cisco.com>
Wed, 8 Oct 2014 00:06:10 +0000 (17:06 -0700)
committerIhar Hrachyshka <ihrachys@redhat.com>
Wed, 22 Oct 2014 11:20:29 +0000 (11:20 +0000)
This patch makes sure that while updating network profile to add new
tenants, it does not delete the older profile bindings and maintains
them, while adds only the new tenant ids to the bindings.

Change-Id: I862eb1c400e022334a2f6a4078425448bb144843
Closes-bug: #1379609
(cherry picked from commit c97069dc9a73344ebdc7b686133269850a81b3b2)

neutron/plugins/cisco/db/n1kv_db_v2.py
neutron/tests/unit/cisco/n1kv/test_n1kv_plugin.py

index d694b236764373e14283adf19625daaf5f7d80f1..ed0b5fdbdd9ffbed4b2aca48c495992c68a880b3 100644 (file)
@@ -957,14 +957,22 @@ def _get_profile_bindings(db_session, profile_type=None):
     If profile type is None, return profile-tenant binding for all
     profile types.
     """
-    LOG.debug(_("_get_profile_bindings()"))
     if profile_type:
-        profile_bindings = (db_session.query(n1kv_models_v2.ProfileBinding).
-                            filter_by(profile_type=profile_type))
-        return profile_bindings
+        return (db_session.query(n1kv_models_v2.ProfileBinding).
+                filter_by(profile_type=profile_type))
     return db_session.query(n1kv_models_v2.ProfileBinding)
 
 
+def _get_profile_bindings_by_uuid(db_session, profile_id):
+    """
+    Retrieve a list of profile bindings.
+
+    Get all profile-tenant bindings based on profile UUID.
+    """
+    return (db_session.query(n1kv_models_v2.ProfileBinding).
+            filter_by(profile_id=profile_id))
+
+
 class NetworkProfile_db_mixin(object):
 
     """Network Profile Mixin."""
@@ -1099,8 +1107,10 @@ class NetworkProfile_db_mixin(object):
         original_net_p = get_network_profile(context.session, id)
         # Update network profile to tenant id binding.
         if context.is_admin and c_const.ADD_TENANTS in p:
-            if context.tenant_id not in p[c_const.ADD_TENANTS]:
-                p[c_const.ADD_TENANTS].append(context.tenant_id)
+            profile_bindings = _get_profile_bindings_by_uuid(context.session,
+                                                             profile_id=id)
+            for bindings in profile_bindings:
+                p[c_const.ADD_TENANTS].append(bindings.tenant_id)
             update_profile_binding(context.session, id,
                                    p[c_const.ADD_TENANTS], c_const.NETWORK)
             is_updated = True
index e412dfc7e68d86dc0e3f8fbf598d00edf06f9a07..f926a4920056ec67012660d997202644ff288616 100644 (file)
@@ -612,7 +612,7 @@ class TestN1kvNetworkProfiles(N1kvPluginTestCase):
                                              net_p['network_profile']['id'])
         update_req.environ['neutron.context'] = context.Context('',
                                                                self.tenant_id,
-                                                               is_admin = True)
+                                                               is_admin=True)
         update_res = update_req.get_response(self.ext_api)
         self.assertEqual(200, update_res.status_int)
         db_session = db.get_session()
@@ -623,7 +623,7 @@ class TestN1kvNetworkProfiles(N1kvPluginTestCase):
                                                 net_p['network_profile']['id'])
         self.assertRaises(c_exc.ProfileTenantBindingNotFound,
                           n1kv_db_v2.get_profile_binding,
-                          db_session, 'tenant2',
+                          db_session, 'tenant4',
                           net_p['network_profile']['id'])
         tenant3 = n1kv_db_v2.get_profile_binding(db_session, 'tenant3',
                                                 net_p['network_profile']['id'])
@@ -637,24 +637,39 @@ class TestN1kvNetworkProfiles(N1kvPluginTestCase):
                                              net_p['network_profile']['id'])
         update_req.environ['neutron.context'] = context.Context('',
                                                                self.tenant_id,
-                                                               is_admin = True)
+                                                               is_admin=True)
         update_res = update_req.get_response(self.ext_api)
         self.assertEqual(200, update_res.status_int)
         # current tenant_id should always present
         tenant_id = n1kv_db_v2.get_profile_binding(db_session, self.tenant_id,
                                                 net_p['network_profile']['id'])
+        self.assertIsNotNone(tenant_id)
         self.assertRaises(c_exc.ProfileTenantBindingNotFound,
                           n1kv_db_v2.get_profile_binding,
                           db_session, 'tenant1',
                           net_p['network_profile']['id'])
         self.assertRaises(c_exc.ProfileTenantBindingNotFound,
                           n1kv_db_v2.get_profile_binding,
-                          db_session, 'tenant2',
+                          db_session, 'tenant4',
                           net_p['network_profile']['id'])
         tenant3 = n1kv_db_v2.get_profile_binding(db_session, 'tenant3',
                                                 net_p['network_profile']['id'])
-        self.assertIsNotNone(tenant_id)
         self.assertIsNotNone(tenant3)
+        # Add new tenant4 to network profile and make sure existing tenants
+        # are not deleted.
+        data = {'network_profile': {c_const.ADD_TENANTS:
+                                    ['tenant4']}}
+        update_req = self.new_update_request('network_profiles',
+                                             data,
+                                             net_p['network_profile']['id'])
+        update_req.environ['neutron.context'] = context.Context('',
+                                                               self.tenant_id,
+                                                               is_admin=True)
+        update_res = update_req.get_response(self.ext_api)
+        self.assertEqual(200, update_res.status_int)
+        tenant4 = n1kv_db_v2.get_profile_binding(db_session, 'tenant4',
+                                                net_p['network_profile']['id'])
+        self.assertIsNotNone(tenant4)
 
 
 class TestN1kvBasicGet(test_plugin.TestBasicGet,