Skip to content

Commit

Permalink
Updated metal-go client for sub-commands ports
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Oct 2, 2023
1 parent ccfb9bd commit 2c3c387
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
48 changes: 30 additions & 18 deletions internal/ports/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
package ports

import (
"context"
"fmt"
"log"
"net/http"
"strconv"

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

Expand All @@ -53,26 +55,38 @@ func (c *Client) Convert() *cobra.Command {
// TODO: can we add ip-reservation-id?
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
inc := []string{}
exc := []string{}
f := cmd.Flag("bonded")
if !f.Changed {
_, _, err := c.PortService.BondPort(context.Background(), portID).BulkEnable(bulk).Execute()
if err != nil {
return fmt.Errorf("failed to change port bonding: %w", err)
}
}

if f := cmd.Flag("bonded"); f.Changed {
_, _, err := map[bool]func(string, bool) (*packngo.Port, *packngo.Response, error){
true: c.PortService.Bond,
false: c.PortService.Disbond,
}[bonded](portID, bulk)
if f.Changed {
_, _, err := c.PortService.DisbondPort(context.Background(), portID).BulkDisable(bulk).Execute()
if err != nil {
return fmt.Errorf("failed to change port bonding: %w", err)
}
}
addrs := []packngo.AddressRequest{{AddressFamily: 4, Public: false}}
addressFamily := int32(4)
public := false
addrs := []metal.IPAddress{{AddressFamily: &addressFamily, Public: &public}}

Check failure on line 76 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / test

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 76 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 76 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 76 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

if f := cmd.Flag("public-ipv4"); f.Changed {
addrs = append(addrs, packngo.AddressRequest{AddressFamily: 4, Public: true})
addressFamily = int32(4)
public = true
addrs = append(addrs, metal.IPAddress{AddressFamily: &addressFamily, Public: &public})

Check failure on line 81 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / test

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 81 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 81 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal
}
if f := cmd.Flag("public-ipv6"); f.Changed {
addrs = append(addrs, packngo.AddressRequest{AddressFamily: 6, Public: true})
addressFamily = int32(6)
public = true
addrs = append(addrs, metal.IPAddress{AddressFamily: &addressFamily, Public: &public})

Check failure on line 86 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / test

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 86 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal

Check failure on line 86 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &addressFamily (value of type *int32) as type *v1.IPAddressAddressFamily in struct literal
}

convToL2 := func(portID string) (*packngo.Port, *packngo.Response, error) {
convToL2 := func(portID string) (*metal.Port, *http.Response, error) {
if !force {
prompt := promptui.Prompt{
Label: fmt.Sprintf("Are you sure you want to convert Port %s to Layer2 and remove assigned IP addresses: ", portID),
Expand All @@ -84,32 +98,30 @@ func (c *Client) Convert() *cobra.Command {
return nil, nil, nil
}
}
return c.PortService.ConvertToLayerTwo(portID)
return c.PortService.ConvertLayer2(context.Background(), portID).Execute()
}
convToL3 := func(portID string) (*packngo.Port, *packngo.Response, error) {
convToL3 := func(portID string) (*metal.Port, *http.Response, error) {
log.Printf("Converting port %s to layer-3 with addresses %v", portID, addrs)
return c.PortService.ConvertToLayerThree(portID, addrs)
return c.PortService.ConvertLayer3(context.Background(), portID).Execute()
}
if f := cmd.Flag("layer2"); f.Changed {
_, _, err := map[bool]func(string) (*packngo.Port, *packngo.Response, error){
_, _, err := map[bool]func(string) (*metal.Port, *http.Response, error){
true: convToL2,
false: convToL3,
}[layer2](portID)
if err != nil {
return fmt.Errorf("failed to change port network mode: %w", err)
}
}
listOpts := c.Servicer.ListOptions(nil, nil)

getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
port, _, err := c.PortService.Get(portID, getOpts)
port, _, err := c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude(exc).Execute()

Check failure on line 117 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / test

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)

Check failure on line 117 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)

Check failure on line 117 in internal/ports/convert.go

View workflow job for this annotation

GitHub Actions / lint

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)
if err != nil {
return fmt.Errorf("Could not get Port: %w", err)
}

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

data[0] = []string{port.ID, port.Name, port.Type, port.NetworkType, port.Data.MAC, strconv.FormatBool(port.Data.Bonded)}
data[0] = []string{port.GetId(), port.GetName(), port.GetType(), port.GetNetworkType(), port.Data.GetMac(), strconv.FormatBool(port.Data.GetBonded())}
header := []string{"ID", "Name", "Type", "Network Type", "MAC", "Bonded"}

return c.Out.Output(port, header, &data)
Expand Down
13 changes: 6 additions & 7 deletions internal/ports/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
package ports

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
PortService packngo.PortService
VLANService packngo.VLANAssignmentService
PortService metal.PortsApiService
VLANService metal.VLANsApiService
Out outputs.Outputer
}

Expand All @@ -45,8 +45,8 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.PortService = c.Servicer.API(cmd).Ports
c.VLANService = c.Servicer.API(cmd).VLANAssignments
c.PortService = *c.Servicer.MetalAPI(cmd).PortsApi
c.VLANService = *c.Servicer.MetalAPI(cmd).VLANsApi
},
}

Expand All @@ -59,8 +59,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
10 changes: 5 additions & 5 deletions internal/ports/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
package ports

import (
"context"
"fmt"
"strconv"

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

Expand All @@ -41,17 +41,17 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil)
inc := []string{}
exc := []string{}

getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
port, _, err := c.PortService.Get(portID, getOpts)
port, _, err := c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude(exc).Execute()

Check failure on line 47 in internal/ports/retrieve.go

View workflow job for this annotation

GitHub Actions / test

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)

