|
@@ -4,9 +4,11 @@ from concurrent import futures
|
|
|
import grpc
|
|
|
import pydash
|
|
|
|
|
|
+import llnwLDS_pb2_grpc
|
|
|
import llnw_pb2_grpc
|
|
|
from ll_sdk.config_api import ConfigApiClient
|
|
|
from llnw_pb2 import *
|
|
|
+from llnwLDS_pb2 import *
|
|
|
|
|
|
|
|
|
def get_cl(base):
|
|
@@ -76,6 +78,34 @@ def delivery_from_grpc(delivery_entity: DeliveryEntity):
|
|
|
}
|
|
|
|
|
|
|
|
|
+def lds_config_from_grpc(request):
|
|
|
+ storage = {
|
|
|
+ "accessKey": request.accessKey,
|
|
|
+ "bucketName": request.bucketName,
|
|
|
+ "location": request.location,
|
|
|
+ "path": request.path,
|
|
|
+ "region": request.region,
|
|
|
+ "secretKey": request.secretKey
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ "compressionType": "lz4",
|
|
|
+ "isActive": "true",
|
|
|
+ "logFields": ["sc-request-id", "s-host", "datetime", "c-country-code", "sc-status", "c-ip", "cs-uri",
|
|
|
+ "sc-bytes"],
|
|
|
+ "name": request.name,
|
|
|
+ "serviceType": "http",
|
|
|
+ "storage": storage
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def lds_uuids_to_grpc(res):
|
|
|
+ uuids = []
|
|
|
+ results = res.get("results")
|
|
|
+ for result in results:
|
|
|
+ uuids.append(result.get("uuid"))
|
|
|
+ return uuids
|
|
|
+
|
|
|
+
|
|
|
def check_error(res, context):
|
|
|
if not res.get('success', True):
|
|
|
match res.get('errorType'):
|
|
@@ -85,6 +115,13 @@ def check_error(res, context):
|
|
|
context.abort(grpc.StatusCode.UNKNOWN, json.dumps(res))
|
|
|
|
|
|
|
|
|
+def check_lds_result(res):
|
|
|
+ if res.get('isEnabled', True):
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
|
|
|
|
|
|
def ListDelivery(self, request, context):
|
|
@@ -123,9 +160,35 @@ class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
|
|
|
return delivery_to_grpc(res)
|
|
|
|
|
|
|
|
|
+class LogDeliveryServiceServicer(llnwLDS_pb2_grpc.LogDeliveryServiceServicer):
|
|
|
+
|
|
|
+ def CreateLDS(self, request, context):
|
|
|
+ print("请求:{}", request)
|
|
|
+ lds_config = lds_config_from_grpc(request)
|
|
|
+ res = get_cl(request.base).create_lds(request.shortname, lds_config).json()
|
|
|
+ print("响应:{}", res)
|
|
|
+ return check_lds_result(res)
|
|
|
+
|
|
|
+ def DeleteLDS(self, request, context):
|
|
|
+ print("请求:{}", request)
|
|
|
+ res = get_cl(request.base).delete_lds(request.shortname, request.uuid).json()
|
|
|
+ print("响应:{}", res)
|
|
|
+ if res.get('success', True):
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|
|
|
+
|
|
|
+ def ListLDS(self, request, context):
|
|
|
+ print("请求:{}", request)
|
|
|
+ res = get_cl(request.base).list_lds(request.shortname).json()
|
|
|
+ print("响应:{}", res)
|
|
|
+ return lds_uuids_to_grpc(res)
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
|
|
llnw_pb2_grpc.add_DeliveryServiceServicer_to_server(DeliveryServiceServicer(), server)
|
|
|
+ llnwLDS_pb2_grpc.add_LogDeliveryServiceServicer_to_server(LogDeliveryServiceServicer(), server)
|
|
|
server.add_insecure_port('[::]:50052')
|
|
|
server.start()
|
|
|
server.wait_for_termination()
|