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

feat: add IP addresses to the metal device get output #476

Merged
merged 4 commits into from
Aug 7, 2024
Merged
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
76 changes: 41 additions & 35 deletions internal/devices/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"strconv"

"github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,56 +53,61 @@ func (c *Client) Retrieve() *cobra.Command {
}
cmd.SilenceUsage = true

devices := make([]metalv1.Device, 0)

if deviceID != "" {
device, _, err := c.Service.FindDeviceById(context.Background(), deviceID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil)).Execute()
if err != nil {
return fmt.Errorf("Could not get Devices: %w", err)
return fmt.Errorf("could not get Devices: %w", err)
}
devices = append(devices, *device)
} else {
request := c.Service.FindProjectDevices(context.Background(), projectID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil))
filters := c.Servicer.Filters()
if filters["type"] != "" {
request = request.Type_(filters["type"])
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}

data := make([][]string, 1)
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), fmt.Sprintf("%v", device.GetState()), device.GetCreatedAt().String()}

return c.Out.Output(device, header, &data)
}

request := c.Service.FindProjectDevices(context.Background(), projectID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil))
filters := c.Servicer.Filters()
if filters["type"] != "" {
request = request.Type_(filters["type"])
}
if filters["facility"] != "" {
request = request.Facility(filters["facility"])
}

if filters["facility"] != "" {
request = request.Facility(filters["facility"])
}
if filters["hostname"] != "" {
request = request.Hostname(filters["hostname"])
}

if filters["hostname"] != "" {
request = request.Hostname(filters["hostname"])
}
if filters["reserved"] != "" {
value := filters["reserved"]
reserve, rerr := strconv.ParseBool(value)
if rerr != nil {
request = request.Reserved(reserve)
}
}

if filters["reserved"] != "" {
value := filters["reserved"]
reserve, rerr := strconv.ParseBool(value)
if rerr != nil {
request = request.Reserved(reserve)
if filters["tag"] != "" {
request = request.Tag(filters["tag"])
}
}

if filters["tag"] != "" {
request = request.Tag(filters["tag"])
resp, err := request.ExecuteWithPagination()
if err != nil {
return fmt.Errorf("could not list Devices: %w", err)
}
devices = append(devices, resp.Devices...)
}

resp, err := request.ExecuteWithPagination()
if err != nil {
return fmt.Errorf("Could not list Devices: %w", err)
}
devices := resp.Devices
data := make([][]string, len(devices))

for i, dc := range devices {
data[i] = []string{dc.GetId(), dc.GetHostname(), dc.OperatingSystem.GetName(), fmt.Sprintf("%v", dc.GetState()), dc.GetCreatedAt().String()}
ipAddresses := make(map[string]string)
for _, ip := range dc.GetIpAddresses() {
if ip.Address != nil {
key := fmt.Sprintf("ipv%d_%s", ip.GetAddressFamily(),
map[bool]string{true: "public", false: "private"}[ip.GetPublic()])
ipAddresses[key] = ip.GetAddress()
}
}
data[i] = []string{dc.GetId(), dc.GetHostname(), ipAddresses["ipv4_public"], ipAddresses["ipv6_public"], ipAddresses["ipv4_private"], dc.OperatingSystem.GetName(), fmt.Sprintf("%v", dc.GetState()), dc.GetCreatedAt().String()}
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}
header := []string{"ID", "Hostname", "IPv4 Public", "IPv6 Public", "IPv4 Private", "OS", "State", "Created"}

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