diff --git a/02-Keyword/tsg_bfapi/policy_object/ProcessPolicyBody.robot b/02-Keyword/tsg_bfapi/policy_object/ProcessPolicyBody.robot index fec8db4..884d8cb 100644 --- a/02-Keyword/tsg_bfapi/policy_object/ProcessPolicyBody.robot +++ b/02-Keyword/tsg_bfapi/policy_object/ProcessPolicyBody.robot @@ -1,519 +1,519 @@ -*** Settings *** -Library json -Library Collections -Resource ../../../03-Variable/BifangApiVariable.txt -Resource ../../../03-Variable/PolicyObjectDefault.txt -Resource ../../tsg_common/ManagePolicyBody.robot - -*** Variables *** -#arraySplitor:数组元素之间的分隔符 -${arraySplitor} , -#protocolFiledSplitor:protocolFiled之间的分隔符 -${protocolFiledSplitor} & -#objectIdSplitor:objectId之间的分隔符 -${objectIdSplitor} : -#idFiledSplitor: objectId和protocolFiled之间的分隔符 -${idFiledSplitor} | - -*** Keywords *** -PolicyListDataOperation - [Arguments] ${returnData} ${policyList} ${opAction} ${policyObjectVersion} - [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' - ... 参数:returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。 - ... policyList: 必传,字典类型。字典内可选参数信息: - ... policyObjectVersion: 必传,v1,v2,涉及策略版本 - ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt - ... 多个策略处理 - # ... 特殊参数说明: - # ... 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],... - # ... 在v1版本中referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN - # ... 为字符串类型,每条引用的策略对象信息以','分隔 - # ... [objectId]|[protocolFileds],... ,多个protocolFileds用'&'连接 - # ... scheduleId=${3},${1} - # ... 为字符串类型,以','分隔 - # ... 在v1版本接口中appObjectIdArray==${2},${3} - # ... 在v2版本接口中appIdObjects==${2},${3} - # ... 为字符串类型,以','分隔 - # ... - # ... 注:数字类型变量为${0}、${1}、${...} - # ... 在v2版本中 无referenceObject,取而代之的是source,destination,filterList - # ... source= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI - # ... destination= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI - # ... filterList= ${265}:${257}|TSG_FIELD_HTTP_HOST,${266}:${299}|TSG_FIELD_SSL_SNI - # ... filterList由多个数字用‘:’分隔,加上|对上对应的protocolFiled, - # ... 一个filter中的protocolFiled是确定的,一个protocolFiled对应多个objectId - Log To Console Call PolicyListDataOperation - ${policyListJson} Set Variable [ - FOR ${policy} IN @{policyList} - ${json} PolicyOrganize ${policy} ${opAction} ${policyObjectVersion} - ${policyListJson} Set Variable ${policyListJson}${json}, - END - ${policyListJson}= Replace String Using Regexp ${policyListJson} ,$ ] - # 转为json结构并返回 - ${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList - ${bodyJson} json.Dumps ${dict} ensure_ascii=False - ${json} Replace String ${bodyJson} "replace:policyList" ${policyListJson} - Log Policy_Request_Body-${json} - [Return] ${json} - -PolicyDataOperation - [Arguments] ${returnData} ${policy} ${opAction} ${policyObjectVersion} - [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' - ... 参数:returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。 - ... policy: 必传,字典类型。字典内可选参数信息: - ... policyObjectVersion: 必传,v1,v2,涉及策略版本 - ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt - ... 单个策略处理 - Log To Console Call PolicyDataOperation - ${json} PolicyOrganize ${policy} ${opAction} ${policyObjectVersion} - # 转为json结构并返回 - ${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList - ${bodyJson} json.Dumps ${dict} ensure_ascii=False - ${json} Replace String ${bodyJson} "replace:policyList" ${json} - Log Policy_Request_Body-${json} - [Return] ${json} - -PolicyOrganize - [Arguments] ${policy} ${opAction} ${policyObjectVersion} - [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' - ... policy: 必传,字典类型。字典内可选参数信息: - ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt - ... policyObjectVersion: 必传,v1,v2,涉及策略版本 - ... 单个策略转换为json - Log To Console Call PolicyOrganize - ${returnDict} Create Dictionary - ${emptyList} Create List - #v1版本接口与v2版本接口不同的字段 - Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} source - Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} destination - Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} filterList - Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} appIdObjects - Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} referenceObject - Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} appObjectIdArray - #更新时 policyId不允许为空 - ${return} ${policyId} Run Keyword And Ignore Error Get From Dictionary ${policy} policyId - Run Keyword If "${opAction}"=="update" and "${return}"=="FAIL" Fail policyId is required when opAction is update - Run Keyword If "${opAction}"=="update" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is update - Run Keyword If "${opAction}"=="disable" and "${return}"=="FAIL" Fail policyId is required when opAction is disable - Run Keyword If "${opAction}"=="disable" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is disable - Run Keyword If "${opAction}"=="enable" and "${return}"=="FAIL" Fail policyId is required when opAction is enable - Run Keyword If "${opAction}"=="enable" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is enable - ${policyId} Run Keyword If "${opAction}"=="add" Set Variable ${EMPTY} - ... ELSE Get From Dictionary ${policy} policyId - Set To Dictionary ${returnDict} policyId=${policyId} - ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType - Run Keyword If "${return}"=="FAIL" or "${policyType}"=="${EMPTY}" Set To Dictionary ${returnDict} policyType=${Default_PolicyType} - ... ELSE Set To Dictionary ${returnDict} policyType=${policyType} - #enable disable直接返回json - Run Keyword And Return If "${opAction}"=="enable" or "${opAction}"=="disable" ManagePolicyEnableAndDisable ${returnDict} ${opAction} - # 取可选参数,为传入设置默认值 - ${return} ${policyName} Run Keyword And Ignore Error Get From Dictionary ${policy} policyName - Run Keyword If "${return}"=="FAIL" or "${policyName}"=="${EMPTY}" Set To Dictionary ${returnDict} policyName=${Default_PolicyName} - ... ELSE Set To Dictionary ${returnDict} policyName=${policyName} - #policyDesc 不是必填的选项 - ${return} ${policyDesc} Run Keyword And Ignore Error Get From Dictionary ${policy} policyDesc - Run Keyword If "${return}"=="FAIL" or "${policyDesc}"=="${EMPTY}" Set To Dictionary ${returnDict} policyDesc=${Default_PolicyDesc} - ... ELSE Set To Dictionary ${returnDict} policyDesc=${policyDesc} - ${return} ${action} Run Keyword And Ignore Error Get From Dictionary ${policy} action - Run Keyword If "${return}"=="FAIL" or "${action}"=="${EMPTY}" Set To Dictionary ${returnDict} action=${Default_Action} - ... ELSE Set To Dictionary ${returnDict} action=${action} - #userTags 不是必填的选项 - ${return} ${userTags} Run Keyword And Ignore Error Get From Dictionary ${policy} userTags - Run Keyword If "${return}"=="FAIL" or "${userTags}"=="${EMPTY}" Set To Dictionary ${returnDict} userTags=${Default_UserTags} - ... ELSE Set To Dictionary ${returnDict} userTags=${userTags} - ${return} ${doLog} Run Keyword And Ignore Error Get From Dictionary ${policy} doLog - Run Keyword If "${return}"=="FAIL" or "${doLog}"=="${EMPTY}" Set To Dictionary ${returnDict} doLog=${Default_DoLog} - ... ELSE Set To Dictionary ${returnDict} doLog=${doLog} - ${return} ${isValid} Run Keyword And Ignore Error Get From Dictionary ${policy} isValid - Run Keyword If "${return}"=="FAIL" or "${isValid}"=="${EMPTY}" Set To Dictionary ${returnDict} isValid=${Default_IsValid} - ... ELSE Set To Dictionary ${returnDict} isValid=${isValid} - ${return} ${scheduleId} Run Keyword And Ignore Error Get From Dictionary ${policy} scheduleId - ${scheduleIdV} Run Keyword If "${return}"!="FAIL" and "${scheduleId}"!="${EMPTY}" and "${scheduleId}"!="${None}" Evaluate list(map(int, re.split('[,]', '${scheduleId}'))) re - ... ELSE Set Variable ${emptyList} - Set To Dictionary ${returnDict} scheduleId=${scheduleIdV} - ${return} ${appObjectIdArray} Run Keyword If "${policyObjectVersion}"=="v1" Run Keyword And Ignore Error Get From Dictionary ${policy} appObjectIdArray - ... ELSE Run Keyword And Ignore Error Get From Dictionary ${policy} appIdObjects - ${appObjectIdArrayV} Run Keyword If "${return}"=="FAIL" or "${appObjectIdArray}"=="${EMPTY}" Evaluate list(map(int, re.split('[,]', '${Default_AppObjectIdArray}'))) re - ... ELSE Evaluate list(map(int, re.split('[,]', '${appObjectIdArray}'))) re - Run Keyword If "${policyObjectVersion}"=="v1" Set To Dictionary ${returnDict} appObjectIdArray=${appObjectIdArrayV} - ... ELSE IF "${policyObjectVersion}"=="v2" Set To Dictionary ${returnDict} appIdObjects=${appObjectIdArrayV} - # 固定值 - Set To Dictionary ${returnDict} doBlacklist=${0} - # 分别处理EffectiveRange、UserRegion和ReferenceObject参数 - ProcessEffectiveRange ${policy} ${returnDict} - #WxManageUserRegion ${policy} - Run Keyword If "${policyObjectVersion}"=="v1" ProcessReferenceObject ${policy} ${returnDict} - ... ELSE IF "${policyObjectVersion}"=="v2" ManageObjectV2 ${policy} ${returnDict} - #先取出useragion 再放回json - ${return} ${userRegion} Run Keyword And Ignore Error Get From Dictionary ${policy} userRegion - Set To Dictionary ${returnDict} userRegion=${Default_UserRegion} - ${userRegion} Run Keyword If "${return}"=="FAIL" or '${userRegion}'=="${EMPTY}" Set Variable "" - ... ELSE Set Variable ${userRegion} - #针对主动防御特殊处理,主动防御不应用对象 - ${active_defence_emptyList} Create List - ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType - Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} referenceObject=${active_defence_emptyList} - Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} appObjectIdArray=${active_defence_emptyList} - #v版本主动防御 - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} source=${active_defence_emptyList} - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} destination=${active_defence_emptyList} - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} filterList=${active_defence_emptyList} - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} appIdObjects=${active_defence_emptyList} - #针对WAN NAT特殊处理,主动防御不应用对象 - ${wannat_emptyList} Create List - ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="wannat" Set To Dictionary ${returnDict} appIdObjects=${wannat_emptyList} - Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="wannat" Set To Dictionary ${returnDict} filterList=${wannat_emptyList} - # 转为json结构并返回 - ${json} json.Dumps ${returnDict} ensure_ascii=False - ${json} Replace String ${json} "method:rst" ${userRegion} - #${json} Replace String ${json} "effectiveRange:Empty" {} - Log Policy-${json} - [Return] ${json} - -ManagePolicyEnableAndDisable - [Arguments] ${returnDict} ${opAction} - [Documentation] 启用与禁用时调用 - Run Keyword If "${opAction}"=="enable" Set To Dictionary ${returnDict} isValid=1 - Run Keyword If "${opAction}"=="disable" Set To Dictionary ${returnDict} isValid=0 - ${json} json.Dumps ${returnDict} ensure_ascii=False - Log Policy-${json} - [Return] ${json} - -ProcessEffectiveRange - [Arguments] ${policyList} ${returnDict} - [Documentation] 处理EffectiveRange - ... 加入effectiveRange={}的处理支持 - Log To Console Call ProcessEffectiveRange - ${emptyList} Create List - ${emptyListDouble} Create List ${emptyList} - ${return} ${effectiveRange} Run Keyword And Ignore Error Get From Dictionary ${policyList} effectiveRange - ${effectiveRange} Run Keyword If "${return}"=="FAIL" or "${effectiveRange}"=="${EMPTY}" Set Variable ${Default_EffectiveRange} - ... ELSE Set Variable ${effectiveRange} - ${value} Run Keyword If "${effectiveRange}"=="${EMPTY}" Create Dictionary tag_sets=${emptyListDouble} - ... ELSE IF "${effectiveRange}"=="{}" Create Dictionary - ... ELSE AnalysisEffectiveRange ${effectiveRange} - Set To Dictionary ${returnDict} effectiveRange=${value} - -ProcessReferenceObject - [Arguments] ${policy} ${returnDict} - [Documentation] 处理引用对象字符串 - ... v1版本接口使用 - Log To Console Call ProcessReferenceObject - ${emptyList} Create List - #默认客户端条件类型:clientip or clientsubid - ${return} ${defaultClient} Run Keyword And Ignore Error Get From Dictionary ${policy} defalutClient - ${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type} - ... ELSE Set Variable ${defaultClient} - Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObject ${emptyList} - Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObject ${emptyList} - #Run Keyword If '${addTestClentIPFlag}'=='1' ManageClientObject ${emptyList} - # Run Keyword If "${clientHostObj}"!="${None}" Append To List ${emptyList} ${clientHostObj} - ${defaultObj} Create Dictionary - ${return} ${referenceObject} Run Keyword And Ignore Error Get From Dictionary ${policy} referenceObject - ${referenceObject} Run Keyword If "${return}"=="FAIL" or "${referenceObject}"=="${EMPTY}" Set Variable ${Default_ReferenceObject} - ... ELSE Set Variable ${referenceObject} - ${value} Run Keyword If "${referenceObject}"=="${EMPTY}" Create List ${defaultObj} - ... ELSE AnalysisReferenceObject ${referenceObject} - # 若参数为空,设置默认结构 - # FOR ${var} IN @{value} - # Run Keyword If ${var}!={} Append To List ${referenceObject} ${var} - # END - ObjectLoop ${value} ${emptyList} - Set To Dictionary ${returnDict} referenceObject=${emptyList} - -ObjectLoop - [Arguments] ${value} ${valueList} - # 若参数为空,设置默认结构 - FOR ${var} IN @{value} - Run Keyword If ${var}!={} Append To List ${valueList} ${var} - END - [Return] ${valueList} - -ManageObjectV2 - [Arguments] ${policy} ${returnDict} - [Documentation] 处理引用对象字符串 - ... v2版本接口使用 - ... source,destination,filterList - # 处理引用对象字符串 - Log To Console Call ManageObjectV2 - #默认客户端条件类型:clientip or clientsubid - ${return} ${defaultClient} Run Keyword And Ignore Error Get From Dictionary ${policy} defalutClient - ${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type} - ... ELSE Set Variable ${defaultClient} - ${sourceList} Create List - ${destinationList} Create List - ${filterList} Create List - Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObjectV2 ${sourceList} - Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObjectV2 ${sourceList} - AnalysisObjectV2 ${policy} source ${sourceList} - AnalysisObjectV2 ${policy} destination ${destinationList} - AnalysisObjectV2 ${policy} filterList ${filterList} - Set To Dictionary ${returnDict} source=${sourceList} destination=${destinationList} filterList=${filterList} - Log ${policy} - -ManageClientIPObjectV2 - [Arguments] ${emptyList} - # 增加引用测试终端IP策略对象 - ${obj1} Create Dictionary objectId=${testClentID} protocolField=TSG_SECURITY_SOURCE_ADDR - Append To List ${emptyList} ${obj1} - -ManageClientSubIDObjectV2 - [Arguments] ${emptyList} - # 增加引用测试终端IP策略对象 - ${protocolField} Set Variable - ${obj1} Create Dictionary objectId=${testClentSubID} protocolField=${protocolField} - Append To List ${emptyList} ${obj1} - -AnalysisObjectV2 - [Arguments] ${policy} ${objectType} ${objList} - Log To Console Call AnalysisObjectV2 - ${policyObjectDefault} Run Keyword If "${objectType}"=="source" Set Variable ${EMPTY} - ... ELSE IF "${objectType}"=="destination" Set Variable ${EMPTY} - ... ELSE IF "${objectType}"=="filterList" Set Variable ${EMPTY} - ${return} ${objectList} Run Keyword And Ignore Error Get From Dictionary ${policy} ${objectType} - # source,destination,filterList - ${objectList} Run Keyword If "${return}"=="FAIL" or "${objectList}"=="${EMPTY}" Set Variable ${policyObjectDefault} - ... ELSE Set Variable ${objectList} - ${list} Evaluate re.split('[${arraySplitor}]', '${objectList}') re - FOR ${var} IN @{list} - ${tempObj} Run Keyword If "${objectType}"=="filterList" and "${var}" != "" ManageFilter ${var} - ... ELSE IF "${var}" != "" ManageObject ${var} - ... ELSE Set Variable ${EMPTY} - Run Keyword If "${tempObj}" != "${EMPTY}" Append To List ${objList} ${tempObj} - END - Log ${objList} - -ManageObject - [Arguments] ${object} - [Documentation] source,destination处理 - Log To Console Call ManageObject - ${objectList} Create List - #截取objectId - ${objectId} Evaluate '${object}'[0:'${object}'.find('${idFiledSplitor}')] - #截取protocolField - ${protocolField} Evaluate '${object}'['${object}'.find('${idFiledSplitor}')+1:] - ${tempObj} Create Dictionary - ${objectId} Run Keyword If '${objectId}'.isdigit() Evaluate int('${objectId}') - ... ELSE Set Variable ${objectId} - Set To Dictionary ${tempObj} objectId=${objectId} - Set To Dictionary ${tempObj} protocolField=${protocolField} - [Return] ${tempObj} - -ManageFilter - [Arguments] ${filter} - [Documentation] filterList处理 - Log Call ManageFilter - ${filterObjectList} Create List - #截取objectId - ${objectIds} Evaluate '${filter}'[0:'${filter}'.find('${idFiledSplitor}')] - #截取protocolField - ${protocolField} Evaluate '${filter}'['${filter}'.find('${idFiledSplitor}')+1:] - #遍历 objectId - ${list} Evaluate re.split('[${objectIdSplitor}]', '${objectIds}') re - Log list ${list} - FOR ${var} IN @{list} - ${tempObj} Create Dictionary - ${var} Run Keyword If '${var}'.isdigit() Evaluate int('${var}') - ... ELSE Set Variable ${var} - Set To Dictionary ${tempObj} objectId=${var} - Set To Dictionary ${tempObj} protocolField=${protocolField} - Append To List ${filterObjectList} ${tempObj} - END - ${filterObject} Create Dictionary filter=${filterObjectList} - [Return] ${filterObject} - -Get-PolicyIds - [Arguments] ${value} - [Documentation] 获取结果中的策略ID - ${policyIds} Create List - FOR ${policy} IN @{value['data']['policyList']} - #${policyIdList} Create List ${policy['policyId']} - #${policyObj} Create Dictionary policyType=${policy['policyType']} policyIds=${policyIdList} - #Append To List ${policyIds} ${policyObj} - Append To List ${policyIds} ${policy['policyId']} - END - ${len} Get Length ${policyIds} - ${policyIds} Run Keyword If ${len}==1 Set Variable ${policyIds[0]} - ... ELSE Set Variable ${policyIds} - [Return] ${policyIds} - -DeletePolicyWithParamDict - [Arguments] ${params} - [Documentation] 删除策略 - ... params,传入的删除字典 - ... 结构为policyIds=${policyIds} (List) policyType=${policyType} (variable) - ${response} BaseDeleteRequest /${version}${policyUrl} ${params} - ${rescode} Set Variable ${response['code']} - Should Be Equal As Strings ${rescode} 200 - [Return] ${rescode} - -VerifyPolicyList - [Arguments] ${verifyList} - [Documentation] 策略验证 - ... 多个策略验证 - Log To Console Call VerifyPolicyList - ${verifyListJson} Set Variable [ - FOR ${verifyObj} IN @{verifyList} - ${json} VerifyPolicyObj ${verifyObj} - ${verifyListJson} Set Variable ${verifyListJson}${json}, - END - ${verifyListJson}= Replace String Using Regexp ${verifyListJson} ,$ ] - Log Policy_Request_Body-${verifyListJson} - [Return] ${verifyListJson} - -VerifyPolicyObj - [Arguments] ${verifyObj} - [Documentation] 策略验证 - ... 单个策略验证 - Log To Console Call VerifyPolicyObj - ${emptyList} Create List - ${return} ${policyType} Run Keyword And Ignore Error Set Variable ${verifyObj['policyType']} - #Should Not Be Empty ${policyType} - ${verifySession} Get From Dictionary ${verifyObj} verifySession - Should Not Be Empty ${verifySession} - ${attributes} Get From Dictionary ${verifySession} attributes - Should Not Be Empty ${attributes} - FOR ${attribute} IN @{attributes} - ${attributeType} Get From Dictionary ${attribute} attributeType - ${return} ${tableName} Run Keyword And Ignore Error Set Variable ${attribute['tableName']} - ${return} ${protocol} Run Keyword And Ignore Error Set Variable ${attribute['protocol']} - ${attributeName} Get From Dictionary ${attribute} attributeName - ${attributeValue} Get From Dictionary ${attribute} attributeValue - #将json转换为dict - ${attributeValue} Convert To String ${attributeValue} - ${start} Get Regexp Matches ${attributeValue} ^{\" - ${start1} Get Regexp Matches ${attributeValue} ^{\' - ${len} Get Length ${start} - ${len1} Get Length ${start1} - ${attributeValue} Run Keyword If ${len}>0 Evaluate eval('${attributeValue}') - ... ELSE IF ${len1}>0 Evaluate eval("${attributeValue}") - ... ELSE Set Variable ${attributeValue} - ${type} Run Keyword If ${len}>0 or ${len1}>0 Set Variable dict - ... ELSE Set Variable str - ${attributeValue} Run Keyword If "${attributeType}"=="ip" ManageIpAttributeValue ${attributeValue} ${attributeName} ${type} - ... ELSE IF "${attributeType}"=="signature" ManageSignatureAttributeValue ${attributeValue} ${type} - ... ELSE ManageStringAttributeValue ${attributeValue} ${type} - Set To Dictionary ${attribute} attributeValue=${attributeValue} - END - Set To Dictionary ${verifyObj} verifySession=${verifySession} - # 转为json结构并返回 - ${json} json.Dumps ${verifyObj} ensure_ascii=False - Log VerifyPolicy-${json} - [Return] ${json} - -ManageIpAttributeValue - [Arguments] ${attributeValue} ${attributeName} ${type} - Run Keyword And Return If "${type}"=="str" ExtractIpAttributeValue ${attributeValue} - ${dict} Create Dictionary - ${return} ${ip} Run Keyword And Ignore Error Set Variable ${attributeValue['ip']} - Run Keyword If "${ip}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${ip} - Run Keyword If "${ip}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${ip} - Set To Dictionary ${dict} ip=${ip} - ${return} ${port} Run Keyword And Ignore Error Set Variable ${attributeValue['port']} - Run Keyword If "${port}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${port} - Run Keyword If "${port}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${port} - Set To Dictionary ${dict} port=${port} - ${return} ${addrType} Run Keyword And Ignore Error Set Variable ${attributeValue['addrType']} - Run Keyword If "${addrType}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${addrType} - Run Keyword If "${addrType}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${addrType} - Set To Dictionary ${dict} addrType=${addrType} - ${return} ${protocol} Run Keyword And Ignore Error Set Variable ${attributeValue['protocol']} - Run Keyword If "${protocol}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${protocol} - Run Keyword If "${protocol}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${protocol} - Set To Dictionary ${dict} protocol=${protocol} - ${return} ${tableName} Run Keyword And Ignore Error Set Variable ${attributeValue['tableName']} - Run Keyword If "${tableName}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${tableName} - Run Keyword If "${tableName}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${tableName} - Set To Dictionary ${dict} tableName=${tableName} - [Return] ${dict} - -ExtractIpAttributeValue - [Arguments] ${attributeValue} - ${list} Split String ${attributeValue} | - ${len} Get Length ${list} - Should Be Equal As Integers ${len} 5 - ${addrType} Evaluate int(${list}[2]) - ${protocol} Set Variable ${list}[3] - ${tableName} Set Variable ${list}[4] - ${dict} Create Dictionary ip=${list}[0] port=${list}[1] tableName=${tableName} addrType=${addrType} protocol=${protocol} - [Return] ${dict} - -ExtractIpAttributeValue_bak - [Arguments] ${attributeValue} - ${list} Split String ${attributeValue} | - ${len} Get Length ${list} - Should Be Equal As Integers ${len} 4 - ${addrType} Evaluate int(${list}[2]) - ${protocol} Evaluate int(${list}[3]) - ${dict} Create Dictionary ip=${list}[0] port=${list}[1] addrType=${addrType} protocol=${protocol} - [Return] ${dict} - -ManageSignatureAttributeValue - [Arguments] ${attributeValue} ${type} - Run Keyword And Return If "${type}"=="str" ExtractSignatureAttributeValue ${attributeValue} - ${dict} Create Dictionary - ${district} Get From Dictionary ${attributeValue} district - Should Not Be Empty ${district} - Set To Dictionary ${dict} district=${district} - ${string} Get From Dictionary ${attributeValue} string - Should Not Be Empty ${string} - Set To Dictionary ${dict} string=${string} - [Return] ${dict} - -ExtractSignatureAttributeValue - [Arguments] ${attributeValue} - ${list} Split String ${attributeValue} | - ${len} Get Length ${list} - Should Be Equal As Integers ${len} 2 - ${dict} Create Dictionary district=${list}[0] string=${list}[1] - [Return] ${dict} - -ManageStringAttributeValue - [Arguments] ${attributeValue} ${type} - ${dict} Create Dictionary - Run Keyword And Return If "${type}"=="str" Create Dictionary string=${attributeValue} - ${string} Get From Dictionary ${attributeValue} string - Should Not Be Empty ${string} - Set To Dictionary ${dict} string=${string} - [Return] ${dict} - -SortPolicyList - [Arguments] ${policyList} - [Documentation] 多策略排序 - ... policyList为数组结构 - Log Call SortPolicyList - ${policyListJson} Set Variable [ - FOR ${policyObj} IN @{policyList} - ${json} SortPolicyObject ${policyObj} - ${policyListJson} Set Variable ${policyListJson}${json}, - END - ${policyListJson}= Replace String Using Regexp ${policyListJson} ,$ ] - Log SortPolicyList-${policyListJson} - [Return] ${policyListJson} - -SortPolicyObject - [Arguments] ${policyObject} - [Documentation] 策略排序 - ... policyObject为字典结构 - ... policyObject policyId=${policyId} policyType=${policyType} beforePolicyId=${beforePolicyId} afterPolicyId=${afterPolicyId} - Dictionary Should Contain Key ${policyObject} policyId - Dictionary Should Contain Key ${policyObject} policyType - ${policyId} Get From Dictionary ${policyObject} policyId - Run Keyword If "${policyId}"=="${EMPTY}" Should Not Be Empty ${policyId} - ${policyType} Get From Dictionary ${policyObject} policyType - Should Not Be Empty ${policyType} - ${return} ${beforePolicyId} Run Keyword And Ignore Error Get From Dictionary ${policyObject} beforePolicyId - ${return1} ${afterPolicyId} Run Keyword And Ignore Error Get From Dictionary ${policyObject} afterPolicyId - Run Keyword If "${return}"=="FAIL" and "${return1}"=="FAIL" Fail beforePolicyId和 afterPolicyId必须设置一个! - Run Keyword If "${return}"=="PASS" and "${return1}"=="PASS" and "${beforePolicyId}"=="${EMPTY}" and "${afterPolicyId}"=="${EMPTY}" Fail beforePolicyId和 afterPolicyId不能同时为空! - ${json} json.Dumps ${policyObject} ensure_ascii=False - [Return] ${json} +*** Settings *** +Library json +Library Collections +Resource ../../../03-Variable/BifangApiVariable.txt +Resource ../../../03-Variable/PolicyObjectDefault.txt +Resource ../../tsg_common/ManagePolicyBody.robot + +*** Variables *** +#arraySplitor:数组元素之间的分隔符 +${arraySplitor} , +#protocolFiledSplitor:protocolFiled之间的分隔符 +${protocolFiledSplitor} & +#objectIdSplitor:objectId之间的分隔符 +${objectIdSplitor} : +#idFiledSplitor: objectId和protocolFiled之间的分隔符 +${idFiledSplitor} | + +*** Keywords *** +PolicyListDataOperation + [Arguments] ${returnData} ${policyList} ${opAction} ${policyObjectVersion} + [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' + ... 参数:returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。 + ... policyList: 必传,字典类型。字典内可选参数信息: + ... policyObjectVersion: 必传,v1,v2,涉及策略版本 + ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt + ... 多个策略处理 + # ... 特殊参数说明: + # ... 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],... + # ... 在v1版本中referenceObject=${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI&TSG_FIELD_SSL_SAN + # ... 为字符串类型,每条引用的策略对象信息以','分隔 + # ... [objectId]|[protocolFileds],... ,多个protocolFileds用'&'连接 + # ... scheduleId=${3},${1} + # ... 为字符串类型,以','分隔 + # ... 在v1版本接口中appObjectIdArray==${2},${3} + # ... 在v2版本接口中appIdObjects==${2},${3} + # ... 为字符串类型,以','分隔 + # ... + # ... 注:数字类型变量为${0}、${1}、${...} + # ... 在v2版本中 无referenceObject,取而代之的是source,destination,filterList + # ... source= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI + # ... destination= ${265}|TSG_FIELD_HTTP_HOST,${266}|TSG_FIELD_SSL_SNI + # ... filterList= ${265}:${257}|TSG_FIELD_HTTP_HOST,${266}:${299}|TSG_FIELD_SSL_SNI + # ... filterList由多个数字用‘:’分隔,加上|对上对应的protocolFiled, + # ... 一个filter中的protocolFiled是确定的,一个protocolFiled对应多个objectId + Log To Console Call PolicyListDataOperation + ${policyListJson} Set Variable [ + FOR ${policy} IN @{policyList} + ${json} PolicyOrganize ${policy} ${opAction} ${policyObjectVersion} + ${policyListJson} Set Variable ${policyListJson}${json}, + END + ${policyListJson}= Replace String Using Regexp ${policyListJson} ,$ ] + # 转为json结构并返回 + ${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList + ${bodyJson} json.Dumps ${dict} ensure_ascii=False + ${json} Replace String ${bodyJson} "replace:policyList" ${policyListJson} + Log Policy_Request_Body-${json} + [Return] ${json} + +PolicyDataOperation + [Arguments] ${returnData} ${policy} ${opAction} ${policyObjectVersion} + [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' + ... 参数:returnData: 必传,是否返回请求数据。可选${0}(不返回)/${1}(返回)。 + ... policy: 必传,字典类型。字典内可选参数信息: + ... policyObjectVersion: 必传,v1,v2,涉及策略版本 + ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt + ... 单个策略处理 + Log To Console Call PolicyDataOperation + ${json} PolicyOrganize ${policy} ${opAction} ${policyObjectVersion} + # 转为json结构并返回 + ${dict} Create Dictionary opAction=${opAction} returnData=${returnData} policyList=replace:policyList + ${bodyJson} json.Dumps ${dict} ensure_ascii=False + ${json} Replace String ${bodyJson} "replace:policyList" ${json} + Log Policy_Request_Body-${json} + [Return] ${json} + +PolicyOrganize + [Arguments] ${policy} ${opAction} ${policyObjectVersion} + [Documentation] 描述:入口,opAction参数可为'add','update','disable','enable' + ... policy: 必传,字典类型。字典内可选参数信息: + ... 其它参数默认值见../../03-Variable/PolicyObjectDefault.txt + ... policyObjectVersion: 必传,v1,v2,涉及策略版本 + ... 单个策略转换为json + Log To Console Call PolicyOrganize + ${returnDict} Create Dictionary + ${emptyList} Create List + #v1版本接口与v2版本接口不同的字段 + Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} source + Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} destination + Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} filterList + Run Keyword If "${policyObjectVersion}"=="v1" Dictionary Should Not Contain Key ${policy} appIdObjects + Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} referenceObject + Run Keyword If "${policyObjectVersion}"=="v2" Dictionary Should Not Contain Key ${policy} appObjectIdArray + #更新时 policyId不允许为空 + ${return} ${policyId} Run Keyword And Ignore Error Get From Dictionary ${policy} policyId + Run Keyword If "${opAction}"=="update" and "${return}"=="FAIL" Fail policyId is required when opAction is update + Run Keyword If "${opAction}"=="update" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is update + Run Keyword If "${opAction}"=="disable" and "${return}"=="FAIL" Fail policyId is required when opAction is disable + Run Keyword If "${opAction}"=="disable" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is disable + Run Keyword If "${opAction}"=="enable" and "${return}"=="FAIL" Fail policyId is required when opAction is enable + Run Keyword If "${opAction}"=="enable" and "${policyId}"=="${EMPTY}" Fail policyId is required when opAction is enable + ${policyId} Run Keyword If "${opAction}"=="add" Set Variable ${EMPTY} + ... ELSE Get From Dictionary ${policy} policyId + Set To Dictionary ${returnDict} policyId=${policyId} + ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType + Run Keyword If "${return}"=="FAIL" or "${policyType}"=="${EMPTY}" Set To Dictionary ${returnDict} policyType=${Default_PolicyType} + ... ELSE Set To Dictionary ${returnDict} policyType=${policyType} + #enable disable直接返回json + Run Keyword And Return If "${opAction}"=="enable" or "${opAction}"=="disable" ManagePolicyEnableAndDisable ${returnDict} ${opAction} + # 取可选参数,为传入设置默认值 + ${return} ${policyName} Run Keyword And Ignore Error Get From Dictionary ${policy} policyName + Run Keyword If "${return}"=="FAIL" or "${policyName}"=="${EMPTY}" Set To Dictionary ${returnDict} policyName=${Default_PolicyName} + ... ELSE Set To Dictionary ${returnDict} policyName=${policyName} + #policyDesc 不是必填的选项 + ${return} ${policyDesc} Run Keyword And Ignore Error Get From Dictionary ${policy} policyDesc + Run Keyword If "${return}"=="FAIL" or "${policyDesc}"=="${EMPTY}" Set To Dictionary ${returnDict} policyDesc=${Default_PolicyDesc} + ... ELSE Set To Dictionary ${returnDict} policyDesc=${policyDesc} + ${return} ${action} Run Keyword And Ignore Error Get From Dictionary ${policy} action + Run Keyword If "${return}"=="FAIL" or "${action}"=="${EMPTY}" Set To Dictionary ${returnDict} action=${Default_Action} + ... ELSE Set To Dictionary ${returnDict} action=${action} + #userTags 不是必填的选项 + ${return} ${userTags} Run Keyword And Ignore Error Get From Dictionary ${policy} userTags + Run Keyword If "${return}"=="FAIL" or "${userTags}"=="${EMPTY}" Set To Dictionary ${returnDict} userTags=${Default_UserTags} + ... ELSE Set To Dictionary ${returnDict} userTags=${userTags} + ${return} ${doLog} Run Keyword And Ignore Error Get From Dictionary ${policy} doLog + Run Keyword If "${return}"=="FAIL" or "${doLog}"=="${EMPTY}" Set To Dictionary ${returnDict} doLog=${Default_DoLog} + ... ELSE Set To Dictionary ${returnDict} doLog=${doLog} + ${return} ${isValid} Run Keyword And Ignore Error Get From Dictionary ${policy} isValid + Run Keyword If "${return}"=="FAIL" or "${isValid}"=="${EMPTY}" Set To Dictionary ${returnDict} isValid=${Default_IsValid} + ... ELSE Set To Dictionary ${returnDict} isValid=${isValid} + ${return} ${scheduleId} Run Keyword And Ignore Error Get From Dictionary ${policy} scheduleId + ${scheduleIdV} Run Keyword If "${return}"!="FAIL" and "${scheduleId}"!="${EMPTY}" and "${scheduleId}"!="${None}" Evaluate list(map(int, re.split('[,]', '${scheduleId}'))) re + ... ELSE Set Variable ${emptyList} + Set To Dictionary ${returnDict} scheduleId=${scheduleIdV} + ${return} ${appObjectIdArray} Run Keyword If "${policyObjectVersion}"=="v1" Run Keyword And Ignore Error Get From Dictionary ${policy} appObjectIdArray + ... ELSE Run Keyword And Ignore Error Get From Dictionary ${policy} appIdObjects + ${appObjectIdArrayV} Run Keyword If "${return}"=="FAIL" or "${appObjectIdArray}"=="${EMPTY}" Evaluate list(map(int, re.split('[,]', '${Default_AppObjectIdArray}'))) re + ... ELSE Evaluate list(map(int, re.split('[,]', '${appObjectIdArray}'))) re + Run Keyword If "${policyObjectVersion}"=="v1" Set To Dictionary ${returnDict} appObjectIdArray=${appObjectIdArrayV} + ... ELSE IF "${policyObjectVersion}"=="v2" Set To Dictionary ${returnDict} appIdObjects=${appObjectIdArrayV} + # 固定值 + Set To Dictionary ${returnDict} doBlacklist=${0} + # 分别处理EffectiveRange、UserRegion和ReferenceObject参数 + ProcessEffectiveRange ${policy} ${returnDict} + #WxManageUserRegion ${policy} + Run Keyword If "${policyObjectVersion}"=="v1" ProcessReferenceObject ${policy} ${returnDict} + ... ELSE IF "${policyObjectVersion}"=="v2" ManageObjectV2 ${policy} ${returnDict} + #先取出useragion 再放回json + ${return} ${userRegion} Run Keyword And Ignore Error Get From Dictionary ${policy} userRegion + Set To Dictionary ${returnDict} userRegion=${Default_UserRegion} + ${userRegion} Run Keyword If "${return}"=="FAIL" or '${userRegion}'=="${EMPTY}" Set Variable "" + ... ELSE Set Variable ${userRegion} + #针对主动防御特殊处理,主动防御不应用对象 + ${active_defence_emptyList} Create List + ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType + Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} referenceObject=${active_defence_emptyList} + Run Keyword If "${policyObjectVersion}"=="v1" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} appObjectIdArray=${active_defence_emptyList} + #v版本主动防御 + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} source=${active_defence_emptyList} + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} destination=${active_defence_emptyList} + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} filterList=${active_defence_emptyList} + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="active_defence" Set To Dictionary ${returnDict} appIdObjects=${active_defence_emptyList} + #针对WAN NAT特殊处理 + ${wannat_emptyList} Create List + ${return} ${policyType} Run Keyword And Ignore Error Get From Dictionary ${policy} policyType + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="wannat" Set To Dictionary ${returnDict} appIdObjects=${wannat_emptyList} + Run Keyword If "${policyObjectVersion}"=="v2" and "${policyType}"=="wannat" Set To Dictionary ${returnDict} filterList=${wannat_emptyList} + # 转为json结构并返回 + ${json} json.Dumps ${returnDict} ensure_ascii=False + ${json} Replace String ${json} "method:rst" ${userRegion} + #${json} Replace String ${json} "effectiveRange:Empty" {} + Log Policy-${json} + [Return] ${json} + +ManagePolicyEnableAndDisable + [Arguments] ${returnDict} ${opAction} + [Documentation] 启用与禁用时调用 + Run Keyword If "${opAction}"=="enable" Set To Dictionary ${returnDict} isValid=1 + Run Keyword If "${opAction}"=="disable" Set To Dictionary ${returnDict} isValid=0 + ${json} json.Dumps ${returnDict} ensure_ascii=False + Log Policy-${json} + [Return] ${json} + +ProcessEffectiveRange + [Arguments] ${policyList} ${returnDict} + [Documentation] 处理EffectiveRange + ... 加入effectiveRange={}的处理支持 + Log To Console Call ProcessEffectiveRange + ${emptyList} Create List + ${emptyListDouble} Create List ${emptyList} + ${return} ${effectiveRange} Run Keyword And Ignore Error Get From Dictionary ${policyList} effectiveRange + ${effectiveRange} Run Keyword If "${return}"=="FAIL" or "${effectiveRange}"=="${EMPTY}" Set Variable ${Default_EffectiveRange} + ... ELSE Set Variable ${effectiveRange} + ${value} Run Keyword If "${effectiveRange}"=="${EMPTY}" Create Dictionary tag_sets=${emptyListDouble} + ... ELSE IF "${effectiveRange}"=="{}" Create Dictionary + ... ELSE AnalysisEffectiveRange ${effectiveRange} + Set To Dictionary ${returnDict} effectiveRange=${value} + +ProcessReferenceObject + [Arguments] ${policy} ${returnDict} + [Documentation] 处理引用对象字符串 + ... v1版本接口使用 + Log To Console Call ProcessReferenceObject + ${emptyList} Create List + #默认客户端条件类型:clientip or clientsubid + ${return} ${defaultClient} Run Keyword And Ignore Error Get From Dictionary ${policy} defalutClient + ${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type} + ... ELSE Set Variable ${defaultClient} + Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObject ${emptyList} + Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObject ${emptyList} + #Run Keyword If '${addTestClentIPFlag}'=='1' ManageClientObject ${emptyList} + # Run Keyword If "${clientHostObj}"!="${None}" Append To List ${emptyList} ${clientHostObj} + ${defaultObj} Create Dictionary + ${return} ${referenceObject} Run Keyword And Ignore Error Get From Dictionary ${policy} referenceObject + ${referenceObject} Run Keyword If "${return}"=="FAIL" or "${referenceObject}"=="${EMPTY}" Set Variable ${Default_ReferenceObject} + ... ELSE Set Variable ${referenceObject} + ${value} Run Keyword If "${referenceObject}"=="${EMPTY}" Create List ${defaultObj} + ... ELSE AnalysisReferenceObject ${referenceObject} + # 若参数为空,设置默认结构 + # FOR ${var} IN @{value} + # Run Keyword If ${var}!={} Append To List ${referenceObject} ${var} + # END + ObjectLoop ${value} ${emptyList} + Set To Dictionary ${returnDict} referenceObject=${emptyList} + +ObjectLoop + [Arguments] ${value} ${valueList} + # 若参数为空,设置默认结构 + FOR ${var} IN @{value} + Run Keyword If ${var}!={} Append To List ${valueList} ${var} + END + [Return] ${valueList} + +ManageObjectV2 + [Arguments] ${policy} ${returnDict} + [Documentation] 处理引用对象字符串 + ... v2版本接口使用 + ... source,destination,filterList + # 处理引用对象字符串 + Log To Console Call ManageObjectV2 + #默认客户端条件类型:clientip or clientsubid + ${return} ${defaultClient} Run Keyword And Ignore Error Get From Dictionary ${policy} defalutClient + ${defaultClient} Run Keyword If "${return}"=="FAIL" or "${defaultClient}"=="${EMPTY}" Set Variable ${Default_Client_Type} + ... ELSE Set Variable ${defaultClient} + ${sourceList} Create List + ${destinationList} Create List + ${filterList} Create List + Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientip' ManageClientIPObjectV2 ${sourceList} + Run Keyword If '${addTestClentIPFlag}'=='1' and '${defaultClient}'=='clientsubid' ManageClientSubIDObjectV2 ${sourceList} + AnalysisObjectV2 ${policy} source ${sourceList} + AnalysisObjectV2 ${policy} destination ${destinationList} + AnalysisObjectV2 ${policy} filterList ${filterList} + Set To Dictionary ${returnDict} source=${sourceList} destination=${destinationList} filterList=${filterList} + Log ${policy} + +ManageClientIPObjectV2 + [Arguments] ${emptyList} + # 增加引用测试终端IP策略对象 + ${obj1} Create Dictionary objectId=${testClentID} protocolField=TSG_SECURITY_SOURCE_ADDR + Append To List ${emptyList} ${obj1} + +ManageClientSubIDObjectV2 + [Arguments] ${emptyList} + # 增加引用测试终端IP策略对象 + ${protocolField} Set Variable + ${obj1} Create Dictionary objectId=${testClentSubID} protocolField=${protocolField} + Append To List ${emptyList} ${obj1} + +AnalysisObjectV2 + [Arguments] ${policy} ${objectType} ${objList} + Log To Console Call AnalysisObjectV2 + ${policyObjectDefault} Run Keyword If "${objectType}"=="source" Set Variable ${EMPTY} + ... ELSE IF "${objectType}"=="destination" Set Variable ${EMPTY} + ... ELSE IF "${objectType}"=="filterList" Set Variable ${EMPTY} + ${return} ${objectList} Run Keyword And Ignore Error Get From Dictionary ${policy} ${objectType} + # source,destination,filterList + ${objectList} Run Keyword If "${return}"=="FAIL" or "${objectList}"=="${EMPTY}" Set Variable ${policyObjectDefault} + ... ELSE Set Variable ${objectList} + ${list} Evaluate re.split('[${arraySplitor}]', '${objectList}') re + FOR ${var} IN @{list} + ${tempObj} Run Keyword If "${objectType}"=="filterList" and "${var}" != "" ManageFilter ${var} + ... ELSE IF "${var}" != "" ManageObject ${var} + ... ELSE Set Variable ${EMPTY} + Run Keyword If "${tempObj}" != "${EMPTY}" Append To List ${objList} ${tempObj} + END + Log ${objList} + +ManageObject + [Arguments] ${object} + [Documentation] source,destination处理 + Log To Console Call ManageObject + ${objectList} Create List + #截取objectId + ${objectId} Evaluate '${object}'[0:'${object}'.find('${idFiledSplitor}')] + #截取protocolField + ${protocolField} Evaluate '${object}'['${object}'.find('${idFiledSplitor}')+1:] + ${tempObj} Create Dictionary + ${objectId} Run Keyword If '${objectId}'.isdigit() Evaluate int('${objectId}') + ... ELSE Set Variable ${objectId} + Set To Dictionary ${tempObj} objectId=${objectId} + Set To Dictionary ${tempObj} protocolField=${protocolField} + [Return] ${tempObj} + +ManageFilter + [Arguments] ${filter} + [Documentation] filterList处理 + Log Call ManageFilter + ${filterObjectList} Create List + #截取objectId + ${objectIds} Evaluate '${filter}'[0:'${filter}'.find('${idFiledSplitor}')] + #截取protocolField + ${protocolField} Evaluate '${filter}'['${filter}'.find('${idFiledSplitor}')+1:] + #遍历 objectId + ${list} Evaluate re.split('[${objectIdSplitor}]', '${objectIds}') re + Log list ${list} + FOR ${var} IN @{list} + ${tempObj} Create Dictionary + ${var} Run Keyword If '${var}'.isdigit() Evaluate int('${var}') + ... ELSE Set Variable ${var} + Set To Dictionary ${tempObj} objectId=${var} + Set To Dictionary ${tempObj} protocolField=${protocolField} + Append To List ${filterObjectList} ${tempObj} + END + ${filterObject} Create Dictionary filter=${filterObjectList} + [Return] ${filterObject} + +Get-PolicyIds + [Arguments] ${value} + [Documentation] 获取结果中的策略ID + ${policyIds} Create List + FOR ${policy} IN @{value['data']['policyList']} + #${policyIdList} Create List ${policy['policyId']} + #${policyObj} Create Dictionary policyType=${policy['policyType']} policyIds=${policyIdList} + #Append To List ${policyIds} ${policyObj} + Append To List ${policyIds} ${policy['policyId']} + END + ${len} Get Length ${policyIds} + ${policyIds} Run Keyword If ${len}==1 Set Variable ${policyIds[0]} + ... ELSE Set Variable ${policyIds} + [Return] ${policyIds} + +DeletePolicyWithParamDict + [Arguments] ${params} + [Documentation] 删除策略 + ... params,传入的删除字典 + ... 结构为policyIds=${policyIds} (List) policyType=${policyType} (variable) + ${response} BaseDeleteRequest /${version}${policyUrl} ${params} + ${rescode} Set Variable ${response['code']} + Should Be Equal As Strings ${rescode} 200 + [Return] ${rescode} + +VerifyPolicyList + [Arguments] ${verifyList} + [Documentation] 策略验证 + ... 多个策略验证 + Log To Console Call VerifyPolicyList + ${verifyListJson} Set Variable [ + FOR ${verifyObj} IN @{verifyList} + ${json} VerifyPolicyObj ${verifyObj} + ${verifyListJson} Set Variable ${verifyListJson}${json}, + END + ${verifyListJson}= Replace String Using Regexp ${verifyListJson} ,$ ] + Log Policy_Request_Body-${verifyListJson} + [Return] ${verifyListJson} + +VerifyPolicyObj + [Arguments] ${verifyObj} + [Documentation] 策略验证 + ... 单个策略验证 + Log To Console Call VerifyPolicyObj + ${emptyList} Create List + ${return} ${policyType} Run Keyword And Ignore Error Set Variable ${verifyObj['policyType']} + #Should Not Be Empty ${policyType} + ${verifySession} Get From Dictionary ${verifyObj} verifySession + Should Not Be Empty ${verifySession} + ${attributes} Get From Dictionary ${verifySession} attributes + Should Not Be Empty ${attributes} + FOR ${attribute} IN @{attributes} + ${attributeType} Get From Dictionary ${attribute} attributeType + ${return} ${tableName} Run Keyword And Ignore Error Set Variable ${attribute['tableName']} + ${return} ${protocol} Run Keyword And Ignore Error Set Variable ${attribute['protocol']} + ${attributeName} Get From Dictionary ${attribute} attributeName + ${attributeValue} Get From Dictionary ${attribute} attributeValue + #将json转换为dict + ${attributeValue} Convert To String ${attributeValue} + ${start} Get Regexp Matches ${attributeValue} ^{\" + ${start1} Get Regexp Matches ${attributeValue} ^{\' + ${len} Get Length ${start} + ${len1} Get Length ${start1} + ${attributeValue} Run Keyword If ${len}>0 Evaluate eval('${attributeValue}') + ... ELSE IF ${len1}>0 Evaluate eval("${attributeValue}") + ... ELSE Set Variable ${attributeValue} + ${type} Run Keyword If ${len}>0 or ${len1}>0 Set Variable dict + ... ELSE Set Variable str + ${attributeValue} Run Keyword If "${attributeType}"=="ip" ManageIpAttributeValue ${attributeValue} ${attributeName} ${type} + ... ELSE IF "${attributeType}"=="signature" ManageSignatureAttributeValue ${attributeValue} ${type} + ... ELSE ManageStringAttributeValue ${attributeValue} ${type} + Set To Dictionary ${attribute} attributeValue=${attributeValue} + END + Set To Dictionary ${verifyObj} verifySession=${verifySession} + # 转为json结构并返回 + ${json} json.Dumps ${verifyObj} ensure_ascii=False + Log VerifyPolicy-${json} + [Return] ${json} + +ManageIpAttributeValue + [Arguments] ${attributeValue} ${attributeName} ${type} + Run Keyword And Return If "${type}"=="str" ExtractIpAttributeValue ${attributeValue} + ${dict} Create Dictionary + ${return} ${ip} Run Keyword And Ignore Error Set Variable ${attributeValue['ip']} + Run Keyword If "${ip}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${ip} + Run Keyword If "${ip}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${ip} + Set To Dictionary ${dict} ip=${ip} + ${return} ${port} Run Keyword And Ignore Error Set Variable ${attributeValue['port']} + Run Keyword If "${port}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${port} + Run Keyword If "${port}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${port} + Set To Dictionary ${dict} port=${port} + ${return} ${addrType} Run Keyword And Ignore Error Set Variable ${attributeValue['addrType']} + Run Keyword If "${addrType}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${addrType} + Run Keyword If "${addrType}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${addrType} + Set To Dictionary ${dict} addrType=${addrType} + ${return} ${protocol} Run Keyword And Ignore Error Set Variable ${attributeValue['protocol']} + Run Keyword If "${protocol}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${protocol} + Run Keyword If "${protocol}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${protocol} + Set To Dictionary ${dict} protocol=${protocol} + ${return} ${tableName} Run Keyword And Ignore Error Set Variable ${attributeValue['tableName']} + Run Keyword If "${tableName}"=="${EMPTY}" and "${attributeName}"=="source" Should Not Be Empty ${tableName} + Run Keyword If "${tableName}"=="${EMPTY}" and "${attributeName}"=="destination" Should Not Be Empty ${tableName} + Set To Dictionary ${dict} tableName=${tableName} + [Return] ${dict} + +ExtractIpAttributeValue + [Arguments] ${attributeValue} + ${list} Split String ${attributeValue} | + ${len} Get Length ${list} + Should Be Equal As Integers ${len} 5 + ${addrType} Evaluate int(${list}[2]) + ${protocol} Set Variable ${list}[3] + ${tableName} Set Variable ${list}[4] + ${dict} Create Dictionary ip=${list}[0] port=${list}[1] tableName=${tableName} addrType=${addrType} protocol=${protocol} + [Return] ${dict} + +ExtractIpAttributeValue_bak + [Arguments] ${attributeValue} + ${list} Split String ${attributeValue} | + ${len} Get Length ${list} + Should Be Equal As Integers ${len} 4 + ${addrType} Evaluate int(${list}[2]) + ${protocol} Evaluate int(${list}[3]) + ${dict} Create Dictionary ip=${list}[0] port=${list}[1] addrType=${addrType} protocol=${protocol} + [Return] ${dict} + +ManageSignatureAttributeValue + [Arguments] ${attributeValue} ${type} + Run Keyword And Return If "${type}"=="str" ExtractSignatureAttributeValue ${attributeValue} + ${dict} Create Dictionary + ${district} Get From Dictionary ${attributeValue} district + Should Not Be Empty ${district} + Set To Dictionary ${dict} district=${district} + ${string} Get From Dictionary ${attributeValue} string + Should Not Be Empty ${string} + Set To Dictionary ${dict} string=${string} + [Return] ${dict} + +ExtractSignatureAttributeValue + [Arguments] ${attributeValue} + ${list} Split String ${attributeValue} | + ${len} Get Length ${list} + Should Be Equal As Integers ${len} 2 + ${dict} Create Dictionary district=${list}[0] string=${list}[1] + [Return] ${dict} + +ManageStringAttributeValue + [Arguments] ${attributeValue} ${type} + ${dict} Create Dictionary + Run Keyword And Return If "${type}"=="str" Create Dictionary string=${attributeValue} + ${string} Get From Dictionary ${attributeValue} string + Should Not Be Empty ${string} + Set To Dictionary ${dict} string=${string} + [Return] ${dict} + +SortPolicyList + [Arguments] ${policyList} + [Documentation] 多策略排序 + ... policyList为数组结构 + Log Call SortPolicyList + ${policyListJson} Set Variable [ + FOR ${policyObj} IN @{policyList} + ${json} SortPolicyObject ${policyObj} + ${policyListJson} Set Variable ${policyListJson}${json}, + END + ${policyListJson}= Replace String Using Regexp ${policyListJson} ,$ ] + Log SortPolicyList-${policyListJson} + [Return] ${policyListJson} + +SortPolicyObject + [Arguments] ${policyObject} + [Documentation] 策略排序 + ... policyObject为字典结构 + ... policyObject policyId=${policyId} policyType=${policyType} beforePolicyId=${beforePolicyId} afterPolicyId=${afterPolicyId} + Dictionary Should Contain Key ${policyObject} policyId + Dictionary Should Contain Key ${policyObject} policyType + ${policyId} Get From Dictionary ${policyObject} policyId + Run Keyword If "${policyId}"=="${EMPTY}" Should Not Be Empty ${policyId} + ${policyType} Get From Dictionary ${policyObject} policyType + Should Not Be Empty ${policyType} + ${return} ${beforePolicyId} Run Keyword And Ignore Error Get From Dictionary ${policyObject} beforePolicyId + ${return1} ${afterPolicyId} Run Keyword And Ignore Error Get From Dictionary ${policyObject} afterPolicyId + Run Keyword If "${return}"=="FAIL" and "${return1}"=="FAIL" Fail beforePolicyId和 afterPolicyId必须设置一个! + Run Keyword If "${return}"=="PASS" and "${return1}"=="PASS" and "${beforePolicyId}"=="${EMPTY}" and "${afterPolicyId}"=="${EMPTY}" Fail beforePolicyId和 afterPolicyId不能同时为空! + ${json} json.Dumps ${policyObject} ensure_ascii=False + [Return] ${json}