Skip to content

Commit

Permalink
Updated metal-go client for sub-commands gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jul 26, 2023
1 parent b078844 commit 083c748
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
21 changes: 12 additions & 9 deletions internal/gateway/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
package gateway

import (
"context"
"fmt"
"strconv"

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

Expand All @@ -46,25 +47,27 @@ func (c *Client) Create() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

req := &packngo.MetalGatewayCreateRequest{
VirtualNetworkID: vnID,
IPReservationID: reservationID,
PrivateIPv4SubnetSize: netSize,
nSize := int32(netSize)
gatewayRequestInput := &metal.MetalGatewayCreateInput{
VirtualNetworkId: vnID,
IpReservationId: &reservationID,
PrivateIpv4SubnetSize: &nSize,
}

n, _, err := c.Service.Create(projectID, req)
gatewayRequest := metal.CreateMetalGatewayRequest{MetalGatewayCreateInput: gatewayRequestInput}
n, _, err := c.Service.CreateMetalGateway(context.Background(), projectID).CreateMetalGatewayRequest(gatewayRequest).Execute()
if err != nil {
return fmt.Errorf("Could not create Metal Gateway: %w", err)
}

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

if n.IPReservation != nil {
address = n.IPReservation.Address + "/" + strconv.Itoa(n.IPReservation.CIDR)
if n.MetalGateway.IpReservation != nil {
address = n.MetalGateway.IpReservation.GetAddress() + "/" + strconv.Itoa(int(n.MetalGateway.IpReservation.GetCidr()))
}

data[0] = []string{n.ID, n.VirtualNetwork.MetroCode, strconv.Itoa(n.VirtualNetwork.VXLAN), address, string(n.State), n.CreatedAt}
data[0] = []string{n.MetalGateway.GetId(), n.MetalGateway.VirtualNetwork.GetMetroCode(), strconv.Itoa(int(n.MetalGateway.VirtualNetwork.GetVxlan())), address, string(n.MetalGateway.GetState()), n.MetalGateway.CreatedAt.String()}

header := []string{"ID", "Metro", "VXLAN", "Addresses", "State", "Created"}

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

import (
"context"
"fmt"

"github.com/manifoldco/promptui"
Expand All @@ -34,7 +35,7 @@ func (c *Client) Delete() *cobra.Command {
)

deleteGway := func(id string) error {
_, err := c.Service.Delete(id)
_, _, err := c.Service.DeleteMetalGateway(context.Background(), gwayID).Execute()
if err != nil {
return err
}
Expand Down
13 changes: 8 additions & 5 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
package gateway

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
Service packngo.MetalGatewayService
Service metal.MetalGatewaysApiService
Out outputs.Outputer
}

Expand All @@ -45,7 +45,7 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.Service = c.Servicer.API(cmd).MetalGateways
c.Service = *c.Servicer.MetalAPI(cmd).MetalGatewaysApi
},
}

Expand All @@ -58,8 +58,11 @@ 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
Format() outputs.Format
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down
12 changes: 7 additions & 5 deletions internal/gateway/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"strconv"

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

Expand All @@ -42,8 +43,9 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil).Including("virtual_network", "ip_reservation")
gways, _, err := c.Service.List(projectID, listOpts)
inc := []string{"virtual_network", "ip_reservation"}
exc := []string{}
gways, err := pager.GetMetalGatewaysByProject(c.Service, projectID, inc, exc)
if err != nil {
return fmt.Errorf("Could not list Project Metal Gateways: %w", err)
}
Expand All @@ -53,11 +55,11 @@ func (c *Client) Retrieve() *cobra.Command {
for i, n := range gways {
address := ""

if n.IPReservation != nil {
address = n.IPReservation.Address + "/" + strconv.Itoa(n.IPReservation.CIDR)
if n.MetalGateway.IpReservation != nil {
address = n.MetalGateway.IpReservation.GetAddress() + "/" + strconv.Itoa(int(n.MetalGateway.IpReservation.GetCidr()))
}

data[i] = []string{n.ID, n.VirtualNetwork.MetroCode, strconv.Itoa(n.VirtualNetwork.VXLAN), address, string(n.State), n.CreatedAt}
data[i] = []string{n.MetalGateway.GetId(), n.MetalGateway.VirtualNetwork.GetMetroCode(), strconv.Itoa(int(n.MetalGateway.VirtualNetwork.GetVxlan())), address, string(n.MetalGateway.GetState()), n.MetalGateway.CreatedAt.String()}
}
header := []string{"ID", "Metro", "VXLAN", "Addresses", "State", "Created"}

Expand Down
19 changes: 19 additions & 0 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@ func GetAllOrganizations(s metal.OrganizationsApiService, include, exclude []str
return orgs, nil
}
}

func GetMetalGatewaysByProject(s metal.MetalGatewaysApiService, projectId string, inc, exc []string) ([]metal.MetalGatewayListMetalGatewaysInner, error) {
var metalGateways []metal.MetalGatewayListMetalGatewaysInner

page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(20) // int32 | Items returned per page (optional) (default to 10)
for {
metalGatewayPage, _, err := s.FindMetalGatewaysByProject(context.Background(), projectId).Include(inc).Exclude(exc).Page(page).PerPage(perPage).Execute()
if err != nil {
return nil, err
}
metalGateways = append(metalGateways, metalGatewayPage.GetMetalGateways()...)
if metalGatewayPage.Meta.GetLastPage() > metalGatewayPage.Meta.GetCurrentPage() {
page = page + 1
continue
}
return metalGateways, nil
}
}

0 comments on commit 083c748

Please sign in to comment.