Skip to content

CLOUDP-299865: Correctly Generate Connection Secrets for Flex Clusters #2119

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

Merged
merged 2 commits into from
Feb 12, 2025
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
13 changes: 13 additions & 0 deletions internal/translation/deployment/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,19 @@ func fillServerlessPrivateEndpoints(cpeList []admin.ServerlessConnectionStringsP
return pes
}

func flexToConnections(flex []adminv20241113001.FlexClusterDescription20241113) []Connection {
conns := []Connection{}
for _, f := range flex {
conns = append(conns, Connection{
Name: f.GetName(),
ConnURL: f.ConnectionStrings.GetStandard(),
SrvConnURL: f.ConnectionStrings.GetStandardSrv(),
Serverless: false,
})
}
return conns
}

func connectionSet(conns ...[]Connection) []Connection {
return set(func(conn Connection) string { return conn.Name }, conns...)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/translation/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,19 @@ func (ds *ProductionAtlasDeployments) ListDeploymentConnections(ctx context.Cont
return clusterConns, nil
}

flex, _, flexErr := ds.flexAPI.ListFlexClusters(ctx, projectID).Execute()
if flexErr != nil {
return nil, fmt.Errorf("failed to list flex clusters for project %s: %w", projectID, err)
}
flexConns := flexToConnections(flex.GetResults())

serverless, _, serverlessErr := ds.serverlessAPI.ListServerlessInstances(ctx, projectID).Execute()
if serverlessErr != nil {
return nil, fmt.Errorf("failed to list serverless deployments for project %s: %w", projectID, err)
}
serverlessConns := serverlessToConnections(serverless.GetResults())

return connectionSet(clusterConns, serverlessConns), nil
return connectionSet(clusterConns, serverlessConns, flexConns), nil
}

