Browse Source

added: network editing

master
Till Wegmüller 1 month ago
parent
commit
b813d53dd7
3 changed files with 101 additions and 9 deletions
  1. 1
    1
      Vagrantfile
  2. 78
    0
      cmd/podadm/network-edit.go
  3. 22
    8
      net/net.go

+ 1
- 1
Vagrantfile View File

@@ -101,7 +101,7 @@ EOF
zoneadm -z template install

echo "Installing developer tools"
pkg install -v golang-111 system/header developer/build/gnu-make git developer/gcc-8 system/library/gcc-8-runtime jq
pkg install -v golang-112 system/header developer/build/gnu-make git developer/gcc-8 system/library/gcc-8-runtime jq

echo "Installing Task"
#curl -sL https://taskfile.dev/install.sh | sh

+ 78
- 0
cmd/podadm/network-edit.go View File

@@ -0,0 +1,78 @@
package podadm

import (
"fmt"

"github.com/ztrue/tracerr"

opcNet "git.wegmueller.it/opencloud/opencloud/net"
"github.com/spf13/cobra"
)

var networkEditCMD = &cobra.Command{
Use: "edit",
Short: "edit [-a CIDR] [-s start IP] [-e end IP] -n dnsoption name",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !h.HasNetwork(args[0]) {
return fmt.Errorf("network %s does not exist", args[0])
}
fl := cmd.Flags()

n, err := h.GetNetwork(args[0])
if err != nil {
return tracerr.Wrap(err)
}

if typeFlag := fl.Lookup("type"); typeFlag != nil {
switch typeFlag.Value.String() {
case "enc", "encapsulated":
n.Type = opcNet.NetworkTypeEncapsulated
case "direct":
n.Type = opcNet.NetworkTypeDirect
}
}

dOps, err := fl.GetStringArray("dns-options")
if err != nil {
return tracerr.Wrap(err)
}
if dOps != nil {
n.AppendDNSOptions(dOps)
}

start, err := fl.GetInt("start")
if err != nil {
return tracerr.Wrap(err)
}
end, err := fl.GetInt("end")
if err != nil {
return tracerr.Wrap(err)
}
if start != 0 {
n.PoolStart = start
}

if end != 0 {
n.PoolEnd = end
}

n, err = h.UpdateNetwork(n)
if err != nil {
return tracerr.Wrap(err)
}
return nil
},
}

func init() {
networkCMD.AddCommand(networkEditCMD)
f := networkEditCMD.Flags()
f.StringP("address-range", "a", "", "set address-range allowed inside the network")
f.IntP("start", "s", 0, "start IP of the pool")
f.IntP("end", "e", 0, "end ip of the pool")
f.StringArrayP("dns-options", "n", nil, "dns options to set in /etc/resolv.conf")
f.StringP("physical", "p", "", "The Network Adapter used to direct traffic over.")
f.StringP("defrouter", "r", "", "The default router which handles network traffic for this network")
f.StringP("type", "t", "DEFAULT", "The Type of network to setup")
}

+ 22
- 8
net/net.go View File

@@ -60,6 +60,26 @@ func NewNetwork(name string) *Network {
return &Network{Name: name, AddressLease: make([]string, 0), PoolEnd: 254, PoolStart: 128, DNSOptions: make(DNSOptions), Type: NetworkTypeDirect}
}

func (n *Network) AppendDNSOptions(dnsOptions []string) {
if n.DNSOptions == nil {
n.DNSOptions = make(DNSOptions)
}

for _, dnsOpt := range dnsOptions {
if idx := strings.Index(dnsOpt, "="); idx != -1 {
if oldVal, ok := n.DNSOptions[dnsOpt[:idx]]; ok {
n.DNSOptions[dnsOpt[:idx]] = oldVal + ";" + dnsOpt[idx+1:]
} else {
n.DNSOptions[dnsOpt[:idx]] = dnsOpt[idx+1:]
}
} else {
if _, ok := n.DNSOptions[dnsOpt]; ok {
delete(n.DNSOptions, dnsOpt)
}
}
}
}

func (n *Network) Save() error {
if n.Lowerlink == "" {
return fmt.Errorf("network configuration not capable to be setup")
@@ -137,14 +157,8 @@ func (n *Network) Update(addressPool, physical, defRouter string, dnsOptions []s
n.DefaultRouter = defRouter
}

for _, dnsOpt := range dnsOptions {
if idx := strings.Index(dnsOpt, "="); idx != -1 {
if oldVal, ok := n.DNSOptions[dnsOpt[:idx]]; ok {
n.DNSOptions[dnsOpt[:idx]] = oldVal + ";" + dnsOpt[idx+1:]
} else {
n.DNSOptions[dnsOpt[:idx]] = dnsOpt[idx+1:]
}
}
if dnsOptions != nil {
n.AppendDNSOptions(dnsOptions)
}

n.Type = netType

Loading…
Cancel
Save