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"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2023-09-13 18:48:40 +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-01-30 18:12:09 +00:00
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
grpcFlags *client.GRPCFlags
|
|
|
|
resourceFlags *client.ResourceFlags
|
|
|
|
help string
|
2023-09-05 16:17:19 +00:00
|
|
|
|
|
|
|
filePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2024-01-30 18:12:09 +00:00
|
|
|
c.flags.StringVar(&c.filePath, "f", "",
|
|
|
|
"File path with resource definition")
|
|
|
|
|
|
|
|
c.grpcFlags = &client.GRPCFlags{}
|
|
|
|
c.resourceFlags = &client.ResourceFlags{}
|
|
|
|
client.MergeFlags(c.flags, c.grpcFlags.ClientFlags())
|
|
|
|
client.MergeFlags(c.flags, c.resourceFlags.ResourceFlags())
|
|
|
|
c.help = client.Usage(help, c.flags)
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
2024-01-30 18:12:09 +00:00
|
|
|
var resourceType *pbresource.Type
|
|
|
|
var resourceTenancy *pbresource.Tenancy
|
2023-09-05 16:17:19 +00:00
|
|
|
var resourceName string
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-01-30 18:12:09 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to run read command: %v", err))
|
|
|
|
return 1
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:12:09 +00:00
|
|
|
// collect resource type, name and tenancy
|
2023-09-05 16:17:19 +00:00
|
|
|
if c.flags.Lookup("f").Value.String() != "" {
|
2024-01-30 18:12:09 +00:00
|
|
|
if c.filePath == "" {
|
2023-09-05 16:17:19 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Please provide an input file with resource definition"))
|
|
|
|
return 1
|
|
|
|
}
|
2024-01-30 18:12:09 +00:00
|
|
|
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("The parsed resource is nil")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceType = parsedResource.Id.Type
|
|
|
|
resourceTenancy = parsedResource.Id.Tenancy
|
|
|
|
resourceName = parsedResource.Id.Name
|
2023-09-05 16:17:19 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
2024-01-20 03:11:41 +00:00
|
|
|
resourceType, resourceName, err = resource.GetTypeAndResourceName(args)
|
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:]
|
2023-09-13 18:48:40 +00:00
|
|
|
err = resource.ParseInputParams(inputArgs, c.flags)
|
|
|
|
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-01-30 18:12:09 +00:00
|
|
|
resourceTenancy = &pbresource.Tenancy{
|
|
|
|
Partition: c.resourceFlags.Partition(),
|
|
|
|
Namespace: c.resourceFlags.Namespace(),
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:12:09 +00:00
|
|
|
// initialize client
|
|
|
|
config, err := client.LoadGRPCConfig(nil)
|
2023-09-05 16:17:19 +00:00
|
|
|
if err != nil {
|
2024-01-30 18:12:09 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error loading config: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.grpcFlags.MergeFlagsIntoGRPCConfig(config)
|
|
|
|
resourceClient, err := client.NewGRPCClient(config)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2023-09-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:12:09 +00:00
|
|
|
// read resource
|
|
|
|
res := resource.ResourceGRPC{C: resourceClient}
|
|
|
|
entry, err := res.Read(resourceType, resourceTenancy, resourceName, c.resourceFlags.Stale())
|
2023-09-05 16:17:19 +00:00
|
|
|
if err != nil {
|
2024-01-30 18:12:09 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error reading resource %s/%s: %v", resourceType, resourceName, err))
|
2023-09-05 16:17:19 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:12:09 +00:00
|
|
|
// display response
|
|
|
|
b, err := json.MarshalIndent(entry, "", resource.JSON_INDENT)
|
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-01-30 18:12:09 +00:00
|
|
|
type, name, partition, 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-01-30 18:12:09 +00:00
|
|
|
<<<<<<< HEAD
|
|
|
|
Type = gvk("catalog.v2beta1.Service")
|
|
|
|
Name = "card-processor"
|
|
|
|
Tenancy {
|
|
|
|
Partition = "billing"
|
|
|
|
Namespace = "payments"
|
|
|
|
}
|
2023-09-05 16:17:19 +00:00
|
|
|
}
|
|
|
|
`
|