cli: add prefix ID and wildcard namespace support for service info (#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
This commit is contained in:
Tim Gross
2023-10-23 13:17:51 -04:00
committed by GitHub
parent 8a311255a2
commit 1b3920f96b
3 changed files with 41 additions and 8 deletions

3
.changelog/18836.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added support for prefix ID matching and wildcard namespaces to `service info` command
```

View File

@@ -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,
PerPage: int32(perPage),
NextToken: pageToken,
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
}
serviceInfo, qm, err := client.Services().Get(args[0], &opts)
// 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(serviceID, &opts)
if err != nil {
s.Ui.Error(fmt.Sprintf("Error listing service registrations: %s", err))
return 1

View File

@@ -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.
@@ -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"
}
@@ -171,7 +171,7 @@ 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, ","))
@@ -179,5 +179,5 @@ func (s *ServiceListCommand) formatOutput(regs []*api.ServiceRegistrationListStu
}
}
s.Ui.Output(formatList(outputTable))
return formatList(outputTable)
}