Skip to content

Commit

Permalink
grpc CLI client read command (#20178)
Browse files Browse the repository at this point in the history
* apply commmand works
  • Loading branch information
xwa153 authored Jan 20, 2024
1 parent a9dd6f5 commit 6188db4
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 22 deletions.
2 changes: 2 additions & 0 deletions command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ import (
resourcedelete "github.com/hashicorp/consul/command/resource/delete"
resourcelist "github.com/hashicorp/consul/command/resource/list"
resourceread "github.com/hashicorp/consul/command/resource/read"
resourcereadgrpc "github.com/hashicorp/consul/command/resource/read-grpc"
"github.com/hashicorp/consul/command/rtt"
"github.com/hashicorp/consul/command/services"
svcsderegister "github.com/hashicorp/consul/command/services/deregister"
Expand Down Expand Up @@ -261,6 +262,7 @@ func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory {
entry{"resource apply", func(ui cli.Ui) (cli.Command, error) { return resourceapply.New(ui), nil }},
// will be refactored to resource apply
entry{"resource apply-grpc", func(ui cli.Ui) (cli.Command, error) { return resourceapplygrpc.New(ui), nil }},
entry{"resource read-grpc", func(ui cli.Ui) (cli.Command, error) { return resourcereadgrpc.New(ui), nil }},
entry{"resource list", func(ui cli.Ui) (cli.Command, error) { return resourcelist.New(ui), nil }},
entry{"rtt", func(ui cli.Ui) (cli.Command, error) { return rtt.New(ui), nil }},
entry{"services", func(cli.Ui) (cli.Command, error) { return services.New(), nil }},
Expand Down
11 changes: 3 additions & 8 deletions command/resource/apply-grpc/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,20 @@ func (c *cmd) Run(args []string) int {
}

// write resource
gvk := &resource.GVK{
Group: parsedResource.Id.Type.GetGroup(),
Version: parsedResource.Id.Type.GetGroupVersion(),
Kind: parsedResource.Id.Type.GetKind(),
}
res := resource.ResourceGRPC{C: resourceClient}
entry, err := res.Apply(parsedResource)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing resource %s/%s: %v", gvk, parsedResource.Id.GetName(), err))
c.UI.Error(fmt.Sprintf("Error writing resource %s/%s: %v", parsedResource.Id.Type, parsedResource.Id.GetName(), err))
return 1
}

// display response
b, err := json.MarshalIndent(entry, "", " ")
b, err := json.MarshalIndent(entry, "", resource.JSON_INDENT)
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(fmt.Sprintf("%s.%s.%s '%s' created.", parsedResource.Id.Type.Group, parsedResource.Id.Type.GroupVersion, parsedResource.Id.Type.Kind, parsedResource.Id.GetName()))
c.UI.Info(string(b))

return 0
Expand Down
50 changes: 50 additions & 0 deletions command/resource/client/grpc-resource-flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package client

import "flag"

type ResourceFlags struct {
namespace TValue[string]
partition TValue[string]
peername TValue[string]
stale TValue[bool]
}

func (f *ResourceFlags) ResourceFlags() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Var(&f.namespace, "namespace",
"Specifies the namespace to query. If not provided, the namespace will be inferred "+
"from the request's ACL token, or will default to the `default` namespace.")
fs.Var(&f.partition, "partition",
"Specifies the admin partition to query. If not provided, the admin partition will be inferred "+
"from the request's ACL token, or will default to the `default` admin partition. "+
"Admin Partitions are a Consul Enterprise feature.")
fs.Var(&f.peername, "peer", "Specifies the name of peer to query. By default, it is `local`.")
fs.Var(&f.stale, "stale",
"Permit any Consul server (non-leader) to respond to this request. This "+
"allows for lower latency and higher throughput, but can result in "+
"stale data. This option has no effect on non-read operations. The "+
"default value is false.")
return fs
}

func (f *ResourceFlags) Namespace() string {
return f.namespace.String()
}

func (f *ResourceFlags) Partition() string {
return f.partition.String()
}

func (f *ResourceFlags) Peername() string {
return f.peername.String()
}

func (f *ResourceFlags) Stale() bool {
if f.stale.v == nil {
return false
}
return *f.stale.v
}
9 changes: 8 additions & 1 deletion command/resource/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/resource"
"github.com/hashicorp/consul/command/resource/client"
"github.com/hashicorp/consul/proto-public/pbresource"
)

