Skip to content

Commit

Permalink
Updated metal-go client for sub-command devices
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jun 21, 2023
1 parent b5f4ee5 commit dad5716
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 57 deletions.
64 changes: 41 additions & 23 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
package devices

import (
"context"
"fmt"
"os"
"time"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -64,7 +65,6 @@ func (c *Client) Create() *cobra.Command {
metal device create -p $METAL_PROJECT_ID -P c3.medium.x86 -m sv -H test-rocky -O rocky_8 -r 47161704-1715-4b45-8549-fb3f4b2c32c7`,

RunE: func(cmd *cobra.Command, args []string) error {
var endDt *packngo.Timestamp

if userdata != "" && userdataFile != "" {
return fmt.Errorf("either userdata or userdata-file should be set")
Expand All @@ -78,48 +78,66 @@ func (c *Client) Create() *cobra.Command {
}
userdata = string(userdataRaw)
}
var endDt time.Time

if terminationTime != "" {
parsedTime, err := time.Parse(time.RFC3339, terminationTime)
if err != nil {
return fmt.Errorf("Could not parse time %q: %w", terminationTime, err)
}
endDt = &packngo.Timestamp{Time: parsedTime}
endDt = parsedTime
}

var facilityArgs []string
var request metal.CreateDeviceRequest
spm := float32(spotPriceMax)

if facility != "" {
facilityArgs = append(facilityArgs, facility)

deviceCreateInFacilityInput := &metal.DeviceCreateInFacilityInput{
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
BillingCycle: &billingCycle,
Userdata: &userdata,
IpxeScriptUrl: &ipxescripturl,
AlwaysPxe: &alwaysPXE,
HardwareReservationId: &hardwareReservationID,
SpotInstance: &spotInstance,
SpotPriceMax: &spm,
TerminationTime: &endDt,
Facility: facilityArgs,
}
request = metal.CreateDeviceRequest{DeviceCreateInFacilityInput: deviceCreateInFacilityInput, DeviceCreateInMetroInput: nil}
}

request := &packngo.DeviceCreateRequest{
Hostname: hostname,
Plan: plan,
Facility: facilityArgs,
Metro: metro,
OS: operatingSystem,
BillingCycle: billingCycle,
ProjectID: projectID,
UserData: userdata,
CustomData: customdata,
IPXEScriptURL: ipxescripturl,
Tags: tags,
PublicIPv4SubnetSize: publicIPv4SubnetSize,
AlwaysPXE: alwaysPXE,
HardwareReservationID: hardwareReservationID,
SpotInstance: spotInstance,
SpotPriceMax: spotPriceMax,
TerminationTime: endDt,
if metro != "" {
deviceCreateInMetroInput := &metal.DeviceCreateInMetroInput{
Metro: metro,
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
BillingCycle: &billingCycle,
Userdata: &userdata,
IpxeScriptUrl: &ipxescripturl,
AlwaysPxe: &alwaysPXE,
HardwareReservationId: &hardwareReservationID,
SpotInstance: &spotInstance,
SpotPriceMax: &spm,
TerminationTime: &endDt,
}
request = metal.CreateDeviceRequest{DeviceCreateInFacilityInput: nil, DeviceCreateInMetroInput: deviceCreateInMetroInput}
}

device, _, err := c.Service.Create(request)
device, _, err := c.Service.CreateDevice(context.Background(), projectID).CreateDeviceRequest(request).Execute()
if err != nil {
return fmt.Errorf("Could not create Device: %w", err)
}

header := []string{"ID", "Hostname", "OS", "State", "Created"}
data := make([][]string, 1)
data[0] = []string{device.ID, device.Hostname, device.OS.Name, device.State, device.Created}
data[0] = []string{device.GetId(), device.GetHostname(), *device.GetOperatingSystem().Name, device.GetState(), device.GetCreatedAt().String()}

return c.Out.Output(device, header, &data)
},
Expand Down
3 changes: 2 additions & 1 deletion internal/devices/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package devices

import (
"context"
"fmt"

"github.com/manifoldco/promptui"
Expand All @@ -31,7 +32,7 @@ func (c *Client) Delete() *cobra.Command {
var deviceID string
var force bool
deleteDevice := func(id string) error {
_, err := c.Service.Delete(id, force)
_, err := c.Service.DeleteDevice(context.Background(), id).ForceDelete(force).Execute()
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions internal/devices/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
package devices

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"
"github.com/spf13/viper"
)

type Client struct {
Servicer Servicer
Service packngo.DeviceService
Service metal.DevicesApiService
Out outputs.Outputer
}

Expand All @@ -47,7 +47,7 @@ func (c *Client) NewCommand() *cobra.Command {
}
}

c.Service = c.Servicer.API(cmd).Devices
c.Service = *c.Servicer.MetalAPI(cmd).DevicesApi
},
}

Expand All @@ -65,8 +65,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
Config(cmd *cobra.Command) *viper.Viper
}

Expand Down
5 changes: 4 additions & 1 deletion internal/devices/reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package devices

import (
"context"
"fmt"

metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -38,7 +40,8 @@ func (c *Client) Reboot() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.Reboot(deviceID)
DeviceAction := metal.NewDeviceActionInput("reboot")
_, err := c.Service.PerformAction(context.Background(), deviceID).DeviceActionInput(*DeviceAction).Execute()
if err != nil {
return fmt.Errorf("Could not reboot Device: %w", err)
}
Expand Down
29 changes: 23 additions & 6 deletions internal/devices/reinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package devices

import (
"context"
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -47,14 +48,30 @@ func (c *Client) Reinstall() *cobra.Command {
metal device reinstall -d 50382f72-02b7-4b40-ac8d-253713e1e174 -O ubuntu_22_04 --preserve-data`,
RunE: func(cmd *cobra.Command, args []string) error {

request := packngo.DeviceReinstallFields{
OperatingSystem: operatingSystem,
PreserveData: preserveData,
DeprovisionFast: deprovisionFast,
DeviceAction := metal.NewDeviceActionInput("reinstall")

if preserveData {
DeviceAction.PreserveData = &preserveData
}
if deprovisionFast {
DeviceAction.DeprovisionFast = &deprovisionFast
}
device, _, Err := c.Service.FindDeviceById(context.Background(), id).Execute()
if Err != nil {
fmt.Printf("Error when calling `DevicesApiService.FindDeviceByID``: %v\n", Err)
Err = fmt.Errorf("Could not reinstall Device: %w", Err)
return Err
}

if operatingSystem == "" || operatingSystem == "null" {
DeviceAction.SetOperatingSystem(device.OperatingSystem.GetSlug())
} else {
DeviceAction.OperatingSystem = &operatingSystem
}

_, err := c.Service.Reinstall(id, &request)
_, err := c.Service.PerformAction(context.Background(), id).DeviceActionInput(*DeviceAction).Execute()
if err != nil {
fmt.Printf("Error when calling `DevicesApiService.PerformAction``: %v\n", err)
err = fmt.Errorf("Could not reinstall Device: %w", err)
}

Expand Down
16 changes: 10 additions & 6 deletions internal/devices/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package devices

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -47,27 +48,30 @@ func (c *Client) Retrieve() *cobra.Command {
}
cmd.SilenceUsage = true

include := []string{"Inner_example"}
exclude := []string{"Inner_example"}

if deviceID != "" {
device, _, err := c.Service.Get(deviceID, nil)
device, _, err := c.Service.FindDeviceById(context.Background(), deviceID).Include(include).Exclude(exclude).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.ID, device.Hostname, device.OS.Name, device.State, device.Created}
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), device.GetState(), device.GetCreatedAt().String()}

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

devices, _, err := c.Service.List(projectID, c.Servicer.ListOptions(nil, nil))
devices, _, err := c.Service.FindProjectDevices(context.Background(), projectID).Execute()
if err != nil {
return fmt.Errorf("Could not list Devices: %w", err)
}
data := make([][]string, len(devices))
data := make([][]string, len(devices.GetDevices()))

for i, dc := range devices {
data[i] = []string{dc.ID, dc.Hostname, dc.OS.Name, dc.State, dc.Created}
for i, dc := range devices.GetDevices() {
data[i] = []string{dc.GetId(), dc.GetHostname(), dc.OperatingSystem.GetName(), dc.GetState(), dc.GetCreatedAt().String()}
}
header := []string{"ID", "Hostname", "OS", "State", "Created"}

Expand Down
5 changes: 4 additions & 1 deletion internal/devices/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package devices

import (
"context"
"fmt"

metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -38,7 +40,8 @@ func (c *Client) Start() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.PowerOn(deviceID)
DeviceAction := metal.NewDeviceActionInput("power_on")
_, err := c.Service.PerformAction(context.Background(), deviceID).DeviceActionInput(*DeviceAction).Execute()
if err != nil {
return fmt.Errorf("Could not start Device: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/devices/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package devices

import (
"context"
"fmt"

metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -37,7 +39,8 @@ func (c *Client) Stop() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, err := c.Service.PowerOff(deviceID)
DeviceAction := metal.NewDeviceActionInput("power_off")
_, err := c.Service.PerformAction(context.Background(), deviceID).DeviceActionInput(*DeviceAction).Execute()
if err != nil {
return fmt.Errorf("Could not stop Device: %w", err)
}
Expand Down
33 changes: 20 additions & 13 deletions internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package devices

import (
"context"
"encoding/json"
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -51,48 +53,53 @@ func (c *Client) Update() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.DeviceUpdateRequest{}
deviceUpdate := metal.NewDeviceUpdateInput()

if hostname != "" {
req.Hostname = &hostname
deviceUpdate.Hostname = &hostname
}

if description != "" {
req.Description = &description
deviceUpdate.Description = &description
}

if userdata != "" {
req.UserData = &userdata
deviceUpdate.Userdata = &userdata
}

if locked {
req.Locked = &locked
deviceUpdate.Locked = &locked
}

if len(tags) > 0 {
req.Tags = &tags
deviceUpdate.Tags = tags
}

if alwaysPXE {
req.AlwaysPXE = &alwaysPXE
deviceUpdate.AlwaysPxe = &alwaysPXE
}

if ipxescripturl != "" {
req.IPXEScriptURL = &ipxescripturl
deviceUpdate.IpxeScriptUrl = &ipxescripturl
}

if customdata != "" {
req.CustomData = &customdata
}
var customdataIntr map[string]interface{}
err := json.Unmarshal([]byte(customdata), &customdataIntr)
if err != nil {
panic(err)
}

device, _, err := c.Service.Update(deviceID, req)
deviceUpdate.Customdata = customdataIntr
}
device, _, err := c.Service.UpdateDevice(context.Background(), deviceID).DeviceUpdateInput(*deviceUpdate).Execute()
if err != nil {
return fmt.Errorf("Could not update Device: %w", err)
}

header := []string{"ID", "Hostname", "OS", "State"}
data := make([][]string, 1)
data[0] = []string{device.ID, device.Hostname, device.OS.Name, device.State}
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), device.GetState()}

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

0 comments on commit dad5716

Please sign in to comment.