Skip to content

Commit

Permalink
cli: add prefix ID and wildcard namespace support for service info (#…
Browse files Browse the repository at this point in the history
…18836)

The `nomad service info` command doesn't support using a wildcard namespace with
a prefix match, the way that we do for many other commands. Update the command
to do a prefix match list query for the services before making the get query.

Fixes: #18831
  • Loading branch information
tgross authored Oct 23, 2023
1 parent 8a31125 commit 1b3920f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/18836.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added support for prefix ID matching and wildcard namespaces to `service info` command
```
32 changes: 31 additions & 1 deletion command/service_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,44 @@ func (s *ServiceInfoCommand) Run(args []string) int {
return 1
}

ns := s.Meta.namespace
serviceID := args[0]

// Set up the options to capture any filter passed.
opts := api.QueryOptions{
Filter: filter,
Prefix: serviceID,
Namespace: ns,
}
services, _, err := client.Services().List(&opts)
if err != nil {
s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err))
return 1
}
switch len(services) {
case 0:
s.Ui.Error(fmt.Sprintf("No service registrations with prefix %q found", serviceID))
return 1
case 1:
ns = services[0].Namespace
if len(services[0].Services) > 0 { // should always be valid
serviceID = services[0].Services[0].ServiceName
}
default:
s.Ui.Error(fmt.Sprintf("Prefix matched multiple services\n\n%s",
formatServiceListOutput(s.Meta.namespace, services)))
return 1
}

// Set up the options to capture any filter passed.
opts = api.QueryOptions{
Filter: filter,
PerPage: int32(perPage),
NextToken: pageToken,
Namespace: ns,
}

serviceInfo, qm, err := client.Services().Get(args[0], &opts)
serviceInfo, qm, err := client.Services().Get(serviceID, &opts)
if err != nil {
s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err))
return 1
Expand Down
10 changes: 5 additions & 5 deletions command/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func (s *ServiceListCommand) Run(args []string) int {
return 0
}

s.formatOutput(list)
s.Ui.Output(formatServiceListOutput(s.Meta.namespace, list))
return 0
}

func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStub) {
func formatServiceListOutput(cmdNS string, regs []*api.ServiceRegistrationListStub) string {

// Create objects to hold sorted a sorted namespace array and a mapping, so
// we can perform service lookups on a namespace basis.
Expand All @@ -137,7 +137,7 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu

// If the request was made using the wildcard namespace, include this in
// the output.
if s.Meta.namespace == api.AllNamespacesNamespace {
if cmdNS == api.AllNamespacesNamespace {
outputTable[0] += "|Namespace"
}

Expand Down Expand Up @@ -171,13 +171,13 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu
// Build the output array entry.
regOutput := serviceName

if s.Meta.namespace == api.AllNamespacesNamespace {
if cmdNS == api.AllNamespacesNamespace {
regOutput += "|" + ns
}
regOutput += "|" + fmt.Sprintf("[%s]", strings.Join(tags, ","))
outputTable = append(outputTable, regOutput)
}
}

s.Ui.Output(formatList(outputTable))
return formatList(outputTable)
}

0 comments on commit 1b3920f

Please sign in to comment.