Skip to content

Commit d875abf

Browse files
authored
Merge pull request #2 from jkrivas/feature/first_available_subnet
Added functions to get/create first free available subnets using /api/my_app/subnets/{id}/first_subnet/{mask}/ API method
2 parents 3a7bd29 + 86aacb2 commit d875abf

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

controllers/subnets/subnets.go

+12
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ func (c *Controller) CreateSubnet(in Subnet) (message string, err error) {
117117
return
118118
}
119119

120+
// CreateFirstFreeSubnet creates a first free child subnet inside subnet with specified mask by sending a POST request.
121+
func (c *Controller) CreateFirstFreeSubnet(id int, mask int, in Subnet) (message string, err error) {
122+
err = c.SendRequest("POST", fmt.Sprintf("/subnets/%d/first_subnet/%d/", id, mask), &in, &message)
123+
return
124+
}
125+
120126
// GetSubnetByID GETs a subnet via its ID.
121127
func (c *Controller) GetSubnetByID(id int) (out Subnet, err error) {
122128
err = c.SendRequest("GET", fmt.Sprintf("/subnets/%d/", id), &struct{}{}, &out)
@@ -135,6 +141,12 @@ func (c *Controller) GetSubnetsByCIDR(cidr string) (out []Subnet, err error) {
135141
return
136142
}
137143

144+
// GetFirstFreeSubnet GETs the first free child subnet inside subnet with specified mask
145+
func (c *Controller) GetFirstFreeSubnet(id int, mask int) (message string, err error) {
146+
err = c.SendRequest("GET", fmt.Sprintf("/subnets/%d/first_subnet/%d/", id, mask), &struct{}{}, &message)
147+
return
148+
}
149+
138150
// GetFirstFreeAddress GETs the first free IP address in a subnet and returns
139151
// it as a string. This can be used to automatically determine the next address
140152
// you should use. If there are no more available addresses, the string will be

controllers/subnets/subnets_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ const testCreateSubnetOutputJSON = `
3131
}
3232
`
3333

34+
var testCreateFirstFreeSubnetInput = Subnet{
35+
Description: "Subnet1",
36+
}
37+
38+
const testCreateFirstFreeSubnetOutputExpected = "10.10.4.0/25"
39+
const testCreateFirstFreeSubnetOutputJSON = `
40+
{
41+
"code": 201,
42+
"success": true,
43+
"message": "Subnet created",
44+
"id": "10",
45+
"data": "10.10.4.0/25"
46+
}
47+
`
48+
3449
var testGetSubnetByIDOutputExpected = Subnet{
3550
ID: 8,
3651
SubnetAddress: "10.10.3.0",
@@ -189,6 +204,15 @@ const testGetSubnetsByCIDROutputJSON = `
189204
}
190205
`
191206

207+
const testGetFirstFreeSubnetOutputExpected = "10.10.4.0/25"
208+
const testGetFirstFreeSubnetOutputJSON = `
209+
{
210+
"code": 200,
211+
"success": true,
212+
"data": "10.10.4.0/25"
213+
}
214+
`
215+
192216
const testGetFirstFreeAddressOutputExpected = "10.10.1.1"
193217
const testGetFirstFreeAddressOutputJSON = `
194218
{
@@ -503,6 +527,27 @@ func TestCreateSubnet(t *testing.T) {
503527
}
504528
}
505529

530+
func TestCreateFirstFreeSubnet(t *testing.T){
531+
ts := httpCreatedTestServer(testCreateFirstFreeSubnetOutputJSON)
532+
defer ts.Close()
533+
sess := fullSessionConfig()
534+
sess.Config.Endpoint = ts.URL
535+
client := NewController(sess)
536+
537+
in := testCreateFirstFreeSubnetInput
538+
mask := 25
539+
id := 2
540+
expected := testCreateFirstFreeSubnetOutputExpected
541+
actual, err := client.CreateFirstFreeSubnet(id, mask, in)
542+
if err != nil {
543+
t.Fatalf("Bad: %s", err)
544+
}
545+
546+
if !reflect.DeepEqual(expected, actual) {
547+
t.Fatalf("Expected %#v, got %#v", expected, actual)
548+
}
549+
}
550+
506551
func TestGetSubnetByID(t *testing.T) {
507552
ts := httpOKTestServer(testGetSubnetByIDOutputJSON)
508553
defer ts.Close()
@@ -539,6 +584,26 @@ func TestGetSubnetsByCIDR(t *testing.T) {
539584
}
540585
}
541586

587+
func TestGetFirstFreeSubnet(t *testing.T) {
588+
ts := httpOKTestServer(testGetFirstFreeSubnetOutputJSON)
589+
defer ts.Close()
590+
sess := fullSessionConfig()
591+
sess.Config.Endpoint = ts.URL
592+
client := NewController(sess)
593+
594+
id := 2
595+
mask := 25
596+
expected := testGetFirstFreeSubnetOutputExpected
597+
actual, err := client.GetFirstFreeSubnet(id, mask)
598+
if err != nil {
599+
t.Fatalf("Bad: %s", err)
600+
}
601+
602+
if !reflect.DeepEqual(expected, actual) {
603+
t.Fatalf("Expected %#v, got %#v", expected, actual)
604+
}
605+
}
606+
542607
func TestGetFirstFreeAddress(t *testing.T) {
543608
ts := httpOKTestServer(testGetFirstFreeAddressOutputJSON)
544609
defer ts.Close()

0 commit comments

Comments
 (0)