jpictl: introduce gateway command
Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
This commit is contained in:
+74
-8
@@ -1,6 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -31,8 +37,19 @@ var gatewaySetCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewaySet(_ zones.ZoneIterator, _ string) error {
|
func gatewaySet(zi zones.ZoneIterator, gw string) error {
|
||||||
return nil
|
var err error
|
||||||
|
zi.ForEachZone(func(z *zones.Zone) bool {
|
||||||
|
for _, m := range z.Machines {
|
||||||
|
if m.Name == gw {
|
||||||
|
z.SetGateway(m.ID, true)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("machine %s not found", gw)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// gateway unset
|
// gateway unset
|
||||||
@@ -56,8 +73,20 @@ var gatewayUnsetCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayUnset(_ zones.ZoneIterator, _ string) error {
|
func gatewayUnset(zi zones.ZoneIterator, ngw string) error {
|
||||||
return nil
|
var err error
|
||||||
|
zi.ForEachZone(func(z *zones.Zone) bool {
|
||||||
|
for _, m := range z.Machines {
|
||||||
|
if m.Name == ngw && m.IsGateway() {
|
||||||
|
z.SetGateway(m.ID, false)
|
||||||
|
m.RemoveWireguardConfig(0)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("machine %s not found", ngw)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// gateway list
|
// gateway list
|
||||||
@@ -85,12 +114,49 @@ var gatewayListCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayListAll(_ zones.ZoneIterator) error {
|
func gatewayListAll(zi zones.ZoneIterator) error {
|
||||||
return nil
|
var b bytes.Buffer
|
||||||
|
var err error
|
||||||
|
zi.ForEachZone(func(z *zones.Zone) bool {
|
||||||
|
b.WriteString(z.Name + ":")
|
||||||
|
var sIDs []string
|
||||||
|
ids, num := z.GatewayIDs()
|
||||||
|
if num == 0 {
|
||||||
|
err = fmt.Errorf("no gateway in zone %s", z.Name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, i := range ids {
|
||||||
|
sIDs = append(sIDs, strconv.Itoa(i))
|
||||||
|
}
|
||||||
|
b.WriteString(strings.Join(sIDs, ", "))
|
||||||
|
b.WriteString("\n")
|
||||||
|
_, err = b.WriteTo(os.Stdout)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayList(_ zones.ZoneIterator, _ string) error {
|
func gatewayList(zi zones.ZoneIterator, m string) error {
|
||||||
return nil
|
var b bytes.Buffer
|
||||||
|
var err error
|
||||||
|
zi.ForEachZone(func(z *zones.Zone) bool {
|
||||||
|
if z.Name == m {
|
||||||
|
b.WriteString(z.Name + ":")
|
||||||
|
ids, num := z.GatewayIDs()
|
||||||
|
if num == 0 {
|
||||||
|
err = fmt.Errorf("no gateway in zone %s", z.Name)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString(fmt.Sprint(ids))
|
||||||
|
b.WriteString("\n")
|
||||||
|
_, err = b.WriteTo(os.Stdout)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("zone %s not found", m)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
Reference in New Issue
Block a user