diff --git a/docs/metal_device_create.md b/docs/metal_device_create.md index a13133c8..16f55b10 100644 --- a/docs/metal_device_create.md +++ b/docs/metal_device_create.md @@ -23,7 +23,7 @@ metal device create -p (-m | -f ) -P -H [-H ] [-d ] [--locked ### Options ``` - -a, --always-pxe Sets the device to always iPXE on reboot. + -a, --always-pxe string Sets the device to always iPXE on reboot. -c, --customdata string Adds or updates custom data to be included with your device's metadata. -d, --description string Adds or updates the description for the device. -h, --help help for update diff --git a/internal/devices/create.go b/internal/devices/create.go index f9873fcb..968e8559 100644 --- a/internal/devices/create.go +++ b/internal/devices/create.go @@ -48,7 +48,7 @@ func (c *Client) Create() *cobra.Command { tags []string ipxescripturl string publicIPv4SubnetSize int - alwaysPXE bool + alwaysPXE string hardwareReservationID string spotInstance bool spotPriceMax float64 @@ -125,8 +125,13 @@ func (c *Client) Create() *cobra.Command { if billingCycle != "" { facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(*validBillingCycle) } - if alwaysPXE { - facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE) + if alwaysPXE != "" { + if alwaysPXE == "true" { + facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(true) + } + if alwaysPXE == "false" { + facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(false) + } } if ipxescripturl != "" { @@ -165,8 +170,13 @@ func (c *Client) Create() *cobra.Command { metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(*validBillingCycle) } - if alwaysPXE { - metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(alwaysPXE) + if alwaysPXE != "" { + if alwaysPXE == "true" { + metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(true) + } + if alwaysPXE == "false" { + metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(false) + } } if ipxescripturl != "" { @@ -219,7 +229,7 @@ func (c *Client) Create() *cobra.Command { 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().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets whether the device always PXE boots on reboot.") + createDeviceCmd.Flags().StringVarP(&alwaysPXE, "always-pxe", "a", "", "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="2023-08-24T15:04:05Z"`) diff --git a/internal/devices/update.go b/internal/devices/update.go index 71b342ea..2da1ebe4 100644 --- a/internal/devices/update.go +++ b/internal/devices/update.go @@ -37,7 +37,7 @@ func (c *Client) Update() *cobra.Command { userdata string hostname string tags []string - alwaysPXE bool + alwaysPXE string ipxescripturl string customdata string deviceID string @@ -74,8 +74,13 @@ func (c *Client) Update() *cobra.Command { deviceUpdate.Tags = tags } - if alwaysPXE { - deviceUpdate.SetAlwaysPxe(alwaysPXE) + if alwaysPXE != "" { + if alwaysPXE == "true" { + deviceUpdate.SetAlwaysPxe(true) + } + if alwaysPXE == "false" { + deviceUpdate.SetAlwaysPxe(false) + } } if ipxescripturl != "" { @@ -93,7 +98,7 @@ func (c *Client) Update() *cobra.Command { } device, _, err := c.Service.UpdateDevice(context.Background(), deviceID).DeviceUpdateInput(*deviceUpdate).Execute() if err != nil { - return fmt.Errorf("Could not update Device: %w", err) + return fmt.Errorf("could not update Device: %w", err) } header := []string{"ID", "Hostname", "OS", "State"} @@ -110,7 +115,7 @@ func (c *Client) Update() *cobra.Command { updateDeviceCmd.Flags().StringVarP(&userdata, "userdata", "u", "", "Adds or updates the userdata for the device.") updateDeviceCmd.Flags().BoolVarP(&locked, "locked", "l", false, "Locks or unlocks the device for future changes.") updateDeviceCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, `Adds or updates the tags for the device --tags="tag1,tag2".`) - updateDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets the device to always iPXE on reboot.") + updateDeviceCmd.Flags().StringVarP(&alwaysPXE, "always-pxe", "a", "", "Sets the device to always iPXE on reboot.") updateDeviceCmd.Flags().StringVarP(&ipxescripturl, "ipxe-script-url", "s", "", "Add or update the URL of the iPXE script.") updateDeviceCmd.Flags().StringVarP(&customdata, "customdata", "c", "", "Adds or updates custom data to be included with your device's metadata.") _ = updateDeviceCmd.MarkFlagRequired("id") diff --git a/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go b/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go index de98d5fa..b981676b 100644 --- a/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go +++ b/test/e2e/devices/devicecreateflagstest/device_create_flags_test.go @@ -72,7 +72,7 @@ func TestCli_Devices_Create_Flags(t *testing.T) { // Extract the ID from the match if len(match) > 1 { deviceId = strings.TrimSpace(match[1]) - resp, err = helper.IsDeviceStateActive(deviceId) + resp, err = helper.IsDeviceStateActive(deviceId, "active") if err == nil && resp == true { err = helper.CleanTestDevice(deviceId) if err != nil { diff --git a/test/e2e/devices/devicecreatetest/device_create_test.go b/test/e2e/devices/devicecreatetest/device_create_test.go index efc8fce7..35f5c663 100644 --- a/test/e2e/devices/devicecreatetest/device_create_test.go +++ b/test/e2e/devices/devicecreatetest/device_create_test.go @@ -72,10 +72,10 @@ func TestCli_Devices_Create(t *testing.T) { // Extract the ID from the match if len(match) > 1 { deviceId = strings.TrimSpace(match[1]) - resp, err = helper.IsDeviceStateActive(deviceId) + resp, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil || resp { if !resp { - resp, err = helper.IsDeviceStateActive(deviceId) + resp, err = helper.IsDeviceStateActive(deviceId, "active") } err = helper.CleanTestDevice(deviceId) if err != nil { diff --git a/test/e2e/devices/devicegettest/device_get_test.go b/test/e2e/devices/devicegettest/device_get_test.go index a8383b53..6ae790d4 100644 --- a/test/e2e/devices/devicegettest/device_get_test.go +++ b/test/e2e/devices/devicegettest/device_get_test.go @@ -63,7 +63,7 @@ func TestCli_Devices_Get(t *testing.T) { t.Error("expected output should include " + deviceId) } if len(projectId) != 0 && len(deviceId) != 0 { - resp, err = helper.IsDeviceStateActive(deviceId) + resp, err = helper.IsDeviceStateActive(deviceId, "active") if err == nil && resp == true { err = helper.CleanTestDevice(deviceId) if err != nil { diff --git a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go index f40bd47d..b65683c6 100644 --- a/test/e2e/devices/devicereinstalltest/device_reinstall_test.go +++ b/test/e2e/devices/devicereinstalltest/device_reinstall_test.go @@ -46,9 +46,9 @@ func TestCli_Devices_Update(t *testing.T) { if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) } @@ -60,11 +60,11 @@ func TestCli_Devices_Update(t *testing.T) { if err != nil { t.Error(err) } else { - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") // The below case will excute in both Device Active and Non-active states. if err != nil || status { if !status { - _, err = helper.IsDeviceStateActive(deviceId) + _, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) } diff --git a/test/e2e/devices/devicestarttest/device_start_test.go b/test/e2e/devices/devicestarttest/device_start_test.go index 23136d10..8a6ece34 100644 --- a/test/e2e/devices/devicestarttest/device_start_test.go +++ b/test/e2e/devices/devicestarttest/device_start_test.go @@ -1,7 +1,6 @@ package devicestarttest import ( - "fmt" "io" "os" "strings" @@ -46,28 +45,30 @@ func TestCli_Devices_Update(t *testing.T) { if err != nil { t.Error(err) } + deviceId, err = helper.CreateTestDevice(projectId, "metal-cli-start-dev") if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(deviceId) + + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { - _, err := helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) - } else { - err = helper.StopTestDevice(deviceId) - if err != nil { - t.Error(err) - } - status, err = helper.IsDeviceStateActive(deviceId) - if err == nil { - t.Error(err) - } } } - if len(projectId) != 0 && len(deviceId) != 0 && !status { + err = helper.StopTestDevice(deviceId) + if err != nil { + t.Error(err) + } + + status, err = helper.IsDeviceStateActive(deviceId, "inactive") + if err != nil { + t.Error(err) + } + if len(projectId) != 0 && len(deviceId) != 0 && status { root.SetArgs([]string{subCommand, "start", "--id", deviceId}) rescueStdout := os.Stdout r, w, _ := os.Pipe() @@ -81,25 +82,25 @@ func TestCli_Devices_Update(t *testing.T) { if !strings.Contains(string(out[:]), "Device "+deviceId+" successfully started.") { t.Error("expected output should include" + "Device " + deviceId + " successfully started." + "in the out string ") } else { - status, _ = helper.IsDeviceStateActive(deviceId) + status, _ = helper.IsDeviceStateActive(deviceId, "active") if err != nil || status { if !status { - _, err = helper.IsDeviceStateActive(deviceId) + _, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) } } - fmt.Print("Device is Active") + err = helper.CleanTestDevice(deviceId) if err != nil { t.Error(err) } - fmt.Print("Cleaned Test Device") + err = helper.CleanTestProject(projectId) if err != nil { t.Error(err) } - fmt.Print("Cleaned Test Project") + } } } diff --git a/test/e2e/devices/devicestoptest/device_stop_test.go b/test/e2e/devices/devicestoptest/device_stop_test.go index d755d476..d3aac50a 100644 --- a/test/e2e/devices/devicestoptest/device_stop_test.go +++ b/test/e2e/devices/devicestoptest/device_stop_test.go @@ -48,7 +48,7 @@ func TestCli_Devices_Update(t *testing.T) { if err != nil { t.Error(err) } - status, err := helper.IsDeviceStateActive(deviceId) + status, err := helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) } diff --git a/test/e2e/devices/deviceupdatetest/device_update_test.go b/test/e2e/devices/deviceupdatetest/device_update_test.go index bb6d64e0..ec2d02af 100644 --- a/test/e2e/devices/deviceupdatetest/device_update_test.go +++ b/test/e2e/devices/deviceupdatetest/device_update_test.go @@ -48,7 +48,7 @@ func TestCli_Devices_Update(t *testing.T) { if err != nil { t.Error(err) } - status, err := helper.IsDeviceStateActive(deviceId) + status, err := helper.IsDeviceStateActive(deviceId, "active") if err != nil { t.Error(err) } diff --git a/test/e2e/events/deviceeventstest/device_event_test.go b/test/e2e/events/deviceeventstest/device_event_test.go index ac09459d..c7172e64 100644 --- a/test/e2e/events/deviceeventstest/device_event_test.go +++ b/test/e2e/events/deviceeventstest/device_event_test.go @@ -41,7 +41,6 @@ func TestCli_Events_Get(t *testing.T) { want: &cobra.Command{}, cmdFunc: func(t *testing.T, c *cobra.Command) { root := c.Root() - projectId, err = helper.CreateTestProject("metal-cli-events-pro") if err != nil { t.Error(err) @@ -50,9 +49,9 @@ func TestCli_Events_Get(t *testing.T) { if err != nil { t.Error(err) } - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil { - status, err = helper.IsDeviceStateActive(deviceId) + status, err = helper.IsDeviceStateActive(deviceId, "active") if err != nil || !status { t.Error(err) } diff --git a/test/helper/helper.go b/test/helper/helper.go index 3bbb2f5f..a319f79a 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -51,7 +51,7 @@ func CreateTestDevice(projectId, name string) (string, error) { return deviceResp.GetId(), nil } -func IsDeviceStateActive(deviceId string) (bool, error) { +func IsDeviceStateActive(deviceId string, state string) (bool, error) { TestApiClient := TestClient() predefinedTime := 500 * time.Second // Adjust this as needed retryInterval := 10 * time.Second // Adjust this as needed @@ -60,16 +60,16 @@ func IsDeviceStateActive(deviceId string) (bool, error) { resp, _, err := TestApiClient.DevicesApi.FindDeviceById(context.Background(), deviceId).Execute() if err != nil { fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.FindDeviceById``: %v\n", err) - return false, err + return false, fmt.Errorf("timed out waiting for device %v to become %v", deviceId, state) } - if resp.GetState() == "active" { + if resp.GetState() == state { return true, nil } // Sleep for the specified interval time.Sleep(retryInterval) } - return false, fmt.Errorf("timed out waiting for device %v to become active", deviceId) + return false, fmt.Errorf("timed out waiting for device %v to become %v", deviceId, state) } func StopTestDevice(deviceId string) error {