Skip to content

Commit

Permalink
refactor the resource client (#20343)
Browse files Browse the repository at this point in the history
* renaming files
  • Loading branch information
xwa153 authored Jan 30, 2024
1 parent 2b89025 commit 3c5cb04
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 464 deletions.
8 changes: 3 additions & 5 deletions command/resource/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/mitchellh/cli"

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

Expand Down Expand Up @@ -59,7 +58,7 @@ func (c *cmd) Run(args []string) int {
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)
parsedResource, err := client.ParseResourceInput(input, c.testStdin)
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
return 1
Expand All @@ -83,15 +82,14 @@ func (c *cmd) Run(args []string) int {
}

// write resource
res := resource.ResourceGRPC{C: resourceClient}
entry, err := res.Apply(parsedResource)
entry, err := resourceClient.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)
b, err := json.MarshalIndent(entry, "", client.JSON_INDENT)
if err != nil {
c.UI.Error("Failed to encode output data")
return 1
Expand Down
172 changes: 172 additions & 0 deletions command/resource/client/client.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 client

import (
"context"
"fmt"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

"github.com/hashicorp/consul/proto-public/pbresource"
)

const (
HeaderConsulToken = "x-consul-token"
)

type GRPCClient struct {
Client pbresource.ResourceServiceClient
Config *GRPCConfig
Conn *grpc.ClientConn
}

func NewGRPCClient(config *GRPCConfig) (*GRPCClient, error) {
conn, err := dial(config)
if err != nil {
return nil, fmt.Errorf("error dialing grpc: %+v", err)
}
return &GRPCClient{
Client: pbresource.NewResourceServiceClient(conn),
Config: config,
Conn: conn,
}, nil
}

func (client *GRPCClient) Apply(parsedResource *pbresource.Resource) (*pbresource.Resource, error) {
token, err := client.Config.GetToken()
if err != nil {
return nil, err
}
ctx := context.Background()
if token != "" {
ctx = metadata.AppendToOutgoingContext(ctx, HeaderConsulToken, token)
}

defer client.Conn.Close()
writeRsp, err := client.Client.Write(ctx, &pbresource.WriteRequest{Resource: parsedResource})
if err != nil {
return nil, fmt.Errorf("error writing resource: %+v", err)
}

return writeRsp.Resource, err
}

func (client *GRPCClient) Read(resourceType *pbresource.Type, resourceTenancy *pbresource.Tenancy, resourceName string, stale bool) (*pbresource.Resource, error) {
token, err := client.Config.GetToken()
if err != nil {
return nil, err
}
ctx := context.Background()
if !stale {
ctx = metadata.AppendToOutgoingContext(ctx, "x-consul-consistency-mode", "consistent")
}
if token != "" {
ctx = metadata.AppendToOutgoingContext(ctx, HeaderConsulToken, token)
}

defer client.Conn.Close()
readRsp, err := client.Client.Read(ctx, &pbresource.ReadRequest{
Id: &pbresource.ID{
Type: resourceType,
Tenancy: resourceTenancy,
Name: resourceName,
},
})

if err != nil {
return nil, fmt.Errorf("error reading resource: %+v", err)
}

return readRsp.Resource, err
}

func (client *GRPCClient) List(resourceType *pbresource.Type, resourceTenancy *pbresource.Tenancy, prefix string, stale bool) ([]*pbresource.Resource, error) {
token, err := client.Config.GetToken()
if err != nil {
return nil, err
}
ctx := context.Background()
if !stale {
ctx = metadata.AppendToOutgoingContext(ctx, "x-consul-consistency-mode", "consistent")
}
if token != "" {
ctx = metadata.AppendToOutgoingContext(context.Background(), HeaderConsulToken, token)
}

defer client.Conn.Close()
listRsp, err := client.Client.List(ctx, &pbresource.ListRequest{
Type: resourceType,
Tenancy: resourceTenancy,
NamePrefix: prefix,
})

if err != nil {
return nil, fmt.Errorf("error listing resource: %+v", err)
}

return listRsp.Resources, err
}

func (client *GRPCClient) Delete(resourceType *pbresource.Type, resourceTenancy *pbresource.Tenancy, resourceName string) error {
token, err := client.Config.GetToken()
if err != nil {
return err
}
ctx := context.Background()
if token != "" {
ctx = metadata.AppendToOutgoingContext(context.Background(), HeaderConsulToken, token)
}

defer client.Conn.Close()
_, err = client.Client.Delete(ctx, &pbresource.DeleteRequest{
Id: &pbresource.ID{
Type: resourceType,
Tenancy: resourceTenancy,
Name: resourceName,
},
})

if err != nil {
return fmt.Errorf("error deleting resource: %+v", err)
}

return nil
}

func dial(c *GRPCConfig) (*grpc.ClientConn, error) {
err := checkCertificates(c)
if err != nil {
return nil, err
}
var dialOpts []grpc.DialOption
if c.GRPCTLS {
tlsConfig, err := SetupTLSConfig(c)
if err != nil {
return nil, fmt.Errorf("failed to setup tls config when tried to establish grpc call: %w", err)
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

return grpc.Dial(c.Address, dialOpts...)
}

func checkCertificates(c *GRPCConfig) error {
if c.GRPCTLS {
certFileEmpty := c.CertFile == ""
keyFileEmpty := c.CertFile == ""

// both files need to be empty or both files need to be provided
if certFileEmpty != keyFileEmpty {
return fmt.Errorf("you have to provide client certificate file and key file at the same time " +
"if you intend to communicate in TLS/SSL mode")
}
}
return nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 0 additions & 65 deletions command/resource/client/grpc-client.go

This file was deleted.

Loading

0 comments on commit 3c5cb04

Please sign in to comment.