Skip to content

Commit 20ee963

Browse files
committed
Updated metal-go client for sub-commands ips
1 parent b5f4ee5 commit 20ee963

File tree

8 files changed

+81
-38
lines changed

8 files changed

+81
-38
lines changed

internal/ips/assign.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
2526
"strconv"
2627

27-
"github.com/packethost/packngo"
28+
metal "github.com/equinix-labs/metal-go/metal/v1"
2829
"github.com/spf13/cobra"
2930
)
3031

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

4546
RunE: func(cmd *cobra.Command, args []string) error {
4647
cmd.SilenceUsage = true
47-
assignment, _, err := c.DeviceService.Assign(deviceID, &packngo.AddressStruct{Address: address})
48+
IPAssignmentInput := metal.NewIPAssignmentInput(address)
49+
50+
assignment, _, err := c.DeviceService.CreateIPAssignment(context.Background(), deviceID).IPAssignmentInput(*IPAssignmentInput).Execute()
4851
if err != nil {
4952
return fmt.Errorf("Could not assign Device IP address: %w", err)
5053
}
5154

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

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

5760
return c.Out.Output(assignment, header, &data)

internal/ips/available.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
26+
"strconv"
2527

26-
"github.com/packethost/packngo"
2728
"github.com/spf13/cobra"
2829
)
2930

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

4243
RunE: func(cmd *cobra.Command, args []string) error {
4344
cmd.SilenceUsage = true
44-
result, _, err := c.ProjectService.AvailableAddresses(reservationID, &packngo.AvailableRequest{CIDR: cidr})
45+
Cidr := strconv.Itoa(cidr)
46+
resultList, _, err := c.IPService.FindIPAvailabilities(context.Background(), reservationID).Cidr(Cidr).Execute()
4547
if err != nil {
4648
return fmt.Errorf("Could not get available IP addresses: %w", err)
4749
}
50+
result := resultList.GetAvailable()
4851
data := make([][]string, len(result))
4952
for i, r := range result {
5053
data[i] = []string{r}

internal/ips/ip.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
package ips
2222

2323
import (
24+
metal "github.com/equinix-labs/metal-go/metal/v1"
2425
"github.com/equinix/metal-cli/internal/outputs"
25-
"github.com/packethost/packngo"
2626
"github.com/spf13/cobra"
2727
)
2828

2929
type Client struct {
30-
Servicer Servicer
31-
ProjectService packngo.ProjectIPService
32-
DeviceService packngo.DeviceIPService
33-
Out outputs.Outputer
30+
Servicer Servicer
31+
IPService metal.IPAddressesApiService
32+
DeviceService metal.DevicesApiService
33+
Out outputs.Outputer
3434
}
3535

3636
func (c *Client) NewCommand() *cobra.Command {
@@ -46,8 +46,8 @@ func (c *Client) NewCommand() *cobra.Command {
4646
root.PersistentPreRun(cmd, args)
4747
}
4848
}
49-
c.ProjectService = c.Servicer.API(cmd).ProjectIPs
50-
c.DeviceService = c.Servicer.API(cmd).DeviceIPs
49+
c.IPService = *c.Servicer.MetalAPI(cmd).IPAddressesApi
50+
c.DeviceService = *c.Servicer.MetalAPI(cmd).DevicesApi
5151
},
5252
}
5353

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

6565
type Servicer interface {
66-
API(*cobra.Command) *packngo.Client
67-
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
66+
MetalAPI(*cobra.Command) *metal.APIClient
6867
}
6968

