From b145cd9d9279e23b60a1fb0a5b92dc4f930c588c Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 1 Jul 2024 08:15:13 +0200 Subject: [PATCH] move to *net.IPNet --- checkly.go | 13 +++++++++++-- checkly_test.go | 8 ++++++-- types.go | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/checkly.go b/checkly.go index c89472e..244f2db 100644 --- a/checkly.go +++ b/checkly.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "net/http/httputil" "net/url" @@ -1393,7 +1394,11 @@ func (c *client) GetStaticIPs( } for region, ip := range datav6 { - IPs = append(IPs, StaticIP{Value: ip, Family: "IPv6", Region: region}) + _, addr, err := net.ParseCIDR(ip) + if err != nil { + return nil, fmt.Errorf("could not parse CIDR from %s: %v", ip, err) + } + IPs = append(IPs, StaticIP{Region: region, Address: addr}) } // and then IPv4 @@ -1418,7 +1423,11 @@ func (c *client) GetStaticIPs( for region, ips := range datav4 { for _, ip := range ips { - IPs = append(IPs, StaticIP{Value: ip, Family: "IPv4", Region: region}) + _, addr, err := net.ParseCIDR(ip + "/32") + if err != nil { + return nil, fmt.Errorf("could not parse CIDR from %s: %v", ip, err) + } + IPs = append(IPs, StaticIP{Region: region, Address: addr}) } } diff --git a/checkly_test.go b/checkly_test.go index f60cab4..939a411 100644 --- a/checkly_test.go +++ b/checkly_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "net/http/httptest" "os" @@ -1551,9 +1552,12 @@ func TestGetStaticIPs(t *testing.T) { t.Error(err) } + _, exampleIPv4, err := net.ParseCIDR("54.151.146.209/32") + _, exampleIPv6, err := net.ParseCIDR("2600:1f18:12ca:3000::/56") + expected := []checkly.StaticIP{ - {Value: "54.151.146.209", Family: "IPv4", Region: "ap-southeast-1"}, - {Value: "2600:1f18:12ca:3000::/56", Family: "IPv6", Region: "us-east-1"}, + {Region: "ap-southeast-1", Address: exampleIPv4}, + {Region: "us-east-1", Address: exampleIPv6}, } for _, exp := range expected { diff --git a/types.go b/types.go index a025a87..f0d1e4d 100644 --- a/types.go +++ b/types.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "log" + "net" "net/http" "time" ) @@ -915,9 +916,8 @@ type Runtime struct { } type StaticIP struct { - Value string - Family string Region string + Address *net.IPNet } // SetConfig sets config of alert channel based on it's type