Skip to content

Commit

Permalink
feat: add IP addresses to the metal device get output (#476)
Browse files Browse the repository at this point in the history
Closes: #242
  • Loading branch information
ajmaidak authored Aug 7, 2024
1 parent 2d11773 commit 74d68bf
Showing 1 changed file with 41 additions and 35 deletions.
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

0 comments on commit 74d68bf

Please sign in to comment.