func (ds *ProductionAtlasDeployments) ClusterExists(ctx context.Context, projectID, name string) (bool, error) {
Expand Down
70 changes: 68 additions & 2 deletions internal/translation/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestProductionAtlasDeployments_ListDeploymentConnections(t *testing.T) {
t.Run("Shouldn't call the serverless api if running in Gov", func(t *testing.T) {
t.Run("Shouldn't call the serverless or flex api if running in Gov", func(t *testing.T) {
mockClustersAPI := mockadmin.NewClustersApi(t)
mockClustersAPI.EXPECT().ListClusters(context.Background(), mock.Anything).Return(
admin.ListClustersApiRequest{ApiService: mockClustersAPI})
Expand All @@ -34,17 +34,20 @@ func TestProductionAtlasDeployments_ListDeploymentConnections(t *testing.T) {

mockServerlessAPI := mockadmin.NewServerlessInstancesApi(t)
mockServerlessAPI.EXPECT().ListServerlessInstancesExecute(mock.Anything).Unset()
mockFlexAPI := mockadminv20241113001.NewFlexClustersApi(t)
mockFlexAPI.EXPECT().ListFlexClustersExecute(mock.Anything).Unset()
ds := &ProductionAtlasDeployments{
clustersAPI: mockClustersAPI,
serverlessAPI: mockServerlessAPI,
flexAPI: mockFlexAPI,
isGov: true,
}
projectID := "testProjectID"
_, err := ds.ListDeploymentConnections(context.Background(), projectID)
assert.Nil(t, err)
})

t.Run("Should call the serverless api if not running in Gov", func(t *testing.T) {
t.Run("Should call the serverless and flex apis if not running in Gov", func(t *testing.T) {
mockClustersAPI := mockadmin.NewClustersApi(t)
mockClustersAPI.EXPECT().ListClusters(context.Background(), mock.Anything).Return(
admin.ListClustersApiRequest{ApiService: mockClustersAPI})
Expand All @@ -57,15 +60,78 @@ func TestProductionAtlasDeployments_ListDeploymentConnections(t *testing.T) {
mockServerlessAPI.EXPECT().ListServerlessInstancesExecute(
admin.ListServerlessInstancesApiRequest{ApiService: mockServerlessAPI}).Return(
nil, &http.Response{StatusCode: http.StatusOK}, nil)

mockFlexAPI := mockadminv20241113001.NewFlexClustersApi(t)
mockFlexAPI.EXPECT().ListFlexClusters(context.Background(), mock.Anything).Return(
adminv20241113001.ListFlexClustersApiRequest{ApiService: mockFlexAPI})
mockFlexAPI.EXPECT().ListFlexClustersExecute(
adminv20241113001.ListFlexClustersApiRequest{ApiService: mockFlexAPI}).Return(
nil, &http.Response{StatusCode: http.StatusOK}, nil)

ds := &ProductionAtlasDeployments{
clustersAPI: mockClustersAPI,
serverlessAPI: mockServerlessAPI,
flexAPI: mockFlexAPI,
isGov: false,
}
projectID := "testProjectID"
_, err := ds.ListDeploymentConnections(context.Background(), projectID)
assert.Nil(t, err)
})

t.Run("Should create connection for each cluster type", func(t *testing.T) {
mockClustersAPI := mockadmin.NewClustersApi(t)
mockClustersAPI.EXPECT().ListClusters(context.Background(), mock.Anything).Return(
admin.ListClustersApiRequest{ApiService: mockClustersAPI})
mockClustersAPI.EXPECT().ListClustersExecute(admin.ListClustersApiRequest{ApiService: mockClustersAPI}).Return(
&admin.PaginatedAdvancedClusterDescription{
Results: &[]admin.AdvancedClusterDescription{
{
Name: pointer.MakePtr("testCluster"),
ConnectionStrings: &admin.ClusterConnectionStrings{StandardSrv: pointer.MakePtr("clusterSRV")},
},
},
}, &http.Response{StatusCode: http.StatusOK}, nil)

mockServerlessAPI := mockadmin.NewServerlessInstancesApi(t)
mockServerlessAPI.EXPECT().ListServerlessInstances(context.Background(), mock.Anything).Return(
admin.ListServerlessInstancesApiRequest{ApiService: mockServerlessAPI})
mockServerlessAPI.EXPECT().ListServerlessInstancesExecute(
admin.ListServerlessInstancesApiRequest{ApiService: mockServerlessAPI}).Return(
&admin.PaginatedServerlessInstanceDescription{
Results: &[]admin.ServerlessInstanceDescription{
{
Name: pointer.MakePtr("testServerless"),
ConnectionStrings: &admin.ServerlessInstanceDescriptionConnectionStrings{StandardSrv: pointer.MakePtr("serverlessSRV")},
},
},
}, &http.Response{StatusCode: http.StatusOK}, nil)

mockFlexAPI := mockadminv20241113001.NewFlexClustersApi(t)
mockFlexAPI.EXPECT().ListFlexClusters(context.Background(), mock.Anything).Return(
adminv20241113001.ListFlexClustersApiRequest{ApiService: mockFlexAPI})
mockFlexAPI.EXPECT().ListFlexClustersExecute(
adminv20241113001.ListFlexClustersApiRequest{ApiService: mockFlexAPI}).Return(
&adminv20241113001.PaginatedFlexClusters20241113{
Results: &[]adminv20241113001.FlexClusterDescription20241113{
{
Name: pointer.MakePtr("testFlex"),
ConnectionStrings: &adminv20241113001.FlexConnectionStrings20241113{StandardSrv: pointer.MakePtr("flexSRV")},
},
},
}, &http.Response{StatusCode: http.StatusOK}, nil)

ds := &ProductionAtlasDeployments{
clustersAPI: mockClustersAPI,
serverlessAPI: mockServerlessAPI,
flexAPI: mockFlexAPI,
isGov: false,
}
projectID := "testProjectID"
conns, err := ds.ListDeploymentConnections(context.Background(), projectID)
assert.Nil(t, err)
assert.Equal(t, len(conns), 3)
})
}

func TestClusterExists(t *testing.T) {
Expand Down
Loading