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

Allow dynamic creation of IP on CreateAddress #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 21 additions & 1 deletion controllers/addresses/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
package addresses

import (
"errors"
"fmt"
"math/rand"
"time"

"github.com/paybyphone/phpipam-sdk-go/phpipam"
"github.com/paybyphone/phpipam-sdk-go/phpipam/client"
Expand Down Expand Up @@ -87,7 +90,24 @@ func NewController(sess *session.Session) *Controller {

// CreateAddress creates an address by sending a POST request.
func (c *Controller) CreateAddress(in Address) (message string, err error) {
err = c.SendRequest("POST", "/addresses/", &in, &message)
if in.IPAddress == "" && in.SubnetID == 0 {
return message, errors.New("ip address or subnet id must be defined")
}

if in.IPAddress == "" {
// Retry
for i := 0; i <= 5; i++ {
err = c.SendRequest("POST", "/addresses/first_free", &in, &message)
if err == nil {
break
}
r := rand.Intn(500)
time.Sleep(time.Duration(r) * time.Microsecond)
}
} else {
err = c.SendRequest("POST", "/addresses/", &in, &message)
}

return
}

Expand Down