Browse Source

Add Application Policy Group resource

Change-Id: I0eb9f444ffb8325af0dfee48e1a07eda41f53161
Implements: blueprint application-group
Sumit Naiksatam 2 years ago
parent
commit
5e3e66f6a8

+ 82
- 6
gbpautomation/heat/engine/resources/grouppolicy.py View File

@@ -21,6 +21,76 @@ from heat.engine import properties
21 21
 from neutronclient.common.exceptions import NeutronClientException
22 22
 
23 23
 
24
+class ApplicationPolicyGroup(gbpresource.GBPResource):
25
+
26
+    PROPERTIES = (
27
+        TENANT_ID, NAME, DESCRIPTION, SHARED
28
+    ) = (
29
+        'tenant_id', 'name', 'description', 'shared'
30
+    )
31
+
32
+    properties_schema = {
33
+        TENANT_ID: properties.Schema(
34
+            properties.Schema.STRING,
35
+            _('Tenant id of the Application Policy Group.')
36
+        ),
37
+        NAME: properties.Schema(
38
+            properties.Schema.STRING,
39
+            _('Name of the Application Policy Group.'),
40
+            update_allowed=True
41
+        ),
42
+        DESCRIPTION: properties.Schema(
43
+            properties.Schema.STRING,
44
+            _('Description of the Application Policy Group.'),
45
+            update_allowed=True
46
+        ),
47
+        SHARED: properties.Schema(
48
+            properties.Schema.BOOLEAN,
49
+            _('Shared.'),
50
+            update_allowed=True, required=True
51
+        )
52
+    }
53
+
54
+    def _show_resource(self):
55
+        client = self.grouppolicy()
56
+        application_policy_group_id = self.resource_id
57
+        return client.show_application_policy_group(
58
+            application_policy_group_id)['application_policy_group']
59
+
60
+    def handle_create(self):
61
+        client = self.grouppolicy()
62
+
63
+        props = {}
64
+        for key in self.properties:
65
+            if self.properties.get(key) is not None:
66
+                props[key] = self.properties.get(key)
67
+
68
+        application_policy_group = (
69
+            client.create_application_policy_group(
70
+                {'application_policy_group':
71
+                 props})['application_policy_group'])
72
+
73
+        self.resource_id_set(application_policy_group['id'])
74
+
75
+    def handle_delete(self):
76
+
77
+        client = self.grouppolicy()
78
+        application_policy_group_id = self.resource_id
79
+
80
+        try:
81
+            client.delete_application_policy_group(
82
+                application_policy_group_id)
83
+        except NeutronClientException as ex:
84
+            self.client_plugin().ignore_not_found(ex)
85
+        else:
86
+            return self._delete_task()
87
+
88
+    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
89
+        if prop_diff:
90
+            self.grouppolicy().update_application_policy_group(
91
+                self.resource_id, {'application_policy_group': prop_diff})
92
+
93
+
24 94
 class PolicyTarget(gbpresource.GBPResource):
25 95
 