func New(ui cli.Ui) *cmd {
Expand Down Expand Up @@ -85,7 +86,13 @@ func (c *cmd) Run(args []string) int {
}
} else {
var err error
gvk, resourceName, err = resource.GetTypeAndResourceName(args)
var resourceType *pbresource.Type
resourceType, resourceName, err = resource.GetTypeAndResourceName(args)
gvk = &resource.GVK{
Group: resourceType.GetGroup(),
Version: resourceType.GetGroupVersion(),
Kind: resourceType.GetKind(),
}
if err != nil {
c.UI.Error(fmt.Sprintf("Incorrect argument format: %s", err))
return 1
Expand Down
26 changes: 14 additions & 12 deletions command/resource/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/hashicorp/consul/proto-public/pbresource"
)

const JSON_INDENT = " "

type OuterResource struct {
ID *ID `json:"id"`
Owner *ID `json:"owner"`
Expand Down Expand Up @@ -150,7 +152,7 @@ func ParseInputParams(inputArgs []string, flags *flag.FlagSet) error {
return nil
}

func GetTypeAndResourceName(args []string) (gvk *GVK, resourceName string, e error) {
func GetTypeAndResourceName(args []string) (resourceType *pbresource.Type, resourceName string, e error) {
if len(args) < 2 {
return nil, "", fmt.Errorf("Must specify two arguments: resource type and resource name")
}
Expand All @@ -160,9 +162,9 @@ func GetTypeAndResourceName(args []string) (gvk *GVK, resourceName string, e err
}
resourceName = args[1]

gvk, e = inferGVKFromResourceType(args[0])
resourceType, e = inferTypeFromResourceType(args[0])

return
return resourceType, resourceName, e
}

type Resource struct {
Expand Down Expand Up @@ -267,7 +269,7 @@ func (resource *Resource) List(gvk *GVK, q *client.QueryOptions) (*ListResponse,
return out, nil
}

func inferGVKFromResourceType(resourceType string) (*GVK, error) {
func inferTypeFromResourceType(resourceType string) (*pbresource.Type, error) {
s := strings.Split(resourceType, ".")
switch length := len(s); {
// only kind is provided
Expand All @@ -282,20 +284,20 @@ func inferGVKFromResourceType(resourceType string) (*GVK, error) {
case 1:
// infer gvk from resource kind
gvkSplit := strings.Split(kindToGVKMap[kind][0], ".")
return &GVK{
Group: gvkSplit[0],
Version: gvkSplit[1],
Kind: gvkSplit[2],
return &pbresource.Type{
Group: gvkSplit[0],
GroupVersion: gvkSplit[1],
Kind: gvkSplit[2],
}, nil
// it alerts error if any conflict is found
default:
return nil, fmt.Errorf("The shorthand name has conflicts %v, please use the full name", kindToGVKMap[s[0]])
}
case length == 3:
return &GVK{
Group: s[0],
Version: s[1],
Kind: s[2],
return &pbresource.Type{
Group: s[0],
GroupVersion: s[1],
Kind: s[2],
}, nil
default:
return nil, fmt.Errorf("Must provide resource type argument with either in group.verion.kind format or its shorthand name")
Expand Down
172 changes: 172 additions & 0 deletions command/resource/read-grpc/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// 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"
"github.com/hashicorp/consul/command/resource"
"github.com/hashicorp/consul/command/resource/client"
"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
grpcFlags *client.GRPCFlags
resourceFlags *client.ResourceFlags
help string

filePath string
}

func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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)
}

func (c *cmd) Run(args []string) int {
var resourceType *pbresource.Type
var resourceTenancy *pbresource.Tenancy
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
}
c.UI.Error(fmt.Sprintf("Failed to run read command: %v", err))
return 1
}

// collect resource type, name and tenancy
if c.flags.Lookup("f").Value.String() != "" {
if c.filePath == "" {
c.UI.Error(fmt.Sprintf("Please provide an input file with resource definition"))
return 1
}
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
} else {
var err error
resourceType, resourceName, err = resource.GetTypeAndResourceName(args)
if err != nil {
c.UI.Error(fmt.Sprintf("Incorrect argument format: %s", err))
return 1
}

inputArgs := args[2:]
err = resource.ParseInputParams(inputArgs, c.flags)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing input arguments: %v", err))
return 1
}
if c.filePath != "" {
c.UI.Error("Incorrect argument format: File argument is not needed when resource information is provided with the command")
return 1
}
resourceTenancy = &pbresource.Tenancy{
Namespace: c.resourceFlags.Namespace(),
Partition: c.resourceFlags.Partition(),
PeerName: c.resourceFlags.Peername(),
}
}

// initialize client
config, err := client.LoadGRPCConfig(nil)
if err != nil {
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))
return 1
}

// read resource
res := resource.ResourceGRPC{C: resourceClient}
entry, err := res.Read(resourceType, resourceTenancy, resourceName, c.resourceFlags.Stale())
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading resource %s/%s: %v", resourceType, resourceName, err))
return 1
}

// display response
b, err := json.MarshalIndent(entry, "", resource.JSON_INDENT)
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
type, name, partition, namespace and peer and outputs its JSON representation.
consul resource read [type] [name] -partition=<default> -namespace=<default> -peer=<local>
consul resource read -f [resource_file_path]
But you could only use one of the approaches.
Example:
$ consul resource read catalog.v2beta1.Service card-processor -partition=billing -namespace=payments -peer=eu
$ consul resource read -f resource.hcl
In resource.hcl, it could be:
ID {
Type = gvk("catalog.v2beta1.Service")
Name = "card-processor"
Tenancy {
Namespace = "payments"
Partition = "billing"
PeerName = "eu"
}
}
`
Loading

0 comments on commit 6188db4

Please # to comment.