Browse Source

Support PATCH partial updates

Change-Id: I4bb34bccc0c95e985ab10190fc11ed21d0664be8
(cherry picked from commit e93309ff86)
changes/65/680365/1
Adit Sarfaty 1 month ago
parent
commit
1cbb8d815c

+ 2
- 2
vmware_nsxlib/tests/unit/v3/policy/policy_testcase.py View File

@@ -29,7 +29,7 @@ class TestPolicyApi(nsxlib_testcase.NsxClientTestCase):
29 29
 
30 30
         super(TestPolicyApi, self).setUp()
31 31
 
32
-    def assert_json_call(self, method, client, url, data=None):
32
+    def assert_json_call(self, method, client, url, data=None, headers=None):
33 33
         url = BASE_POLICY_URI + url
34 34
         return super(TestPolicyApi, self).assert_json_call(
35
-            method, client, url, data=data)
35
+            method, client, url, data=data, headers=headers)

+ 1
- 8
vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py View File

@@ -852,7 +852,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
852 852
 
853 853
             expected_def = lb_defs.LBVirtualServerDef(
854 854
                 virtual_server_id=vs_obj_id,
855
-                name=vs_name,
856 855
                 rules=[lb_rule])
857 856
             self.assert_called_with_def(update_call, expected_def)
858 857
 
@@ -880,7 +879,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
880 879
 
881 880
             expected_def = lb_defs.LBVirtualServerDef(
882 881
                 virtual_server_id=vs_obj_id,
883
-                name=vs_name,
884 882
                 rules=[lb_rule,
885 883
                        {'display_name': 'xx'},
886 884
                        {'display_name': 'yy'}])
@@ -908,7 +906,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
908 906
 
909 907
             expected_def = lb_defs.LBVirtualServerDef(
910 908
                 virtual_server_id=vs_obj_id,
911
-                name=vs_name,
912 909
                 rules=[{'display_name': 'xx'},
913 910
                        {'display_name': 'yy'},
914 911
                        lb_rule])
@@ -937,7 +934,7 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
937 934
                 rule_match_strategy, rule_phase)
938 935
 
939 936
             expected_def = lb_defs.LBVirtualServerDef(
940
-                virtual_server_id=vs_obj_id, name=vs_name,
937
+                virtual_server_id=vs_obj_id,
941 938
                 rules=[{'display_name': 'xx'},
942 939
                        {'display_name': 'yy'},
943 940
                        lb_rule])
@@ -965,7 +962,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
965 962
                 rule_match_strategy, rule_phase)
966 963
 
