Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xw/net 6307 grpc client apply #20107

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import (
"github.com/hashicorp/consul/command/reload"
"github.com/hashicorp/consul/command/resource"
resourceapply "github.com/hashicorp/consul/command/resource/apply"
resourceapplygrpc "github.com/hashicorp/consul/command/resource/apply-grpc"
resourcedelete "github.com/hashicorp/consul/command/resource/delete"
resourcelist "github.com/hashicorp/consul/command/resource/list"
resourceread "github.com/hashicorp/consul/command/resource/read"
Expand Down Expand Up @@ -258,6 +259,8 @@ func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory {
entry{"resource read", func(ui cli.Ui) (cli.Command, error) { return resourceread.New(ui), nil }},
entry{"resource delete", func(ui cli.Ui) (cli.Command, error) { return resourcedelete.New(ui), nil }},
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 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
155 changes: 155 additions & 0 deletions command/resource/apply-grpc/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package apply

import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"

"github.com/mitchellh/cli"

"github.com/hashicorp/consul/command/resource"
"github.com/hashicorp/consul/command/resource/client"
)

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
help string

filePath string

testStdin io.Reader
}

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{}
client.MergeFlags(c.flags, c.grpcFlags.ClientFlags())
c.help = client.Usage(help, c.flags)
}

func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
if !errors.Is(err, flag.ErrHelp) {
mkeeler marked this conversation as resolved.
Show resolved Hide resolved
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
return 1
}
c.UI.Error(fmt.Sprintf("Failed to run apply command: %v", err))
return 1
}

// parse resource
input := c.filePath
if input == "" {
c.UI.Error("Required '-f' flag was not provided to specify where to load the resource content from")
return 1
}
parsedResource, 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
}
if parsedResource == nil {
c.UI.Error("Unable to parse the file argument")
return 1
}

// 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 connect to Consul agent: %s", err))
return 1
}

// 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))
return 1
}

// display response
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 client.Usage(c.help, nil)
}

const synopsis = "Writes/updates resource information"

const help = `
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):

$ consul resource apply -f=demo.hcl

Example (from stdin):

$ consul resource apply -f - < demo.hcl

Sample demo.hcl:

ID {
Type = gvk("group.version.kind")
Name = "resource-name"
Tenancy {
Namespace = "default"
Partition = "default"
PeerName = "local"
}
}

Data {
Name = "demo"
}

Metadata = {
"foo" = "bar"
}
`
Loading
Loading