Skip to content

Commit

Permalink
Refactor resource objects & add in a connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaley committed Nov 22, 2016
1 parent e55a0cd commit a6b0be0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 124 deletions.
32 changes: 12 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"errors"
"fmt"
"io"
"log"
"mime"

"net/http"
"time"

"github.com/RackHD/ipam/controllers/helpers"
"github.com/RackHD/ipam/interfaces"
Expand All @@ -29,27 +30,18 @@ func NewClient(address string) *Client {
Address: address,
Scheme: "http",
}
return c
}

//Leases returns a handle to the Leases routes
func (c *Client) Leases() *Leases {
return &Leases{c}
}

//Reservations returns a handle to the Reservations routes
func (c *Client) Reservations() *Reservations {
return &Reservations{c}
}

//Subnets returns a handle to the Subnets routes
func (c *Client) Subnets() *Subnets {
return &Subnets{c}
}
// Make sure IPAM connection is alive, with retries
for i := 0; i < 5; i++ {
_, err := c.IndexPools()
if err == nil {
return c
}
log.Println("Could not connect to IPAM, retrying in 5 Seconds...")
time.Sleep(5 * time.Second)
}

//Pools returns a handle to the Pools routes
func (c *Client) Pools() *Pools {
return &Pools{c}
return nil
}

// SendResource is used to send a generic resource type
Expand Down
32 changes: 16 additions & 16 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var _ = Describe("Client tests", func() {
})

AfterEach(func() {
poolLocation, err := ipamClient.Pools().Delete(pool.ID, pool)
poolLocation, err := ipamClient.DeletePool(pool.ID, pool)
Expect(err).To(BeNil())
Expect(poolLocation).To(Equal("/pools"))
})
Expand All @@ -38,7 +38,7 @@ var _ = Describe("Client tests", func() {
Metadata: "yodawg I heard you like interfaces",
}

pool, err = ipamClient.Pools().CreateShowPool(pool)
pool, err = ipamClient.CreateShowPool(pool)
Expect(err).To(BeNil())
Expect(pool.ID).ToNot(Equal(""))
Expect(pool.Name).To(Equal("Pool1"))
Expand All @@ -59,15 +59,15 @@ var _ = Describe("Client tests", func() {
Name: "SubnetTestPool1",
}

pool, err = ipamClient.Pools().CreateShowPool(pool)
pool, err = ipamClient.CreateShowPool(pool)
Expect(err).To(BeNil())
Expect(pool.ID).ToNot(Equal(""))
Expect(pool.Name).To(Equal("SubnetTestPool1"))

})

