diff --git a/02-Keyword/tsg_bfapi/PolicyObject.robot b/02-Keyword/tsg_bfapi/PolicyObject.robot index e329e24..30d1164 100644 --- a/02-Keyword/tsg_bfapi/PolicyObject.robot +++ b/02-Keyword/tsg_bfapi/PolicyObject.robot @@ -1,5 +1,6 @@ *** Settings *** Resource ../../03-Variable/BifangApiVariable.txt +Resource ../tsg_common/ManagePolicyBody.robot Library REST http://${host}:${port} Library Collections Library RequestsLibrary @@ -56,6 +57,14 @@ AddPolicy ${rescode} Set Variable ${response['code']} [Return] ${rescode} ${policyId} +AddPolicy2 + [Arguments] ${returnData} ${policyList} + ${body} ParamsOpertion ${returnData} ${policyList} + ${response} BasePostRequest /v1/policy/compile body=${body} + ${policyId} Set Variable ${response['data']['policyList'][0]['policyId']} + ${rescode} Set Variable ${response['code']} + [Return] ${rescode} ${policyId} + EditPolicy [Arguments] @{body} [Return] ${rescode} ${objectId} diff --git a/02-Keyword/tsg_common/LoginAndLogoutTypeSwitch.robot b/02-Keyword/tsg_common/LoginAndLogoutTypeSwitch.robot index 2300e80..cb9000a 100644 --- a/02-Keyword/tsg_common/LoginAndLogoutTypeSwitch.robot +++ b/02-Keyword/tsg_common/LoginAndLogoutTypeSwitch.robot @@ -2,6 +2,7 @@ Resource ../../03-Variable/BifangApiVariable.txt Library REST http://${host}:${port} Library Collections +Library ExtensionLibrary Resource ../tsg_bfapi/LoginLogout.robot Resource ../tsg_ui/Logout/Logout.robot Resource ../tsg_ui/Login/Login.robot @@ -9,8 +10,9 @@ Resource ../tsg_ui/Login/Login.robot *** Keywords *** LoginAndAddLocalIP # 获取主机IP - #${ip} Get Host IP - #Set Global Variable ${testClentIP} ${ip} + ${ip} Get Host IP + Run Keyword If '${ip}' != '${EMPTY}' Set Global Variable ${testClentIP} ${ip} + Run Keyword If '${loginType}' == 'api' ApiLoginAndAddLocalIP aa ... ELSE IF '${loginType}' == 'cli' CliLogin ... ELSE UiLoginAndAaddLocalIP bb diff --git a/02-Keyword/tsg_common/ManagePolicyBody.robot b/02-Keyword/tsg_common/ManagePolicyBody.robot new file mode 100644 index 0000000..b9594a4 --- /dev/null +++ b/02-Keyword/tsg_common/ManagePolicyBody.robot @@ -0,0 +1,251 @@ +*** Settings *** +Library json +Library Collections +Resource ../../03-Variable/BifangApiVariable.txt +Resource ../../02-Keyword/tsg_bfapi/policyobject.robot + +*** Keywords *** +# ... 特殊参数说明: +# ... effectiveRange=Nursurtan|Transtelecom&Astel,Almaty&Nursurtan,|Astel&Transtelecom +# ... 为字符串类型,每条生效区域信息以','分隔 +# ... ①以'|'字符开头表示仅为isp信息,如:effectiveRange=|Astel&Transtelecom +# ... ②非'|'字符开头表示仅为location信息或location和isp信息,如: +# ... effectiveRange=Nursurtan(仅为location信息) +# ... effectiveRange=Nursurtan|Transtelecom&Astel(location和isp信息) +# ... userRegion=protocol:HTTP,keyring:${1} +# ... 为字符串类型,每条动作参数以','分隔 +# ... [key]:[value],... +# ... referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN +# ... 为字符串类型,每条引用的策略对象信息以','分隔 +# ... [objectId]|[protocolFileds],... ,多个protocolFileds用'&'连接 +# ... scheduleId=${3},${1} +# ... 为字符串类型,以','分隔 +# ... appObjectIdArray==${2},${3} +# ... 为字符串类型,以','分隔 +# ... +# ... 注:数字类型变量为${0}、${1}、${...} + +# # 例子 +# ${dict} Create Dictionary + # ... policyName=test_policy_add + # ... policyType=tsg_security + # ... policyDesc=desc + # ... action=intercept + # ... userTags= + # ... doLog=${2} + # ... effectiveRange=Nursurtan|Transtelecom&Astel,Almaty&Nursurtan,|Astel&Transtelecom + # ... userRegion=protocol:HTTP,keyring:${1} + # ... referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN + # ... isValid=${0} + # ... scheduleId=${3},${1} + # ... appObjectIdArray=${2},${3} +# ${rescode} ${policyId} AddPolicy2 ${1} ${dict} + +ParamsOpertion + [Documentation] + ... 描述:入口,opAction参数固定为'add' + ... 参数:returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。 + ... policyList: 必传,字典类型。字典内可选参数信息: + ... action: 必传,无默认值 + ... policyType: 必传,无默认值 + ... policyName: 可选,默认为'test_policy_add' + ... policyDesc: 可选,无默认值 + ... userTags: 可选,无默认值 + ... doLog: 可选,是否生成日志,${0}(不需要),${1}(需要),${2}(只记录结构化日志)。默认为${2} + ... isValid: 可选,${0}(无效),${1}(生效) + ... effectiveRange: 可选 + ... userRegion: 可选 + ... referenceObject: 可选 + ... scheduleId: 可选 + ... appObjectIdArray: 可选 + + [Arguments] ${returnData} ${policyList} + ${emptyList} Create List + # 断言必传参数 + Should Not Be Empty ${policyList}[policyType] + Should Not Be Empty ${policyList}[action] + # 取可选参数,为传入设置默认值 + ${return} ${policyName} Run Keyword And Ignore Error Set Variable ${policyList}[policyName] + Run Keyword If "${return}"=="FAIL" or "${policyName}"=="${EMPTY}" Set To Dictionary ${policyList} policyName=test_policy_add + ${return} ${policyDesc} Run Keyword And Ignore Error Set Variable ${policyList}[policyDesc] + Run Keyword If "${return}"=="FAIL" or "${policyDesc}"=="${EMPTY}" Set To Dictionary ${policyList} policyDesc= + ${return} ${userTags} Run Keyword And Ignore Error Set Variable ${policyList}[userTags] + Run Keyword If "${return}"=="FAIL" or "${userTags}"=="${EMPTY}" Set To Dictionary ${policyList} userTags= + ${return} ${doLog} Run Keyword And Ignore Error Set Variable ${policyList}[doLog] + Run Keyword If "${return}"=="FAIL" or "${doLog}"=="${EMPTY}" Set To Dictionary ${policyList} doLog=${2} + ${return} ${isValid} Run Keyword And Ignore Error Set Variable ${policyList}[isValid] + Run Keyword If "${return}"=="FAIL" or "${isValid}"=="${EMPTY}" Set To Dictionary ${policyList} isValid=${0} + ${return} ${scheduleId} Run Keyword And Ignore Error Set Variable ${policyList}[scheduleId] + ${scheduleIdV} Run Keyword If "${return}"=="FAIL" or "${scheduleId}"=="${EMPTY}" Set Variable ${emptyList} + ... ELSE Evaluate re.split('[,]', '${scheduleId}') re + ${return} ${appObjectIdArray} Run Keyword And Ignore Error Set Variable ${policyList}[appObjectIdArray] + ${appObjectIdArrayV} Run Keyword If "${return}"=="FAIL" or "${appObjectIdArray}"=="${EMPTY}" Set Variable ${emptyList} + ... ELSE Evaluate re.split('[,]', '${appObjectIdArray}') re + Set To Dictionary ${policyList} scheduleId=${scheduleIdV} + Set To Dictionary ${policyList} appObjectIdArray=${appObjectIdArrayV} + # 固定值 + Set To Dictionary ${policyList} policyId= doBlacklist=${0} + # 分别处理EffectiveRange、UserRegion和ReferenceObject参数 + ManageEffectiveRange ${policyList} + ManageUserRegion ${policyList} + ManageReferenceObject ${policyList} + # 转为json结构并返回 + ${dict} Create Dictionary opAction=add returnData=${returnData} policyList=${policyList} + ${json} json.Dumps ${dict} ensure_ascii=False + Log ${json} + [Return] ${json} + +ManageEffectiveRange + # 处理EffectiveRange + [Arguments] ${policyList} + ${emptyList} Create List + ${emptyListDouble} Create List ${emptyList} + + ${return} ${effectiveRange} Run Keyword And Ignore Error Set Variable ${policyList}[effectiveRange] + ${value} Run Keyword If "${return}"=="FAIL" or "${effectiveRange}"=="${EMPTY}" Create Dictionary tag_sets=${emptyListDouble} + ... ELSE AnalysisEffectiveRange ${effectiveRange} + Set To Dictionary ${policyList} effectiveRange=${value} + +AnalysisEffectiveRange + # 解析EffectiveRange,区分值类型 + [Arguments] ${effectiveRange} + ${value} Create List + ${list} Evaluate re.split('[,]', '${effectiveRange}') re + FOR ${var} IN @{list} + ${param} Run Keyword If '${var}'.startswith('|') EffectiveRangeValueType1 ${var} + ... ELSE IF '|' in '${var}' EffectiveRangeValueType2 ${var} + ... ELSE EffectiveRangeValueType3 ${var} + Append To List ${value} ${param} + END + ${obj} Create Dictionary tag_sets=${value} + [Return] ${obj} + +EffectiveRangeValueType1 + # EffectiveRange值类型1,仅为isp信息 + [Arguments] ${str} + # #Astel + ${value} Evaluate '${str}'[1:] + + ${valueList} Create List + ${list} Evaluate re.split('[&]', '${value}') re + FOR ${var} IN @{list} + Append To List ${valueList} ${var} + END + ${obj} Create Dictionary tag=isp value=${valueList} + ${return} Create List ${obj} + [Return] ${return} + +EffectiveRangeValueType2 + # EffectiveRange值类型2,location|isp都包含 + [Arguments] ${str} + # Nursurtan#Transtelecom + ${list} Evaluate re.split('[|]', '${str}') re + # Location + ${valueList1} Create List + ${list1} Evaluate re.split('[&]', '${list}[0]') re + FOR ${var} IN @{list1} + Append To List ${valueList1} ${var} + END + ${obj1} Create Dictionary tag=location value=${valueList1} + # ISP + ${valueList2} Create List + ${list2} Evaluate re.split('[&]', '${list}[1]') re + FOR ${var} IN @{list2} + Append To List ${valueList2} ${var} + END + ${obj2} Create Dictionary tag=isp value=${valueList2} + + ${return} Create List ${obj1} ${obj2} + [Return] ${return} + +EffectiveRangeValueType3 + # EffectiveRange值类型3,仅为location信息 + [Arguments] ${str} + # Almaty + ${valueList} Create List + ${list} Evaluate re.split('[&]', '${str}') re + FOR ${var} IN @{list} + Append To List ${valueList} ${var} + END + ${obj} Create Dictionary tag=location value=${valueList} + ${return} Create List ${obj} + [Return] ${return} + +ManageUserRegion + [Arguments] ${policyList} + ${emptyObj} Create Dictionary + + ${return} ${userRegion} Run Keyword And Ignore Error Set Variable ${policyList}[userRegion] + ${value} Run Keyword If "${return}"=="FAIL" or "${userRegion}"=="${EMPTY}" Create Dictionary + ... ELSE AnalysisUserRegion ${userRegion} + Set To Dictionary ${policyList} userRegion=${value} + + Log ${policyList}[userRegion] + +AnalysisUserRegion + [Arguments] ${userRegion} + # protocol:HTTP,keyring:1 + ${obj} Create Dictionary + ${list} Evaluate re.split('[,]', '${userRegion}') re + FOR ${var} IN @{list} + ${k} Evaluate '${var}'[0:'${var}'.find(':')] + ${v} Evaluate '${var}'['${var}'.find(':')+1:] + ${v} Run Keyword If '${v}'.isdigit() Evaluate int('${v}') + ... ELSE Set Variable ${v} + Set To Dictionary ${obj} ${k}=${v} + END + [Return] ${obj} + +ManageReferenceObject + # 处理引用对象字符串 + [Arguments] ${policyList} + ${emptyList} Create List + ${clientHostObj} Run Keyword If '${addTestClentIPFlag}'=='1' ManageClientObject + Run Keyword If "${clientHostObj}"!="${None}" Append To List ${emptyList} ${clientHostObj} + + ${defaultObj} Create Dictionary + ${return} ${referenceObject} Run Keyword And Ignore Error Set Variable ${policyList}[referenceObject] + ${value} Run Keyword If "${return}"=="FAIL" or "${referenceObject}"=="${EMPTY}" Create List ${defaultObj} + ... ELSE AnalysisReferenceObject ${referenceObject} + # 若参数为空,设置默认结构 + FOR ${var} IN @{value} + Run Keyword If ${var}!={} Append To List ${emptyList} ${var} + END + Set To Dictionary ${policyList} referenceObject=${emptyList} + +ManageClientObject + # 增加引用测试终端IP策略对象 + ${emptyList} Create List + ${obj} Create Dictionary objectId=${testClentID} protocolFields=${emptyList} + [Return] ${obj} + + +AnalysisReferenceObject + # 解析引用对象 + [Arguments] ${referenceObject} + ${objList} Create List + ${list} Evaluate re.split('[,]', '${referenceObject}') re + FOR ${var} IN @{list} + ${emptyList} Create List + ${emptyObj} Create Dictionary + ${key} Evaluate '${var}'[0:'${var}'.find('|')] + ${values} Evaluate '${var}'['${var}'.find('|')+1:] + ${valueList} InnerForEach ${values} + ${key} Run Keyword If '${key}'.isdigit() Evaluate int('${key}') + ... ELSE Set Variable ${key} + Set To Dictionary ${emptyObj} objectId=${key} + Set To Dictionary ${emptyObj} protocolFields=${valueList} + Append To List ${objList} ${emptyObj} + END + [Return] ${objList} + +InnerForEach + # 循环嵌套 + [Arguments] ${values} + ${emptyList} Create List + ${list} Evaluate re.split('[&]', '${values}') re + FOR ${var} IN @{list} + Append To List ${emptyList} ${var} + END + [Return] ${list} + diff --git a/04-CustomLibrary/ExtensionPackages/ExtensionLibrary/mytool.py b/04-CustomLibrary/ExtensionPackages/ExtensionLibrary/mytool.py index c399544..03de99d 100644 --- a/04-CustomLibrary/ExtensionPackages/ExtensionLibrary/mytool.py +++ b/04-CustomLibrary/ExtensionPackages/ExtensionLibrary/mytool.py @@ -6,11 +6,21 @@ class mytool(): def __init__(self): pass - def get_host_IP(self): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(('8.8.8.8', 80)) - ip = s.getsockname()[0] - finally: - s.close() + def get_host_IP(self, flag="50"): + #try: + #s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + #s.connect(('8.8.8.8', 80)) + #ip = s.getsockname()[0] + #finally: + #s.close() + hostname = socket.gethostname() + ipTriple = socket.gethostbyname_ex(hostname) + ips = list(ipTriple[2:]) + ip = "" + for i in ips: + ipList = list(i) + for ip1 in ipList: + if "."+flag+"." in ip1: + ip = ip1 + break return ip \ No newline at end of file