github.com/hernad/nomad@v1.6.112/command/volume_snapshot_delete.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/hernad/nomad/api" 11 "github.com/hernad/nomad/api/contexts" 12 flaghelper "github.com/hernad/nomad/helper/flags" 13 "github.com/posener/complete" 14 ) 15 16 type VolumeSnapshotDeleteCommand struct { 17 Meta 18 } 19 20 func (c *VolumeSnapshotDeleteCommand) Help() string { 21 helpText := ` 22 Usage: nomad volume snapshot delete [options] <plugin id> <snapshot id> 23 24 Delete a snapshot from an external storage provider. 25 26 When ACLs are enabled, this command requires a token with the 27 'csi-write-volume' and 'plugin:read' capabilities. 28 29 General Options: 30 31 ` + generalOptionsUsage(usageOptsDefault) + ` 32 33 Snapshot Options: 34 35 -secret 36 Secrets to pass to the plugin to delete the snapshot. Accepts multiple 37 flags in the form -secret key=value 38 39 ` 40 return strings.TrimSpace(helpText) 41 } 42 43 func (c *VolumeSnapshotDeleteCommand) AutocompleteFlags() complete.Flags { 44 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 45 complete.Flags{ 46 "-secret": complete.PredictNothing, 47 }) 48 } 49 50 func (c *VolumeSnapshotDeleteCommand) AutocompleteArgs() complete.Predictor { 51 return complete.PredictFunc(func(a complete.Args) []string { 52 client, err := c.Meta.Client() 53 if err != nil { 54 return nil 55 } 56 57 resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Plugins, nil) 58 if err != nil { 59 return []string{} 60 } 61 return resp.Matches[contexts.Plugins] 62 }) 63 } 64 65 func (c *VolumeSnapshotDeleteCommand) Synopsis() string { 66 return "Delete a snapshot" 67 } 68 69 func (c *VolumeSnapshotDeleteCommand) Name() string { return "volume snapshot delete" } 70 71 func (c *VolumeSnapshotDeleteCommand) Run(args []string) int { 72 var secretsArgs flaghelper.StringFlag 73 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 74 flags.Usage = func() { c.Ui.Output(c.Help()) } 75 flags.Var(&secretsArgs, "secret", "secrets for snapshot, ex. -secret key=value") 76 77 if err := flags.Parse(args); err != nil { 78 c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err)) 79 return 1 80 } 81 // Check that we get exactly two arguments 82 args = flags.Args() 83 if l := len(args); l < 2 { 84 c.Ui.Error("This command takes two arguments: <plugin id> <snapshot id>") 85 c.Ui.Error(commandErrorText(c)) 86 return 1 87 } 88 pluginID := args[0] 89 snapID := args[1] 90 91 // Get the HTTP client 92 client, err := c.Meta.Client() 93 if err != nil { 94 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 95 return 1 96 } 97 98 secrets := api.CSISecrets{} 99 for _, kv := range secretsArgs { 100 if key, value, found := strings.Cut(kv, "="); found { 101 secrets[key] = value 102 } else { 103 c.Ui.Error("Secret must be in the format: -secret key=value") 104 return 1 105 } 106 } 107 108 err = client.CSIVolumes().DeleteSnapshot(&api.CSISnapshot{ 109 ID: snapID, 110 PluginID: pluginID, 111 Secrets: secrets, 112 }, nil) 113 if err != nil { 114 c.Ui.Error(fmt.Sprintf("Error deleting volume: %s", err)) 115 return 1 116 } 117 118 return 0 119 }