7069
func NewClient(s Servicer, out outputs.Outputer) *Client {

internal/ips/remove.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
2526

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

3940
RunE: func(cmd *cobra.Command, args []string) error {
4041
cmd.SilenceUsage = true
41-
_, err := c.ProjectService.Remove(reservationID)
42+
_, err := c.IPService.DeleteIPAddress(context.Background(), reservationID).Execute()
4243
if err != nil {
4344
return fmt.Errorf("Could not remove IP address Reservation: %w", err)
4445
}

internal/ips/request.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
2526
"strconv"
2627

27-
"github.com/packethost/packngo"
28+
metal "github.com/equinix-labs/metal-go/metal/v1"
2829
"github.com/spf13/cobra"
2930
)
3031

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

5051
RunE: func(cmd *cobra.Command, args []string) error {
5152
cmd.SilenceUsage = true
52-
req := &packngo.IPReservationRequest{
53-
Type: packngo.IPReservationType(ttype),
54-
Quantity: quantity,
55-
Facility: &facility,
53+
54+
req := &metal.IPReservationRequestInput{
5655
Metro: &metro,
5756
Tags: tags,
57+
Quantity: int32(quantity),
58+
Type: ttype,
59+
Facility: &facility,
60+
}
61+
62+
requestIPReservationRequest := &metal.RequestIPReservationRequest{
63+
IPReservationRequestInput: req,
5864
}
5965

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

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

67-
data[0] = []string{reservation.ID, reservation.Address, strconv.FormatBool(reservation.Public), reservation.Created}
73+
data[0] = []string{reservation.IPReservation.GetId(),
74+
reservation.IPReservation.GetAddress(),
75+
strconv.FormatBool(reservation.IPReservation.GetPublic()),
76+
reservation.IPReservation.CreatedAt.String()}
6877
header := []string{"ID", "Address", "Public", "Created"}
6978

7079
return c.Out.Output(reservation, header, &data)

internal/ips/retrieve.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
2526
"strconv"
2627

28+
pager "github.com/equinix/metal-cli/internal/pagination"
2729
"github.com/spf13/cobra"
2830
)
2931

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

6163
cmd.SilenceUsage = true
62-
listOpts := c.Servicer.ListOptions(nil, nil)
64+
inc := []string{}
65+
exc := []string{}
66+
types := []string{}
67+
6368
if assignmentID != "" {
64-
ip, _, err := c.ProjectService.Get(assignmentID, listOpts)
69+
ip, _, err := c.IPService.FindIPAddressById(context.Background(), assignmentID).Include(inc).Exclude(exc).Execute()
6570
if err != nil {
6671
return fmt.Errorf("Could not get Device IP address: %w", err)
6772
}
6873

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

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

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

8186
data := make([][]string, 1)
8287
code := ""
8388
metro := ""
84-
if ip.Facility != nil {
85-
code = ip.Facility.Code
89+
if ip.IPReservation.Facility != nil {
90+
code = ip.IPReservation.Facility.GetCode()
8691
}
87-
if ip.Metro != nil {
88-
metro = ip.Metro.Code
92+
93+
if ip.IPReservation.Metro != nil {
94+
metro = ip.IPReservation.Metro.GetCode()
8995
}
90-
data[0] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}
96+
97+
data[0] = []string{ip.IPReservation.GetId(), ip.IPReservation.GetAddress(), metro, code, strconv.FormatBool(ip.IPReservation.GetPublic()), ip.IPReservation.CreatedAt.String()}
9198
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}
9299

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

96-
ips, _, err := c.ProjectService.List(projectID, listOpts)
103+
ips, err := pager.GetAllIPReservations(c.IPService, projectID, inc, exc, types)
97104
if err != nil {
98105
return fmt.Errorf("Could not list Project IP addresses: %w", err)
99106
}
@@ -103,13 +110,13 @@ func (c *Client) Retrieve() *cobra.Command {
103110
for i, ip := range ips {
104111
code := ""
105112
metro := ""
106-
if ip.Facility != nil {
107-
code = ip.Facility.Code
113+
if ip.IPReservation.Facility != nil {
114+
code = ip.IPReservation.Facility.GetCode()
108115
}
109-
if ip.Metro != nil {
110-
metro = ip.Metro.Code
116+
if ip.IPReservation.Metro != nil {
117+
metro = ip.IPReservation.Metro.GetCode()
111118
}
112-
data[i] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}
119+
data[i] = []string{ip.IPReservation.GetId(), ip.IPReservation.GetAddress(), metro, code, strconv.FormatBool(ip.IPReservation.GetPublic()), ip.IPReservation.CreatedAt.String()}
113120
}
114121
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}
115122

internal/ips/unassign.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ips
2222

2323
import (
24+
"context"
2425
"fmt"
2526

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

3940
RunE: func(cmd *cobra.Command, args []string) error {
4041
cmd.SilenceUsage = true
41-
_, err := c.DeviceService.Unassign(assignmentID)
42+
_, err := c.IPService.DeleteIPAddress(context.Background(), assignmentID).Execute()
4243
if err != nil {
4344
return fmt.Errorf("Could not unassign IP address: %w", err)
4445
}

internal/pagination/pager.go

+20
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ func GetAllProjects(s metal.ProjectsApiService, include []string, exclude []stri
2424
return projects, nil
2525
}
2626
}
27+
28+
func GetAllIPReservations(s metal.IPAddressesApiService, projectId string, inc, exc, types []string) ([]metal.IPReservationListIpAddressesInner, error) {
29+
var ipReservations []metal.IPReservationListIpAddressesInner
30+
page := int32(1) // int32 | Page to return (optional) (default to 1)
31+
perPage := int32(20) // int32 | Items returned per page (optional) (default to 10)
32+
33+
for {
34+
ipReservationsPage, _, err := s.FindIPReservations(context.Background(), projectId).Types(types).Include(inc).Exclude(exc).PerPage(perPage).Execute()
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
ipReservations = append(ipReservations, ipReservationsPage.GetIpAddresses()...)
40+
if ipReservationsPage.Meta.GetLastPage() > ipReservationsPage.Meta.GetCurrentPage() {
41+
page = page + 1
42+
continue
43+
}
44+
return ipReservations, nil
45+
}
46+
}

0 commit comments

Comments
 (0)