2023-09-08 16:20:14 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-09-14 22:19:04 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-09-08 16:20:14 +00:00
|
|
|
|
|
|
|
package apply
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-10-13 13:24:16 +00:00
|
|
|
"io"
|
2023-09-08 16:20:14 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2023-09-11 14:06:00 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource"
|
2023-09-22 20:32:08 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource/client"
|
2023-09-08 16:20:14 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
filePath string
|
2023-10-13 13:24:16 +00:00
|
|
|
|
|
|
|
testStdin io.Reader
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.StringVar(&c.filePath, "f", "",
|
|
|
|
"File path with resource definition")
|
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
func makeWriteRequest(parsedResource *pbresource.Resource) (payload *resource.WriteRequest, error error) {
|
2023-09-08 16:20:14 +00:00
|
|
|
// The parsed hcl file has data field in proto message format anypb.Any
|
|
|
|
// Converting to json format requires us to fisrt marshal it then unmarshal it
|
|
|
|
data, err := protojson.Marshal(parsedResource.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unrecognized hcl format: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resourceData map[string]any
|
|
|
|
err = json.Unmarshal(data, &resourceData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unrecognized hcl format: %s", err)
|
|
|
|
}
|
|
|
|
delete(resourceData, "@type")
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
return &resource.WriteRequest{
|
2023-09-08 16:20:14 +00:00
|
|
|
Data: resourceData,
|
|
|
|
Metadata: parsedResource.GetMetadata(),
|
|
|
|
Owner: parsedResource.GetOwner(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2023-09-11 14:06:00 +00:00
|
|
|
if !errors.Is(err, flag.ErrHelp) {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
input := c.filePath
|
|
|
|
|
|
|
|
if input == "" && len(c.flags.Args()) > 0 {
|
|
|
|
input = c.flags.Arg(0)
|
|
|
|
}
|
|
|
|
|
2023-09-08 16:20:14 +00:00
|
|
|
var parsedResource *pbresource.Resource
|
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
if input != "" {
|
|
|
|
data, err := resource.ParseResourceInput(input, c.testStdin)
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
2023-09-11 14:06:00 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
|
2023-09-08 16:20:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
2023-09-11 14:06:00 +00:00
|
|
|
parsedResource = data
|
2023-09-08 16:20:14 +00:00
|
|
|
} else {
|
2023-10-13 13:24:16 +00:00
|
|
|
c.UI.Error("Incorrect argument format: Must provide exactly one positional argument to specify the resource to write")
|
2023-09-08 16:20:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedResource == nil {
|
|
|
|
c.UI.Error("Unable to parse the file argument")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
config := api.DefaultConfig()
|
|
|
|
|
|
|
|
c.http.MergeOntoConfig(config)
|
|
|
|
resourceClient, err := client.NewClient(config)
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connect to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
res := resource.Resource{C: resourceClient}
|
|
|
|
|
|
|
|
opts := &client.QueryOptions{
|
2023-09-08 16:20:14 +00:00
|
|
|
Namespace: parsedResource.Id.Tenancy.GetNamespace(),
|
|
|
|
Partition: parsedResource.Id.Tenancy.GetPartition(),
|
|
|
|
Peer: parsedResource.Id.Tenancy.GetPeerName(),
|
|
|
|
Token: c.http.Token(),
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
gvk := &resource.GVK{
|
2023-09-08 16:20:14 +00:00
|
|
|
Group: parsedResource.Id.Type.GetGroup(),
|
|
|
|
Version: parsedResource.Id.Type.GetGroupVersion(),
|
|
|
|
Kind: parsedResource.Id.Type.GetKind(),
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRequest, err := makeWriteRequest(parsedResource)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing hcl input: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-09-22 20:32:08 +00:00
|
|
|
entry, err := res.Apply(gvk, parsedResource.Id.GetName(), opts, writeRequest)
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error writing resource %s/%s: %v", gvk, parsedResource.Id.GetName(), err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(entry, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error("Failed to encode output data")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Info(fmt.Sprintf("%s.%s.%s '%s' created.", gvk.Group, gvk.Version, gvk.Kind, parsedResource.Id.GetName()))
|
|
|
|
c.UI.Info(string(b))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return flags.Usage(c.help, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Writes/updates resource information"
|
2023-09-14 22:19:04 +00:00
|
|
|
|
2023-09-08 16:20:14 +00:00
|
|
|
const help = `
|
2023-10-13 13:24:16 +00:00
|
|
|
Usage: consul resource apply [options] <resource>
|
|
|
|
|
|
|
|
Write and/or update a resource by providing the definition. The configuration
|
|
|
|
argument is either a file path or '-' to indicate that the resource
|
|
|
|
should be read from stdin. The data should be either in HCL or
|
|
|
|
JSON form.
|
|
|
|
|
|
|
|
Example (with flag):
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
$ consul resource apply -f=demo.hcl
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
Example (from file):
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
$ consul resource apply demo.hcl
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
Example (from stdin):
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
$ consul resource apply -
|
|
|
|
|
|
|
|
Sample demo.hcl:
|
|
|
|
|
|
|
|
ID {
|
|
|
|
Type = gvk("group.version.kind")
|
|
|
|
Name = "resource-name"
|
|
|
|
Tenancy {
|
|
|
|
Namespace = "default"
|
|
|
|
Partition = "default"
|
|
|
|
PeerName = "local"
|
|
|
|
}
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
Data {
|
|
|
|
Name = "demo"
|
|
|
|
}
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
Metadata = {
|
|
|
|
"foo" = "bar"
|
|
|
|
}
|
2023-09-08 16:20:14 +00:00
|
|
|
`
|