Browse Source

Add trigger verification in API

Currently, we use 'cast' instead of 'call' to send request
from api to operationengine when creating a new trigger.
With 'cast', it will not give a return value to api even the
trigger is invalid, so users can create triggers successfully
with using invalid triggers.

This patch add trigger verification in API side. Before send
create request to operationengine, API send a verify request
to operationengine first, which type is 'call', to make sure
the trigger will be created is valid.

Change-Id: Iea734e1b4a63f6e35afe32cbbb2b60f79d6db085
Closes-Bug: #1736657
jiaopengju 1 year ago
parent
commit
bba93226e6

+ 1
- 0
karbor/api/v1/triggers.py View File

@@ -104,6 +104,7 @@ class TriggersController(wsgi.Controller):
104 104
         }
105 105
         try:
106 106
             trigger = objects.Trigger(context=context, **trigger_definition)
107
+            self.operationengine_api.verify_trigger(context, trigger)
107 108
             self.operationengine_api.create_trigger(context, trigger)
108 109
             trigger.create()
109 110
         except exception.Invalid as ex:

+ 3
- 0
karbor/services/operationengine/api.py View File

@@ -40,6 +40,9 @@ class API(base.Base):
40 40
         self.operationengine_rpcapi.resume_scheduled_operation(
41 41
             context, operation_id, trigger_id)
42 42
 
43
+    def verify_trigger(self, context, trigger):
44
+        self.operationengine_rpcapi.verify_trigger(context, trigger)
45
+
43 46
     def create_trigger(self, context, trigger):
44 47
         self.operationengine_rpcapi.create_trigger(context, trigger)
45 48
 

+ 7
- 0
karbor/services/operationengine/manager.py View File

@@ -219,6 +219,13 @@ class OperationEngineManager(manager.Manager):
219 219
         except Exception:
220 220
             raise
221 221
 
222
+    @messaging.expected_exceptions(exception.InvalidInput)
223
+    def verify_trigger(self, context, trigger):
224
+        LOG.debug('Verifying trigger (id: "%s" type: "%s")',
225
+                  trigger.id, trigger.type)
226
+        self.trigger_manager.check_trigger_definition(
227
+            trigger.type, trigger.properties)
228
+
222 229
     @messaging.expected_exceptions(exception.InvalidInput)
223 230
     def create_trigger(self, context, trigger):
224 231
         LOG.debug('Creating trigger (id: "%s" type: "%s")',

+ 3
- 0
karbor/services/operationengine/rpcapi.py View File

@@ -63,6 +63,9 @@ class OperationEngineAPI(object):
63 63
                                  operation_id=operation_id,
64 64
                                  trigger_id=trigger_id)
65 65
 
66
+    def verify_trigger(self, ctxt, trigger):
67
+        return self._client.call(ctxt, 'verify_trigger', trigger=trigger)
68
+
66 69
     def create_trigger(self, ctxt, trigger):
67 70
         self._client.prepare(fanout=True).cast(ctxt, 'create_trigger',
68 71
                                                trigger=trigger)

+ 3
- 0
karbor/tests/unit/api/v1/test_triggers.py View File

@@ -31,6 +31,9 @@ class FakeRemoteOperationApi(object):
31 31
             msg = (_("Invalid trigger time format type"))
32 32
             raise exception.InvalidInput(msg)
33 33
 
34
+    def verify_trigger(self, context, trigger):
35
+        pass
36
+
34 37
     def delete_trigger(self, context, trigger_id):
35 38
         pass
36 39
 

+ 9
- 0
karbor/tests/unit/operationengine/test_manager.py View File

@@ -39,6 +39,9 @@ class FakeTriggerManager(object):
39 39
     def unregister_operation(self, trigger_id, operation_id, **kwargs):
40 40
         pass
41 41
 
42
+    def check_trigger_definition(self, trigger_type, trigger_definition):
43
+        pass
44
+
42 45
     def add_trigger(self, trigger_id, trigger_type, trigger_property):
43 46
         self._trigger[trigger_id] = []
44 47
 
@@ -150,6 +153,12 @@ class OperationEngineManagerTestCase(base.TestCase):
150 153
         self.manager.suspend_scheduled_operation(self.ctxt, op_id, trigger_id)
151 154
         unregister.assert_called_once_with(trigger_id, op_id)
152 155
 
156
+    @mock.patch.object(FakeTriggerManager, 'check_trigger_definition')
157
+    def test_verify_trigger(self, check_trigger_definition):
158
+        self.manager.verify_trigger(self.ctxt, self._trigger)
159
+        check_trigger_definition.assert_called_once_with(
160
+            self._trigger.type, self._trigger.properties)
161
+
153 162
     def _create_one_trigger(self):
154 163
         trigger_info = {
155 164
             'project_id': "123",

Loading…
Cancel
Save