967 964
             expected_def = lb_defs.LBVirtualServerDef(
968
-                name=vs_name,
969 965
                 virtual_server_id=vs_obj_id,
970 966
                 rules=[{'display_name': 'xx'},
971 967
                        lb_rule,
@@ -983,7 +979,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
983 979
             self.resourceApi.update_lb_rule(vs_obj_id, 'xx', actions='22')
984 980
 
985 981
             expected_def = lb_defs.LBVirtualServerDef(
986
-                name=vs_name,
987 982
                 virtual_server_id=vs_obj_id,
988 983
                 rules=[{'display_name': 'xx', 'actions': '22'},
989 984
                        {'display_name': 'yy'}])
@@ -1002,7 +997,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
1002 997
 
1003 998
             expected_def = lb_defs.LBVirtualServerDef(
1004 999
                 virtual_server_id=vs_obj_id,
1005
-                name=vs_name,
1006 1000
                 rules=[{'display_name': 'yy'},
1007 1001
                        {'display_name': 'xx', 'actions': '22'}])
1008 1002
             self.assert_called_with_def(update_call, expected_def)
@@ -1018,7 +1012,6 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
1018 1012
 
1019 1013
             expected_def = lb_defs.LBVirtualServerDef(
1020 1014
                 virtual_server_id=vs_obj_id,
1021
-                name=vs_name,
1022 1015
                 rules=[{'display_name': 'yy'}])
1023 1016
             self.assert_called_with_def(update_call, expected_def)
1024 1017
 

+ 13
- 5
vmware_nsxlib/tests/unit/v3/policy/test_resources.py View File

@@ -18,6 +18,7 @@ import mock
18 18
 
19 19
 from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
20 20
 from vmware_nsxlib.tests.unit.v3.policy import policy_testcase
21
+from vmware_nsxlib.tests.unit.v3 import test_client
21 22
 from vmware_nsxlib.v3 import exceptions as nsxlib_exc
22 23
 from vmware_nsxlib.v3 import nsx_constants
23 24
 from vmware_nsxlib.v3 import policy
@@ -39,7 +40,7 @@ class NsxPolicyLibTestCase(policy_testcase.TestPolicyApi):
39 40
         # Mock the nsx-lib for the passthrough api
40 41
         # TODO(annak): move version forward with backend releases
41 42
         with mock.patch("vmware_nsxlib.v3.NsxLib") as mock_lib:
42
-            mock_lib.return_value.get_version.return_value = "2.5.0"
43
+            mock_lib.return_value.get_version.return_value = "3.0.0"
43 44
             self.policy_lib = policy.NsxPolicyLib(nsxlib_config)
44 45
 
45 46
         self.policy_api = self.policy_lib.policy_api
@@ -216,7 +217,8 @@ class TestPolicyDomain(NsxPolicyLibTestCase):
216 217
 
217 218
             self.assert_json_call('PATCH', self.client,
218 219
                                   '%s/domains/%s' % (TEST_TENANT, domain_id),
219
-                                  data=expected_body)
220
+                                  data=expected_body,
221
+                                  headers=test_client.PARTIAL_UPDATE_HEADERS)
220 222
 
221 223
 
222 224
 class TestPolicyGroup(NsxPolicyLibTestCase):
@@ -595,7 +597,8 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
595 597
                                   '%s/domains/%s/groups/%s' % (TEST_TENANT,
596 598
                                                                domain_id,
597 599
                                                                group_id),
598
-                                  data=expected_body)
600
+                                  data=expected_body,
601
+                                  headers=test_client.PARTIAL_UPDATE_HEADERS)
599 602
 
600 603
     def test_get_realized(self):
601 604
         domain_id = 'd1'
@@ -2385,6 +2388,7 @@ class TestPolicyTier1(NsxPolicyLibTestCase):
2385 2388
     def setUp(self, *args, **kwargs):
2386 2389
         super(TestPolicyTier1, self).setUp(*args, **kwargs)
2387 2390
         self.resourceApi = self.policy_lib.tier1
2391
+        self.partial_updates = True
2388 2392
 
2389 2393
     def test_create(self):
2390 2394
         name = 'test'
@@ -2551,9 +2555,10 @@ class TestPolicyTier1(NsxPolicyLibTestCase):
2551 2555
 
2552 2556
             expected_def = core_defs.Tier1Def(
2553 2557
                 tier1_id=obj_id,
2554
-                name=rtr_name,
2555 2558
                 route_advertisement=new_adv,
2556 2559
                 tenant=TEST_TENANT)
2560
+            if not self.partial_updates:
2561
+                expected_def.attrs['name'] = rtr_name
2557 2562
 
2558 2563
             self.assert_called_with_def(
2559 2564
                 update_call, expected_def)
@@ -2583,10 +2588,11 @@ class TestPolicyTier1(NsxPolicyLibTestCase):
2583 2588
 
2584 2589
             expected_def = core_defs.Tier1Def(
2585 2590
                 tier1_id=obj_id,
2586
-                name=rtr_name,
2587 2591
                 route_advertisement=new_adv,
2588 2592
                 tier0=tier0,
2589 2593
                 tenant=TEST_TENANT)
2594
+            if not self.partial_updates:
2595
+                expected_def.attrs['name'] = rtr_name
2590 2596
 
2591 2597
             self.assert_called_with_def(
2592 2598
                 update_call, expected_def)
@@ -2902,6 +2908,8 @@ class TestPolicyTier1NoPassthrough(TestPolicyTier1):
2902 2908
     def setUp(self, *args, **kwargs):
2903 2909
         super(TestPolicyTier1NoPassthrough, self).setUp(
2904 2910
             allow_passthrough=False)
2911
+        # No passthrough also means no partial updates
2912
+        self.partial_updates = False
2905 2913
 
2906 2914
     def test_update_transport_zone(self):
