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"
|
2024-02-23 20:27:49 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/hashicorp/consul/command/resource"
|
2023-09-22 20:32:08 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource/client"
|
2024-02-23 20:27:49 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-09-08 16:20:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
2024-02-23 20:27:49 +00:00
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
2023-09-08 16:20:14 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeWriteRequest(parsedResource *pbresource.Resource) (payload *resource.WriteRequest, error error) {
|
|
|
|
// 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")
|
|
|
|
|
|
|
|
return &resource.WriteRequest{
|
|
|
|
Data: resourceData,
|
|
|
|
Metadata: parsedResource.GetMetadata(),
|
|
|
|
Owner: parsedResource.GetOwner(),
|
|
|
|
}, nil
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-02-23 20:27:49 +00:00
|
|
|
|
|
|
|
if input == "" && len(c.flags.Args()) > 0 {
|
|
|
|
input = c.flags.Arg(0)
|
2023-10-13 13:24:16 +00:00
|
|
|
}
|
2024-02-23 20:27:49 +00:00
|
|
|
|
|
|
|
var parsedResource *pbresource.Resource
|
|
|
|
|
|
|
|
if input != "" {
|
|
|
|
data, err := resource.ParseResourceInput(input, c.testStdin)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
parsedResource = data
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
2024-02-23 20:27:49 +00:00
|
|
|
|
2023-09-08 16:20:14 +00:00
|
|
|
if parsedResource == nil {
|
|
|
|
c.UI.Error("Unable to parse the file argument")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +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 {
|
2024-02-23 20:27:49 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connect to Consul agent: %s", err))
|
2023-09-08 16:20:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
2024-02-23 20:27:49 +00:00
|
|
|
|
|
|
|
res := resource.Resource{C: resourceClient}
|
|
|
|
|
|
|
|
opts := &client.QueryOptions{
|
|
|
|
Namespace: parsedResource.Id.Tenancy.GetNamespace(),
|
|
|
|
Partition: parsedResource.Id.Tenancy.GetPartition(),
|
|
|
|
Token: c.http.Token(),
|
|
|
|
}
|
|
|
|
|
|
|
|
gvk := &resource.GVK{
|
|
|
|
Group: parsedResource.Id.Type.GetGroup(),
|
|
|
|
Version: parsedResource.Id.Type.GetGroupVersion(),
|
|
|
|
Kind: parsedResource.Id.Type.GetKind(),
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRequest, err := makeWriteRequest(parsedResource)
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
2024-02-23 20:27:49 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing hcl input: %v", err))
|
2023-09-08 16:20:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
entry, err := res.Apply(gvk, parsedResource.Id.GetName(), opts, writeRequest)
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
2024-02-23 20:27:49 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error writing resource %s/%s: %v", gvk, parsedResource.Id.GetName(), err))
|
2023-09-08 16:20:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
b, err := json.MarshalIndent(entry, "", " ")
|
2023-09-08 16:20:14 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error("Failed to encode output data")
|
|
|
|
return 1
|
|
|
|
}
|
2024-01-30 18:12:09 +00:00
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("%s.%s.%s '%s' created.", gvk.Group, gvk.Version, gvk.Kind, parsedResource.Id.GetName()))
|
|
|
|
c.UI.Info(string(b))
|
2023-09-08 16:20:14 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2024-02-23 20:27:49 +00:00
|
|
|
return flags.Usage(c.help, nil)
|
2023-09-08 16:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
Example (from file):
|
|
|
|
|
|
|
|
$ consul resource apply demo.hcl
|
|
|
|
|
2023-10-13 13:24:16 +00:00
|
|
|
Example (from stdin):
|
2023-09-08 16:20:14 +00:00
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
$ consul resource apply -
|
2023-10-13 13:24:16 +00:00
|
|
|
|
|
|
|
Sample demo.hcl:
|
|
|
|
|
|
|
|
ID {
|
|
|
|
Type = gvk("group.version.kind")
|
|
|
|
Name = "resource-name"
|
|
|
|
Tenancy {
|
2024-02-23 20:27:49 +00:00
|
|
|
Namespace = "default"
|
|
|
|
Partition = "default"
|
2023-10-13 13:24:16 +00:00
|
|
|
}
|
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
|
|
|
`
|