AfterEach(func() {
poolLocation, err := ipamClient.Pools().Delete(pool.ID, pool)
poolLocation, err := ipamClient.DeletePool(pool.ID, pool)
Expect(err).To(BeNil())
Expect(poolLocation).To(Equal("/pools"))
})
Expand All @@ -81,7 +81,7 @@ var _ = Describe("Client tests", func() {
End: end,
}

subnet, err = ipamClient.Subnets().CreateShowSubnet(pool.ID, subnet)
subnet, err = ipamClient.CreateShowSubnet(pool.ID, subnet)
Expect(err).To(BeNil())
Expect(subnet.ID).ToNot(Equal(""))
Expect(subnet.Name).To(Equal("Subnet1"))
Expand All @@ -105,7 +105,7 @@ var _ = Describe("Client tests", func() {
Name: "ReservationTestPool1",
}

pool, err = ipamClient.Pools().CreateShowPool(pool)
pool, err = ipamClient.CreateShowPool(pool)
Expect(err).To(BeNil())
Expect(pool.ID).ToNot(Equal(""))
Expect(pool.Name).To(Equal("ReservationTestPool1"))
Expand All @@ -117,7 +117,7 @@ var _ = Describe("Client tests", func() {
End: end,
}

subnet, err = ipamClient.Subnets().CreateShowSubnet(pool.ID, subnet)
subnet, err = ipamClient.CreateShowSubnet(pool.ID, subnet)
Expect(err).To(BeNil())
Expect(subnet.ID).ToNot(Equal(""))
Expect(subnet.Name).To(Equal("ReservationTestSubnet1"))
Expand All @@ -126,7 +126,7 @@ var _ = Describe("Client tests", func() {
})

AfterEach(func() {
poolLocation, err := ipamClient.Pools().Delete(pool.ID, pool)
poolLocation, err := ipamClient.DeletePool(pool.ID, pool)
Expect(err).To(BeNil())
Expect(poolLocation).To(Equal("/pools"))
})
Expand All @@ -138,7 +138,7 @@ var _ = Describe("Client tests", func() {
Subnet: subnet.ID,
}

reservation, err = ipamClient.Reservations().CreateShowReservation(subnet.ID, reservation)
reservation, err = ipamClient.CreateShowReservation(subnet.ID, reservation)
Expect(err).To(BeNil())
Expect(reservation.ID).ToNot(Equal(""))
Expect(reservation.Name).To(Equal("Reservation1"))
Expand All @@ -162,7 +162,7 @@ var _ = Describe("Client tests", func() {
Name: "LeaseTestPool1",
}

pool, err = ipamClient.Pools().CreateShowPool(pool)
pool, err = ipamClient.CreateShowPool(pool)
Expect(err).To(BeNil())
Expect(pool.ID).ToNot(Equal(""))
Expect(pool.Name).To(Equal("LeaseTestPool1"))
Expand All @@ -174,7 +174,7 @@ var _ = Describe("Client tests", func() {
End: end,
}

subnet, err = ipamClient.Subnets().CreateShowSubnet(pool.ID, subnet)
subnet, err = ipamClient.CreateShowSubnet(pool.ID, subnet)
Expect(err).To(BeNil())
Expect(subnet.ID).ToNot(Equal(""))
Expect(subnet.Name).To(Equal("LeaseTestSubnet1"))
Expand All @@ -185,7 +185,7 @@ var _ = Describe("Client tests", func() {
Subnet: subnet.ID,
}

reservation, err = ipamClient.Reservations().CreateShowReservation(subnet.ID, reservation)
reservation, err = ipamClient.CreateShowReservation(subnet.ID, reservation)
Expect(err).To(BeNil())
Expect(reservation.ID).ToNot(Equal(""))
Expect(reservation.Name).To(Equal("LeaseTestReservation1"))
Expand All @@ -196,7 +196,7 @@ var _ = Describe("Client tests", func() {
Subnet: subnet.ID,
}

reservation2, err = ipamClient.Reservations().CreateShowReservation(subnet.ID, reservation2)
reservation2, err = ipamClient.CreateShowReservation(subnet.ID, reservation2)
Expect(err).To(BeNil())
Expect(reservation2.ID).ToNot(Equal(""))
Expect(reservation2.Name).To(Equal("LeaseTestReservation2"))
Expand All @@ -205,18 +205,18 @@ var _ = Describe("Client tests", func() {
})

AfterEach(func() {
poolLocation, err := ipamClient.Pools().Delete(pool.ID, pool)
poolLocation, err := ipamClient.DeletePool(pool.ID, pool)
Expect(err).To(BeNil())
Expect(poolLocation).To(Equal("/pools"))
})

It("Should show all leases", func() {
leases, err = ipamClient.Leases().Index(reservation.ID)
leases, err = ipamClient.IndexLeases(reservation.ID)
Expect(err).To(BeNil())
Expect(leases.Leases[0].ID).ToNot(Equal(""))
Expect(leases.Leases[0].Reservation).To(Equal(reservation.ID))

leases2, err = ipamClient.Leases().Index(reservation2.ID)
leases2, err = ipamClient.IndexLeases(reservation2.ID)
Expect(err).To(BeNil())
Expect(leases2.Leases[0].ID).ToNot(Equal(""))
Expect(leases2.Leases[0].Reservation).To(Equal(reservation2.ID))
Expand Down
27 changes: 11 additions & 16 deletions client/leases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import (
"github.com/RackHD/ipam/resources"
)

//Leases can be used to query the Leases routes
type Leases struct {
client *Client
}

// Index returns a list of Leases.
func (l *Leases) Index(reservationID string) (resources.LeasesV1, error) {
returnedLeases, err := l.client.ReceiveResource("GET", "/reservations/"+reservationID+"/leases", "", "")
// IndexLeases returns a list of Leases.
func (c *Client) IndexLeases(reservationID string) (resources.LeasesV1, error) {
returnedLeases, err := c.ReceiveResource("GET", "/reservations/"+reservationID+"/leases", "", "")
if err != nil {
return resources.LeasesV1{}, err
}
Expand All @@ -23,9 +18,9 @@ func (l *Leases) Index(reservationID string) (resources.LeasesV1, error) {
return resources.LeasesV1{}, errors.New("Lease Index call error.")
}

// Show returns the requested Lease.
func (l *Leases) Show(leaseID string, leaseToShow resources.LeaseV1) (resources.LeaseV1, error) {
returnedLease, err := l.client.ReceiveResource("GET", "/leases/"+leaseID, leaseToShow.Type(), leaseToShow.Version())
// ShowLease returns the requested Lease.
func (c *Client) ShowLease(leaseID string, leaseToShow resources.LeaseV1) (resources.LeaseV1, error) {
returnedLease, err := c.ReceiveResource("GET", "/leases/"+leaseID, leaseToShow.Type(), leaseToShow.Version())
if err != nil {
return resources.LeaseV1{}, err
}
Expand All @@ -35,18 +30,18 @@ func (l *Leases) Show(leaseID string, leaseToShow resources.LeaseV1) (resources.
return resources.LeaseV1{}, errors.New("Lease Show call error.")
}

// Update updates the requested Lease and returns its location.
func (l *Leases) Update(leaseID string, leaseToUpdate resources.LeaseV1) (string, error) {
leaseLocation, err := l.client.SendResource("PATCH", "/leases/"+leaseID, &leaseToUpdate)
// UpdateLease updates the requested Lease and returns its location.
func (c *Client) UpdateLease(leaseID string, leaseToUpdate resources.LeaseV1) (string, error) {
leaseLocation, err := c.SendResource("PATCH", "/leases/"+leaseID, &leaseToUpdate)
if err != nil {
return "", err
}
return leaseLocation, nil
}

// UpdateShowLease updates a Lease and then returns that Lease.
func (l *Leases) UpdateShowLease(leaseID string, leaseToUpdate resources.LeaseV1) (resources.LeaseV1, error) {
returnedLease, err := l.client.SendReceiveResource("PATCH", "GET", "/leases/"+leaseID, &leaseToUpdate)
func (c *Client) UpdateShowLease(leaseID string, leaseToUpdate resources.LeaseV1) (resources.LeaseV1, error) {
returnedLease, err := c.SendReceiveResource("PATCH", "GET", "/leases/"+leaseID, &leaseToUpdate)
if err != nil {
return resources.LeaseV1{}, err
}
Expand Down
43 changes: 19 additions & 24 deletions client/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import (
"github.com/RackHD/ipam/resources"
)

//Pools can be used to query the Pools routes
type Pools struct {
client *Client
}

// Index returns a list of Pools.
func (p *Pools) Index() (resources.PoolsV1, error) {
pools, err := p.client.ReceiveResource("GET", "/pools", "", "")
// IndexPools returns a list of Pools.
func (c *Client) IndexPools() (resources.PoolsV1, error) {
pools, err := c.ReceiveResource("GET", "/pools", "", "")
if err != nil {
return resources.PoolsV1{}, err
}
Expand All @@ -24,19 +19,19 @@ func (p *Pools) Index() (resources.PoolsV1, error) {
return resources.PoolsV1{}, errors.New("Pool Index call error.")
}

// Create a pool and returns the location.
func (p *Pools) Create(poolToCreate resources.PoolV1) (string, error) {
// CreatePool a pool and returns the location.
func (c *Client) CreatePool(poolToCreate resources.PoolV1) (string, error) {

poolLocation, err := p.client.SendResource("POST", "/pools", &poolToCreate)
poolLocation, err := c.SendResource("POST", "/pools", &poolToCreate)
if err != nil {
return "", err
}
return poolLocation, nil
}

// CreateShowPool creates a pool and then returns that pool.
func (p *Pools) CreateShowPool(poolToCreate resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := p.client.SendReceiveResource("POST", "GET", "/pools", &poolToCreate)
func (c *Client) CreateShowPool(poolToCreate resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := c.SendReceiveResource("POST", "GET", "/pools", &poolToCreate)
if err != nil {
return resources.PoolV1{}, err
}
Expand All @@ -46,9 +41,9 @@ func (p *Pools) CreateShowPool(poolToCreate resources.PoolV1) (resources.PoolV1,
return resources.PoolV1{}, errors.New("CreateShowPool call error.")
}

// Show returns the requested Pool.
func (p *Pools) Show(poolID string, poolToShow resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := p.client.ReceiveResource("GET", "/pools/"+poolID, poolToShow.Type(), poolToShow.Version())
// ShowPool returns the requested Pool.
func (c *Client) ShowPool(poolID string, poolToShow resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := c.ReceiveResource("GET", "/pools/"+poolID, poolToShow.Type(), poolToShow.Version())
if err != nil {
return resources.PoolV1{}, err
}
Expand All @@ -58,18 +53,18 @@ func (p *Pools) Show(poolID string, poolToShow resources.PoolV1) (resources.Pool
return resources.PoolV1{}, errors.New("Pools Show call error.")
}

// Update updates the requested Pool and returns its location.
func (p *Pools) Update(poolID string, poolToUpdate resources.PoolV1) (string, error) {
location, err := p.client.SendResource("PATCH", "/pools/"+poolID, &poolToUpdate)
// UpdatePool updates the requested Pool and returns its location.
func (c *Client) UpdatePool(poolID string, poolToUpdate resources.PoolV1) (string, error) {
location, err := c.SendResource("PATCH", "/pools/"+poolID, &poolToUpdate)
if err != nil {
return "", err
}
return location, nil
}

// UpdateShowPool updates a pool and then returns that pool.
func (p *Pools) UpdateShowPool(poolID string, poolToUpdate resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := p.client.SendReceiveResource("PATCH", "GET", "/pools/"+poolID, &poolToUpdate)
func (c *Client) UpdateShowPool(poolID string, poolToUpdate resources.PoolV1) (resources.PoolV1, error) {
receivedPool, err := c.SendReceiveResource("PATCH", "GET", "/pools/"+poolID, &poolToUpdate)
if err != nil {
return resources.PoolV1{}, err
}
Expand All @@ -79,9 +74,9 @@ func (p *Pools) UpdateShowPool(poolID string, poolToUpdate resources.PoolV1) (re
return resources.PoolV1{}, errors.New("UpdateShowPool call error.")
}

// Delete removes the requested Pool and returns the location.
func (p *Pools) Delete(poolID string, poolToDelete resources.PoolV1) (string, error) {
location, err := p.client.SendResource("DELETE", "/pools/"+poolID, &poolToDelete)
// DeletePool removes the requested Pool and returns the location.
func (c *Client) DeletePool(poolID string, poolToDelete resources.PoolV1) (string, error) {
location, err := c.SendResource("DELETE", "/pools/"+poolID, &poolToDelete)
if err != nil {
return "", err
}
Expand Down

0 comments on commit a6b0be0

Please sign in to comment.