diff --git a/docs/metal_device_update.md b/docs/metal_device_update.md index abb7ba3d..58e034d4 100644 --- a/docs/metal_device_update.md +++ b/docs/metal_device_update.md @@ -7,7 +7,7 @@ Updates a device. Updates the hostname of a device. Updates or adds a description, tags, userdata, custom data, and iPXE settings for an already provisioned device. Can also lock or unlock future changes to the device. ``` -metal device update -i [-H ] [-d ] [--locked ] [-t ] [-u | --userdata-file ] [-c ] [-s ] [--always-pxe=] [flags] +metal device update -i [-H ] [-d ] [--locked=] [-t ] [-u | --userdata-file ] [-c ] [-s ] [--always-pxe=] [flags] ``` ### Examples diff --git a/internal/devices/update.go b/internal/devices/update.go index b53187c0..9125d832 100644 --- a/internal/devices/update.go +++ b/internal/devices/update.go @@ -34,7 +34,6 @@ import ( func (c *Client) Update() *cobra.Command { var ( description string - locked bool userdata string userdataFile string hostname string @@ -46,7 +45,7 @@ func (c *Client) Update() *cobra.Command { ) // updateDeviceCmd represents the updateDevice command updateDeviceCmd := &cobra.Command{ - Use: `update -i [-H ] [-d ] [--locked ] [-t ] [-u | --userdata-file ] [-c ] [-s ] [--always-pxe=]`, + Use: `update -i [-H ] [-d ] [--locked=] [-t ] [-u | --userdata-file ] [-c ] [-s ] [--always-pxe=]`, Short: "Updates a device.", Long: "Updates the hostname of a device. Updates or adds a description, tags, userdata, custom data, and iPXE settings for an already provisioned device. Can also lock or unlock future changes to the device.", Example: ` # Updates the hostname of a device: @@ -64,10 +63,6 @@ func (c *Client) Update() *cobra.Command { deviceUpdate.Description = &description } - if userdata != "" && userdataFile != "" { - return fmt.Errorf("either userdata or userdata-file should be set") - } - if userdataFile != "" { userdataRaw, readErr := os.ReadFile(userdataFile) if readErr != nil { @@ -80,7 +75,11 @@ func (c *Client) Update() *cobra.Command { deviceUpdate.Userdata = &userdata } - if locked { + if cmd.Flag("locked").Changed { + locked, err := cmd.Flags().GetBool("locked") + if err != nil { + return fmt.Errorf("could not parse locked value: %w", err) + } deviceUpdate.Locked = &locked } @@ -123,12 +122,13 @@ func (c *Client) Update() *cobra.Command { updateDeviceCmd.Flags().StringVarP(&description, "description", "d", "", "Adds or updates the description for the device.") updateDeviceCmd.Flags().StringVarP(&userdata, "userdata", "u", "", "Adds or updates the userdata for the device.") updateDeviceCmd.Flags().StringVarP(&userdataFile, "userdata-file", "", "", "Path to a userdata file for device initialization. Can not be used with --userdata.") - updateDeviceCmd.Flags().BoolVarP(&locked, "locked", "l", false, "Locks or unlocks the device for future changes ().") + updateDeviceCmd.Flags().BoolP("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, "Updates the always_pxe toggle for the device ().") 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") - + updateDeviceCmd.MarkFlagsMutuallyExclusive("userdata", "userdata-file") + updateDeviceCmd.Args = cobra.NoArgs return updateDeviceCmd } diff --git a/test/e2e/devices/deviceupdatetest/device_update_test.go b/test/e2e/devices/deviceupdatetest/device_update_test.go index 12d2b5d2..f6172d17 100644 --- a/test/e2e/devices/deviceupdatetest/device_update_test.go +++ b/test/e2e/devices/deviceupdatetest/device_update_test.go @@ -42,7 +42,7 @@ func TestCli_Devices_Update(t *testing.T) { t.Fatal(err) } if status == true { - root.SetArgs([]string{subCommand, "update", "-i", device.GetId(), "-H", "metal-cli-update-dev-test", "-d", "This device used for testing"}) + root.SetArgs([]string{subCommand, "update", "-i", device.GetId(), "-H", "metal-cli-update-dev-test", "-d", "This device used for testing", "--locked=true"}) out := helper.ExecuteAndCaptureOutput(t, root) @@ -50,6 +50,23 @@ func TestCli_Devices_Update(t *testing.T) { t.Error("expected output should include metal-cli-update-dev-test in the out string ") } } + + root.SetArgs([]string{subCommand, "delete", "-i", device.GetId()}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), "not delete") { + t.Error("expected output should include 'not delete' in the out string due to device locking") + } + + root.SetArgs([]string{subCommand, "update", "-i", device.GetId(), "--locked=false"}) + + out = helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), "metal-cli-update-dev-test") { + t.Error("expected output should include metal-cli-update-dev-test in the out string ") + } + }, }, }