From 7c8c96a294b042776894004cb791658385a12eab Mon Sep 17 00:00:00 2001 From: "Ivan Kuchin (ikuchin)" Date: Fri, 26 Jan 2024 14:21:12 -0500 Subject: [PATCH] Feature: multiple subnets --- internal/cli/get.go | 77 +++++++++++++++++++++++---------- internal/config_reader/types.go | 12 ++--- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/internal/cli/get.go b/internal/cli/get.go index 06c1b3d..4ef8bf0 100644 --- a/internal/cli/get.go +++ b/internal/cli/get.go @@ -15,9 +15,9 @@ import ( "github.com/spf13/cobra" ) -func getSubnetID(client api_client.Authenticator, cfg *config_reader.Config) (int, error) { +func getSubnetID(client api_client.Authenticator, cfg *config_reader.Config, subnet string) (int, error) { subnet_id := 0 - subnet_json, err := client.Call("GET", cfg.Ipam_site_url+"/api/"+cfg.Ipam_app_id+"/subnets/cidr/"+cfg.Ipam_subnet, "") + subnet_json, err := client.Call("GET", cfg.Ipam_site_url+"/api/"+cfg.Ipam_app_id+"/subnets/cidr/"+subnet, "") if err != nil { return subnet_id, err } @@ -55,6 +55,28 @@ func getIPAddressesBySubnetID(subnetID int, client api_client.Authenticator, cfg return addresses, nil } +func getIPAddressesBelongsToSubnets(client api_client.Authenticator, cfg *config_reader.Config) (IPAddresses, error) { + + addresses := IPAddresses{} + + for _, subnet := range cfg.Ipam_subnets { + + subnet_id, err := getSubnetID(client, config_reader.Cfg, subnet) + if err != nil { + return addresses, err + } + + addresses_temp, err := getIPAddressesBySubnetID(subnet_id, client, config_reader.Cfg) + if err != nil { + return addresses, err + } + + addresses.IPAddresses = append(addresses.IPAddresses, addresses_temp.IPAddresses...) + + } + return addresses, nil +} + func getPiHoleCustomOutput(addresses IPAddresses, cfg *config_reader.Config) string { output := "" @@ -133,15 +155,15 @@ func convertMacToClientIdentifier(mac string) string { return "01" + client_identifier } -func getCiscoDHCPOutput(addresses IPAddresses, cfg *config_reader.Config) (string, error) { +func getCiscoDHCPOutputBySubnet(addresses IPAddresses, subnet string) (string, error) { output := "" - mask, err := getSubnetMaskBySubnetID(cfg.Ipam_subnet) + mask, err := getSubnetMaskBySubnetID(subnet) if err != nil { return output, err } - gw, err := getGWIPBySubnetID(cfg.Ipam_subnet) + gw, err := getGWIPBySubnetID(subnet) if err != nil { return output, err } @@ -175,6 +197,32 @@ func getCiscoDHCPOutput(addresses IPAddresses, cfg *config_reader.Config) (strin return output, nil } +func getCiscoDHCPOutput(client api_client.Authenticator, cfg *config_reader.Config) (string, error) { + output := "" + + for _, subnet := range cfg.Ipam_subnets { + + subnet_id, err := getSubnetID(client, config_reader.Cfg, subnet) + if err != nil { + return "", err + } + + addresses, err := getIPAddressesBySubnetID(subnet_id, client, config_reader.Cfg) + if err != nil { + return "", err + } + + temp_output, err := getCiscoDHCPOutputBySubnet(addresses, subnet) + if err != nil { + return "", err + } + + output += temp_output + } + + return output, nil +} + var getCmd = &cobra.Command{ Use: "get", Short: "Get information from phpIPAM", @@ -194,17 +242,7 @@ var getCiscoDHCP = &cobra.Command{ } defer auth.Logout() - subnet_id, err := getSubnetID(&auth, config_reader.Cfg) - if err != nil { - return err - } - - addresses, err := getIPAddressesBySubnetID(subnet_id, &auth, config_reader.Cfg) - if err != nil { - return err - } - - output, err := getCiscoDHCPOutput(addresses, config_reader.Cfg) + output, err := getCiscoDHCPOutput(&auth, config_reader.Cfg) if err != nil { return err } @@ -228,12 +266,7 @@ var getPiHoleCustom = &cobra.Command{ } defer auth.Logout() - subnet_id, err := getSubnetID(auth, config_reader.Cfg) - if err != nil { - return err - } - - addresses, err := getIPAddressesBySubnetID(subnet_id, auth, config_reader.Cfg) + addresses, err := getIPAddressesBelongsToSubnets(auth, config_reader.Cfg) if err != nil { return err } diff --git a/internal/config_reader/types.go b/internal/config_reader/types.go index 6b32679..ce869af 100644 --- a/internal/config_reader/types.go +++ b/internal/config_reader/types.go @@ -1,12 +1,12 @@ package config_reader type Config struct { - Ipam_site_url string `yaml:"ipam_site_url"` - Ipam_app_id string `yaml:"ipam_app_id"` - Ipam_app_code string `yaml:"ipam_app_code"` - Ipam_subnet string `yaml:"ipam_subnet"` - Domain string `yaml:"domain"` - Pi_hole string `yaml:"pi_hole"` + Ipam_site_url string `yaml:"ipam_site_url"` + Ipam_app_id string `yaml:"ipam_app_id"` + Ipam_app_code string `yaml:"ipam_app_code"` + Ipam_subnets []string `yaml:"ipam_subnets"` + Domain string `yaml:"domain"` + Pi_hole string `yaml:"pi_hole"` } type ConfigReader interface {