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

Added Dynamic IP creation by using API method /addresses/first_free/{subnetId}/ #6

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
50fba1e
Added Dynamic IP creation by using API method /addresses/first_free/{…
Mar 10, 2020
7edd36d
Added gateway parameters to the subnet control
invalid-email-address Mar 11, 2020
d784cb5
Removed not used log module
invalid-email-address Mar 11, 2020
1c4e761
Replaced links from github.com/paybyphone/phpipam-sdk-go to the githu…
invalid-email-address Mar 11, 2020
4fa2cd9
Set theme jekyll-theme-leap-day
pavel-z1 Mar 11, 2020
a1ff3ad
Added debug of Request URL
invalid-email-address Mar 13, 2020
d0ff330
Added fix for /api/myapp/addresses/first_free/id/ url
invalid-email-address Mar 13, 2020
90f3b2a
Missing custom fields is not an error
c0deaddict Mar 24, 2020
f682688
Added support of HTTPS Insecure connections (without ssl issuer valid…
pavel-z1 May 16, 2020
3a7bd29
Merge pull request #1 from wearespindle/feature/optional-custom-fields
pavel-z1 May 16, 2020
190be9a
add function to create first available child subnet
jkrivas Jul 7, 2020
872061f
add get next free subnet function
jkrivas Jul 8, 2020
86aacb2
add unit tests
jkrivas Jul 8, 2020
d875abf
Merge pull request #2 from jkrivas/feature/first_available_subnet
pavel-z1 Sep 17, 2020
c193c4a
Fixed crash when using custom_field_filter with phpipam_addresses dat…
pavel-z1 Sep 18, 2020
4e09538
Expand Nameserver information
poulpreben Jun 7, 2022
8f7ea86
Merge pull request #5 from poulpreben/master
pavel-z1 Jun 13, 2022
d16ccb0
Added search Subnet by CIDR and SectionID
pavel-z1 Jun 15, 2022
215c42b
Merge pull request #6 from pavel-z1/feature_search_in_section_by_cidr
pavel-z1 Jun 15, 2022
c6caeb5
Added search Subnet by CIDR and SectionID
pavel-z1 Jun 15, 2022
5c787bd
Revert "Feature search in section by CIDR"
pavel-z1 Jun 15, 2022
fac7786
Merge pull request #7 from pavel-z1/revert-6-feature_search_in_sectio…
pavel-z1 Jun 15, 2022
4ed7e12
Merge pull request #8 from pavel-z1/feature_search_in_section_by_cidr
pavel-z1 Jun 15, 2022
a12e85c
Added token login support
meisterfischy Sep 21, 2022
40fff95
Merge pull request #9 from meisterfischy/allowAPITokenLogin
lord-kyron Sep 29, 2022
18cc346
enable the http client to pick up proxy from environment
cristicalin Mar 17, 2023
816ee0d
Merge pull request #11 from simplekube-ro/skb/fix-http-proxy-env
pavel-z1 Mar 23, 2023
1688b87
Adding option to search an ip with in a subnet
tlitovsk Mar 30, 2023
85f25ad
Added missing Nameservers details in testGetSubnetByIDOutputExpected
ymartin-ovh Mar 14, 2023
a93e9ab
Merge pull request #12 from tlitovsk/master
pavel-z1 Apr 4, 2023
ed44370
Merge pull request #13 from ymartin-ovh/testfix
pavel-z1 Apr 7, 2023
de8e013
Added logging level support to prevent debug message in production e…
ymartin-ovh Mar 31, 2023
f059a2e
Merge pull request #10 from ymartin-ovh/dev/yannick.martin/logging-level
pavel-z1 Apr 14, 2023
d963038
Added resolveDNS option
pavel-z1 Apr 26, 2023
b4cce26
Added l2domains controller. Vlans - added function GetVLANsByNumberAn…
pavel-z1 May 10, 2023
270c62b
Avoid sending an empty body
UpperM May 11, 2023
21f3f01
Merge pull request #14 from UpperM/fix/avoid-sending-empty-body
pavel-z1 May 12, 2023
275b2fa
fix(request): don't add body on GET request
MatthieuuC May 12, 2023
6fea0cb
Merge pull request #15 from UpperM/fix/avoid-sendig-body-on-get-request
pavel-z1 May 13, 2023
05c01a0
Add isPool to the Subnet struct
JonTheNiceGuy Sep 5, 2023
dfc3998
Merge pull request #16 from JonTheNiceGuy/patch-1
pavel-z1 Nov 2, 2023
532fa71
Backward compatibility of phpipam API format for phpipam 1.6.0 and hi…
May 17, 2024
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![GoDoc](https://godoc.org/github.com/paybyphone/phpipam-sdk-go?status.svg)](https://godoc.org/github.com/paybyphone/phpipam-sdk-go)
[![GoDoc](https://godoc.org/github.com/pavel-z1/phpipam-sdk-go?status.svg)](https://godoc.org/github.com/pavel-z1/phpipam-sdk-go)

# phpipam-sdk-go - Partial SDK for PHPIPAM

Expand All @@ -15,7 +15,7 @@ provider to help insert data gathered from AWS and beyond.

See the [GoDoc][2] for the SDK usage details.

[2]: https://godoc.org/github.com/paybyphone/phpipam-sdk-go
[2]: https://godoc.org/github.com/pavel-z1/phpipam-sdk-go

## A Note on Custom Fields

Expand All @@ -35,6 +35,15 @@ fields, enable the nested functionality - otherwise, ensure that your fields are
not required and choose sane defaults if it's absolutely necessary for data to
be present.

## A Note on Logging

This software uses apex library to handle logging. You can control verbosity by
setting environment variable PHPIPAMSDK_LOGLEVEL to one of supported value:
* debug
* info
* warn
* error
* fatal

## License

Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-leap-day
20 changes: 17 additions & 3 deletions controllers/addresses/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package addresses
import (
"fmt"

"github.com/paybyphone/phpipam-sdk-go/phpipam"
"github.com/paybyphone/phpipam-sdk-go/phpipam/client"
"github.com/paybyphone/phpipam-sdk-go/phpipam/session"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/client"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
)

// Address represents an IP address resource within PHPIPAM.
Expand Down Expand Up @@ -91,6 +91,12 @@ func (c *Controller) CreateAddress(in Address) (message string, err error) {
return
}

// CreateAddress creates a first free in subnet address by sending a POST request.
func (c *Controller) CreateFirstFreeAddress(id int, in Address) (out string, err error) {
err = c.SendRequest("POST", fmt.Sprintf("/addresses/first_free/%d/", id), &in, &out)
return
}

// GetAddressByID GETs an address via its ID.
func (c *Controller) GetAddressByID(id int) (out Address, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/addresses/%d/", id), &struct{}{}, &out)
Expand All @@ -106,6 +112,14 @@ func (c *Controller) GetAddressesByIP(ipaddr string) (out []Address, err error)
return
}

// GetAddressesByIP searches for an address by its IP with in given subnet
// When having multiple subnets with same ip range this will return the address in the given subnet
// Those subnet may not talk to each other but still exist under on phpIPAM instance especially on ones migrated from previous versions
func (c *Controller) GetAddressesByIpInSubnet(ipaddr string,subnetID int) (out Address, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/addresses/%s/%d", ipaddr,subnetID), &struct{}{}, &out)
return
}

// GetAddressCustomFieldsSchema GETs the custom fields for the addresses controller via
// client.GetCustomFieldsSchema.
func (c *Controller) GetAddressCustomFieldsSchema() (out map[string]phpipam.CustomField, err error) {
Expand Down
65 changes: 62 additions & 3 deletions controllers/addresses/addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"reflect"
"testing"

"github.com/paybyphone/phpipam-sdk-go/phpipam"
"github.com/paybyphone/phpipam-sdk-go/phpipam/session"
"github.com/paybyphone/phpipam-sdk-go/testacc"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
"github.com/pavel-z1/phpipam-sdk-go/testacc"
)

var testCreateAddressInput = Address{
Expand Down Expand Up @@ -127,6 +127,46 @@ const testGetAddressesByIPOutputJSON = `
}
`

var testGetAddressesByIpInSubnetOutputExpected = Address{
ID: 11,
SubnetID: 3,
IPAddress: "10.10.1.10",
Description: "foobar",

}

const testGetAddressesByIpInSubnetOutputJSON = `
{
"code": 200,
"success": true,
"data":
{
"id": "11",
"subnetId": "3",
"ip": "10.10.1.10",
"is_gateway": null,
"description": "foobar",
"hostname": null,
"mac": null,
"owner": null,
"port": null,
"note": null,
"lastSeen": null,
"excludePing": null,
"PTRignore": null,
"PTR": "0",
"firewallAddressObject": null,
"editDate": null,
"links": [
{
"rel": "self",
"href": "/api/test/addresses/11/"
}
]
}
}
`

var testGetAddressCustomFieldsSchemaExpected = map[string]phpipam.CustomField{
"CustomTestAddresses": phpipam.CustomField{
Name: "CustomTestAddresses",
Expand Down Expand Up @@ -277,6 +317,25 @@ func TestGetAddressesByIP(t *testing.T) {
}
}

func TestGetAddressesByIpInSubnet(t *testing.T) {
ts := httpOKTestServer(testGetAddressesByIpInSubnetOutputJSON)
defer ts.Close()
sess := fullSessionConfig()
sess.Config.Endpoint = ts.URL
client := NewController(sess)

expected := testGetAddressesByIpInSubnetOutputExpected
actual, err := client.GetAddressesByIpInSubnet("10.10.1.10/24",3)
if err != nil {
t.Fatalf("Bad: %s", err)
}

if !reflect.DeepEqual(expected, actual) {
t.Fatalf("Expected %#v, got %#v", expected, actual)
}
}


func TestGetAddressCustomFieldsSchema(t *testing.T) {
ts := httpOKTestServer(testGetAddressCustomFieldsSchemaJSON)
defer ts.Close()
Expand Down
82 changes: 82 additions & 0 deletions controllers/l2domains/l2domains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Package l2domains provides types and methods for working with the l2domains
// controller.
package l2domains

import (
"fmt"

"github.com/pavel-z1/phpipam-sdk-go/controllers/vlans"
//"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/client"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
)

// L2Domain represents a PHPIPAM l2domain.
type L2Domain struct {
// The L2 domain ID.
ID int `json:"id,string,omitempty"`

// The L2 domains name.
Name string `json:"name,omitempty"`

// The l2domain's description.
Description string `json:"description,omitempty"`

// The ID of the section's parent, if nested.
Sections string `json:"sections,omitempty"`
}

// Controller is the base client for the L2Domains controller.
type Controller struct {
client.Client
}

// NewController returns a new instance of the client for the L2Domains controller.
func NewController(sess *session.Session) *Controller {
c := &Controller{
Client: *client.NewClient(sess),
}
return c
}

// ListL2Domains lists all l2domains.
func (c *Controller) ListL2Domains() (out []L2Domain, err error) {
err = c.SendRequest("GET", "/l2domains/", &struct{}{}, &out)
return
}

// CreateL2Domain creates a l2domain by sending a POST request.
func (c *Controller) CreateL2Domain(in L2Domain) (message string, err error) {
err = c.SendRequest("POST", "/l2domains/", &in, &message)
return
}

// GetL2DomainByID GETs a l2domain via its ID.
func (c *Controller) GetL2DomainByID(id int) (out L2Domain, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/l2domains/%d/", id), &struct{}{}, &out)
return
}

// GetL2DomainByName GETs a l2domain via its name.
func (c *Controller) GetL2DomainByName(name string) (out []L2Domain, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/l2domains/?filter_by=name&filter_value=%s", name), &struct{}{}, &out)
return
}

// GetVlansInL2Domain GETs the vlans in a l2domains by l2domain ID.
func (c *Controller) GetVlansInl2Domain(id int) (out []vlans.VLAN, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/l2domains/%d/vlans/", id), &struct{}{}, &out)
return
}

// UpdateL2Domain updates a l2domain by sending a PATCH request.
func (c *Controller) UpdateL2Domain(in L2Domain) (err error) {
err = c.SendRequest("PATCH", "/l2domains/", &in, &struct{}{})
return
}

// DeleteL2Domain deletes a l2domain by sending a DELETE request. All subnets and
func (c *Controller) DeleteL2Domain(id int) (err error) {
err = c.SendRequest("DELETE", fmt.Sprintf("/l2domains/%d/", id), &struct{}{}, &struct{}{})
return
}
8 changes: 4 additions & 4 deletions controllers/sections/sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package sections
import (
"fmt"

"github.com/paybyphone/phpipam-sdk-go/controllers/subnets"
"github.com/paybyphone/phpipam-sdk-go/phpipam"
"github.com/paybyphone/phpipam-sdk-go/phpipam/client"
"github.com/paybyphone/phpipam-sdk-go/phpipam/session"
"github.com/pavel-z1/phpipam-sdk-go/controllers/subnets"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/client"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
)

// Section represents a PHPIPAM section.
Expand Down
8 changes: 4 additions & 4 deletions controllers/sections/sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/paybyphone/phpipam-sdk-go/controllers/subnets"
"github.com/paybyphone/phpipam-sdk-go/phpipam"
"github.com/paybyphone/phpipam-sdk-go/phpipam/session"
"github.com/paybyphone/phpipam-sdk-go/testacc"
"github.com/pavel-z1/phpipam-sdk-go/controllers/subnets"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
"github.com/pavel-z1/phpipam-sdk-go/testacc"
)

var testListSectionsOutputExpected = []Section{
Expand Down
40 changes: 36 additions & 4 deletions controllers/subnets/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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"
"github.com/paybyphone/phpipam-sdk-go/phpipam/session"
"github.com/pavel-z1/phpipam-sdk-go/controllers/addresses"
"github.com/pavel-z1/phpipam-sdk-go/phpipam"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/client"
"github.com/pavel-z1/phpipam-sdk-go/phpipam/session"
)

// Subnet represents a PHPIPAM subnet.
Expand Down Expand Up @@ -43,6 +43,9 @@ type Subnet struct {
// The ID of the nameserver to attache the subnet to.
NameserverID int `json:"nameserverId,string,omitempty"`

// The ID and IPs of the nameservers for the subnet
Nameservers map[string]interface{} `json:"nameservers,omitempty"`

// true if the name should be displayed in listing instead of the subnet
// address.
ShowName phpipam.BoolIntString `json:"showName,omitempty"`
Expand Down Expand Up @@ -72,6 +75,9 @@ type Subnet struct {
// Controls if we are adding a subnet or folder.
IsFolder phpipam.BoolIntString `json:"isFolder,omitempty"`

// Marks the subnet as permitting allocation of the network and broadcast addresses.
IsPool phpipam.BoolIntString `json:"isPool,omitempty"`

// Marks the subnet as used.
IsFull phpipam.BoolIntString `json:"isFull,omitempty"`

Expand All @@ -84,12 +90,21 @@ type Subnet struct {
// The date of the last edit to this resource.
EditDate string `json:"editDate,omitempty"`

// Gateway IP and ID of Gateway IP
Gateway map[string]interface{} `json:"gateway,omitempty"`

// Gateway IP ID
GatewayID string `json:"gatewayId,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"`

// Controls enabling resolve DNS function.
ResolveDNS phpipam.BoolIntString `json:"resolveDNS,omitempty"`
}

// Controller is the base client for the Subnets controller.
Expand All @@ -111,6 +126,12 @@ func (c *Controller) CreateSubnet(in Subnet) (message string, err error) {
return
}

// CreateFirstFreeSubnet creates a first free child subnet inside subnet with specified mask by sending a POST request.
func (c *Controller) CreateFirstFreeSubnet(id int, mask int, in Subnet) (message string, err error) {
err = c.SendRequest("POST", fmt.Sprintf("/subnets/%d/first_subnet/%d/", id, mask), &in, &message)
return
}

// GetSubnetByID GETs a subnet via its ID.
func (c *Controller) GetSubnetByID(id int) (out Subnet, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/subnets/%d/", id), &struct{}{}, &out)
Expand All @@ -129,6 +150,17 @@ func (c *Controller) GetSubnetsByCIDR(cidr string) (out []Subnet, err error) {
return
}

func (c *Controller) GetSubnetsByCIDRAndSection(cidr string, section_id int) (out []Subnet, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/subnets/cidr/%s/?filter_by=sectionId&filter_value=%d", cidr, section_id), &struct{}{}, &out)
return
}

// GetFirstFreeSubnet GETs the first free child subnet inside subnet with specified mask
func (c *Controller) GetFirstFreeSubnet(id int, mask int) (message string, err error) {
err = c.SendRequest("GET", fmt.Sprintf("/subnets/%d/first_subnet/%d/", id, mask), &struct{}{}, &message)
return
}

// GetFirstFreeAddress GETs the first 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
Expand Down
Loading