Files
nomad/command/agent/consul/namespaces_client.go
Seth Hoenig a97254fa20 consul: plubming for specifying consul namespace in job/group
This PR adds the common OSS changes for adding support for Consul Namespaces,
which is going to be a Nomad Enterprise feature. There is no new functionality
provided by this changeset and hopefully no new bugs.
2021-04-05 10:03:19 -06:00

43 lines
1.2 KiB
Go

package consul
import (
"sort"
"strings"
)
// NamespacesClient is a wrapper for the Consul NamespacesAPI, that is used to
// deal with Consul OSS vs Consul Enterprise behavior in listing namespaces.
type NamespacesClient struct {
namespacesAPI NamespaceAPI
}
// NewNamespacesClient returns a NamespacesClient backed by a NamespaceAPI.
func NewNamespacesClient(namespacesAPI NamespaceAPI) *NamespacesClient {
return &NamespacesClient{
namespacesAPI: namespacesAPI,
}
}
// List returns a list of Consul Namespaces.
//
// If using Consul OSS, the list is a single element with the "default" namespace,
// even though the response from Consul OSS is an error.
func (ns *NamespacesClient) List() ([]string, error) {
namespaces, _, err := ns.namespacesAPI.List(nil)
if err != nil {
// check if the error was a 404, indicating Consul is the OSS version
// which does not have the /v1/namespace handler
if strings.Contains(err.Error(), "response code: 404") {
return []string{"default"}, nil
}
return nil, err
}
result := make([]string, 0, len(namespaces))
for _, namespace := range namespaces {
result = append(result, namespace.Name)
}
sort.Strings(result)
return result, nil
}