Skip to content

Commit

Permalink
Updated metal-go client for sub-command devices (#282)
Browse files Browse the repository at this point in the history
Breakout from #270

What this PR does / why we need it:

This PR replaces packngo with metal-go for all interactions with the
Equinix Metal API specifically for Device sub commands

Test Results:
================
# Retrieve
## Cmd: metal devices get

* Packngo:
```
vasu@vasu-cloud:~/Equinix$ metal devices get
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
|                  ID                  |              HOSTNAME               |        OS        | STATE  |       CREATED        |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
| d231a5d0-3da9-4e4b-bbd6-a367c4c2d34b | test-staging-2                      | Ubuntu 20.04 LTS | active | 2023-06-01T08:42:42Z |
| bcab4df0-945e-4cb5-a8c7-f9dfbaabb3d7 | eksa-node-003                       | Custom iPXE      | active | 2023-04-25T12:33:17Z |
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo                        | Ubuntu 20.04 LTS | active | 2023-06-26T13:09:24Z |
| b396c4fd-c412-4a25-b6b4-df6f2f548629 | test-staging-12                     | Ubuntu 22.04 LTS | active | 2023-06-01T08:43:12Z |
| bad6f156-4777-49a5-87e7-d038e7365c9e | capi-quickstart-control-plane-mkz7l | Ubuntu 22.04 LTS | active | 2023-05-03T09:42:52Z |
| d7126b67-a96f-48b7-9cf1-e1914d84235b | capi-quickstart-worker-a-spr64      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:01Z |
| 4c397b2d-eccf-4948-9fcf-2b325303048c | capi-quickstart-control-plane-98xjq | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:05Z |
| bdbe09fa-d140-4cc9-b988-f51610e8c2f3 | capi-quickstart-worker-a-q89f8      | Ubuntu 22.04 LTS | active | 2023-05-03T09:47:59Z |
| 67992861-066b-4d76-a6a4-a21b3e855722 | capi-quickstart-worker-a-qqnhb      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:00Z |
| b15870cd-a83d-48bd-9552-5281162ae563 | capi-quickstart-control-plane-lxnsr | Ubuntu 22.04 LTS | active | 2023-05-03T09:54:11Z |
| 2f6d7ff2-6aa5-49bc-838a-97a3473ea16a | eksa-node-002                       | Custom iPXE      | active | 2023-04-24T18:24:15Z |
| 46005014-5256-41b3-938a-1b295f931aa2 | eksa-node-001                       | Custom iPXE      | active | 2023-04-24T18:24:12Z |
| c3b1006c-0422-4f01-9c96-24dbcd72f7f1 | eksa-admin                          | Ubuntu 20.04 LTS | active | 2023-04-24T18:22:35Z |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
```
* Metal-Go
```
vasu@vasu-cloud:~/Equinix$ metal devices get
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
|                  ID                  |              HOSTNAME               |        OS        | STATE  |       CREATED        |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
| d231a5d0-3da9-4e4b-bbd6-a367c4c2d34b | test-staging-2                      | Ubuntu 20.04 LTS | active | 2023-06-01T08:42:42Z |
| bcab4df0-945e-4cb5-a8c7-f9dfbaabb3d7 | eksa-node-003                       | Custom iPXE      | active | 2023-04-25T12:33:17Z |
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo                        | Ubuntu 20.04 LTS | active | 2023-06-26T13:09:24Z |
| b396c4fd-c412-4a25-b6b4-df6f2f548629 | test-staging-12                     | Ubuntu 22.04 LTS | active | 2023-06-01T08:43:12Z |
| bad6f156-4777-49a5-87e7-d038e7365c9e | capi-quickstart-control-plane-mkz7l | Ubuntu 22.04 LTS | active | 2023-05-03T09:42:52Z |
| d7126b67-a96f-48b7-9cf1-e1914d84235b | capi-quickstart-worker-a-spr64      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:01Z |
| 4c397b2d-eccf-4948-9fcf-2b325303048c | capi-quickstart-control-plane-98xjq | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:05Z |
| bdbe09fa-d140-4cc9-b988-f51610e8c2f3 | capi-quickstart-worker-a-q89f8      | Ubuntu 22.04 LTS | active | 2023-05-03T09:47:59Z |
| 67992861-066b-4d76-a6a4-a21b3e855722 | capi-quickstart-worker-a-qqnhb      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:00Z |
| b15870cd-a83d-48bd-9552-5281162ae563 | capi-quickstart-control-plane-lxnsr | Ubuntu 22.04 LTS | active | 2023-05-03T09:54:11Z |
| 2f6d7ff2-6aa5-49bc-838a-97a3473ea16a | eksa-node-002                       | Custom iPXE      | active | 2023-04-24T18:24:15Z |
| 46005014-5256-41b3-938a-1b295f931aa2 | eksa-node-001                       | Custom iPXE      | active | 2023-04-24T18:24:12Z |
| c3b1006c-0422-4f01-9c96-24dbcd72f7f1 | eksa-admin                          | Ubuntu 20.04 LTS | active | 2023-04-24T18:22:35Z |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+

```

## Cmd: metal devices get -p <Project_ID>
* Packngo:
```
vasu@vasu-cloud:~/Equinix$ metal devices get -p 65dcd8f4-abd4-4e7a-8b29-da4b030edd96
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
|                  ID                  |              HOSTNAME               |        OS        | STATE  |       CREATED        |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
| d231a5d0-3da9-4e4b-bbd6-a367c4c2d34b | test-staging-2                      | Ubuntu 20.04 LTS | active | 2023-06-01T08:42:42Z |
| bcab4df0-945e-4cb5-a8c7-f9dfbaabb3d7 | eksa-node-003                       | Custom iPXE      | active | 2023-04-25T12:33:17Z |
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo                        | Ubuntu 20.04 LTS | active | 2023-06-26T13:09:24Z |
| b396c4fd-c412-4a25-b6b4-df6f2f548629 | test-staging-12                     | Ubuntu 22.04 LTS | active | 2023-06-01T08:43:12Z |
| bad6f156-4777-49a5-87e7-d038e7365c9e | capi-quickstart-control-plane-mkz7l | Ubuntu 22.04 LTS | active | 2023-05-03T09:42:52Z |
| d7126b67-a96f-48b7-9cf1-e1914d84235b | capi-quickstart-worker-a-spr64      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:01Z |
| 4c397b2d-eccf-4948-9fcf-2b325303048c | capi-quickstart-control-plane-98xjq | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:05Z |
| bdbe09fa-d140-4cc9-b988-f51610e8c2f3 | capi-quickstart-worker-a-q89f8      | Ubuntu 22.04 LTS | active | 2023-05-03T09:47:59Z |
| 67992861-066b-4d76-a6a4-a21b3e855722 | capi-quickstart-worker-a-qqnhb      | Ubuntu 22.04 LTS | active | 2023-05-03T09:48:00Z |
| b15870cd-a83d-48bd-9552-5281162ae563 | capi-quickstart-control-plane-lxnsr | Ubuntu 22.04 LTS | active | 2023-05-03T09:54:11Z |
| 2f6d7ff2-6aa5-49bc-838a-97a3473ea16a | eksa-node-002                       | Custom iPXE      | active | 2023-04-24T18:24:15Z |
| 46005014-5256-41b3-938a-1b295f931aa2 | eksa-node-001                       | Custom iPXE      | active | 2023-04-24T18:24:12Z |
| c3b1006c-0422-4f01-9c96-24dbcd72f7f1 | eksa-admin                          | Ubuntu 20.04 LTS | active | 2023-04-24T18:22:35Z |
+--------------------------------------+-------------------------------------+------------------+--------+----------------------+
```
* Metal-Go
```
vasu@vasu-cloud:~/Equinix/metal-cli/bin$ ./metal devices get -p 65dcd8f4-abd4-4e7a-8b29-da4b030edd96
+--------------------------------------+-------------------------------------+------------------+--------+-------------------------------+
|                  ID                  |              HOSTNAME               |        OS        | STATE  |            CREATED            |
+--------------------------------------+-------------------------------------+------------------+--------+-------------------------------+
| d231a5d0-3da9-4e4b-bbd6-a367c4c2d34b | test-staging-2                      | Ubuntu 20.04 LTS | active | 2023-06-01 08:42:42 +0000 UTC |
| bcab4df0-945e-4cb5-a8c7-f9dfbaabb3d7 | eksa-node-003                       | Custom iPXE      | active | 2023-04-25 12:33:17 +0000 UTC |
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo                        | Ubuntu 20.04 LTS | active | 2023-06-26 13:09:24 +0000 UTC |
| b396c4fd-c412-4a25-b6b4-df6f2f548629 | test-staging-12                     | Ubuntu 22.04 LTS | active | 2023-06-01 08:43:12 +0000 UTC |
| bad6f156-4777-49a5-87e7-d038e7365c9e | capi-quickstart-control-plane-mkz7l | Ubuntu 22.04 LTS | active | 2023-05-03 09:42:52 +0000 UTC |
| d7126b67-a96f-48b7-9cf1-e1914d84235b | capi-quickstart-worker-a-spr64      | Ubuntu 22.04 LTS | active | 2023-05-03 09:48:01 +0000 UTC |
| 4c397b2d-eccf-4948-9fcf-2b325303048c | capi-quickstart-control-plane-98xjq | Ubuntu 22.04 LTS | active | 2023-05-03 09:48:05 +0000 UTC |
| bdbe09fa-d140-4cc9-b988-f51610e8c2f3 | capi-quickstart-worker-a-q89f8      | Ubuntu 22.04 LTS | active | 2023-05-03 09:47:59 +0000 UTC |
| 67992861-066b-4d76-a6a4-a21b3e855722 | capi-quickstart-worker-a-qqnhb      | Ubuntu 22.04 LTS | active | 2023-05-03 09:48:00 +0000 UTC |
| b15870cd-a83d-48bd-9552-5281162ae563 | capi-quickstart-control-plane-lxnsr | Ubuntu 22.04 LTS | active | 2023-05-03 09:54:11 +0000 UTC |
| 2f6d7ff2-6aa5-49bc-838a-97a3473ea16a | eksa-node-002                       | Custom iPXE      | active | 2023-04-24 18:24:15 +0000 UTC |
| 46005014-5256-41b3-938a-1b295f931aa2 | eksa-node-001                       | Custom iPXE      | active | 2023-04-24 18:24:12 +0000 UTC |
| c3b1006c-0422-4f01-9c96-24dbcd72f7f1 | eksa-admin                          | Ubuntu 20.04 LTS | active | 2023-04-24 18:22:35 +0000 UTC |
+--------------------------------------+-------------------------------------+------------------+--------+-------------------------------+
```

## Cmd: metal devices get -i <Device ID>
* Packngo:
```
vasu@vasu-cloud:~/Equinix$ metal devices get -i 09be98b7-0db6-4fdb-90be-901ab847158d
+--------------------------------------+--------------+------------------+--------+----------------------+
|                  ID                  |   HOSTNAME   |        OS        | STATE  |       CREATED        |
+--------------------------------------+--------------+------------------+--------+----------------------+
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo | Ubuntu 20.04 LTS | active | 2023-06-26T13:09:24Z |
+--------------------------------------+--------------+------------------+--------+----------------------+
```
*Metal-go:
```
vasu@vasu-cloud:~/Equinix/metal-cli/bin$ ./metal devices get -i 09be98b7-0db6-4fdb-90be-901ab847158d
+--------------------------------------+--------------+------------------+--------+-------------------------------+
|                  ID                  |   HOSTNAME   |        OS        | STATE  |            CREATED            |
+--------------------------------------+--------------+------------------+--------+-------------------------------+
| 09be98b7-0db6-4fdb-90be-901ab847158d | test-packngo | Ubuntu 20.04 LTS | active | 2023-06-26 13:09:24 +0000 UTC |
+--------------------------------------+--------------+------------------+--------+-------------------------------+
```
# Create

```
Vasubabus-MacBook-Pro:bin vasubabu$ ./metal device create -p c309ce2c-160d-47e2-bb26-9debeb0be87d -P m3.small.x86 -m da -O ubuntu_20_04 -H create-device-dev-cdata --customdata '{"testDate":"fromCli"}'
+--------------------------------------+-------------------------+------------------+--------+-------------------------------+
|                  ID                  |        HOSTNAME         |        OS        | STATE  |            CREATED            |
+--------------------------------------+-------------------------+------------------+--------+-------------------------------+
| 8dc6920e-cf5a-4852-994d-a53094fe9673 | create-device-dev-cdata | Ubuntu 20.04 LTS | queued | 2023-08-24 10:28:29 +0000 UTC |
+--------------------------------------+-------------------------+------------------+--------+-------------------------------+
Vasubabus-MacBook-Pro:bin vasubabu$ ./metal device create -p c309ce2c-160d-47e2-bb26-9debeb0be87d -P m3.small.x86 -m da -O ubuntu_20_04 -H create-dev-cdata --customdata '{"testDate":""}'
+--------------------------------------+------------------+------------------+--------+-------------------------------+
|                  ID                  |     HOSTNAME     |        OS        | STATE  |            CREATED            |
+--------------------------------------+------------------+------------------+--------+-------------------------------+
| a45f8746-f3b5-4fc5-8fe6-b4fa6493d365 | create-dev-cdata | Ubuntu 20.04 LTS | queued | 2023-08-24 10:40:50 +0000 UTC |
+--------------------------------------+------------------+------------------+--------+-------------------------------+
```

```
Vasubabus-MacBook-Pro:bin vasubabu$ ./metal device create -p c309ce2c-160d-47e2-bb26-9debeb0be87d --hostname netboot-custom-ipxe-1 --plan c3.small.x86 --metro sv --operating-system custom_ipxe --userdata='#!ipxe
chain -ar https://boot.netboot.xyz' --always-pxe=true
+--------------------------------------+-----------------------+-------------+--------+-------------------------------+
|                  ID                  |       HOSTNAME        |     OS      | STATE  |            CREATED            |
+--------------------------------------+-----------------------+-------------+--------+-------------------------------+
| b149fc32-bcfd-44c8-9c0e-9e5d49d382e0 | netboot-custom-ipxe-1 | Custom iPXE | queued | 2023-08-24 10:48:33 +0000 UTC |
+--------------------------------------+-----------------------+-------------+--------+-------------------------------+

<img width="1430" alt="image" src="https://github.com/equinix/metal-cli/assets/3358152/ea4d5a5a-b495-40e7-a7ec-e3a6905ac5c7">

```

```
Vasubabus-MacBook-Pro:bin vasubabu$ ./metal device create -p c309ce2c-160d-47e2-bb26-9debeb0be87d --hostname netboot-custom-ipxe --plan c3.small.x86 --metro sv --operating-system custom_ipxe --userdata='#!ipxe
> chain -ar https://boot.netboot.xyz'
+--------------------------------------+---------------------+-------------+--------+-------------------------------+
|                  ID                  |      HOSTNAME       |     OS      | STATE  |            CREATED            |
+--------------------------------------+---------------------+-------------+--------+-------------------------------+
| 4aa8977b-259b-4684-a3b4-9ff49e96db47 | netboot-custom-ipxe | Custom iPXE | queued | 2023-08-24 10:44:02 +0000 UTC |
+--------------------------------------+---------------------+-------------+--------+-------------------------------+

<img width="1416" alt="image" src="https://github.com/equinix/metal-cli/assets/3358152/a9ff3f36-4716-4cbd-a9be-cc2dc8e672b7">

```
  • Loading branch information
codinja1188 authored Aug 29, 2023
1 parent eaf83d5 commit 3d6c824
Show file tree
Hide file tree
Showing 28 changed files with 1,019 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ jobs:
- name: Get dependencies
run: go mod download
- name: Run end-to-end tests
run: go test -v -cover -coverpkg=./... -parallel 4 ./test/e2e/...
run: go test -v -cover -coverpkg=./... -parallel 4 ./test/e2e/... -timeout 1000s
env:
METAL_AUTH_TOKEN: ${{ secrets.METAL_AUTH_TOKEN }}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ generate-docs: clean-docs
go run ./cmd/metal docs ./docs

test:
go test -v ./...
go test -v ./... -timeout 1000s


## --------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/metal_device_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ metal device create -p <project_id> (-m <metro> | -f <facility>) -P <plan> -H <h

```
-a, --always-pxe Sets whether the device always PXE boots on reboot.
-b, --billing-cycle string Billing cycle (default "hourly")
-b, --billing-cycle string Billing cycle (default "hourly")
-c, --customdata string Custom data to be included with your device's metadata.
-f, --facility string Code of the facility where the device will be created
-r, --hardware-reservation-id string The UUID of a hardware reservation, if you are provisioning a server from your reserved hardware.
Expand All @@ -39,7 +39,7 @@ metal device create -p <project_id> (-m <metro> | -f <facility>) -P <plan> -H <h
-s, --spot-instance Provisions the device as a spot instance.
--spot-price-max float Sets the maximum spot market price for the device: --spot-price-max=1.2
-t, --tags strings Tag or list of tags for the device: --tags="tag1,tag2".
-T, --termination-time string Device termination time: --termination-time="15:04:05"
-T, --termination-time string Device termination time: --termination-time="2023-08-24T15:04:05Z"
-u, --userdata string Userdata for device initialization. Can not be used with --userdata-file.
--userdata-file string Path to a userdata file for device initialization. Can not be used with --userdata.
```
Expand Down
127 changes: 95 additions & 32 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
package devices

import (
"context"
"encoding/json"
"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,62 +66,123 @@ 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")
}
cmd.SilenceUsage = true

