Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
from flask.json import jsonify
from flask_restful import Resource
from common.Settings import get_setting
from common.database.api.context.Constants import DEFAULT_CONTEXT_ID
from service.client.ServiceClient import ServiceClient
from service.proto.service_pb2 import Service, ServiceStateEnum, ServiceType
LOGGER = logging.getLogger(__name__)
class Compute(Resource):
def __init__(self) -> None:
super().__init__()
def get(self):
# Here implement HTTP GET method
raise NotImplementedError()
def post(self):
# Here implement HTTP POST method
# Retrieve required data from request
new_service_context_id = DEFAULT_CONTEXT_ID
new_service_id = 'my-service-id'
# Find Service address/port from environment and instantiate client
service_host = get_setting('SERVICESERVICE_SERVICE_HOST')
service_port = get_setting('SERVICESERVICE_SERVICE_PORT_GRPC')
service_client = ServiceClient(service_host, service_port)
# Compose a dummy CreateService request
request = Service()
request.cs_id.contextId.contextUuid.uuid = new_service_context_id
request.cs_id.cs_id.uuid = new_service_id
request.serviceType = ServiceType.L2NM
request.serviceState.serviceState = ServiceStateEnum.PLANNED
# Service component expects a non-empty config in creation, behaviour can be changed, if needed.
request.serviceConfig.serviceConfig = ' '
try:
# Issue gRPC request to Service component
reply = service_client.CreateService(request)
# Parse CreateService reply, here we check that obtained service Id and context are the expected ones.
reply_context_uuid = reply.contextId.contextUuid.uuid
reply_service_uuid = reply.cs_id.uuid
succeeded = (reply_context_uuid == new_service_context_id) and (reply_service_uuid == new_service_id)
reply = {'succeeded': succeeded}
except Exception as e:
LOGGER.exception('Something went wrong Creating Service {}'.format(str(request)))
reply = {'succeeded': False, 'error': str(e)}
return jsonify(reply)