Skip to content

Commit

Permalink
retrieve device by ID and device list should have the same output
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmaidak committed Jun 21, 2024
1 parent b709d64 commit df1f8fe
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions internal/devices/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"

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

Expand Down Expand Up @@ -53,52 +54,49 @@ 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)
}
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)
}
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"])
}

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 {
ips := make([]string, 0)
for _, ip := range dc.GetIpAddresses() {
Expand Down

0 comments on commit df1f8fe

Please sign in to comment.