Browse Source

Group Policy API-4 HEAT resource: Network Service Policy

This is the fourth patch in the Group Policy resources implementation
series. This patch implements:
    Network Service Policy

Change-Id: I7839d74740fd08cfa176df81cac7f5580d796740
Implements: blueprint group-based-policy-automation
susaant.kondapaneni 4 years ago
parent
commit
12ef099a43

+ 76
- 3
gbpautomation/heat/engine/resources/neutron/grouppolicy.py View File

@@ -110,10 +110,10 @@ class EndpointGroup(gbpresource.GBPResource):
110 110
 
111 111
     PROPERTIES = (
112 112
         TENANT_ID, NAME, DESCRIPTION, L2_POLICY_ID,
113
-        PROVIDED_CONTRACTS, CONSUMED_CONTRACTS
113
+        PROVIDED_CONTRACTS, CONSUMED_CONTRACTS, NETWORK_SERVICE_POLICY_ID
114 114
     ) = (
115 115
         'tenant_id', 'name', 'description', 'l2_policy_id',
116
-        'provided_contracts', 'consumed_contracts'
116
+        'provided_contracts', 'consumed_contracts', 'network_service_policy_id'
117 117
     )
118 118
 
119 119
     properties_schema = {
@@ -145,6 +145,11 @@ class EndpointGroup(gbpresource.GBPResource):
145 145
             properties.Schema.LIST,
146 146
             _('Consumed contracts for the endpoint group.'),
147 147
             update_allowed=True
148
+        ),
149
+        NETWORK_SERVICE_POLICY_ID: properties.Schema(
150
+            properties.Schema.STRING,
151
+            _('Network service policy id of the endpoint group.'),
152
+            update_allowed=True, default=None
148 153
         )
149 154
     }
150 155
 
@@ -663,6 +668,73 @@ class Contract(gbpresource.GBPResource):
663 668
                 self.resource_id, {'contract': prop_diff})
664 669
 
665 670
 
671
+class NetworkServicePolicy(gbpresource.GBPResource):
672
+
673
+    PROPERTIES = (
674
+        TENANT_ID, NAME, DESCRIPTION, NETWORK_SERVICE_PARAMS
675
+    ) = (
676
+        'tenant_id', 'name', 'description', 'network_service_params'
677
+    )
678
+
679
+    properties_schema = {
680
+        TENANT_ID: properties.Schema(
681
+            properties.Schema.STRING,
682
+            _('Tenant id of the network service policy.')
683
+        ),
684
+        NAME: properties.Schema(
685
+            properties.Schema.STRING,
686
+            _('Name of the network service policy.'),
687
+            update_allowed=True
688
+        ),
689
+        DESCRIPTION: properties.Schema(
690
+            properties.Schema.STRING,
691
+            _('Description of the network service policy.'),
692
+            update_allowed=True
693
+        ),
694
+        NETWORK_SERVICE_PARAMS: properties.Schema(
695
+            properties.Schema.LIST,
696
+            _('List of network service policy dicts.'),
697
+            default=None, update_allowed=True
698
+        )
699
+    }
700
+
701
+    def _show_resource(self):
702
+        client = self.grouppolicy()
703
+        nsp_id = self.resource_id
704
+        nsp = client.show_network_service_policy(nsp_id)
705
+        return nsp['network_service_policy']
706
+
707
+    def handle_create(self):
708
+        client = self.grouppolicy()
709
+
710
+        props = {}
711
+        for key in self.properties:
712
+            if self.properties.get(key) is not None:
713
+                props[key] = self.properties.get(key)
714
+
715
+        nsp = client.create_network_service_policy(
716
+            {'network_service_policy': props})['network_service_policy']
717
+
718
+        self.resource_id_set(nsp['id'])
719
+
720
+    def handle_delete(self):
721
+
722
+        client = self.grouppolicy()
723
+        nsp_id = self.resource_id
724
+
725
+        try:
726
+            client.delete_network_service_policy(nsp_id)
727
+        except NeutronClientException as ex:
728
+            self.client_plugin().ignore_not_found(ex)
729
+        else:
730
+            return self._delete_task()
731
+
732
+    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
733
+        if prop_diff:
734
+            self.grouppolicy().update_network_service_policy(
735
+                self.resource_id, {'network_service_policy': prop_diff})
736
+
737
+
666 738
 def resource_mapping():
