github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/namespace_apply.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	flaghelper "github.com/hashicorp/nomad/helper/flags"
     9  	"github.com/posener/complete"
    10  )
    11  
    12  type NamespaceApplyCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *NamespaceApplyCommand) Help() string {
    17  	helpText := `
    18  Usage: nomad namespace apply [options] <namespace>
    19  
    20    Apply is used to create or update a namespace. It takes the namespace name to
    21    create or update as its only argument.
    22  
    23    If ACLs are enabled, this command requires a management ACL token.
    24  
    25  General Options:
    26  
    27    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    28  
    29  Apply Options:
    30  
    31    -quota
    32      The quota to attach to the namespace.
    33  
    34    -description
    35      An optional description for the namespace.
    36  `
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (c *NamespaceApplyCommand) AutocompleteFlags() complete.Flags {
    41  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    42  		complete.Flags{
    43  			"-description": complete.PredictAnything,
    44  			"-quota":       QuotaPredictor(c.Meta.Client),
    45  		})
    46  }
    47  
    48  func (c *NamespaceApplyCommand) AutocompleteArgs() complete.Predictor {
    49  	return NamespacePredictor(c.Meta.Client, nil)
    50  }
    51  
    52  func (c *NamespaceApplyCommand) Synopsis() string {
    53  	return "Create or update a namespace"
    54  }
    55  
    56  func (c *NamespaceApplyCommand) Name() string { return "namespace apply" }
    57  
    58  func (c *NamespaceApplyCommand) Run(args []string) int {
    59  	var description, quota *string
    60  
    61  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    62  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    63  	flags.Var((flaghelper.FuncVar)(func(s string) error {
    64  		description = &s
    65  		return nil
    66  	}), "description", "")
    67  	flags.Var((flaghelper.FuncVar)(func(s string) error {
    68  		quota = &s
    69  		return nil
    70  	}), "quota", "")
    71  
    72  	if err := flags.Parse(args); err != nil {
    73  		return 1
    74  	}
    75  
    76  	// Check that we get exactly one argument
    77  	args = flags.Args()
    78  	if l := len(args); l != 1 {
    79  		c.Ui.Error("This command takes one argument: <namespace>")
    80  		c.Ui.Error(commandErrorText(c))
    81  		return 1
    82  	}
    83  
    84  	name := args[0]
    85  
    86  	// Validate we have at-least a name
    87  	if name == "" {
    88  		c.Ui.Error("Namespace name required")
    89  		return 1
    90  	}
    91  
    92  	// Get the HTTP client
    93  	client, err := c.Meta.Client()
    94  	if err != nil {
    95  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    96  		return 1
    97  	}
    98  
    99  	// Lookup the given namespace
   100  	ns, _, err := client.Namespaces().Info(name, nil)
   101  	if err != nil && !strings.Contains(err.Error(), "404") {
   102  		c.Ui.Error(fmt.Sprintf("Error looking up namespace: %s", err))
   103  		return 1
   104  	}
   105  
   106  	if ns == nil {
   107  		ns = &api.Namespace{
   108  			Name: name,
   109  		}
   110  	}
   111  
   112  	// Add what is set
   113  	if description != nil {
   114  		ns.Description = *description
   115  	}
   116  	if quota != nil {
   117  		ns.Quota = *quota
   118  	}
   119  
   120  	_, err = client.Namespaces().Register(ns, nil)
   121  	if err != nil {
   122  		c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
   123  		return 1
   124  	}
   125  
   126  	c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", name))
   127  	return 0
   128  }