一、环境依赖
对f5进行操作,我们需要下载f5-sdk的依赖包。
f5-sdk==3.0.21
urllib3==1.25.7
# 安装命令:
pip install f5-sdk==3.0.21 urllib3==1.25.7
二、需求研究
经过需求确认、文档和客户端操作的调研,若设置persist为cookie,则HTTP Profile需要选择一个值,如/Common/http,但是vs的类型是standard,经过文档的挖掘,变相实现了类型的选择,可惜的是只能选择Standard和Performance (Layer 4)。
在此,创建VS的时候加入了一些字段,类型、sourceAddressTranslation、persist、profiles。
def create_vs(self,
vs_name,
vs_type,
vs_use_pool,
destination_ip,
destination_port,
sat_type,
sat_pool,
persist=None,
partition=None,
http_profile=None):
"""
:param vs_name:
:param vs_use_pool:
:param destination_ip:
:param destination_port:
:param vs_type:
:param sat_type:
:param sat_pool:
:param persist:
:param partition:
:param http_profile:
:return:
"""
data = {}
if persist:
persist = [{"name": persist}]
else:
persist = []
try:
host = '%s:%s' % (destination_ip, destination_port)
self.mgmt.tm.ltm.virtuals.virtual.create(
name=vs_name,
partition=partition,
destination=host,
pool=vs_use_pool,
ipProtocol=vs_type, # 类型any
sourceAddressTranslation={ # sourceAddressTranslation
"type": sat_type,
"pool": sat_pool
},
persist=persist,
profiles=http_profile
)
data['result'] = True
data['message'] = 'Virtual Server create success'
except Exception as e:
logger.error(u'创建vs抛出error:{0}'.format(e))
data['result'] = False
data['message'] = self.deal_message(e)
return data
我在这里采用的是通过改变ipProtocol的类型来实现控制VS的类型的目的,当ipProtocol是tcp的时候,VS的类型是Standard;当ipProtocol是any的时候,VS的类型是Performance (Layer 4);
sat_type的值为
SAT_TYPE = [
{
"name": "None",
"value": "none"
},
{
"name": "SNAT",
"value": "snat"
},
{
"name": "Auto Map",
"value": "automap"
}
]