Skip to content

Commit

Permalink
revert grpc to http (hashicorp#20716)
Browse files Browse the repository at this point in the history
* Revert "refactor the resource client (hashicorp#20343)"

This reverts commit 3c5cb04.

* Revert "clean up http client (hashicorp#20342)"

This reverts commit 2b89025.

* remove deprecated peer

* fix the typo

* remove forwarding test as it tests grpc, should add it back
  • Loading branch information
wangxinyi7 authored Feb 23, 2024
1 parent e721524 commit b1bd6ab
Show file tree
Hide file tree
Showing 30 changed files with 3,282 additions and 861 deletions.
13 changes: 11 additions & 2 deletions command/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ 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"
resourcedeletegrpc "github.com/hashicorp/consul/command/resource/delete-grpc"
resourcelist "github.com/hashicorp/consul/command/resource/list"
resourcelistgrpc "github.com/hashicorp/consul/command/resource/list-grpc"
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 @@ -256,10 +260,15 @@ func RegisteredCommands(ui cli.Ui) map[string]mcli.CommandFactory {
entry{"peering read", func(ui cli.Ui) (cli.Command, error) { return peerread.New(ui), nil }},
entry{"reload", func(ui cli.Ui) (cli.Command, error) { return reload.New(ui), nil }},
entry{"resource", func(cli.Ui) (cli.Command, error) { return resource.New(), nil }},
entry{"resource apply", func(ui cli.Ui) (cli.Command, error) { return resourceapply.New(ui), nil }},
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 read-grpc", func(ui cli.Ui) (cli.Command, error) { return resourcereadgrpc.New(ui), nil }},
entry{"resource list-grpc", func(ui cli.Ui) (cli.Command, error) { return resourcelistgrpc.New(ui), nil }},
entry{"resource delete-grpc", func(ui cli.Ui) (cli.Command, error) { return resourcedeletegrpc.New(ui), nil }},
entry{"resource list", func(ui cli.Ui) (cli.Command, error) { return resourcelist.New(ui), nil }},
entry{"resource read", func(ui cli.Ui) (cli.Command, error) { return resourceread.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 }},
entry{"services register", func(ui cli.Ui) (cli.Command, error) { return svcsregister.New(ui), nil }},
Expand Down
150 changes: 150 additions & 0 deletions command/resource/apply-grpc/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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) {
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
res := resource.ResourceGRPC{C: resourceClient}
entry, err := res.Apply(parsedResource)
if err != nil {
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, "", 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.", parsedResource.Id.Type.Group, parsedResource.Id.Type.GroupVersion, parsedResource.Id.Type.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 {
Partition = "default"
Namespace = "default"
PeerName = "local"
}
}
Data {
Name = "demo"
}
Metadata = {
"foo" = "bar"
}
`
Loading

0 comments on commit b1bd6ab

Please sign in to comment.