github.com/hernad/nomad@v1.6.112/command/volume_create.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/posener/complete"
    13  )
    14  
    15  type VolumeCreateCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *VolumeCreateCommand) Help() string {
    20  	helpText := `
    21  Usage: nomad volume create [options] <input>
    22  
    23    Creates a volume in an external storage provider and registers it in Nomad.
    24  
    25    If the supplied path is "-" the volume file is read from stdin. Otherwise, it
    26    is read from the file at the supplied path.
    27  
    28    When ACLs are enabled, this command requires a token with the
    29    'csi-write-volume' capability for the volume's namespace.
    30  
    31  General Options:
    32  
    33    ` + generalOptionsUsage(usageOptsDefault)
    34  
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *VolumeCreateCommand) AutocompleteFlags() complete.Flags {
    39  	return c.Meta.AutocompleteFlags(FlagSetClient)
    40  }
    41  
    42  func (c *VolumeCreateCommand) AutocompleteArgs() complete.Predictor {
    43  	return complete.PredictFiles("*")
    44  }
    45  
    46  func (c *VolumeCreateCommand) Synopsis() string {
    47  	return "Create an external volume"
    48  }
    49  
    50  func (c *VolumeCreateCommand) Name() string { return "volume create" }
    51  
    52  func (c *VolumeCreateCommand) Run(args []string) int {
    53  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    54  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    55  
    56  	if err := flags.Parse(args); err != nil {
    57  		c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err))
    58  		return 1
    59  	}
    60  
    61  	// Check that we get exactly one argument
    62  	args = flags.Args()
    63  	if l := len(args); l != 1 {
    64  		c.Ui.Error("This command takes one argument: <input>")
    65  		c.Ui.Error(commandErrorText(c))
    66  		return 1
    67  	}
    68  
    69  	// Read the file contents
    70  	file := args[0]
    71  	var rawVolume []byte
    72  	var err error
    73  	if file == "-" {
    74  		rawVolume, err = io.ReadAll(os.Stdin)
    75  		if err != nil {
    76  			c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err))
    77  			return 1
    78  		}
    79  	} else {
    80  		rawVolume, err = os.ReadFile(file)
    81  		if err != nil {
    82  			c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err))
    83  			return 1
    84  		}
    85  	}
    86  
    87  	ast, volType, err := parseVolumeType(string(rawVolume))
    88  	if err != nil {
    89  		c.Ui.Error(fmt.Sprintf("Error parsing the volume type: %s", err))
    90  		return 1
    91  	}
    92  
    93  	// Get the HTTP client
    94  	client, err := c.Meta.Client()
    95  	if err != nil {
    96  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    97  		return 1
    98  	}
    99  
   100  	switch strings.ToLower(volType) {
   101  	case "csi":
   102  		code := c.csiCreate(client, ast)
   103  		return code
   104  	default:
   105  		c.Ui.Error(fmt.Sprintf("Error unknown volume type: %s", volType))
   106  		return 1
   107  	}
   108  }