Skip to content

Commit

Permalink
fix: add network from a json data
Browse files Browse the repository at this point in the history
danieldin95 committed Apr 8, 2024
1 parent dfee4c9 commit 0f3a9cb
Showing 7 changed files with 49 additions and 56 deletions.
7 changes: 5 additions & 2 deletions cmd/api/v5/network.go
Original file line number Diff line number Diff line change
@@ -33,9 +33,12 @@ func (u Network) List(c *cli.Context) error {

func (u Network) Add(c *cli.Context) error {
file := c.String("file")
network := &schema.Network{
File: file,
network := &schema.Network{}

if err := libol.UnmarshalLoad(&network.Config, file); err != nil {
return err
}

url := u.Url(c.String("url"), "")
clt := u.NewHttp(c.String("token"))
if err := clt.PostJSON(url, network, nil); err != nil {
15 changes: 10 additions & 5 deletions pkg/api/network.go
Original file line number Diff line number Diff line change
@@ -52,17 +52,22 @@ func (h Network) Post(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := libol.Marshal(&network.Config, true)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
cs := h.Switcher.Config()
file := cs.Dir("network", network.File)
if err := libol.FileExist(file); err != nil {
name, err := cs.AddNetwork(data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
cs.AddNetwork(file)
if obj := cs.GetNetworkWithFile(file); obj != nil {

if obj := cs.GetNetwork(name); obj != nil {
h.Switcher.AddNetwork(obj.Name)
} else {
http.Error(w, "Network not found", http.StatusBadRequest)
http.Error(w, name+" not found", http.StatusBadRequest)
return
}
ResponseJson(w, "success")
41 changes: 21 additions & 20 deletions pkg/config/switch.go
Original file line number Diff line number Diff line change
@@ -175,20 +175,23 @@ func (s *Switch) FormatNetwork() {
}
}

func (s *Switch) LoadNetworkWithFile(file string) {
func (s *Switch) LoadNetworkWithData(data []byte) (*Network, error) {
libol.Debug("Switch.LoadNetworkWithData %s", data)
obj := &Network{
Alias: s.Alias,
File: file,
ConfDir: s.ConfDir,
}
if err := libol.UnmarshalLoad(obj, file); err != nil {
libol.Error("Switch.LoadNetwork %s", err)
return
if err := libol.Unmarshal(obj, data); err != nil {
return nil, err
}
if _, ok := s.Network[obj.Name]; ok {
return nil, libol.NewErr("already existed")
}
obj.LoadLink()
obj.LoadRoute()
obj.LoadOutput()
s.Network[obj.Name] = obj
return obj, nil
}

func (s *Switch) correctNetworkWithObj(obj *Network) {
@@ -222,11 +225,14 @@ func (s *Switch) CorrectNetwork() {
}
}

func (s *Switch) AddNetwork(file string) {
s.LoadNetworkWithFile(file)
net := s.GetNetworkWithFile(file)
s.formatNetworkWithObj(net)
s.correctNetworkWithObj(net)
func (s *Switch) AddNetwork(data []byte) (string, error) {
obj, err := s.LoadNetworkWithData(data)
if err != nil {
return "", err
}
s.formatNetworkWithObj(obj)
s.correctNetworkWithObj(obj)
return obj.Name, nil
}

func (s *Switch) LoadNetwork() {
@@ -235,7 +241,11 @@ func (s *Switch) LoadNetwork() {
libol.Error("Switch.LoadNetwork %s", err)
}
for _, k := range files {
s.LoadNetworkWithFile(k)
if data, err := libol.LoadWithoutAnn(k); err != nil {
libol.Warn("Switch.LoadNetwork %s", err)
} else if _, err := s.LoadNetworkWithData(data); err != nil {
libol.Warn("Switch.LoadNetwork %s", err)
}
}
s.FormatNetwork()
s.CorrectNetwork()
@@ -331,15 +341,6 @@ func (s *Switch) GetNetwork(name string) *Network {
return s.Network[name]
}

func (s *Switch) GetNetworkWithFile(file string) *Network {
for _, obj := range s.Network {
if obj.File == file {
return obj
}
}
return nil
}

func (s *Switch) GetACL(name string) *ACL {
return s.Acl[name]
}
15 changes: 8 additions & 7 deletions pkg/models/network.go
Original file line number Diff line number Diff line change
@@ -38,13 +38,14 @@ func (u *Route) SetOrigin(value string) {
}

type Network struct {
Name string `json:"name"`
Tenant string `json:"tenant,omitempty"`
IfAddr string `json:"ifAddr"`
IpStart string `json:"ipStart"`
IpEnd string `json:"ipEnd"`
Netmask string `json:"netmask"`
Routes []*Route `json:"routes"`
Name string `json:"name"`
Tenant string `json:"tenant,omitempty"`
IfAddr string `json:"ifAddr"`
IpStart string `json:"ipStart"`
IpEnd string `json:"ipEnd"`
Netmask string `json:"netmask"`
Routes []*Route `json:"routes"`
Config interface{} `json:"config"`
}

func NewNetwork(name string, ifAddr string) (this *Network) {
20 changes: 2 additions & 18 deletions pkg/models/schema.go
Original file line number Diff line number Diff line change
@@ -95,24 +95,8 @@ func SchemaToUserModel(user *schema.User) *User {

func NewNetworkSchema(n *Network) schema.Network {
sn := schema.Network{
Name: n.Name,
Subnet: schema.Subnet{
IfAddr: n.IfAddr,
IpStart: n.IpStart,
IpEnd: n.IpEnd,
Netmask: n.Netmask,
},
Routes: make([]schema.PrefixRoute, 0, 32),
}
for _, route := range n.Routes {
sn.Routes = append(sn.Routes,
schema.PrefixRoute{
NextHop: route.NextHop,
Prefix: route.Prefix,
Metric: route.Metric,
Mode: route.Mode,
Origin: route.Origin,
})
Name: n.Name,
Config: n.Config,
}
return sn
}
6 changes: 2 additions & 4 deletions pkg/schema/network.go
Original file line number Diff line number Diff line change
@@ -24,8 +24,6 @@ type Subnet struct {
}

type Network struct {
File string `json:"file,omitempty"`
Name string `json:"name"`
Subnet Subnet `json:"subnet"`
Routes []PrefixRoute `json:"routes"`
Name string `json:"name"`
Config interface{} `json:"config"`
}
1 change: 1 addition & 0 deletions pkg/switch/network.go
Original file line number Diff line number Diff line change
@@ -125,6 +125,7 @@ func (w *WorkerImpl) Initialize() {
Netmask: cfg.Subnet.Netmask,
IfAddr: cfg.Bridge.Address,
Routes: make([]*models.Route, 0, 2),
Config: cfg,
}
for _, rt := range cfg.Routes {
nRoute := w.newRoute(&rt)

0 comments on commit 0f3a9cb

Please sign in to comment.