26 96
     PROPERTIES = (
@@ -148,13 +218,14 @@ class PolicyTarget(gbpresource.GBPResource):
148 218
 class PolicyTargetGroup(gbpresource.GBPResource):
149 219
 
150 220
     PROPERTIES = (
151
-        TENANT_ID, NAME, DESCRIPTION, L2_POLICY_ID, PROVIDED_POLICY_RULE_SETS,
152
-        CONSUMED_POLICY_RULE_SETS, NETWORK_SERVICE_POLICY_ID, SHARED,
153
-        INTRA_PTG_ALLOW
221
+        TENANT_ID, NAME, DESCRIPTION, APPLICATION_POLICY_GROUP_ID,
222
+        L2_POLICY_ID, PROVIDED_POLICY_RULE_SETS, CONSUMED_POLICY_RULE_SETS,
223
+        NETWORK_SERVICE_POLICY_ID, SHARED, INTRA_PTG_ALLOW
154 224
     ) = (
155
-        'tenant_id', 'name', 'description', 'l2_policy_id',
156
-        'provided_policy_rule_sets', 'consumed_policy_rule_sets',
157
-        'network_service_policy_id', 'shared', 'intra_ptg_allow'
225
+        'tenant_id', 'name', 'description', 'application_policy_group_id',
226
+        'l2_policy_id', 'provided_policy_rule_sets',
227
+        'consumed_policy_rule_sets', 'network_service_policy_id', 'shared',
228
+        'intra_ptg_allow'
158 229
     )
159 230
 
160 231
     properties_schema = {
@@ -172,6 +243,11 @@ class PolicyTargetGroup(gbpresource.GBPResource):
172 243
             _('Description of the policy target group.'),
173 244
             update_allowed=True
174 245
         ),
246
+        APPLICATION_POLICY_GROUP_ID: properties.Schema(
247
+            properties.Schema.STRING,
248
+            _('Application Policy Group id of the policy target group.'),
249
+            update_allowed=True
250
+        ),
175 251
         L2_POLICY_ID: properties.Schema(
176 252
             properties.Schema.STRING,
177 253
             _('L2 policy id of the policy target group.'),

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

@@ -23,6 +23,25 @@ from heat.engine import scheduler
23 23
 from heat.tests import utils
24 24
 
25 25
 
26
+application_policy_group_template = '''
27
+{
28
+  "AWSTemplateFormatVersion" : "2010-09-09",
29
+  "Description" : "Template to test GBP application policy group",
30
+  "Parameters" : {},
31
+  "Resources" : {
32
+    "application_policy_group": {
33
+      "Type": "OS::GroupBasedPolicy::ApplicationPolicyGroup",
34
+      "Properties": {
35
+        "name": "test-application-policy-group",
36
+        "description": "test APG resource",
37
+        "shared": True
38
+      }
39
+    }
40
+  }
41
+}
42
+'''
43
+
44
+
26 45
 policy_target_template = '''
27 46
 {
28 47
   "AWSTemplateFormatVersion" : "2010-09-09",
@@ -97,6 +116,39 @@ policy_target_group_template = '''
97 116
 }
98 117
 '''
99 118
 
119
+policy_target_group_with_apg_template = '''
120
+{
121
+  "AWSTemplateFormatVersion" : "2010-09-09",
122
+  "Description" : "Template to test neutron policy target group resource",
123
+  "Parameters" : {},
124
+  "Resources" : {
125
+    "policy_target_group": {
126
+      "Type": "OS::GroupBasedPolicy::PolicyTargetGroup",
127
+      "Properties": {
128
+        "name": "test-policy-target-group",
129
+        "description": "test policy target group resource",
130
+        "application_policy_group_id": "apg-id",
131
+        "l2_policy_id": "l2-policy-id",
132
+        "provided_policy_rule_sets": [
133
+            {"policy_rule_set_id": "policy_rule_set1",
134
+             "policy_rule_set_scope": "scope1"},
135
+            {"policy_rule_set_id": "policy_rule_set2",
136
+             "policy_rule_set_scope": "scope2"}
137
+        ],
138
+        "consumed_policy_rule_sets": [
139
+            {"policy_rule_set_id": "policy_rule_set3",
140
+             "policy_rule_set_scope": "scope3"},
141
+            {"policy_rule_set_id": "policy_rule_set4",
142
+             "policy_rule_set_scope": "scope4"}
143
+        ],
144
+        "shared": True,
145
+        "intra_ptg_allow": False
146
+      }
147
+    }
148
+  }
149
+}
150
+'''
151
+
100 152
 l2_policy_template = '''
101 153
 {
102 154
   "AWSTemplateFormatVersion" : "2010-09-09",
@@ -329,6 +381,122 @@ nat_pool_template = '''
329 381
 '''
330 382
 
331 383
 
384
+class ApplicationPolicyGroupTest(HeatTestCase):
385
+
386
+    def setUp(self):
387
+        super(ApplicationPolicyGroupTest, self).setUp()
388
+        self.m.StubOutWithMock(gbpclient.Client,
389
+                               'create_application_policy_group')
390
+        self.m.StubOutWithMock(gbpclient.Client,
391
+                               'delete_application_policy_group')
392
+        self.m.StubOutWithMock(gbpclient.Client,
393
+                               'show_application_policy_group')
394
+        self.m.StubOutWithMock(gbpclient.Client,
395
+                               'update_application_policy_group')
396
+        self.stub_keystoneclient()
397
+
398
+    def create_application_policy_group(self):
399
+        gbpclient.Client.create_application_policy_group({
400
+            'application_policy_group': {
401
+                "name": "test-application-policy-group",
402
+                "description": "test APG resource",
403
+                "shared": True
404
+            }
405
+        }).AndReturn({'application_policy_group': {'id': '5678'}})
406
+
407
+        snippet = template_format.parse(application_policy_group_template)
408
+        self.stack = utils.parse_stack(snippet)
409
+        resource_defns = self.stack.t.resource_definitions(self.stack)
410
+        return grouppolicy.ApplicationPolicyGroup(
411
+            'application_policy_group',
412
+            resource_defns['application_policy_group'], self.stack)
413
+
414
+    def test_create(self):
415
+        rsrc = self.create_application_policy_group()
416
+        self.m.ReplayAll()
417
+        scheduler.TaskRunner(rsrc.create)()
418
+        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
419
+        self.m.VerifyAll()
420
+
421
+    def test_create_failed(self):
422
+        gbpclient.Client.create_application_policy_group({
423
+            'application_policy_group': {
424
+                "name": "test-application-policy-group",
425
+                "description": "test APG resource",
426
+                "shared": True
427
+            }
428
+        }).AndRaise(grouppolicy.NeutronClientException())
429
+        self.m.ReplayAll()
430
+
431
+        snippet = template_format.parse(application_policy_group_template)
432
+        self.stack = utils.parse_stack(snippet)
433
+        resource_defns = self.stack.t.resource_definitions(self.stack)
434
+        rsrc = grouppolicy.ApplicationPolicyGroup(
435
+            'application_policy_group',
436
+            resource_defns['application_policy_group'], self.stack)
437
+
438
+        error = self.assertRaises(exception.ResourceFailure,
439
+                                  scheduler.TaskRunner(rsrc.create))
440
+        self.assertEqual(
441
+            'NeutronClientException: resources.application_policy_group: '
442
+            'An unknown exception occurred.',
443
+            six.text_type(error))
444
+        self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)
445
+        self.m.VerifyAll()
446
+
447
+    def test_delete(self):
448
+        gbpclient.Client.delete_application_policy_group('5678')
449
+        gbpclient.Client.show_application_policy_group('5678').AndRaise(
450
+            grouppolicy.NeutronClientException(status_code=404))
451
+
452
+        rsrc = self.create_application_policy_group()
453
+        self.m.ReplayAll()
454
+        scheduler.TaskRunner(rsrc.create)()
455
+        scheduler.TaskRunner(rsrc.delete)()
456
+        self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
457
+        self.m.VerifyAll()
458
+
459
+    def test_delete_already_gone(self):
460
+        gbpclient.Client.delete_application_policy_group('5678').AndRaise(
461
+            grouppolicy.NeutronClientException(status_code=404))
462
+
463
+        rsrc = self.create_application_policy_group()
464
+        self.m.ReplayAll()
465
+        scheduler.TaskRunner(rsrc.create)()
466
+        scheduler.TaskRunner(rsrc.delete)()
467
+        self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
468
+        self.m.VerifyAll()
469
+
470
+    def test_delete_failed(self):
471
+        gbpclient.Client.delete_application_policy_group('5678').AndRaise(
472
+            grouppolicy.NeutronClientException(status_code=400))
473
+
474
+        rsrc = self.create_application_policy_group()
475
+        self.m.ReplayAll()
476
+        scheduler.TaskRunner(rsrc.create)()
477
+        error = self.assertRaises(exception.ResourceFailure,
478
+                                  scheduler.TaskRunner(rsrc.delete))
479
+        self.assertEqual(
480
+            'NeutronClientException: resources.application_policy_group: '
481
+            'An unknown exception occurred.',
482
+            six.text_type(error))
483
+        self.assertEqual((rsrc.DELETE, rsrc.FAILED), rsrc.state)
484
+        self.m.VerifyAll()
485
+
486
+    def test_update(self):
487
+        rsrc = self.create_application_policy_group()
488
+        gbpclient.Client.update_application_policy_group(
489
+            '5678', {'application_policy_group': {'name': 'new name'}})
490
+        self.m.ReplayAll()
491
+        scheduler.TaskRunner(rsrc.create)()
492
+
493
+        update_template = copy.deepcopy(rsrc.t)
494
+        update_template['Properties']['name'] = 'new name'
495
+        scheduler.TaskRunner(rsrc.update, update_template)()
496
+
497
+        self.m.VerifyAll()
498
+
499
+
332 500
 class PolicyTargetTest(HeatTestCase):
333 501
 
334 502
     def setUp(self):
@@ -529,6 +697,33 @@ class PolicyTargetGroupTest(HeatTestCase):
529 697
             'policy_target_group', resource_defns['policy_target_group'],
530 698
             self.stack)
