From cf0b03f58d1fc73fdc51d56e290a70909602a01c Mon Sep 17 00:00:00 2001 From: Samson Gudise Date: Sat, 27 Oct 2018 11:46:10 -0600 Subject: [PATCH] checkout first free subnet --- controllers/subnets/subnets.go | 22 +++++++++++++--------- phpipam/request/request.go | 27 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/controllers/subnets/subnets.go b/controllers/subnets/subnets.go index 0a5840a..367bd1a 100644 --- a/controllers/subnets/subnets.go +++ b/controllers/subnets/subnets.go @@ -4,7 +4,6 @@ package subnets import ( "fmt" - "github.com/paybyphone/phpipam-sdk-go/controllers/addresses" "github.com/paybyphone/phpipam-sdk-go/phpipam" "github.com/paybyphone/phpipam-sdk-go/phpipam/client" @@ -83,13 +82,6 @@ type Subnet struct { // The date of the last edit to this resource. EditDate string `json:"editDate,omitempty"` - - // A map[string]interface{} of custom fields to set on the resource. Note - // that this functionality requires PHPIPAM 1.3 or higher with the "Nest - // custom fields" flag set on the specific API integration. If this is not - // enabled, this map will be nil on GETs and POSTs and PATCHes with this - // field set will fail. Use the explicit custom field functions instead. - CustomFields map[string]interface{} `json:"custom_fields,omitempty"` } // Controller is the base client for the Subnets controller. @@ -129,7 +121,19 @@ func (c *Controller) GetSubnetsByCIDR(cidr string) (out []Subnet, err error) { return } -// GetFirstFreeAddress GETs the first free IP address in a subnet and returns +// Create new child subnet inside subnet with specified mask. +func (c *Controller) CreateFirstFreeSubnet(id, mask int) (out string, err error) { + err = c.SendRequest("POST", fmt.Sprintf("/subnets/%d/first_subnet/%d", id, mask),&struct{}{}, &out) + return +} + +// Get first free/available subnet from master subnet +func (c *Controller) GetFirstFreeSubnet(id, mask int) (out string, err error) { + err = c.SendRequest("GET", fmt.Sprintf("/subnets/%d/first_subnet/%d", id, mask),&struct{}{}, &out) + return +} + +// GetFirstFreeAddress GETs the free IP address in a subnet and returns // it as a string. This can be used to automatically determine the next address // you should use. If there are no more available addresses, the string will be // blank. diff --git a/phpipam/request/request.go b/phpipam/request/request.go index d8f47dd..a6258ea 100644 --- a/phpipam/request/request.go +++ b/phpipam/request/request.go @@ -28,6 +28,21 @@ type APIResponse struct { Success bool } +type APIResponseInt struct { + // The HTTP result code. + Code int + + // The response data. This is further unmarshaled into the data type set by + // Request.Output. + Data json.RawMessage + + // The error message, if the request failed. + Message string + + // Whether or not the API request was successful. + Success int +} + // Request represents the API request. type Request struct { // The API session. @@ -76,10 +91,18 @@ func (r *requestResponse) BodyString() string { // request is successful and the response data is unmarshalled. func (r *requestResponse) ReadResponseJSON(v interface{}) error { var resp APIResponse + var respi APIResponseInt if err := json.Unmarshal(r.Body, &resp); err != nil { - return fmt.Errorf("JSON parsing error: %s - Response body: %s", err, r.Body) - } + if err := json.Unmarshal(r.Body, &respi); err != nil { + return fmt.Errorf("JSON parsing error: %s - Response body: %s", err, r.Body) + } else { + if (respi.Success == 0) { resp.Success = true } else { resp.Success = false } + resp.Code = respi.Code + resp.Data = respi.Data + resp.Message = respi.Message + } + } if !resp.Success { return r.handleError() }