/
detach-volume.go
49 lines (42 loc) · 1.33 KB
/
detach-volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cmd
import (
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/emc-advanced-dev/pkg/errors"
"github.com/solo-io/unik/pkg/client"
)
var detachCmd = &cobra.Command{
Use: "detach-volume",
Aliases: []string{"detach"},
Short: "Detach an attached volume from a stopped instance",
Long: `Detaches a volume to a stopped instance at a specified mount point.
You specify the volume by name or id.
After detaching the volume, the volume can be mounted to another instance.
If the instance is not stopped, detach will result in an error.`,
Run: func(cmd *cobra.Command, args []string) {
if err := func() error {
if err := readClientConfig(); err != nil {
return err
}
if volumeName == "" {
return errors.New("must specify --volume", nil)
}
if host == "" {
host = clientConfig.Host
}
logrus.WithFields(logrus.Fields{"host": host, "volume": volumeName}).Info("detaching volume")
if err := client.UnikClient(host).Volumes().Detach(volumeName); err != nil {
return err
}
return nil
}(); err != nil {
logrus.Errorf("failed deleting volume: %v", err)
os.Exit(-1)
}
},
}
func init() {
RootCmd.AddCommand(detachCmd)
detachCmd.Flags().StringVar(&volumeName, "volume", "", "<string,required> name or id of volume to detach. unik accepts a prefix of the name or id")
}