Check failure on line 47 in internal/ports/retrieve.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)

Check failure on line 47 in internal/ports/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

c.PortService.FindPortById(context.Background(), portID).Include(inc).Exclude undefined (type v1.ApiFindPortByIdRequest has no field or method Exclude)
if err != nil {
return fmt.Errorf("Could not get Port: %w", err)
}

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

data[0] = []string{port.ID, port.Name, port.Type, port.NetworkType, port.Data.MAC, strconv.FormatBool(port.Data.Bonded)}
data[0] = []string{port.GetId(), port.GetName(), port.GetType(), port.GetNetworkType(), port.Data.GetMac(), strconv.FormatBool(port.Data.GetBonded())}
header := []string{"ID", "Name", "Type", "Network Type", "MAC", "Bonded"}

return c.Out.Output(port, header, &data)
Expand Down
34 changes: 21 additions & 13 deletions internal/ports/vlans.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
package ports

import (
"context"
"errors"
"fmt"
"strconv"

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

Expand All @@ -50,28 +51,35 @@ func (c *Client) Vlans() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions([]string{"port"}, nil)
// inc := []string{"port"}
// exc := []string{}

req := metal.NewPortVlanAssignmentBatchCreateInput()

getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
req := &packngo.VLANAssignmentBatchCreateRequest{}
f := false
t := true
for _, vlan := range assignments {
assignment := packngo.VLANAssignmentCreateRequest{VLAN: vlan, State: packngo.VLANAssignmentAssigned, Native: &f}
req.VLANAssignments = append(req.VLANAssignments, assignment)
CreateInputVlanAssignmentsInne := []metal.PortVlanAssignmentBatchCreateInputVlanAssignmentsInner{}
CreateInputVlanAssignmentsInne[0].SetVlan(vlan)
CreateInputVlanAssignmentsInne[0].SetNative(f)
req.SetVlanAssignments(CreateInputVlanAssignmentsInne)
}
for _, vlan := range unassignments {
assignment := packngo.VLANAssignmentCreateRequest{VLAN: vlan, State: packngo.VLANAssignmentUnassigned}
req.VLANAssignments = append(req.VLANAssignments, assignment)
CreateInputVlanAssignmentsInne := []metal.PortVlanAssignmentBatchCreateInputVlanAssignmentsInner{}
CreateInputVlanAssignmentsInne[0].SetVlan(vlan)
req.SetVlanAssignments(CreateInputVlanAssignmentsInne)
}
if native != "" {
assignment := packngo.VLANAssignmentCreateRequest{VLAN: native, State: packngo.VLANAssignmentAssigned, Native: &t}
req.VLANAssignments = append(req.VLANAssignments, assignment)
CreateInputVlanAssignmentsInner := []metal.PortVlanAssignmentBatchCreateInputVlanAssignmentsInner{}
CreateInputVlanAssignmentsInner[0].SetVlan(native)
CreateInputVlanAssignmentsInner[0].SetNative(t)
req.SetVlanAssignments(CreateInputVlanAssignmentsInner)
}
if len(req.VLANAssignments) == 0 {

if len(req.GetVlanAssignments()) == 0 {
return errors.New("no VLAN assignments specified")
}
batch, _, err := c.VLANService.CreateBatch(portID, req, getOpts)
batch, _, err := c.PortService.CreatePortVlanAssignmentBatch(context.Background(), portID).PortVlanAssignmentBatchCreateInput(*req).Execute()
if err != nil {
return fmt.Errorf("Could not update port VLAN assignments: %w", err)
}
Expand All @@ -81,7 +89,7 @@ func (c *Client) Vlans() *cobra.Command {

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

data[0] = []string{port.ID, port.Name, port.Type, port.NetworkType, port.Data.MAC, strconv.FormatBool(port.Data.Bonded)}
data[0] = []string{port.GetId(), port.GetName(), port.GetType(), port.GetNetworkType(), port.Data.GetMac(), strconv.FormatBool(port.Data.GetBonded())}

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / test

cannot use port.GetType() (value of type v1.PortType) as type string in array or slice literal

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / test

cannot use port.GetNetworkType() (value of type v1.PortNetworkType) as type string in array or slice literal

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

cannot use port.GetType() (value of type v1.PortType) as type string in array or slice literal

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

cannot use port.GetNetworkType() (value of type v1.PortNetworkType) as type string in array or slice literal

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / lint

cannot use port.GetType() (value of type v1.PortType) as type string in array or slice literal

Check failure on line 92 in internal/ports/vlans.go

View workflow job for this annotation

GitHub Actions / lint

cannot use port.GetNetworkType() (value of type v1.PortNetworkType) as type string in array or slice literal (typecheck)
header := []string{"ID", "Name", "Type", "Network Type", "MAC", "Bonded"}

return c.Out.Output(port, header, &data)
Expand Down

0 comments on commit 2c3c387

Please sign in to comment.