add sync task in workflow
[vfc/nfvo/lcm.git] / lcm / workflows / graphflow / task / sync_rest_task.py
1 # Copyright 2018 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from lcm.workflows.graphflow.task.sync_task import SyncTask
16 import logging
17
18 logger = logging.getLogger(__name__)
19
20
21 class SyncRestTask(SyncTask):
22
23     STATUS_OK = (HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED) = ('200', '201', '204', '202')
24     HTTP_METHOD = (POST, GET, PUT, DELETE) = ("POST", "GET", "PUT", "DELETE")
25
26     def __init__(self, *args):
27         super(SyncRestTask, self).__init__(*args)
28         self.url = self.input.get(self.URL, "")
29         self.method = self.input.get(self.METHOD, "")
30         self.content = self.input.get(self.CONTENT, "")
31
32     def run(self):
33         status, output = self.call_rest(self.url, self.method, self.content)
34         if status in self.STATUS_OK:
35             status = self.FINISHED
36         else:
37             status = self.ERROR
38         logger.debug("SyncRestTask status %s, output %s" % (status, output))
39         return status, output
40
41     def call_rest(self, url, method, content):
42         pass