|
@@ -101,9 +101,12 @@ def lds_config_from_grpc(request):
|
|
|
def lds_uuids_to_grpc(res):
|
|
|
uuids = []
|
|
|
results = res.get("results")
|
|
|
- for result in results:
|
|
|
- uuids.append(result.get("uuid"))
|
|
|
- return uuids
|
|
|
+ if results is None:
|
|
|
+ return ListLDSResponse(uuids=uuids)
|
|
|
+ else:
|
|
|
+ for result in results:
|
|
|
+ uuids.append(result.get("uuid"))
|
|
|
+ return ListLDSResponse(uuids=uuids)
|
|
|
|
|
|
|
|
|
def check_error(res, context):
|
|
@@ -115,11 +118,22 @@ 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
|
|
|
+def check_lds_create_result(res):
|
|
|
+ if res.get('isEnabled') is None:
|
|
|
+ return CreateLDSResponse(isSuccess=False)
|
|
|
else:
|
|
|
- return False
|
|
|
+ if res.get('isEnabled') is True:
|
|
|
+ return CreateLDSResponse(isSuccess=True)
|
|
|
+ else:
|
|
|
+ return CreateLDSResponse(isSuccess=False)
|
|
|
+
|
|
|
+
|
|
|
+def check_lds_delete_result(res_code):
|
|
|
+ print("响应码:", res_code.status_code)
|
|
|
+ if res_code.status_code == 200:
|
|
|
+ return DeleteLDSResponse(isSuccess=True)
|
|
|
+ else:
|
|
|
+ return DeleteLDSResponse(isSuccess=False)
|
|
|
|
|
|
|
|
|
class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
|
|
@@ -163,26 +177,37 @@ class DeliveryServiceServicer(llnw_pb2_grpc.DeliveryServiceServicer):
|
|
|
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)
|
|
|
+ print("CreateLDS")
|
|
|
+ print("请求:", request)
|
|
|
+ try:
|
|
|
+ lds_config = lds_config_from_grpc(request)
|
|
|
+ res = get_cl(request.base).create_lds(shortname=request.shortName, config={"body": lds_config}).json()
|
|
|
+ print("响应:", res)
|
|
|
+ except:
|
|
|
+ return CreateLDSResponse(isSuccess=False)
|
|
|
+ else:
|
|
|
+ return check_lds_create_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
|
|
|
+ print("DeleteLDS")
|
|
|
+ print("请求:", request)
|
|
|
+ try:
|
|
|
+ res_code = get_cl(request.base).delete_lds(shortname=request.shortName, uuid=request.uuid)
|
|
|
+ except:
|
|
|
+ return DeleteLDSResponse(isSuccess=False)
|
|
|
else:
|
|
|
- return False
|
|
|
+ return check_lds_delete_result(res_code)
|
|
|
|
|
|
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)
|
|
|
+ print("ListLDS")
|
|
|
+ print("请求:", request)
|
|
|
+ try:
|
|
|
+ res = get_cl(request.base).list_lds(shortname=request.shortName).json()
|
|
|
+ print("响应:", res)
|
|
|
+ except:
|
|
|
+ return ListLDSResponse(uuids=[])
|
|
|
+ else:
|
|
|
+ return lds_uuids_to_grpc(res)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|