-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add subnet start/end addresses with ipv4/ipv6 support * add lease creation and initial reservation logic * add lease routes and update reservations
- Loading branch information
Showing
15 changed files
with
394 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,6 @@ release: build | |
|
||
run: release | ||
@docker-compose up | ||
|
||
mongo: | ||
@docker exec -it mongodb mongo ipam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"gopkg.in/mgo.v2/bson" | ||
|
||
"github.com/RackHD/ipam/controllers/helpers" | ||
"github.com/RackHD/ipam/interfaces" | ||
"github.com/RackHD/ipam/models" | ||
"github.com/RackHD/ipam/resources" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
// LeasesController provides methods for handling requests to the Leases API. | ||
type LeasesController struct { | ||
ipam interfaces.Ipam | ||
} | ||
|
||
// NewLeasesController returns a newly configured LeasesController. | ||
func NewLeasesController(router *mux.Router, ipam interfaces.Ipam) (*LeasesController, error) { | ||
c := LeasesController{ | ||
ipam: ipam, | ||
} | ||
|
||
router.Handle("/reservations/{id}/leases", helpers.ErrorHandler(c.Index)).Methods(http.MethodGet) | ||
router.Handle("/leases/{id}", helpers.ErrorHandler(c.Show)).Methods(http.MethodGet) | ||
router.Handle("/leases/{id}", helpers.ErrorHandler(c.Update)).Methods(http.MethodPut, http.MethodPatch) | ||
|
||
return &c, nil | ||
} | ||
|
||
// Index returns a list of Leases. | ||
func (c *LeasesController) Index(w http.ResponseWriter, r *http.Request) error { | ||
vars := mux.Vars(r) | ||
|
||
reservations, err := c.ipam.GetLeases(vars["id"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return helpers.RenderResource(w, r, resources.LeasesResourceType, http.StatusOK, reservations) | ||
} | ||
|
||
// Show returns the requested Lease. | ||
func (c *LeasesController) Show(w http.ResponseWriter, r *http.Request) error { | ||
vars := mux.Vars(r) | ||
|
||
reservation, err := c.ipam.GetLease(vars["id"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return helpers.RenderResource(w, r, resources.LeaseResourceType, http.StatusOK, reservation) | ||
} | ||
|
||
// Update updates the requested Lease. | ||
func (c *LeasesController) Update(w http.ResponseWriter, r *http.Request) error { | ||
vars := mux.Vars(r) | ||
|
||
resource, err := helpers.AcceptResource(r, resources.LeaseResourceType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if reservation, ok := resource.(models.Lease); ok { | ||
reservation.ID = bson.ObjectIdHex(vars["id"]) | ||
|
||
err = c.ipam.UpdateLease(reservation) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return helpers.RenderLocation(w, http.StatusNoContent, fmt.Sprintf("/reservations/%s", reservation.ID.Hex())) | ||
} | ||
|
||
return fmt.Errorf("Invalid Resource Type") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ type Ipam interface { | |
Pools | ||
Subnets | ||
Reservations | ||
Leases | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package interfaces | ||
|
||
import "github.com/RackHD/ipam/models" | ||
|
||
// Leases interface defines the methods for implementing Lease related business logic. | ||
type Leases interface { | ||
GetLeases(string) ([]models.Lease, error) | ||
GetLease(string) (models.Lease, error) | ||
UpdateLease(models.Lease) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ipam | ||
|
||
import ( | ||
"github.com/RackHD/ipam/models" | ||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
// IpamCollectionLeases is the name of the Mongo collection which stores Leases. | ||
const IpamCollectionLeases string = "leases" | ||
|
||
// GetLeases returns a list of Leases. | ||
func (ipam *Ipam) GetLeases(id string) ([]models.Lease, error) { | ||
session := ipam.session.Copy() | ||
defer session.Close() | ||
|
||
var reservations []models.Lease | ||
|
||
session.DB(IpamDatabase).C(IpamCollectionLeases).Find(bson.M{"reservation": bson.ObjectIdHex(id)}).All(&reservations) | ||
|
||
return reservations, nil | ||
} | ||
|
||
// GetLease returns the requested Lease. | ||
func (ipam *Ipam) GetLease(id string) (models.Lease, error) { | ||
session := ipam.session.Copy() | ||
defer session.Close() | ||
|
||
var reservation models.Lease | ||
|
||
return reservation, session.DB(IpamDatabase).C(IpamCollectionLeases).Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&reservation) | ||
} | ||
|
||
// UpdateLease updates a Lease. | ||
func (ipam *Ipam) UpdateLease(reservation models.Lease) error { | ||
session := ipam.session.Copy() | ||
defer session.Close() | ||
|
||
return session.DB(IpamDatabase).C(IpamCollectionLeases).UpdateId(reservation.ID, reservation) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
import "gopkg.in/mgo.v2/bson" | ||
|
||
// Lease is a mgo model representing a collection of Subnet resources. | ||
type Lease struct { | ||
ID bson.ObjectId `bson:"_id"` | ||
Name string `bson:"name"` | ||
Tags []string `bson:"tags"` | ||
Metadata interface{} `bson:"metadata"` | ||
Subnet bson.ObjectId `bson:"subnet,omitempty"` | ||
Reservation bson.ObjectId `bson:"reservation,omitempty"` | ||
Address bson.Binary `bson:"address"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.