java实现对F5设备数据的下发与删除操作

1、前提:

操作F5设备需要导包iControl-13.1.0.jar(这里是我自己使用的,具体的根据自己情况而定),通过API进行对F5设备进行操作。

2、自定义连接池

自己构建一个连接池,避免每次对F5进行操作(增、删、查)时候都创建连接

package com.sino.apprelation.ltm.config;

import com.google.common.collect.Maps;
import iControl.Interfaces;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @Description: F5连接池配置
 * @Author: liu
 * @Version: 1.0
 * @Create Date Time: 2022-06-21 14:47
 * @Update Date Time:
 * @see
 */
@Component
@Slf4j
public class F5ConnectionPoolConfig {
    //用于存放f5的连接数据
    private Map<String, Interfaces> f5InterfacesMap = Maps.newHashMap();

    public Map<String, Interfaces> getF5InterfacesMap() {
        return f5InterfacesMap;
    }

    public void setF5InterfacesMap(Map<String, Interfaces> f5InterfacesMap) {
        this.f5InterfacesMap = f5InterfacesMap;
    }

    public synchronized Interfaces createF5ConnectionPool(String f5_ip, String userName, String passWord) {
        Map<String, Interfaces> f5InterfacesMap = getF5InterfacesMap();
        if (f5InterfacesMap == null) {
            Interfaces f5Interfaces = new Interfaces(f5_ip, userName, passWord);
            //存入到map中
            f5InterfacesMap.put(f5_ip, f5Interfaces);
            return f5Interfaces;
        } else {
            Set<String> f5KeySet = f5InterfacesMap.keySet();
            if (f5KeySet.contains(f5_ip)) {
                return f5InterfacesMap.get(f5_ip);
            } else {
                Interfaces f5Interfaces = new Interfaces(f5_ip, userName, passWord);
                //存入到map中
                f5InterfacesMap.put(f5_ip, f5Interfaces);
                return f5Interfaces;
            }
        }
    }
}

3、测试

以删除VS、Snat、poolName为例子(其他的操作可调用具体的API),测试demo为了更好的学会使用。

package com.sino.apprelation.ltm;

import com.sino.apprelation.ltm.config.F5ConnectionPoolConfig;
import com.sino.apprelation.ltm.service.LtmVsPoolService;
import iControl.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class F5InfoDeleteTest {
    @Autowired
    private F5ConnectionPoolConfig f5ConnectionPoolConfig;

    @Test
    public void test() throws Exception {
        //vs名称
        String[] vsName = {"ebank_3666_vs"};
        //snat的名称和ip
        String[] snat_pools = {"ebank_01_3666_snat"};

        //pool_name 和 poolMember 初始化
        String[] pool_name = {"ebank_01_3666_pool"};
        //通过连接池创建连接
        Interfaces f5Interfaces = f5ConnectionPoolConfig.createF5ConnectionPool("192.168.XXX.XXX", "admin", "admin");
        //直接创建连接
        // Interfaces f5Interfaces = new Interfaces("192.168.XXX.XXX", "admin", "admin");
        //vs初始化
        LocalLBVirtualServerBindingStub localLBVirtualServer = f5Interfaces.getLocalLBVirtualServer();
        //SNAT初始化
        LocalLBSNATPoolBindingStub localLBSNATPool = f5Interfaces.getLocalLBSNATPool();
        //Pool初始化
        LocalLBPoolBindingStub localLBPool = f5Interfaces.getLocalLBPool();

        //删除下发失败的 vs信息
        localLBVirtualServer.delete_virtual_server(vsName);
        //删除snat的poolName和节点信息
        localLBSNATPool.delete_snat_pool(snat_pools);
        //删除poolName
        localLBPool.delete_pool(pool_name);


    }


}

注意:node节点信息是公用的,删除的时候只需要删除vsName、poolName 和snatName即可,不要删除节点信息,避免造成不必要的错误!!!

希望以上内容能够帮助到大家!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值