Skip to content

Commit 8ec5b52

Browse files
authored
fix(agent): update GetClientWithRetry to use fabricAddr and enhance error messages for stack context (#2104)
Agent must call Authenticate to auth to the cloud
1 parent af54c0c commit 8ec5b52

18 files changed

Lines changed: 78 additions & 36 deletions

src/pkg/agent/tools/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"github.com/DefangLabs/defang/src/pkg/cli/client"
88
)
99

10-
func GetClientWithRetry(ctx context.Context, cli CLIInterface, config StackConfig) (*client.GrpcClient, error) {
11-
client, err := cli.Connect(ctx, config.FabricAddr)
10+
func GetClientWithRetry(ctx context.Context, cli CLIInterface, fabricAddr string) (*client.GrpcClient, error) {
11+
client, err := cli.Connect(ctx, fabricAddr)
1212
if err != nil {
13-
err = cli.InteractiveLoginMCP(ctx, config.FabricAddr, common.MCPDevelopmentClient)
13+
err = cli.InteractiveLoginMCP(ctx, fabricAddr, common.MCPDevelopmentClient)
1414
if err != nil {
1515
return nil, err
1616
}
1717

1818
// Reconnect with the new token
19-
client, err = cli.Connect(ctx, config.FabricAddr)
19+
client, err = cli.Connect(ctx, fabricAddr)
2020
if err != nil {
2121
return nil, err
2222
}

src/pkg/agent/tools/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func HandleDeployTool(ctx context.Context, loader client.Loader, params DeployPa
3131
}
3232

3333
term.Debug("Function invoked: cli.Connect")
34-
client, err := GetClientWithRetry(ctx, cli, sc)
34+
client, err := GetClientWithRetry(ctx, cli, sc.FabricAddr)
3535
if err != nil {
3636
var noBrowserErr auth.ErrNoBrowser
3737
if errors.As(err, &noBrowserErr) {

src/pkg/agent/tools/deploy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func (m *MockDeployCLI) Connect(ctx context.Context, fabricAddr string) (*client
5151
return &client.GrpcClient{}, nil
5252
}
5353

54-
func (m *MockDeployCLI) NewProvider(ctx context.Context, providerId client.ProviderID, client client.FabricClient, stack string) client.Provider {
54+
func (m *MockDeployCLI) NewProvider(ctx context.Context, providerId client.ProviderID, fabric client.FabricClient, stack string) client.Provider {
5555
m.CallLog = append(m.CallLog, fmt.Sprintf("NewProvider(%s)", providerId))
56-
return nil
56+
return client.MockProvider{}
5757
}
5858

5959
func (m *MockDeployCLI) InteractiveLoginMCP(ctx context.Context, fabricAddr string, mcpClient string) error {

src/pkg/agent/tools/destroy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type DestroyParams struct {
2020

2121
func HandleDestroyTool(ctx context.Context, loader client.Loader, params DestroyParams, cli CLIInterface, ec elicitations.Controller, sc StackConfig) (string, error) {
2222
term.Debug("Function invoked: cli.Connect")
23-
client, err := GetClientWithRetry(ctx, cli, sc)
23+
client, err := GetClientWithRetry(ctx, cli, sc.FabricAddr)
2424
if err != nil {
2525
var noBrowserErr auth.ErrNoBrowser
2626
if errors.As(err, &noBrowserErr) {

src/pkg/agent/tools/destroy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (m *MockDestroyCLI) Connect(ctx context.Context, fabricAddr string) (*clien
3939

4040
func (m *MockDestroyCLI) NewProvider(ctx context.Context, providerId client.ProviderID, grpcClient client.FabricClient, stack string) client.Provider {
4141
m.CallLog = append(m.CallLog, fmt.Sprintf("NewProvider(%s)", providerId))
42-
return nil
42+
return client.MockProvider{}
4343
}
4444

4545
func (m *MockDestroyCLI) ComposeDown(ctx context.Context, projectName string, grpcClient *client.GrpcClient, provider client.Provider) (string, error) {

src/pkg/agent/tools/estimate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func HandleEstimateTool(ctx context.Context, loader client.Loader, params Estima
2828
}
2929

3030
term.Debug("Function invoked: cli.Connect")
31-
fabric, err := GetClientWithRetry(ctx, cli, sc)
31+
fabric, err := GetClientWithRetry(ctx, cli, sc.FabricAddr)
3232
if err != nil {
3333
var noBrowserErr auth.ErrNoBrowser
3434
if errors.As(err, &noBrowserErr) {

src/pkg/agent/tools/listConfig.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type ListConfigParams struct {
2121
// HandleListConfigTool handles the list config tool logic
2222
func HandleListConfigTool(ctx context.Context, loader client.Loader, params ListConfigParams, cli CLIInterface, ec elicitations.Controller, sc StackConfig) (string, error) {
2323
term.Debug("Function invoked: cli.Connect")
24-
client, err := GetClientWithRetry(ctx, cli, sc)
24+
client, err := GetClientWithRetry(ctx, cli, sc.FabricAddr)
2525
if err != nil {
2626
var noBrowserErr auth.ErrNoBrowser
2727
if errors.As(err, &noBrowserErr) {
@@ -56,11 +56,11 @@ func HandleListConfigTool(ctx context.Context, loader client.Loader, params List
5656

5757
numConfigs := len(config.Names)
5858
if numConfigs == 0 {
59-
return fmt.Sprintf("No config variables found for the project %q.", projectName), nil
59+
return fmt.Sprintf("No config variables found for the project %q in stack %q.", projectName, sc.Stack.Name), nil
6060
}
6161

6262
configNames := make([]string, numConfigs)
6363
copy(configNames, config.Names)
6464

65-
return fmt.Sprintf("Here is the list of config variables for the project %q: %v", projectName, strings.Join(configNames, ", ")), nil
65+
return fmt.Sprintf("Here is the list of config variables for the project %q in stack %q: %v", projectName, sc.Stack.Name, strings.Join(configNames, ", ")), nil
6666
}

src/pkg/agent/tools/listConfig_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func (m *MockListConfigCLI) Connect(ctx context.Context, fabricAddr string) (*cl
3636
return &client.GrpcClient{}, nil
3737
}
3838

39-
func (m *MockListConfigCLI) NewProvider(ctx context.Context, providerId client.ProviderID, client client.FabricClient, stack string) client.Provider {
39+
func (m *MockListConfigCLI) NewProvider(ctx context.Context, providerId client.ProviderID, fabric client.FabricClient, stack string) client.Provider {
4040
m.CallLog = append(m.CallLog, fmt.Sprintf("NewProvider(%s)", providerId))
41-
return nil // Mock provider
41+
return client.MockProvider{}
4242
}
4343

4444
func (m *MockListConfigCLI) LoadProjectNameWithFallback(ctx context.Context, loader client.Loader, provider client.Provider) (string, error) {
@@ -119,7 +119,7 @@ func TestHandleListConfigTool(t *testing.T) {
119119
Names: []string{"DATABASE_URL"},
120120
}
121121
},
122-
expectedTextContains: "Here is the list of config variables for the project \"test-project\": DATABASE_URL",
122+
expectedTextContains: "Here is the list of config variables for the project \"test-project\" in stack \"test-stack\": DATABASE_URL",
123123
},
124124
}
125125

src/pkg/agent/tools/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func HandleLogsTool(ctx context.Context, loader client.Loader, params LogsParams
4242
}
4343

4444
term.Debug("Function invoked: cli.Connect")
45-
client, err := GetClientWithRetry(ctx, cli, sc)
45+
client, err := GetClientWithRetry(ctx, cli, sc.FabricAddr)
4646
if err != nil {
4747
var noBrowserErr auth.ErrNoBrowser
4848
if errors.As(err, &noBrowserErr) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tools
2+
3+
import (
4+
"context"
5+
6+
"github.com/DefangLabs/defang/src/pkg/agent/common"
7+
"github.com/DefangLabs/defang/src/pkg/cli/client"
8+
)
9+
10+
type ProjectNameParams struct {
11+
common.LoaderParams
12+
}
13+
14+
// HandleProjectNameTool handles the project name tool logic
15+
func HandleProjectNameTool(ctx context.Context, loader client.Loader) (string, error) {
16+
pn, _, err := loader.LoadProjectName(ctx)
17+
if err != nil {
18+
return "", err
19+
}
20+
return pn, nil
21+
}

0 commit comments

Comments
 (0)