531 699
 
700
+    def create_policy_target_group_with_apg(self):
701
+        gbpclient.Client.create_policy_target_group({
702
+            "policy_target_group": {
703
+                "name": "test-policy-target-group",
704
+                "description": "test policy target group resource",
705
+                "application_policy_group_id": "apg-id",
706
+                "l2_policy_id": "l2-policy-id",
707
+                "provided_policy_rule_sets": {
708
+                    "policy_rule_set1": "scope1",
709
+                    "policy_rule_set2": "scope2"
710
+                },
711
+                "consumed_policy_rule_sets": {
712
+                    "policy_rule_set3": "scope3",
713
+                    "policy_rule_set4": "scope4"
714
+                },
715
+                "shared": True,
716
+                "intra_ptg_allow": False
717
+            }
718
+        }).AndReturn({'policy_target_group': {'id': '5678'}})
719
+
720
+        snippet = template_format.parse(policy_target_group_with_apg_template)
721
+        self.stack = utils.parse_stack(snippet)
722
+        resource_defns = self.stack.t.resource_definitions(self.stack)
723
+        return grouppolicy.PolicyTargetGroup(
724
+            'policy_target_group', resource_defns['policy_target_group'],
725
+            self.stack)
726
+
532 727
     def test_create(self):
533 728
         rsrc = self.create_policy_target_group()
534 729
         self.m.ReplayAll()
@@ -536,6 +731,13 @@ class PolicyTargetGroupTest(HeatTestCase):
536 731
         self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
537 732
         self.m.VerifyAll()
538 733
 
734
+    def test_create_with_apg(self):
735
+        rsrc = self.create_policy_target_group_with_apg()
736
+        self.m.ReplayAll()
737
+        scheduler.TaskRunner(rsrc.create)()
738
+        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
739
+        self.m.VerifyAll()
740
+
539 741
     def test_create_failed(self):
540 742
         gbpclient.Client.create_policy_target_group({
541 743
             "policy_target_group": {

Loading…
Cancel
Save