Skip to content

Commit

Permalink
Feature: multiple subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKuchin committed Jan 26, 2024
1 parent bc9c909 commit 7c8c96a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
77 changes: 55 additions & 22 deletions internal/cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 := ""

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions internal/config_reader/types.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 7c8c96a

Please sign in to comment.