667 739
     return {
668 740
         'OS::Neutron::Endpoint': Endpoint,
@@ -672,5 +744,6 @@ def resource_mapping():
672 744
         'OS::Neutron::PolicyClassifier': PolicyClassifier,
673 745
         'OS::Neutron::PolicyAction': PolicyAction,
674 746
         'OS::Neutron::PolicyRule': PolicyRule,
675
-        'OS::Neutron::Contract': Contract
747
+        'OS::Neutron::Contract': Contract,
748
+        'OS::Neutron::NetworkServicePolicy': NetworkServicePolicy
676 749
     }

+ 139
- 0
gbpautomation/heat/tests/test_grouppolicy.py View File

@@ -184,6 +184,25 @@ contract_template = '''
184 184
 }
185 185
 '''
186 186
 
187
+network_service_policy_template = '''
188
+{
189
+ "AWSTemplateFormatVersion" : "2010-09-09",
190
+  "Description" : "Template to test network service policy",
191
+  "Parameters" : {},
192
+  "Resources" : {
193
+  "network_service_policy": {
194
+      "Type": "OS::Neutron::NetworkServicePolicy",
195
+      "Properties": {
196
+          "name": "test-nsp",
197
+          "description": "test NSP resource",
198
+          "network_service_params": [{'type': 'ip_single', 'name': 'vip',
199
+                                      'value': 'self_subnet'}]
200
+      }
201
+    }
202
+  }
203
+}
204
+'''
205
+
187 206
 
188 207
 class EndpointTest(HeatTestCase):
189 208
 
@@ -1106,3 +1125,123 @@ class ContractTest(HeatTestCase):
1106 1125
         scheduler.TaskRunner(rsrc.update, update_template)()
1107 1126
 
1108 1127
         self.m.VerifyAll()
