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 20, 2023
1 parent 8a9be52 commit 62d3a0c
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 19 deletions.
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 @@ -41,7 +41,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)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
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 @@ -43,7 +43,7 @@ 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
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand All @@ -57,7 +57,7 @@ func TestCli_Devices_Create(t *testing.T) {

if len(projectId) != 0 {

deviceName := "metal-cli-create-dev" + randomId
deviceName := "metal-cli-create-dev-" + randomId
root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", deviceName})
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/devicegettest/device_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCli_Devices_Get(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-get" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-get-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCli_Devices_Update(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-reinstall" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-reinstall-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/devicestarttest/device_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCli_Devices_Update(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-start" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-start-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/devicestoptest/device_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCli_Devices_Update(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-stop" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-stop-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/devices/deviceupdatetest/device_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCli_Devices_Update(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-update" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-update-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
t.Cleanup(func() {
if err := helper.CleanTestProject(t, projectId); err != nil &&
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/events/deviceeventstest/device_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCli_Events_Get(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectName := "metal-cli-device-events" + helper.GenerateRandomString(5)
projectName := "metal-cli-device-events-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/events/projecteventstest/project_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCli_Events_Get(t *testing.T) {
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()

projectName := "metal-cli-projects-events" + helper.GenerateRandomString(5)
projectName := "metal-cli-projects-events-" + helper.GenerateRandomString(5)
projectId, err = helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
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 @@ -43,7 +43,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)
projectId, err = helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
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 @@ -44,7 +44,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)
projectId, err = helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
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 @@ -40,7 +40,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)
projectId, err = 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 @@ -40,7 +40,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)
projectId, err = 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 @@ -40,7 +40,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)
projectId, err = helper.CreateTestProject(t, projectName)
if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func CreateTestDevice(t *testing.T, projectId, name string) (string, error) {
hostname := name
metroDeviceRequest := openapiclient.CreateDeviceRequest{
DeviceCreateInMetroInput: &openapiclient.DeviceCreateInMetroInput{
Metro: "sv",
Metro: "da",
Plan: "m3.small.x86",
OperatingSystem: "ubuntu_20_04",
Hostname: &hostname,
Expand Down

0 comments on commit 62d3a0c

Please sign in to comment.