Browse Source

LDS grpc 接口实现

ChenHaolong 1 year ago
parent
commit
436824a798
4 changed files with 107 additions and 5 deletions
  1. 1 1
      proto
  2. 1 0
      src/gencode.py
  3. 63 0
      src/main.py
  4. 42 4
      测试.ipynb

+ 1 - 1
proto

@@ -1 +1 @@
-Subproject commit 90ba9c57e09221bc77d79aea0dbeae31733e0b26
+Subproject commit 2209526c85110743b49c506bce2e4b636b3dd46e

+ 1 - 0
src/gencode.py

@@ -8,4 +8,5 @@ if __name__ == '__main__':
         '--pyi_out=.',
         '--grpc_python_out=.',
         '../proto/llnw.proto',
+        '../proto/llnwLDS.proto',
     ))

+ 63 - 0
src/main.py

@@ -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()

+ 42 - 4
测试.ipynb

@@ -24,7 +24,7 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "{'version': '22.12-RC1', 'timestamp': '2022-11-22T10:24:34+0000', 'mysqlConnection': {'connectionTime': '1'}, 'cfgMgmtConnection': {'available': 'true'}, 'remoteComponentsConnection': {'available': 'true'}, 'serviceDbConnection': {'available': 'true'}}\n"
+      "{'version': '22.12-RC1', 'timestamp': '2022-11-22T10:24:34+0000', 'mysqlConnection': {'connectionTime': '0'}, 'cfgMgmtConnection': {'available': 'true'}, 'remoteComponentsConnection': {'available': 'true'}, 'serviceDbConnection': {'available': 'true'}}\n"
      ]
     }
    ],
@@ -403,9 +403,47 @@
   },
   {
    "cell_type": "code",
-   "execution_count": null,
-   "outputs": [],
-   "source": [],
+   "execution_count": 34,
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "{'results': [], 'size': 'all', 'offset': 0, 'page': 1, 'total': 0, 'sort': '_id', 'sort.dir': 'ascending', '$prev': '', '$next': ''}\n"
+     ]
+    }
+   ],
+   "source": [
+    "from ll_sdk.config_api import ConfigApiClient\n",
+    "username = \"chl20000826@163.com\"\n",
+    "shared_key = \"0f4912881c27a33dcfcf2ab6e2dc286c065a559000926bc9d1fc25d4228bb5cd\"\n",
+    "short_name = \"centtest\"\n",
+    "cl = ConfigApiClient('apis.llnw.com', username, shared_key)\n",
+    "\n",
+    "storage = {\n",
+    "    \"accessKey\": \"AKIASB76IRYZX5KCSXMB\",\n",
+    "    \"bucketName\": \"cengcloud-dev\",\n",
+    "    \"location\": \"amazon_s3\",\n",
+    "    \"path\": \"223-0303.chl.tsdb.work/edgio/\",\n",
+    "    \"region\": \"ap-southeast-1\",\n",
+    "    \"secretKey\": \"PXgElj+slvVII8H0DKoMtM2rvmr34mxegP+HHRsb\"\n",
+    "}\n",
+    "\n",
+    "config = {\n",
+    "    \"compressionType\": \"lz4\",\n",
+    "    \"isActive\": \"true\",\n",
+    "    \"logFields\": [\"sc-request-id\",\"s-host\",\"datetime\",\"c-country-code\",\"sc-status\",\"c-ip\",\"cs-uri\",\"sc-bytes\"],\n",
+    "    \"name\": \"test032101\",\n",
+    "    \"serviceType\": \"http\",\n",
+    "    \"storage\": storage\n",
+    "}\n",
+    "\n",
+    "uuid = \"97a3db7d-86b2-4302-92cb-3fc7bfa81a60\"\n",
+    "\n",
+    "# print(cl.create_lds(shortname=short_name, config={\"body\":config}).json())\n",
+    "# print(cl.list_lds(shortname=short_name).json())\n",
+    "# print(cl.delete_lds(shortname=short_name, uuid=uuid).json())"
+   ],
    "metadata": {
     "collapsed": false
    }