Skip to content

Commit

Permalink
feat(alibaba): delete command asks the user for confirmation
Browse files Browse the repository at this point in the history
Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot committed Sep 27, 2020
1 parent 9572fd4 commit 198d0c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,26 @@ func (p *Alibaba) Rollback() error {
}

func (p *Alibaba) DeleteK3sNode(f bool) error {
p.logger = common.NewLogger(common.Debug)
p.logger.Infof("[%s] executing delete cluster logic...\n", p.GetProviderName())
isConfirmed := true

if err := p.generateClientSDK(); err != nil {
return err
if !f {
isConfirmed = utils.AskForConfirmation(fmt.Sprintf("[%s] are you sure to delete cluster %s", p.GetProviderName(), p.Name))
}

if err := p.deleteCluster(f); err != nil {
return err
}
if isConfirmed {
p.logger = common.NewLogger(common.Debug)
p.logger.Infof("[%s] executing delete cluster logic...\n", p.GetProviderName())

p.logger.Infof("[%s] successfully excuted delete cluster logic\n", p.GetProviderName())
if err := p.generateClientSDK(); err != nil {
return err
}

if err := p.deleteCluster(f); err != nil {
return err
}

p.logger.Infof("[%s] successfully excuted delete cluster logic\n", p.GetProviderName())
}

return nil
}
Expand Down Expand Up @@ -450,6 +458,7 @@ func (p *Alibaba) deleteCluster(f bool) error {

if err == nil && len(ids) > 0 {
p.logger.Debugf("[%s] cluster %s will be deleted\n", p.GetProviderName(), p.Name)

p.releaseEipAddresses(false)

request := ecs.CreateDeleteInstancesRequest()
Expand All @@ -460,7 +469,6 @@ func (p *Alibaba) deleteCluster(f bool) error {
request.TerminateSubscription = requests.NewBoolean(true)

_, err := p.c.DeleteInstances(request)

if err != nil {
return fmt.Errorf("[%s] calling deleteInstance error, msg: [%v]", p.GetProviderName(), err)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package utils

import (
"bufio"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"strings"

"github.com/sirupsen/logrus"
)

func RandomToken(size int) (string, error) {
Expand Down Expand Up @@ -30,3 +36,20 @@ func UniqueArray(origin []string) (unique []string) {
}
return
}

func AskForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
if err != nil {
logrus.Fatal(err)
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}

0 comments on commit 198d0c1

Please # to comment.