if userdataFile != "" {
userdataRaw, readErr := os.ReadFile(userdataFile)
if readErr != nil {
return fmt.Errorf("Could not read userdata-file: %w", readErr)
return fmt.Errorf("could not read userdata-file: %w", readErr)
}
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)
return fmt.Errorf("could not parse time %q: %w", terminationTime, err)
}
endDt = &packngo.Timestamp{Time: parsedTime}
endDt = parsedTime
}

formatedCdata := make(map[string]interface{})
if customdata != "" {
err := json.Unmarshal([]byte(customdata), &formatedCdata)
if err != nil {
return fmt.Errorf("could not convert customedata :%w", err)
}
}
var facilityArgs []string

var request metal.ApiCreateDeviceRequest
if facility != "" {
facilityArgs = append(facilityArgs, facility)
}

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,
facilityDeviceRequest := metal.CreateDeviceRequest{
DeviceCreateInFacilityInput: &metal.DeviceCreateInFacilityInput{
Facility: facilityArgs,
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
Userdata: &userdata,
Tags: tags,
},
}

if billingCycle != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(billingCycle)
}
if alwaysPXE {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE)
}

if ipxescripturl != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetIpxeScriptUrl(ipxescripturl)
}
if publicIPv4SubnetSize != 0 {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetPublicIpv4SubnetSize(int32(publicIPv4SubnetSize))
}
if terminationTime != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetTerminationTime(endDt)
}
facilityDeviceRequest.DeviceCreateInFacilityInput.SetSpotInstance(spotInstance)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetSpotPriceMax(float32(spotPriceMax))
facilityDeviceRequest.DeviceCreateInFacilityInput.SetCustomdata(formatedCdata)

