mirror of
https://codeberg.org/lauralani/ipam.git
synced 2024-11-24 04:30:02 +01:00
Change storage backend to json files
This commit is contained in:
parent
5dbb35f0a5
commit
fe41871c8c
4 changed files with 42 additions and 73 deletions
|
@ -7,13 +7,16 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Subnet struct {
|
type Subnet struct {
|
||||||
Subnet netip.Prefix
|
Subnet netip.Prefix `json:"subnet"`
|
||||||
Name string
|
Name string `json:"name"`
|
||||||
Vlan string
|
Vlan string `json:"vlan"`
|
||||||
Addresses []Address
|
ChangedAt time.Time `json:"changedat,omitempty"`
|
||||||
|
ChangedBy string `json:"changedby,omitempty"`
|
||||||
|
Addresses []Address `json:"addresses"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasIP checks if a Subnet already contains given netip.Addr.
|
// HasIP checks if a Subnet already contains given netip.Addr.
|
||||||
|
@ -69,6 +72,8 @@ func (s Subnet) GetIP(ip netip.Addr) (Address, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
IP netip.Addr
|
IP netip.Addr `json:"ip"`
|
||||||
FQDN string
|
FQDN string `json:"fqdn"`
|
||||||
|
ChangedAt time.Time `json:"changedat,omitempty"`
|
||||||
|
ChangedBy string `json:"changedby,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -57,9 +59,10 @@ var ipaddCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet.Addresses = append(subnet.Addresses, Address{ip, hostname})
|
currentuser, _ := user.Current()
|
||||||
|
subnet.Addresses = append(subnet.Addresses, Address{ip, hostname, time.Now(), currentuser.Username})
|
||||||
|
|
||||||
writeerr := WriteSubnet(subnet)
|
writeerr := subnet.WriteSubnet()
|
||||||
if writeerr != nil {
|
if writeerr != nil {
|
||||||
fmt.Println("[ERROR]", writeerr)
|
fmt.Println("[ERROR]", writeerr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -5,7 +5,7 @@ Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
@ -81,7 +81,9 @@ func ListSubnets() []string {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
for _, element := range subnetfiles {
|
for _, element := range subnetfiles {
|
||||||
subnets = append(subnets, strings.Replace(element.Name(), "_", "/", 1))
|
a := strings.Replace(element.Name(), "_", "/", 1)
|
||||||
|
a = strings.Replace(a, ".json", "", 1)
|
||||||
|
subnets = append(subnets, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
return subnets
|
return subnets
|
||||||
|
@ -91,7 +93,7 @@ func ListSubnets() []string {
|
||||||
// file.
|
// file.
|
||||||
//
|
//
|
||||||
// Returns nil on success or the error that happened.
|
// Returns nil on success or the error that happened.
|
||||||
func WriteSubnet(subnet Subnet) error {
|
func (s Subnet) WriteSubnet() error {
|
||||||
var datadir string = viper.GetString("DataPath")
|
var datadir string = viper.GetString("DataPath")
|
||||||
|
|
||||||
_, direrr := os.Stat(datadir)
|
_, direrr := os.Stat(datadir)
|
||||||
|
@ -103,7 +105,9 @@ func WriteSubnet(subnet Subnet) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := datadir + strings.Replace(subnet.Subnet.String(), "/", "_", 1)
|
filename := datadir + strings.Replace(s.Subnet.String(), "/", "_", 1) + ".json"
|
||||||
|
|
||||||
|
data, _ := json.Marshal(s)
|
||||||
|
|
||||||
file, fileerr := os.Create(filename)
|
file, fileerr := os.Create(filename)
|
||||||
if fileerr != nil {
|
if fileerr != nil {
|
||||||
|
@ -112,35 +116,12 @@ func WriteSubnet(subnet Subnet) error {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
_, suberr := file.WriteString(subnet.Subnet.String() + "\n")
|
_, writeerr := file.Write(data)
|
||||||
if suberr != nil {
|
if writeerr != nil {
|
||||||
fmt.Println("[ERROR]", suberr)
|
fmt.Println("[ERROR]", writeerr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, nameerr := file.WriteString(subnet.Name + "\n")
|
|
||||||
if nameerr != nil {
|
|
||||||
fmt.Println("[ERROR]", nameerr)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, vlanerr := file.WriteString(subnet.Vlan + "\n")
|
|
||||||
if vlanerr != nil {
|
|
||||||
fmt.Println("[ERROR]", vlanerr)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(subnet.Addresses) != 0 {
|
|
||||||
subnetsorted := SortAddresses(subnet.Addresses)
|
|
||||||
for _, element := range subnetsorted {
|
|
||||||
_, err := file.WriteString(element.IP.String() + ":" + element.FQDN + "\n")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("[ERROR]", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,43 +132,17 @@ func WriteSubnet(subnet Subnet) error {
|
||||||
// successful, an empty Subnet object and the error otherwise.
|
// successful, an empty Subnet object and the error otherwise.
|
||||||
func GetSubnet(net netip.Prefix) (Subnet, error) {
|
func GetSubnet(net netip.Prefix) (Subnet, error) {
|
||||||
var datadir string = viper.GetString("DataPath")
|
var datadir string = viper.GetString("DataPath")
|
||||||
filename := datadir + strings.Replace(net.String(), "/", "_", 1)
|
filename := datadir + strings.Replace(net.String(), "/", "_", 1) + ".json"
|
||||||
var subnet Subnet = Subnet{}
|
var subnet Subnet = Subnet{}
|
||||||
|
|
||||||
// open file
|
content, readerr := os.ReadFile(filename)
|
||||||
file, openerr := os.Open(filename)
|
if readerr != nil {
|
||||||
if openerr != nil {
|
return Subnet{}, readerr
|
||||||
return Subnet{}, openerr
|
|
||||||
}
|
|
||||||
// remember to close the file at the end of the program
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// read the file line by line using scanner
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
|
|
||||||
var counter int = 0
|
|
||||||
for scanner.Scan() {
|
|
||||||
switch counter {
|
|
||||||
case 0:
|
|
||||||
subnet.Subnet, _ = netip.ParsePrefix(scanner.Text())
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
subnet.Name = scanner.Text()
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
subnet.Vlan = scanner.Text()
|
|
||||||
|
|
||||||
default:
|
|
||||||
s := strings.Split(scanner.Text(), ":")
|
|
||||||
ip, _ := netip.ParseAddr(s[0])
|
|
||||||
a := Address{ip, s[1]}
|
|
||||||
subnet.Addresses = append(subnet.Addresses, a)
|
|
||||||
}
|
|
||||||
counter = counter + 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if scanerr := scanner.Err(); scanerr != nil {
|
marsherr := json.Unmarshal(content, &subnet)
|
||||||
return Subnet{}, openerr
|
if marsherr != nil {
|
||||||
|
return Subnet{}, marsherr
|
||||||
}
|
}
|
||||||
|
|
||||||
return subnet, nil
|
return subnet, nil
|
||||||
|
@ -213,7 +168,7 @@ func SortAddresses(list []Address) []Address {
|
||||||
// Returns nil on success, or a *PathError on failure
|
// Returns nil on success, or a *PathError on failure
|
||||||
func DeleteSubnet(net netip.Prefix) error {
|
func DeleteSubnet(net netip.Prefix) error {
|
||||||
var datadir string = viper.GetString("DataPath")
|
var datadir string = viper.GetString("DataPath")
|
||||||
filename := datadir + strings.Replace(net.String(), "/", "_", 1)
|
filename := datadir + strings.Replace(net.String(), "/", "_", 1) + ".json"
|
||||||
|
|
||||||
removeerr := os.Remove(filename)
|
removeerr := os.Remove(filename)
|
||||||
if removeerr != nil {
|
if removeerr != nil {
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -59,12 +61,16 @@ var subnetaddCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentuser, _ := user.Current()
|
||||||
|
|
||||||
subnetobject := Subnet{}
|
subnetobject := Subnet{}
|
||||||
subnetobject.Subnet = ipnet
|
subnetobject.Subnet = ipnet
|
||||||
subnetobject.Name = netname
|
subnetobject.Name = netname
|
||||||
subnetobject.Vlan = vlanid
|
subnetobject.Vlan = vlanid
|
||||||
|
subnetobject.ChangedAt = time.Now()
|
||||||
|
subnetobject.ChangedBy = currentuser.Username
|
||||||
|
|
||||||
writeerr := WriteSubnet(subnetobject)
|
writeerr := subnetobject.WriteSubnet()
|
||||||
|
|
||||||
if writeerr != nil {
|
if writeerr != nil {
|
||||||
fmt.Println("[ERROR]", writeerr)
|
fmt.Println("[ERROR]", writeerr)
|
||||||
|
|
Loading…
Reference in a new issue