Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated metal-go client for sub-commands ports #290

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions internal/ports/convert.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Equinix Metal Developers <[email protected]>

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

View workflow job for this annotation

GitHub Actions / lint

: # github.com/equinix/metal-cli/internal/ports
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -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 @@
// TODO: can we add ip-reservation-id?
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
inc := []string{}
exc := []string{}
Comment on lines +58 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be wired up to the --include and --exclude flags

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 / 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

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

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 / 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

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
}
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 / 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

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
}

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 @@
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 / 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)

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)
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 @@

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil)
inc := []string{}
exc := []string{}
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should also be wired up to the --include and --exclude flags


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 / 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)

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)
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 @@

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 @@

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 / 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)

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
header := []string{"ID", "Name", "Type", "Network Type", "MAC", "Bonded"}

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