Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout first free subnet #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions controllers/subnets/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 25 additions & 2 deletions phpipam/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
Expand Down