2907 2915
         # Will not work without passthrough api

+ 7
- 0
vmware_nsxlib/tests/unit/v3/test_client.py View File

@@ -39,6 +39,13 @@ JSON_DFT_ACCEPT_HEADERS = {
39 39
     'Cookie': 'JSESSIONID=%s;' % nsxlib_testcase.JSESSIONID
40 40
 }
41 41
 
42
+PARTIAL_UPDATE_HEADERS = {
43
+    'Accept': 'application/json',
44
+    'Content-Type': 'application/json',
45
+    'Cookie': 'JSESSIONID=%s;' % nsxlib_testcase.JSESSIONID,
46
+    'nsx-enable-partial-patch': 'true'
47
+}
48
+
42 49
 
43 50
 def _headers(**kwargs):
44 51
     headers = copy.copy(DFT_ACCEPT_HEADERS)

+ 4
- 5
vmware_nsxlib/v3/policy/__init__.py View File

@@ -156,11 +156,10 @@ class NsxPolicyLib(lib.NsxLibBase):
156 156
             if (feature == nsx_constants.FEATURE_ENS_WITH_QOS):
157 157
                 return True
158 158
 
159
-        # TODO(asarfaty): Uncomment this once partial updates are supported
160
-        # if (version.LooseVersion(self.get_version()) >=
161
-        #     version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)):
162
-        #     if feature == nsx_constants.FEATURE_PARTIAL_UPDATES:
163
-        #         return True
159
+        if (version.LooseVersion(self.get_version()) >=
160
+            version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)):
161
+            if feature == nsx_constants.FEATURE_PARTIAL_UPDATES:
162
+                return True
164 163
 
165 164
         return (feature == nsx_constants.FEATURE_NSX_POLICY)
166 165
 

+ 5
- 2
vmware_nsxlib/v3/policy/core_defs.py View File

@@ -1764,7 +1764,7 @@ class NsxPolicyApi(object):
1764 1764
     def partial_updates_supported(self):
1765 1765
         return self.partial_updates
1766 1766
 
1767
-    def create_or_update(self, resource_def):
1767
+    def create_or_update(self, resource_def, partial_updates=False):
1768 1768
         """Create or update a policy object.
1769 1769
 
1770 1770
         This api will update an existing object, or create a new one if it
@@ -1776,7 +1776,10 @@ class NsxPolicyApi(object):
1776 1776
             self.cache.remove(path)
1777 1777
         body = resource_def.get_obj_dict()
1778 1778
 
1779
-        self.client.patch(path, body)
1779
+        headers = None
1780
+        if partial_updates:
1781
+            headers = {'nsx-enable-partial-patch': 'true'}
1782
+        self.client.patch(path, body, headers=headers)
1780 1783
 
1781 1784
     def create_with_parent(self, parent_def, resource_def):
1782 1785
         path = parent_def.get_resource_path()

+ 5
- 4
vmware_nsxlib/v3/policy/core_resources.py View File

@@ -147,14 +147,16 @@ class NsxPolicyResourceBase(object):
147 147
 
148 148
         if self.policy_api.partial_updates_supported():
149 149
             policy_def = self._init_def(**kwargs)
150
+            partial_updates = True
150 151
         else:
151 152
             policy_def = self._get_and_update_def(**kwargs)
153
+            partial_updates = False
152 154
 
153 155
         if policy_def.bodyless():
154 156
             # Nothing to update - only keys provided in kwargs
155 157
             return
156
-
157
-        self.policy_api.create_or_update(policy_def)
158
+        self.policy_api.create_or_update(
159
+            policy_def, partial_updates=partial_updates)
158 160
 
159 161
     @staticmethod
160 162
     def _init_obj_uuid(obj_uuid):
@@ -994,8 +996,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase):
994 996
         self.update(tier1_id,
995 997
                     route_advertisement=route_adv,
996 998
                     tier0=tier0,
997
-                    tenant=tenant,
998
-                    current_body=tier1_dict)
999
+                    tenant=tenant)
999 1000
 
1000 1001
     def add_advertisement_rule(
1001 1002
             self, tier1_id, name, action=None, prefix_operator=None,

Loading…
Cancel
Save