github.com/hernad/nomad@v1.6.112/command/node_pool_nodes.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "os" 9 "strings" 10 11 "github.com/hernad/nomad/api" 12 "github.com/posener/complete" 13 ) 14 15 type NodePoolNodesCommand struct { 16 Meta 17 } 18 19 func (c *NodePoolNodesCommand) Name() string { 20 return "node pool nodes" 21 } 22 23 func (c *NodePoolNodesCommand) Synopsis() string { 24 return "Fetch a list of nodes in a node pool" 25 } 26 27 func (c *NodePoolNodesCommand) Help() string { 28 helpText := ` 29 Usage: nomad node pool nodes <node-pool> 30 31 Node pool nodes is used to list nodes in a given node pool. 32 33 If ACLs are enabled, this command requires a token with the 'node:read' 34 capability and the 'read' capability in a 'node_pool' policy that matches the 35 node pool being targeted. 36 37 General Options: 38 39 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 40 41 Node Pool Nodes Options: 42 43 -filter 44 Specifies an expression used to filter nodes from the results. The filter 45 is applied to the nodes and not the node pool. 46 47 -json 48 Output the list in JSON format. 49 50 -page-token 51 Where to start pagination. 52 53 -per-page 54 How many results to show per page. If not specified, or set to 0, all 55 results are returned. 56 57 -t 58 Format and display nodes list using a Go template. 59 60 -verbose 61 Display full information. 62 ` 63 return strings.TrimSpace(helpText) 64 } 65 66 func (c *NodePoolNodesCommand) AutocompleteFlags() complete.Flags { 67 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 68 complete.Flags{ 69 "-filter": complete.PredictAnything, 70 "-json": complete.PredictNothing, 71 "-page-token": complete.PredictAnything, 72 "-per-page": complete.PredictAnything, 73 "-t": complete.PredictAnything, 74 "-verbose": complete.PredictNothing, 75 }) 76 } 77 78 func (c *NodePoolNodesCommand) AutocompleteArgs() complete.Predictor { 79 return nodePoolPredictor(c.Client, nil) 80 } 81 82 func (c *NodePoolNodesCommand) Run(args []string) int { 83 var json, verbose bool 84 var perPage int 85 var pageToken, filter, tmpl string 86 87 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 88 flags.Usage = func() { c.Ui.Output(c.Help()) } 89 flags.BoolVar(&json, "json", false, "") 90 flags.StringVar(&filter, "filter", "", "") 91 flags.StringVar(&pageToken, "page-token", "", "") 92 flags.IntVar(&perPage, "per-page", 0, "") 93 flags.StringVar(&tmpl, "t", "", "") 94 flags.BoolVar(&verbose, "verbose", false, "") 95 96 if err := flags.Parse(args); err != nil { 97 return 1 98 } 99 100 // Check that we only have one argument. 101 args = flags.Args() 102 if len(args) != 1 { 103 c.Ui.Error("This command takes one argument: <node-pool>") 104 c.Ui.Error(commandErrorText(c)) 105 return 1 106 } 107 108 // Lookup node pool by prefix. 109 client, err := c.Meta.Client() 110 if err != nil { 111 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 112 return 1 113 } 114 115 pool, possible, err := nodePoolByPrefix(client, args[0]) 116 if err != nil { 117 c.Ui.Error(fmt.Sprintf("Error retrieving node pool: %s", err)) 118 return 1 119 } 120 if len(possible) != 0 { 121 c.Ui.Error(fmt.Sprintf( 122 "Prefix matched multiple node pools\n\n%s", formatNodePoolList(possible))) 123 return 1 124 } 125 126 opts := &api.QueryOptions{ 127 Filter: filter, 128 PerPage: int32(perPage), 129 NextToken: pageToken, 130 } 131 nodes, qm, err := client.NodePools().ListNodes(pool.Name, opts) 132 if err != nil { 133 c.Ui.Error(fmt.Sprintf("Error querying nodes: %s", err)) 134 return 1 135 } 136 137 if len(nodes) == 0 { 138 c.Ui.Output("No nodes") 139 return 0 140 } 141 142 // Format output if requested. 143 if json || tmpl != "" { 144 out, err := Format(json, tmpl, nodes) 145 if err != nil { 146 c.Ui.Error(err.Error()) 147 return 1 148 } 149 150 c.Ui.Output(out) 151 return 0 152 } 153 154 c.Ui.Output(formatNodeStubList(nodes, verbose)) 155 156 if qm.NextToken != "" { 157 c.Ui.Output(fmt.Sprintf(` 158 Results have been paginated. To get the next page run: 159 160 %s -page-token %s`, argsWithoutPageToken(os.Args), qm.NextToken)) 161 } 162 163 return 0 164 }