2023-09-05 16:17:19 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package read
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-09-05 16:17:19 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2024-02-23 20:27:49 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource"
|
2023-09-22 20:32:08 +00:00
|
|
|
"github.com/hashicorp/consul/command/resource/client"
|
2024-01-20 03:11:41 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-09-05 16:17:19 +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-05 16:17:19 +00:00
|
|
|
|
|
|
|
filePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2024-02-23 20:27:49 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
c.flags.StringVar(&c.filePath, "f", "", "File path with resource definition")
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
|
|
|
flags.Merge(c.flags, c.http.MultiTenancyFlags())
|
|
|
|
// TODO(peering/v2) add back ability to query peers
|
|
|
|
// flags.Merge(c.flags, c.http.AddPeerName())
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
2024-02-23 20:27:49 +00:00
|
|
|
var gvk *resource.GVK
|
2023-09-05 16:17:19 +00:00
|
|
|
var resourceName string
|
2024-02-23 20:27:49 +00:00
|
|
|
var opts *client.QueryOptions
|
2023-09-05 16:17:19 +00:00
|
|
|
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
if !errors.Is(err, flag.ErrHelp) {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.flags.Lookup("f").Value.String() != "" {
|
2024-02-23 20:27:49 +00:00
|
|
|
if c.filePath != "" {
|
|
|
|
parsedResource, err := resource.ParseResourceFromFile(c.filePath)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedResource == nil {
|
|
|
|
c.UI.Error("Unable to parse the file argument")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
gvk = &resource.GVK{
|
|
|
|
Group: parsedResource.Id.Type.GetGroup(),
|
|
|
|
Version: parsedResource.Id.Type.GetGroupVersion(),
|
|
|
|
Kind: parsedResource.Id.Type.GetKind(),
|
|
|
|
}
|
|
|
|
resourceName = parsedResource.Id.GetName()
|
|
|
|
opts = &client.QueryOptions{
|
|
|
|
Namespace: parsedResource.Id.Tenancy.GetNamespace(),
|
|
|
|
Partition: parsedResource.Id.Tenancy.GetPartition(),
|
|
|
|
Token: c.http.Token(),
|
|
|
|
RequireConsistent: !c.http.Stale(),
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-05 16:17:19 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Please provide an input file with resource definition"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
2024-02-23 20:27:49 +00:00
|
|
|
var resourceType *pbresource.Type
|
|
|
|
resourceType, resourceName, err = resource.GetTypeAndResourceName(args)
|
|
|
|
gvk = &resource.GVK{
|
|
|
|
Group: resourceType.GetGroup(),
|
|
|
|
Version: resourceType.GetGroupVersion(),
|
|
|
|
Kind: resourceType.GetKind(),
|
|
|
|
}
|
2023-09-05 16:17:19 +00:00
|
|
|
if err != nil {
|
2023-09-18 13:09:31 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Incorrect argument format: %s", err))
|
2023-09-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
inputArgs := args[2:]
|
2024-02-23 20:27:49 +00:00
|
|
|
err = resource.ParseInputParams(inputArgs, c.flags)
|
2023-09-13 18:48:40 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing input arguments: %v", err))
|
2023-09-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if c.filePath != "" {
|
2023-09-18 13:09:31 +00:00
|
|
|
c.UI.Error("Incorrect argument format: File argument is not needed when resource information is provided with the command")
|
|
|
|
return 1
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
2024-02-23 20:27:49 +00:00
|
|
|
opts = &client.QueryOptions{
|
|
|
|
Namespace: c.http.Namespace(),
|
|
|
|
Partition: c.http.Partition(),
|
|
|
|
Token: c.http.Token(),
|
|
|
|
RequireConsistent: !c.http.Stale(),
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
config := api.DefaultConfig()
|
|
|
|
|
|
|
|
c.http.MergeOntoConfig(config)
|
|
|
|
resourceClient, err := client.NewClient(config)
|
2024-01-30 18:12:09 +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-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
res := resource.Resource{C: resourceClient}
|
|
|
|
|
|
|
|
entry, err := res.Read(gvk, resourceName, opts)
|
2023-09-05 16:17:19 +00:00
|
|
|
if err != nil {
|
2024-02-23 20:27:49 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error reading resource %s/%s: %v", gvk, resourceName, err))
|
2023-09-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-23 20:27:49 +00:00
|
|
|
b, err := json.MarshalIndent(entry, "", " ")
|
2023-09-05 16:17:19 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error("Failed to encode output data")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = "Read resource information"
|
|
|
|
const help = `
|
|
|
|
Usage: You have two options to read the resource specified by the given
|
2024-02-23 20:27:49 +00:00
|
|
|
type, name, partition and namespace and outputs its JSON representation.
|
2023-09-05 16:17:19 +00:00
|
|
|
|
2024-01-29 20:08:31 +00:00
|
|
|
consul resource read [type] [name] -partition=<default> -namespace=<default>
|
2023-09-05 16:17:19 +00:00
|
|
|
consul resource read -f [resource_file_path]
|
|
|
|
|
|
|
|
But you could only use one of the approaches.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2024-01-29 20:08:31 +00:00
|
|
|
$ consul resource read catalog.v2beta1.Service card-processor -partition=billing -namespace=payments
|
2023-09-05 16:17:19 +00:00
|
|
|
$ consul resource read -f resource.hcl
|
|
|
|
|
|
|
|
In resource.hcl, it could be:
|
|
|
|
ID {
|
2024-02-23 20:27:49 +00:00
|
|
|
Type = gvk("catalog.v2beta1.Service")
|
|
|
|
Name = "card-processor"
|
|
|
|
Tenancy {
|
|
|
|
Namespace = "payments"
|
|
|
|
Partition = "billing"
|
|
|
|
}
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
`
|