Ai
5 Star 6 Fork 4

zstackio/zstack-vyos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vtysh_zebra.go 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
shixin.ruan 提交于 2024-07-08 14:31 +08:00 . [root]: use openeuler image for vpc
package utils
import (
"errors"
"fmt"
"path/filepath"
)
const (
BLACKHOLE_ROUTE = "null0"
)
type ZebraRoute struct {
Dst string
NextHop string
OutDev string
Distance int
isDelete bool
}
func GetZebraJsonFile() string {
return filepath.Join(GetZvrZsConfigPath(), "zebra.json")
}
func NewZebraRoute() *ZebraRoute {
cmd := &ZebraRoute{
Dst: "",
NextHop: "",
OutDev: "",
Distance: 0,
isDelete: false,
}
return cmd
}
func (z *ZebraRoute) SetDst(dstCidr string) *ZebraRoute {
z.Dst = dstCidr
return z
}
func (z *ZebraRoute) SetDev(devName string) *ZebraRoute {
z.OutDev = devName
return z
}
//If the param is "null0", then zebra installs a blackhole route
func (z *ZebraRoute) SetNextHop(nexthop string) *ZebraRoute {
z.NextHop = nexthop
return z
}
func (z *ZebraRoute) SetDistance(distance int) *ZebraRoute {
z.Distance = distance
return z
}
func (z *ZebraRoute) SetDelete() *ZebraRoute {
z.isDelete = true
return z
}
func (z *ZebraRoute) Apply() error {
var (
cmd string
)
if z.Dst == "" {
return errors.New("dst can not be empty")
}
if z.Distance != 0 && z.OutDev != "" {
return errors.New("cannot set out device and distance at the same time")
} else if z.Distance != 0 && z.OutDev == "" {
cmd = fmt.Sprintf("ip route %s %s %d", z.Dst, z.NextHop, z.Distance)
} else if z.Distance == 0 && z.OutDev != "" {
cmd = fmt.Sprintf("ip route %s %s %s", z.Dst, z.NextHop, z.OutDev)
} else {
cmd = fmt.Sprintf("ip route %s %s", z.Dst, z.NextHop)
}
if z.isDelete {
cmd = "no " + cmd
}
bash := Bash{
Command: fmt.Sprintf("vtysh -c 'configure terminal' -c '%s'", cmd),
}
if _, _, _, err := bash.RunWithReturn(); err != nil {
return err
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zstackio/zstack-vyos.git
git@gitee.com:zstackio/zstack-vyos.git
zstackio
zstack-vyos
zstack-vyos
master

搜索帮助