*** Settings *** Library Collections Library RequestsLibrary Resource ../../../03-Variable/BifangApiVariable.txt Resource ../../common/ApiHttpRequest.robot Library REST http://${host}:${port} Library json *** Variables *** ${policyapiv2} /v2/policy/compile ${objectapiv1} /v1/policy/object ${hosturl} http://${host}:${port} ${header} Create Dictionary Content-Type=application/json Authorization=${token} *** Keywords *** GetRequestResposeCodePrivate [Arguments] ${data} [Documentation] [Get方法封装:第二层,] ${header} Create Dictionary Content-Type=application/json Authorization=${token} ${response} GetRequestBase http://${hosturl} ${policyapiv2} ${header} ${data} ${response} to json ${response} ${code} get from dictionary ${response} code [Return] ${code} GetPolicyData [Arguments] ${host} ${api} ${header} ${data} ${returnKey} [Documentation] [Get方法封装:第二层,] ${response} GetRequestBase ${hosturl} ${policyapiv2} ${header} ${data} ${response} to json ${response} ${returnValue} get from dictionary ${response} ${returnKey} [Return] ${returnValue} AddPolicyData [Arguments] ${returnData} ${data} [Documentation] [Get方法封装:第二层,] ${response} PostHttpRequestBase ${hosturl} ${policyapiv2} ${header} ${data} ${response} to json ${response} ${policyIds} Run Keyword If "${returnData}" == "1" GetPolicyIds ${response} ... ELSE Create List ${rescode} Set Variable ${response['code']} [Return] ${rescode} ${policyIds} AddObjectData [Arguments] ${returnData} ${data} [Documentation] [Get方法封装:第二层,] ${response} PostHttpRequestBase ${hosturl} ${objectapiv1} ${header} ${data} ${response} to json ${response} ${objectIds} Run Keyword If "${returnData}" == "1" GetObjectIds ${response} ... ELSE Create List ${rescode} Set Variable ${response['code']} [Return] ${rescode} ${objectIds} AddPolicyBase [Arguments] ${host} ${api} ${header} ${body} ${header} Create Dictionary Content-Type=application/json Authorization=${token} Create Session api ${hosturl} headers=${header} ${remoteResponse} Post Request api ${api} data=${body} [Return] ${remoteResponse.content} AddPolicyCode [Arguments] ${host} ${api} ${header} ${data} [Documentation] [Get方法封装:第二层,] ${response} PostRequestBase ${hosturl} ${api} ${header} ${data} ${response} to json ${response} ${code} get from dictionary ${response} code [Return] ${code} GetObjectIds [Documentation] ... 获取结果中的对象ID [Arguments] ${value} Log Call Get-ObjectIds ${objectIds} Create List ${objectIdsTemp} Set Variable ${EMPTY} ${return} ${data} Run Keyword And Ignore Error Get From Dictionary ${value} data Return From Keyword If "${return}"=="FAIL" ${objectIdsTemp} FOR ${object} IN @{value['data']['objectList']} Append To List ${objectIds} ${object['objectId']} END ${json} json.Dumps ${objectIds} ensure_ascii=False ${json} Remove String ${json} [ ${json} Remove String ${json} ] [Return] ${json} GetPolicyIds [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} DelPolicysAndGroupObjects [Arguments] ${policyids} ${objectids} #引用此关键字,${objectids}参数必须是双层列表,eg:['[1]','[2,3]','[4,5,6,7]'] #删除和对象 #删除策略 log toDeletePolicy_DelPolicys ${listlenth}= Get Length ${policyids} Run Keyword If "${listlenth}"=="0" log no Policys to del ... ELSE DelPolicys ${policyids} #删除对象 log todeleteobj_DelGroupObjects Run Keyword If "${objectids}"=="${EMPTY}" log no Objects to del ... ELSE DelGroupObjects ${objectids} DelPolicys [Arguments] ${policyids} ${listlenth}= Get Length ${policyids} FOR ${var} IN RANGE ${listlenth} log ${var} DelPolicy ${policyids}[${var}] END DelPolicy [Arguments] ${body} #删除策略 log toDeletePolicy_DeletePolicyDeletePolicy ${response} DeleteRequestBase /${version}/policy/compile ${body} #{"policyType":"tsg_security","policyIds":[${policyids}]} ${response_code} Get From Dictionary ${response} code Should Be Equal As Strings ${response_code} 200 ${response} Convert to String ${response} log ${response} #Should Be Equal As Strings ${response} {'code': 200, 'msg': 'Success', 'success': True} DelGroupObjects [Arguments] ${objectids} FOR ${var} IN @{objectids} log ${var} DelGroupObject ${var} END DelGroupObject [Arguments] ${objectids} #删除对象 log todeleteobj ${response} DeleteRequestBase /${version}/policy/object {"objectIds":${objectids}} ${response_code} Get From Dictionary ${response} code Should Be Equal As Strings ${response_code} 200 ${response} Convert to String ${response} log ${response} DelPolicyAndObjectAndProfile [Arguments] ${policyids} ${objectids} ${profiles} DelPolicysAndGroupObjects ${policyids} ${objectids} DelProfiles ${profiles} DelProfiles [Arguments] ${profiles} FOR ${profile} IN @{profiles} Run Keyword If "${profile}"!="" and ${profile}!="${EMPTY}" DelProfile ${profile}[typeUrl] ${profile}[profileIds] END DelProfile [Arguments] ${typeUrl} ${profileIds} #删除对象 log DeleteProfile ${response} DeleteRequestBase /${version}/policy/profile/${typeUrl} {"profileIds":[${profileIds}]} ${response_code} Get From Dictionary ${response} code Should Be Equal As Strings ${response_code} 200 #Integer ${response_code} 200 ${response} Convert to String ${response} log ${response}