-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright © 2022 Equinix Metal Developers <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
|
@@ -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" | ||
) | ||
|
||
|
@@ -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{} | ||
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 GitHub Actions / docs (1.19)
Check failure on line 76 in internal/ports/convert.go GitHub Actions / lint
Check failure on line 76 in internal/ports/convert.go GitHub Actions / lint
|
||
|
||
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 GitHub Actions / docs (1.19)
Check failure on line 81 in internal/ports/convert.go GitHub Actions / lint
|
||
} | ||
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 GitHub Actions / docs (1.19)
Check failure on line 86 in internal/ports/convert.go GitHub Actions / lint
|
||
} | ||
|
||
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), | ||
|
@@ -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 GitHub Actions / docs (1.19)
Check failure on line 117 in internal/ports/convert.go GitHub Actions / lint
|
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,10 @@ | |
package ports | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/packethost/packngo" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should also be wired up to the |
||
|
||
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 GitHub Actions / docs (1.19)
Check failure on line 47 in internal/ports/retrieve.go GitHub Actions / lint
|
||
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) | ||
|
There was a problem hiding this comment.
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