2019-04-30 23:27:16 +00:00
|
|
|
package write
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-10-06 18:24:05 +00:00
|
|
|
"time"
|
2019-04-30 23:27:16 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/hashicorp/consul/command/helpers"
|
2020-05-27 18:42:01 +00:00
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
2019-06-28 16:35:35 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2019-04-30 23:27:16 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2019-06-28 16:35:35 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2019-04-30 23:27:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
|
|
|
|
cas bool
|
|
|
|
modifyIndex uint64
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
c.flags.BoolVar(&c.cas, "cas", false,
|
|
|
|
"Perform a Check-And-Set operation. Specifying this value also "+
|
|
|
|
"requires the -modify-index flag to be set. The default value "+
|
|
|
|
"is false.")
|
|
|
|
c.flags.Uint64Var(&c.modifyIndex, "modify-index", 0,
|
|
|
|
"Unsigned integer representing the ModifyIndex of the config entry. "+
|
|
|
|
"This is used in combination with the -cas flag.")
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2020-01-24 15:04:58 +00:00
|
|
|
flags.Merge(c.flags, c.http.NamespaceFlags())
|
2019-04-30 23:27:16 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = c.flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.UI.Error("Must provide exactly one positional argument to specify the config entry to write")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := helpers.LoadDataSourceNoRaw(args[0], c.testStdin)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to load data: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:35:28 +00:00
|
|
|
entry, err := parseConfigEntry(data)
|
2019-04-30 23:27:16 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to decode config entry input: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connect to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := client.ConfigEntries()
|
|
|
|
|
|
|
|
written := false
|
|
|
|
if c.cas {
|
|
|
|
written, _, err = entries.CAS(entry, c.modifyIndex, nil)
|
|
|
|
} else {
|
|
|
|
written, _, err = entries.Set(entry, nil)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-29 20:47:40 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error writing config entry %s/%s: %v", entry.GetKind(), entry.GetName(), err))
|
2019-04-30 23:27:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !written {
|
2020-06-29 20:47:40 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Config entry not updated: %s/%s", entry.GetKind(), entry.GetName()))
|
2019-04-30 23:27:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:47:40 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Config entry written: %s/%s", entry.GetKind(), entry.GetName()))
|
2019-04-30 23:27:16 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:35:35 +00:00
|
|
|
func parseConfigEntry(data string) (api.ConfigEntry, error) {
|
|
|
|
// parse the data
|
|
|
|
var raw map[string]interface{}
|
2019-08-07 21:41:33 +00:00
|
|
|
if err := hclDecode(&raw, data); err != nil {
|
2019-06-28 16:35:35 +00:00
|
|
|
return nil, fmt.Errorf("Failed to decode config entry input: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDecodeConfigEntry(raw)
|
|
|
|
}
|
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
// There is a 'structs' variation of this in
|
|
|
|
// agent/structs/config_entry.go:DecodeConfigEntry
|
2019-06-28 16:35:35 +00:00
|
|
|
func newDecodeConfigEntry(raw map[string]interface{}) (api.ConfigEntry, error) {
|
|
|
|
var entry api.ConfigEntry
|
|
|
|
|
|
|
|
kindVal, ok := raw["Kind"]
|
|
|
|
if !ok {
|
|
|
|
kindVal, ok = raw["kind"]
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Payload does not contain a kind/Kind key at the top level")
|
|
|
|
}
|
|
|
|
|
|
|
|
if kindStr, ok := kindVal.(string); ok {
|
|
|
|
newEntry, err := api.MakeConfigEntry(kindStr, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entry = newEntry
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Kind value in payload is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
2020-05-27 18:42:01 +00:00
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
2020-05-27 17:02:22 +00:00
|
|
|
decode.HookWeakDecodeFromSlice,
|
2020-05-27 18:42:01 +00:00
|
|
|
decode.HookTranslateKeys,
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
2020-10-06 18:24:05 +00:00
|
|
|
mapstructure.StringToTimeHookFunc(time.RFC3339),
|
2020-05-27 18:42:01 +00:00
|
|
|
),
|
2019-06-28 16:35:35 +00:00
|
|
|
Metadata: &md,
|
|
|
|
Result: &entry,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range md.Unused {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("invalid config key %q", k))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return flags.Usage(c.help, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Create or update a centralized config entry"
|
|
|
|
const help = `
|
|
|
|
Usage: consul config write [options] <configuration>
|
|
|
|
|
|
|
|
Request a config entry to be created or updated. The configuration
|
|
|
|
argument is either a file path or '-' to indicate that the config
|
|
|
|
should be read from stdin. The data should be either in HCL or
|
|
|
|
JSON form.
|
|
|
|
|
|
|
|
Example (from file):
|
|
|
|
|
|
|
|
$ consul config write web.service.hcl
|
|
|
|
|
|
|
|
Example (from stdin):
|
|
|
|
|
|
|
|
$ consul config write -
|
|
|
|
`
|