Skip to content

Commit

Permalink
enhance: use force and confirmation while delete interconnections
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushrangwala committed Dec 21, 2023
1 parent 1fe303e commit 9ab3560
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 13 deletions.
6 changes: 5 additions & 1 deletion docs/metal_interconnections_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Deletes a interconnection.

### Synopsis

Deletes the specified interconnection.
Deletes the specified interconnection. Use --force to skip confirmation

```
metal interconnections delete -i <connection_id> [flags]
Expand All @@ -15,11 +15,15 @@ metal interconnections delete -i <connection_id> [flags]
```
# Deletes the specified interconnection:
metal interconnections delete -i 7ec86e23-8dcf-48ed-bd9b-c25c20958277
>
✔ Are you sure you want to delete device 7ec86e23-8dcf-48ed-bd9b-c25c20958277 [Y/n]: Y
```

### Options

```
-f, --force Skips confirmation for the interconnection deletion.
-h, --help help for delete
-i, --id string The UUID of the interconnection.
```
Expand Down
28 changes: 25 additions & 3 deletions internal/interconnections/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,44 @@ package interconnections
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
)

func (c *Client) Delete() *cobra.Command {
var connectionID string
var connectionID, confirmation string
var force bool

deleteConnectionCmd := &cobra.Command{
Use: `delete -i <connection_id> `,
Short: "Deletes a interconnection.",
Long: "Deletes the specified interconnection.",
Long: "Deletes the specified interconnection. Use --force to skip confirmation",
Example: ` # Deletes the specified interconnection:
metal interconnections delete -i 7ec86e23-8dcf-48ed-bd9b-c25c20958277`,
metal interconnections delete -i 7ec86e23-8dcf-48ed-bd9b-c25c20958277
>
✔ Are you sure you want to delete device 7ec86e23-8dcf-48ed-bd9b-c25c20958277 [Y/n]: Y
`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if !force {
fmt.Printf("Are you sure you want to delete device %s [Y/n]: ", connectionID)

_, err := fmt.Scanln(&confirmation)
if err != nil {
fmt.Println("Error reading confirmation:", err)
return nil
}

confirmation = strings.TrimSpace(strings.ToLower(confirmation))
if confirmation != "yes" && confirmation != "y" {
fmt.Println("Interconnection deletion cancelled.")
return nil
}
}

_, _, err := c.Service.DeleteInterconnection(context.Background(), connectionID).Execute()
if err != nil {
return err
Expand All @@ -31,6 +52,7 @@ func (c *Client) Delete() *cobra.Command {
}

deleteConnectionCmd.Flags().StringVarP(&connectionID, "id", "i", "", "The UUID of the interconnection.")
deleteConnectionCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the interconnection deletion.")
_ = deleteConnectionCmd.MarkFlagRequired("id")

return deleteConnectionCmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCli_Devices_Create_Flags(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-create-flags" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-create-flags-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)

root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-P", "m3.small.x86", "-m", "da", "-H", "metal-cli-create-flags-dev", "--operating-system", "custom_ipxe", "--always-pxe=true", "--ipxe-script-url", "https://boot.netboot.xyz/"})
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/devices/devicecreatetest/device_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func TestCli_Devices_Create(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-create" + randomId
projectName := "metal-cli-device-create-" + randomId
project := helper.CreateTestProject(t, projectName)

deviceName := "metal-cli-create-dev" + randomId
deviceName := "metal-cli-create-dev-" + randomId
root.SetArgs([]string{subCommand, "create", "-p", project.GetId(), "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", deviceName})

out := helper.ExecuteAndCaptureOutput(t, root)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ipstest/ips_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCli_Ips_Get(t *testing.T) {
t.Skip("Skipping this test because someCondition is true")
}
root := c.Root()
projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5)
projectName := "metal-cli-ips-get-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)
ipsId, err = helper.CreateTestIps(t, project.GetId(), 1, "public_ipv4")
if len(ipsId) != 0 {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ipstest/ips_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCli_Vlan_Create(t *testing.T) {
t.Skip("Skipping temporarily for now")
}
root := c.Root()
projectName := "metal-cli-ips-get" + helper.GenerateRandomString(5)
projectName := "metal-cli-ips-get-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)

root.SetArgs([]string{subCommand, "request", "-p", project.GetId(), "-t", "public_ipv4", "-m", "da", "-q", "4"})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/vlan/vlan_creat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCli_Vlan_Create(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-vlan-create-pro" + helper.GenerateRandomString(5)
projectName := "metal-cli-vlan-create-pro-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/vlan/vlan_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCli_Vlan_Clean(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-vlan-get-pro" + helper.GenerateRandomString(5)
projectName := "metal-cli-vlan-get-pro-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/vlan/vlan_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCli_Vlan_Get(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-vlan-delete-pro" + helper.GenerateRandomString(5)
projectName := "metal-cli-vlan-delete-pro-" + helper.GenerateRandomString(5)
project := helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
Expand Down
3 changes: 2 additions & 1 deletion test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ func CreateTestDevice(t *testing.T, projectId, name string) *metalv1.Device {
TestApiClient := TestClient()

hostname := name

termination := time.Now().Add(1 * time.Hour)
metroDeviceRequest := metalv1.CreateDeviceRequest{
DeviceCreateInMetroInput: &metalv1.DeviceCreateInMetroInput{
Metro: "sv",
Metro: "da",
Plan: "m3.small.x86",
OperatingSystem: "ubuntu_20_04",
Hostname: &hostname,
Expand Down

0 comments on commit 9ab3560

Please sign in to comment.