1128
+
1129
+
1130
+class NetworkServicePolicyTest(HeatTestCase):
1131
+
1132
+    def setUp(self):
1133
+        super(NetworkServicePolicyTest, self).setUp()
1134
+        self.m.StubOutWithMock(gbpclient.Client,
1135
+                               'create_network_service_policy')
1136
+        self.m.StubOutWithMock(gbpclient.Client,
1137
+                               'delete_network_service_policy')
1138
+        self.m.StubOutWithMock(gbpclient.Client,
1139
+                               'show_network_service_policy')
1140
+        self.m.StubOutWithMock(gbpclient.Client,
1141
+                               'update_network_service_policy')
1142
+        self.stub_keystoneclient()
1143
+
1144
+    def create_network_service_policy(self):
1145
+        gbpclient.Client.create_network_service_policy({
1146
+            'network_service_policy': {
1147
+                "name": "test-nsp",
1148
+                "description": "test NSP resource",
1149
+                "network_service_params": [
1150
+                    {'type': 'ip_single', 'name': 'vip',
1151
+                     'value': 'self_subnet'}]
1152
+            }
1153
+        }).AndReturn({'network_service_policy': {'id': '5678'}})
1154
+
1155
+        snippet = template_format.parse(network_service_policy_template)
1156
+        stack = utils.parse_stack(snippet)
1157
+        resource_defns = stack.t.resource_definitions(stack)
1158
+        return grouppolicy.NetworkServicePolicy(
1159
+            'network_service_policy',
1160
+            resource_defns['network_service_policy'], stack)
1161
+
1162
+    def test_create(self):
1163
+        rsrc = self.create_network_service_policy()
1164
+        self.m.ReplayAll()
1165
+        scheduler.TaskRunner(rsrc.create)()
1166
+        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
1167
+        self.m.VerifyAll()
1168
+
1169
+    def test_create_failed(self):
1170
+        gbpclient.Client.create_network_service_policy({
1171
+            'network_service_policy': {
1172
+                "name": "test-nsp",
1173
+                "description": "test NSP resource",
1174
+                "network_service_params": [
1175
+                    {'type': 'ip_single', 'name': 'vip',
1176
+                     'value': 'self_subnet'}]
1177
+            }
1178
+        }).AndRaise(grouppolicy.NeutronClientException())
1179
+        self.m.ReplayAll()
1180
+
1181
+        snippet = template_format.parse(network_service_policy_template)
1182
+        stack = utils.parse_stack(snippet)
1183
+        resource_defns = stack.t.resource_definitions(stack)
1184
+        rsrc = grouppolicy.NetworkServicePolicy(
1185
+            'network_service_policy',
1186
+            resource_defns['network_service_policy'], stack)
1187
+
1188
+        error = self.assertRaises(exception.ResourceFailure,
1189
+                                  scheduler.TaskRunner(rsrc.create))
1190
+        self.assertEqual(
1191
+            'NeutronClientException: An unknown exception occurred.',
1192
+            str(error))
1193
+        self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)
1194
+        self.m.VerifyAll()
1195
+
1196
+    def test_delete(self):
1197
+        gbpclient.Client.delete_network_service_policy('5678')
1198
+        gbpclient.Client.show_network_service_policy('5678').AndRaise(
1199
+            grouppolicy.NeutronClientException(status_code=404))
1200
+
1201
+        rsrc = self.create_network_service_policy()
1202
+        self.m.ReplayAll()
1203
+        scheduler.TaskRunner(rsrc.create)()
1204
+        scheduler.TaskRunner(rsrc.delete)()
1205
+        self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
1206
+        self.m.VerifyAll()
1207
+
1208
+    def test_delete_already_gone(self):
1209
+        gbpclient.Client.delete_network_service_policy('5678').AndRaise(
1210
+            grouppolicy.NeutronClientException(status_code=404))
1211
+
1212
+        rsrc = self.create_network_service_policy()
1213
+        self.m.ReplayAll()
1214
+        scheduler.TaskRunner(rsrc.create)()
1215
+        scheduler.TaskRunner(rsrc.delete)()
1216
+        self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
1217
+        self.m.VerifyAll()
1218
+
1219
+    def test_delete_failed(self):
1220
+        gbpclient.Client.delete_network_service_policy('5678').AndRaise(
1221
+            grouppolicy.NeutronClientException(status_code=400))
1222
+
1223
+        rsrc = self.create_network_service_policy()
1224
+        self.m.ReplayAll()
1225
+        scheduler.TaskRunner(rsrc.create)()
1226
+        error = self.assertRaises(exception.ResourceFailure,
1227
+                                  scheduler.TaskRunner(rsrc.delete))
1228
+        self.assertEqual(
1229
+            'NeutronClientException: An unknown exception occurred.',
1230
+            str(error))
1231
+        self.assertEqual((rsrc.DELETE, rsrc.FAILED), rsrc.state)
1232
+        self.m.VerifyAll()
1233
+
1234
+    def test_update(self):
1235
+        rsrc = self.create_network_service_policy()
1236
+        gbpclient.Client.update_network_service_policy(
1237
+            '5678', {'network_service_policy':
1238
+                     {'network_service_params': [{'name': 'vip-update'}]}})
1239
+        self.m.ReplayAll()
1240
+        scheduler.TaskRunner(rsrc.create)()
1241
+
1242
+        update_template = copy.deepcopy(rsrc.t)
1243
+        update_template['Properties']['network_service_params'] = [
1244
+            {'name': 'vip-update'}]
1245
+        scheduler.TaskRunner(rsrc.update, update_template)()
1246
+
1247
+        self.m.VerifyAll()

Loading…
Cancel
Save