Skip to content

Commit

Permalink
Updated metal-go client for sub-commands ips
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jun 23, 2023
1 parent b5f4ee5 commit 20ee963
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 38 deletions.
9 changes: 6 additions & 3 deletions internal/ips/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
package ips

import (
"context"
"fmt"
"strconv"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -44,14 +45,16 @@ func (c *Client) Assign() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
assignment, _, err := c.DeviceService.Assign(deviceID, &packngo.AddressStruct{Address: address})
IPAssignmentInput := metal.NewIPAssignmentInput(address)

assignment, _, err := c.DeviceService.CreateIPAssignment(context.Background(), deviceID).IPAssignmentInput(*IPAssignmentInput).Execute()
if err != nil {
return fmt.Errorf("Could not assign Device IP address: %w", err)
}

data := make([][]string, 1)

data[0] = []string{assignment.ID, assignment.Address, strconv.FormatBool(assignment.Public), assignment.Created}
data[0] = []string{assignment.GetId(), assignment.GetAddress(), strconv.FormatBool(assignment.GetPublic()), assignment.CreatedAt.String()}
header := []string{"ID", "Address", "Public", "Created"}

return c.Out.Output(assignment, header, &data)
Expand Down
7 changes: 5 additions & 2 deletions internal/ips/available.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package ips

import (
"context"
"fmt"
"strconv"

"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

Expand All @@ -41,10 +42,12 @@ func (c *Client) Available() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
result, _, err := c.ProjectService.AvailableAddresses(reservationID, &packngo.AvailableRequest{CIDR: cidr})
Cidr := strconv.Itoa(cidr)
resultList, _, err := c.IPService.FindIPAvailabilities(context.Background(), reservationID).Cidr(Cidr).Execute()
if err != nil {
return fmt.Errorf("Could not get available IP addresses: %w", err)
}
result := resultList.GetAvailable()
data := make([][]string, len(result))
for i, r := range result {
data[i] = []string{r}
Expand Down
17 changes: 8 additions & 9 deletions internal/ips/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
package ips

import (
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/equinix/metal-cli/internal/outputs"
"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

type Client struct {
Servicer Servicer
ProjectService packngo.ProjectIPService
DeviceService packngo.DeviceIPService
Out outputs.Outputer
Servicer Servicer
IPService metal.IPAddressesApiService
DeviceService metal.DevicesApiService
Out outputs.Outputer
}

func (c *Client) NewCommand() *cobra.Command {
Expand All @@ -46,8 +46,8 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.ProjectService = c.Servicer.API(cmd).ProjectIPs
c.DeviceService = c.Servicer.API(cmd).DeviceIPs
c.IPService = *c.Servicer.MetalAPI(cmd).IPAddressesApi
c.DeviceService = *c.Servicer.MetalAPI(cmd).DevicesApi
},
}

Expand All @@ -63,8 +63,7 @@ func (c *Client) NewCommand() *cobra.Command {
}

type Servicer interface {
API(*cobra.Command) *packngo.Client
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
MetalAPI(*cobra.Command) *metal.APIClient
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down
3 changes: 2 additions & 1 deletion internal/ips/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package ips

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -38,7 +39,7 @@ func (c *Client) Remove() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.ProjectService.Remove(reservationID)
_, err := c.IPService.DeleteIPAddress(context.Background(), reservationID).Execute()
if err != nil {
return fmt.Errorf("Could not remove IP address Reservation: %w", err)
}
Expand Down
23 changes: 16 additions & 7 deletions internal/ips/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
package ips

import (
"context"
"fmt"
"strconv"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -49,22 +50,30 @@ func (c *Client) Request() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.IPReservationRequest{
Type: packngo.IPReservationType(ttype),
Quantity: quantity,
Facility: &facility,

req := &metal.IPReservationRequestInput{
Metro: &metro,
Tags: tags,
Quantity: int32(quantity),
Type: ttype,
Facility: &facility,
}

requestIPReservationRequest := &metal.RequestIPReservationRequest{
IPReservationRequestInput: req,
}

reservation, _, err := c.ProjectService.Request(projectID, req)
reservation, _, err := c.IPService.RequestIPReservation(context.Background(), projectID).RequestIPReservationRequest(*requestIPReservationRequest).Execute()
if err != nil {
return fmt.Errorf("Could not request IP addresses: %w", err)
}

data := make([][]string, 1)

data[0] = []string{reservation.ID, reservation.Address, strconv.FormatBool(reservation.Public), reservation.Created}
data[0] = []string{reservation.IPReservation.GetId(),
reservation.IPReservation.GetAddress(),
strconv.FormatBool(reservation.IPReservation.GetPublic()),
reservation.IPReservation.CreatedAt.String()}
header := []string{"ID", "Address", "Public", "Created"}

return c.Out.Output(reservation, header, &data)
Expand Down
37 changes: 22 additions & 15 deletions internal/ips/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package ips

import (
"context"
"fmt"
"strconv"

pager "github.com/equinix/metal-cli/internal/pagination"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -59,41 +61,46 @@ func (c *Client) Retrieve() *cobra.Command {
}

cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil)
inc := []string{}
exc := []string{}
types := []string{}

if assignmentID != "" {
ip, _, err := c.ProjectService.Get(assignmentID, listOpts)
ip, _, err := c.IPService.FindIPAddressById(context.Background(), assignmentID).Include(inc).Exclude(exc).Execute()
if err != nil {
return fmt.Errorf("Could not get Device IP address: %w", err)
}

data := make([][]string, 1)

data[0] = []string{ip.ID, ip.Address, strconv.FormatBool(ip.Public), ip.Created}
data[0] = []string{ip.IPAssignment.GetId(), ip.IPAssignment.GetAddress(), strconv.FormatBool(ip.IPAssignment.GetPublic()), ip.IPAssignment.CreatedAt.String()}
header := []string{"ID", "Address", "Public", "Created"}

return c.Out.Output(ip, header, &data)
} else if reservationID != "" {
ip, _, err := c.ProjectService.Get(reservationID, listOpts)
ip, _, err := c.IPService.FindIPAddressById(context.Background(), reservationID).Include(inc).Exclude(exc).Execute()
if err != nil {
return fmt.Errorf("Could not get Reservation IP address: %w", err)
}

data := make([][]string, 1)
code := ""
metro := ""
if ip.Facility != nil {
code = ip.Facility.Code
if ip.IPReservation.Facility != nil {
code = ip.IPReservation.Facility.GetCode()
}
if ip.Metro != nil {
metro = ip.Metro.Code

if ip.IPReservation.Metro != nil {
metro = ip.IPReservation.Metro.GetCode()
}
data[0] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}

data[0] = []string{ip.IPReservation.GetId(), ip.IPReservation.GetAddress(), metro, code, strconv.FormatBool(ip.IPReservation.GetPublic()), ip.IPReservation.CreatedAt.String()}
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}

return c.Out.Output(ip, header, &data)
}

ips, _, err := c.ProjectService.List(projectID, listOpts)
ips, err := pager.GetAllIPReservations(c.IPService, projectID, inc, exc, types)
if err != nil {
return fmt.Errorf("Could not list Project IP addresses: %w", err)
}
Expand All @@ -103,13 +110,13 @@ func (c *Client) Retrieve() *cobra.Command {
for i, ip := range ips {
code := ""
metro := ""
if ip.Facility != nil {
code = ip.Facility.Code
if ip.IPReservation.Facility != nil {
code = ip.IPReservation.Facility.GetCode()
}
if ip.Metro != nil {
metro = ip.Metro.Code
if ip.IPReservation.Metro != nil {
metro = ip.IPReservation.Metro.GetCode()
}
data[i] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}
data[i] = []string{ip.IPReservation.GetId(), ip.IPReservation.GetAddress(), metro, code, strconv.FormatBool(ip.IPReservation.GetPublic()), ip.IPReservation.CreatedAt.String()}
}
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}

Expand Down
3 changes: 2 additions & 1 deletion internal/ips/unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package ips

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -38,7 +39,7 @@ func (c *Client) Unassign() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.DeviceService.Unassign(assignmentID)
_, err := c.IPService.DeleteIPAddress(context.Background(), assignmentID).Execute()
if err != nil {
return fmt.Errorf("Could not unassign IP address: %w", err)
}
Expand Down
20 changes: 20 additions & 0 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ func GetAllProjects(s metal.ProjectsApiService, include []string, exclude []stri
return projects, nil
}
}

func GetAllIPReservations(s metal.IPAddressesApiService, projectId string, inc, exc, types []string) ([]metal.IPReservationListIpAddressesInner, error) {
var ipReservations []metal.IPReservationListIpAddressesInner
page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(20) // int32 | Items returned per page (optional) (default to 10)

for {
ipReservationsPage, _, err := s.FindIPReservations(context.Background(), projectId).Types(types).Include(inc).Exclude(exc).PerPage(perPage).Execute()
if err != nil {
return nil, err
}

ipReservations = append(ipReservations, ipReservationsPage.GetIpAddresses()...)
if ipReservationsPage.Meta.GetLastPage() > ipReservationsPage.Meta.GetCurrentPage() {
page = page + 1
continue
}
return ipReservations, nil
}
}

0 comments on commit 20ee963

Please sign in to comment.