if hardwareReservationID != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetHardwareReservationId(hardwareReservationID)
}

request = c.Service.CreateDevice(context.Background(), projectID).CreateDeviceRequest(facilityDeviceRequest).Include(nil).Exclude(nil)
}

device, _, err := c.Service.Create(request)
if metro != "" {

metroDeviceRequest := metal.CreateDeviceRequest{
DeviceCreateInMetroInput: &metal.DeviceCreateInMetroInput{
Metro: metro,
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
Userdata: &userdata,
Tags: tags,
},
}
if billingCycle != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(billingCycle)
}

if alwaysPXE {
metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(alwaysPXE)
}

if ipxescripturl != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetIpxeScriptUrl(ipxescripturl)
}
if publicIPv4SubnetSize != 0 {
metroDeviceRequest.DeviceCreateInMetroInput.SetPublicIpv4SubnetSize(int32(publicIPv4SubnetSize))
}
if terminationTime != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetTerminationTime(endDt)
}
metroDeviceRequest.DeviceCreateInMetroInput.SetSpotInstance(spotInstance)
metroDeviceRequest.DeviceCreateInMetroInput.SetSpotPriceMax(float32(spotPriceMax))
metroDeviceRequest.DeviceCreateInMetroInput.SetCustomdata(formatedCdata)

