Skip to content

Commit

Permalink
Move hcp client to subpackage hcpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Apr 27, 2023
1 parent f03d6a0 commit b560be6
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 33 deletions.
3 changes: 2 additions & 1 deletion agent/consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
agentgrpc "github.com/hashicorp/consul/agent/grpc-internal"
"github.com/hashicorp/consul/agent/grpc-internal/services/subscribe"
"github.com/hashicorp/consul/agent/hcp"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
logdrop "github.com/hashicorp/consul/agent/log-drop"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/pool"
Expand Down Expand Up @@ -1973,7 +1974,7 @@ func (s *Server) trackLeaderChanges() {
// hcpServerStatus is the callback used by the HCP manager to emit status updates to the HashiCorp Cloud Platform when
// enabled.
func (s *Server) hcpServerStatus(deps Deps) hcp.StatusCallback {
return func(ctx context.Context) (status hcp.ServerStatus, err error) {
return func(ctx context.Context) (status hcpclient.ServerStatus, err error) {
status.Name = s.config.NodeName
status.ID = string(s.config.NodeID)
status.Version = cslversion.GetHumanVersion()
Expand Down
9 changes: 4 additions & 5 deletions agent/consul/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import (
"golang.org/x/time/rate"
"google.golang.org/grpc"

"github.com/hashicorp/consul/agent/hcp"

"github.com/hashicorp/consul-net-rpc/net/rpc"

"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/consul/multilimiter"
rpcRate "github.com/hashicorp/consul/agent/consul/rate"
external "github.com/hashicorp/consul/agent/grpc-external"
grpcmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/rpc/middleware"
"github.com/hashicorp/consul/agent/structs"
Expand Down Expand Up @@ -2082,10 +2081,10 @@ func TestServer_hcpManager(t *testing.T) {
_, conf1 := testServerConfig(t)
conf1.BootstrapExpect = 1
conf1.RPCAdvertise = &net.TCPAddr{IP: []byte{127, 0, 0, 2}, Port: conf1.RPCAddr.Port}
hcp1 := hcp.NewMockClient(t)
hcp1.EXPECT().PushServerStatus(mock.Anything, mock.MatchedBy(func(status *hcp.ServerStatus) bool {
hcp1 := hcpclient.NewMockClient(t)
hcp1.EXPECT().PushServerStatus(mock.Anything, mock.MatchedBy(func(status *hcpclient.ServerStatus) bool {
return status.ID == string(conf1.NodeID)
})).Run(func(ctx context.Context, status *hcp.ServerStatus) {
})).Run(func(ctx context.Context, status *hcpclient.ServerStatus) {
require.Equal(t, status.LanAddress, "127.0.0.2")
}).Call.Return(nil)

Expand Down
8 changes: 4 additions & 4 deletions agent/hcp/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"

"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/hcp"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/lib/retry"
)
Expand Down Expand Up @@ -128,9 +128,9 @@ func doHCPBootstrap(ctx context.Context, rc *config.RuntimeConfig, ui UI) (strin
Jitter: retry.NewJitter(50),
}

var bsCfg *hcp.BootstrapConfig
var bsCfg *hcpclient.BootstrapConfig

client, err := hcp.NewClient(rc.Cloud)
client, err := hcpclient.NewClient(rc.Cloud)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func doHCPBootstrap(ctx context.Context, rc *config.RuntimeConfig, ui UI) (strin
return cfgJSON, nil
}

func persistTLSCerts(dataDir string, bsCfg *hcp.BootstrapConfig) error {
func persistTLSCerts(dataDir string, bsCfg *hcpclient.BootstrapConfig) error {
dir := filepath.Join(dataDir, subDir)

if bsCfg.TLSCert == "" || bsCfg.TLSCertKey == "" {
Expand Down
8 changes: 4 additions & 4 deletions agent/hcp/client.go → agent/hcp/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package hcp
package client

import (
"context"
Expand All @@ -11,13 +11,13 @@ import (

httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"

"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/version"
hcpgnm "github.com/hashicorp/hcp-sdk-go/clients/cloud-global-network-manager-service/preview/2022-02-15/client/global_network_manager_service"
gnmmod "github.com/hashicorp/hcp-sdk-go/clients/cloud-global-network-manager-service/preview/2022-02-15/models"
"github.com/hashicorp/hcp-sdk-go/httpclient"
"github.com/hashicorp/hcp-sdk-go/resource"

"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/version"
)

// Client interface exposes HCP operations that can be invoked by Consul
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions agent/hcp/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
package hcp

import (
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/consul/agent/hcp/scada"
"github.com/hashicorp/go-hclog"
)

// Deps contains the interfaces that the rest of Consul core depends on for HCP integration.
type Deps struct {
Client Client
Client hcpclient.Client
Provider scada.Provider
}

func NewDeps(cfg config.CloudConfig, logger hclog.Logger) (d Deps, err error) {
d.Client, err = NewClient(cfg)
d.Client, err = hcpclient.NewClient(cfg)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions agent/hcp/discover/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log"
"time"

"github.com/hashicorp/consul/agent/hcp"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/config"
)

Expand All @@ -32,7 +32,7 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
return nil, err
}

client, err := hcp.NewClient(cfg.CloudConfig)
client, err := hcpclient.NewClient(cfg.CloudConfig)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions agent/hcp/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-hclog"
)
Expand All @@ -18,7 +19,7 @@ var (
)

type ManagerConfig struct {
Client Client
Client hcpclient.Client

StatusFn StatusCallback
MinInterval time.Duration
Expand Down Expand Up @@ -47,7 +48,7 @@ func (cfg *ManagerConfig) nextHeartbeat() time.Duration {
return min + lib.RandomStagger(max-min)
}

type StatusCallback func(context.Context) (ServerStatus, error)
type StatusCallback func(context.Context) (hcpclient.ServerStatus, error)

type Manager struct {
logger hclog.Logger
Expand Down
25 changes: 13 additions & 12 deletions agent/hcp/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"testing"
"time"

hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)

func TestManager_Run(t *testing.T) {
client := NewMockClient(t)
statusF := func(ctx context.Context) (ServerStatus, error) {
return ServerStatus{ID: t.Name()}, nil
client := hcpclient.NewMockClient(t)
statusF := func(ctx context.Context) (hcpclient.ServerStatus, error) {
return hcpclient.ServerStatus{ID: t.Name()}, nil
}
updateCh := make(chan struct{}, 1)
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Once()
client.EXPECT().PushServerStatus(mock.Anything, &hcpclient.ServerStatus{ID: t.Name()}).Return(nil).Once()
mgr := NewManager(ManagerConfig{
Client: client,
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
Expand All @@ -43,14 +44,14 @@ func TestManager_Run(t *testing.T) {
}

func TestManager_SendUpdate(t *testing.T) {
client := NewMockClient(t)
statusF := func(ctx context.Context) (ServerStatus, error) {
return ServerStatus{ID: t.Name()}, nil
client := hcpclient.NewMockClient(t)
statusF := func(ctx context.Context) (hcpclient.ServerStatus, error) {
return hcpclient.ServerStatus{ID: t.Name()}, nil
}
updateCh := make(chan struct{}, 1)

// Expect two calls, once during run startup and again when SendUpdate is called
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Twice()
client.EXPECT().PushServerStatus(mock.Anything, &hcpclient.ServerStatus{ID: t.Name()}).Return(nil).Twice()
mgr := NewManager(ManagerConfig{
Client: client,
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
Expand All @@ -73,14 +74,14 @@ func TestManager_SendUpdate(t *testing.T) {
}

func TestManager_SendUpdate_Periodic(t *testing.T) {
client := NewMockClient(t)
statusF := func(ctx context.Context) (ServerStatus, error) {
return ServerStatus{ID: t.Name()}, nil
client := hcpclient.NewMockClient(t)
statusF := func(ctx context.Context) (hcpclient.ServerStatus, error) {
return hcpclient.ServerStatus{ID: t.Name()}, nil
}
updateCh := make(chan struct{}, 1)

// Expect two calls, once during run startup and again when SendUpdate is called
client.EXPECT().PushServerStatus(mock.Anything, &ServerStatus{ID: t.Name()}).Return(nil).Twice()
client.EXPECT().PushServerStatus(mock.Anything, &hcpclient.ServerStatus{ID: t.Name()}).Return(nil).Twice()
mgr := NewManager(ManagerConfig{
Client: client,
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
Expand Down

0 comments on commit b560be6

Please sign in to comment.