This commit only contains the OSS PR (datacenter query param support).
A separate enterprise PR adds support for ap and namespace query params.
Resources in Consul can exists within scopes such as datacenters, cluster
peers, admin partitions, and namespaces. You can refer to those resources from
interfaces such as the CLI, HTTP API, DNS, and configuration files.
Some scope levels have consistent naming: cluster peers are always referred to
as "peer".
Other scope levels use a short-hand in DNS lookups...
- "ns" for namespace
- "ap" for admin partition
- "dc" for datacenter
...But use long-hand in CLI commands:
- "namespace" for namespace
- "partition" for admin partition
- and "datacenter"
However, HTTP API query parameters do not follow a consistent pattern,
supporting short-hand for some scopes but long-hand for others:
- "ns" for namespace
- "partition" for admin partition
- and "dc" for datacenter.
This inconsistency is confusing, especially for users who have been exposed to
providing scope names through another interface such as CLI or DNS queries.
This commit improves UX by consistently supporting both short-hand and
long-hand forms of the namespace, partition, and datacenter scopes in HTTP API
query parameters.
Below is an example of using the Consul client. To run the example, you must first
install Consul and
Go.
To run the client API, create a new Go module.
go mod init consul-demo
Copy the example code into a file called main.go in the directory where the module is defined.
As seen in the example, the Consul API is often imported with the alias capi.
packagemainimport("fmt"capi"github.com/hashicorp/consul/api")funcmain(){// Get a new client
client,err:=capi.NewClient(capi.DefaultConfig())iferr!=nil{panic(err)}// Get a handle to the KV API
kv:=client.KV()// PUT a new KV pair
p:=&capi.KVPair{Key:"REDIS_MAXCLIENTS",Value:[]byte("1000")}_,err=kv.Put(p,nil)iferr!=nil{panic(err)}// Lookup the pair
pair,_,err:=kv.Get("REDIS_MAXCLIENTS",nil)iferr!=nil{panic(err)}fmt.Printf("KV: %v %s\n",pair.Key,pair.Value)}
Install the Consul API dependency with go mod tidy.
In a separate terminal window, start a local Consul server.
consul agent -dev -node machine
Run the example.
go run .
You should get the following result printed to the terminal.
KV: REDIS_MAXCLIENTS 1000
After running the code, you can also view the values in the Consul UI on your local machine at http://localhost:8500/ui/dc1/kv