if hardwareReservationID != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetHardwareReservationId(hardwareReservationID)
}
request = c.Service.CreateDevice(context.Background(), projectID).CreateDeviceRequest(metroDeviceRequest).Include(nil).Exclude(nil)
}
device, _, err := request.Execute()
if err != nil {
return fmt.Errorf("Could not create Device: %w", err)
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 All @@ -144,11 +207,11 @@ func (c *Client) Create() *cobra.Command {
createDeviceCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, `Tag or list of tags for the device: --tags="tag1,tag2".`)
createDeviceCmd.Flags().IntVarP(&publicIPv4SubnetSize, "public-ipv4-subnet-size", "S", 0, "Size of the public IPv4 subnet.")
createDeviceCmd.Flags().StringVarP(&hardwareReservationID, "hardware-reservation-id", "r", "", "The UUID of a hardware reservation, if you are provisioning a server from your reserved hardware.")
createDeviceCmd.Flags().StringVarP(&billingCycle, "billing-cycle", "b", "hourly", "Billing cycle")
createDeviceCmd.Flags().StringVarP(&billingCycle, "billing-cycle", "b", "hourly", "Billing cycle ")
createDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets whether the device always PXE boots on reboot.")
createDeviceCmd.Flags().BoolVarP(&spotInstance, "spot-instance", "s", false, "Provisions the device as a spot instance.")
createDeviceCmd.Flags().Float64VarP(&spotPriceMax, "spot-price-max", "", 0, `Sets the maximum spot market price for the device: --spot-price-max=1.2`)
createDeviceCmd.Flags().StringVarP(&terminationTime, "termination-time", "T", "", `Device termination time: --termination-time="15:04:05"`)
createDeviceCmd.Flags().StringVarP(&terminationTime, "termination-time", "T", "", `Device termination time: --termination-time="2023-08-24T15:04:05Z"`)

return createDeviceCmd
}
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
12 changes: 7 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,9 +65,11 @@ 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
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
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
Loading

0 comments on commit 3d6c824

Please sign in to comment.