Merge "Publish execution input/output into Kafka topics"
[ccsdk/cds.git] / ms / command-executor / src / main / python / utils.py
1 #
2 # Copyright (C) 2019 Bell Canada.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 from google.protobuf.timestamp_pb2 import Timestamp
17
18 import proto.CommandExecutor_pb2 as CommandExecutor_pb2
19 import json
20
21
22 def get_blueprint_id(request):
23   blueprint_name = request.identifiers.blueprintName
24   blueprint_version = request.identifiers.blueprintVersion
25   return blueprint_name + '/' + blueprint_version
26
27 # Create a response for grpc. Fills in the timestamp as well as removes cds_is_successful element
28 def build_grpc_response(request, log_results, payload_return, is_success=False):
29   if is_success:
30     status = CommandExecutor_pb2.SUCCESS
31   else:
32     status = CommandExecutor_pb2.FAILURE
33
34   timestamp = Timestamp()
35   timestamp.GetCurrentTime()
36
37   if "cds_is_successful" in payload_return:
38     payload_return.pop('cds_is_successful')
39   payload_str = json.dumps(payload_return)
40   return CommandExecutor_pb2.ExecutionOutput(requestId=request.requestId,
41                                              response=log_results,
42                                              status=status,
43                                              payload=payload_str,
44                                              timestamp=timestamp)
45
46 # build a return data structure which may contain an error message
47 def build_ret_data(cds_is_successful, err_msg):
48   ret_data = {"cds_is_successful": cds_is_successful }
49   if err_msg != "":
50     ret_data["err_msg"] = err_msg
51   return ret_data