Skip to content

Commit

Permalink
move to *net.IPNet
Browse files Browse the repository at this point in the history
  • Loading branch information
benben committed Jul 1, 2024
1 parent 9b5d29f commit b145cd9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions checkly.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -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
Expand All @@ -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})
}
}

Expand Down
8 changes: 6 additions & 2 deletions checkly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"time"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b145cd9

Please sign in to comment.