Skip to content
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
44 changes: 17 additions & 27 deletions internal/command/mpg/mpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mp
mpgv1Client := mpgv1.ClientFromContext(ctx)
mpgv2Client := mpgv2.ClientFromContext(ctx)

// If user told us which cluster they want
if clusterID != "" {
if c, err := mpgv1Client.GetManagedClusterById(ctx, clusterID); err == nil {
// Check if its a V2 cluster. Otherwise, fallback to V1.
if c, err := mpgv2Client.GetClusterById(ctx, clusterID); err == nil && c.Data.MpgdClusterId != "" {
cluster := &mpg.Cluster{
Id: c.Data.Id,
Name: c.Data.Name,
Expand All @@ -68,12 +70,13 @@ func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mp
Organization: c.Data.Organization,
IpAssignments: c.Data.IpAssignments,
AttachedApps: c.Data.AttachedApps,
Version: mpg.VersionV1,
Version: mpg.VersionV2,
}

return cluster, cluster.Organization.Slug, nil
}
if c, err := mpgv2Client.GetClusterById(ctx, clusterID); err == nil {

if c, err := mpgv1Client.GetManagedClusterById(ctx, clusterID); err == nil {
cluster := &mpg.Cluster{
Id: c.Data.Id,
Name: c.Data.Name,
Expand All @@ -85,13 +88,18 @@ func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mp
Organization: c.Data.Organization,
IpAssignments: c.Data.IpAssignments,
AttachedApps: c.Data.AttachedApps,
Version: mpg.VersionV2,
Version: mpg.VersionV1,
}

return cluster, cluster.Organization.Slug, nil
}

// There's nothing that List can tell us that Get won't, so if we didn't
// find the cluster, let's just exit early.
return nil, orgSlug, fmt.Errorf("managed postgres cluster %q not found", clusterID)
}

// Prompt for org if empty
if orgSlug == "" {
org, err := prompt.Org(ctx)
if err != nil {
Expand All @@ -113,18 +121,16 @@ func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mp
}

clusters := make([]*mpg.Cluster, 0, len(mc.Data))
options := make([]string, 0, len(mc.Data))

for _, cluster := range mc.Data {
version := mpg.VersionV1
if cluster.Version == 2 {
version = mpg.VersionV2
}
clusterId := ""
if cluster.Version == 2 {
clusterId = cluster.ClusterId
}

clusters = append(clusters, &mpg.Cluster{
Id: cluster.Id,
ClusterId: clusterId,
Name: cluster.Name,
Region: cluster.Region,
Status: cluster.Status,
Expand All @@ -134,29 +140,13 @@ func ClusterFromArgOrSelect(ctx context.Context, clusterID, orgSlug string) (*mp
Organization: cluster.Organization,
Version: version,
})
}

// If a cluster ID is provided via flag, find it
if clusterID != "" {
for _, cluster := range clusters {
if cluster.Id == clusterID {
return cluster, orgSlug, nil
}
}

return nil, orgSlug, fmt.Errorf("managed postgres cluster %q not found in organization %s", clusterID, orgSlug)
}

// Otherwise, prompt the user to select a cluster
var options []string
for _, cluster := range clusters {
options = append(options, fmt.Sprintf("%s [%s] (%s)", cluster.Name, cluster.Id, cluster.Region))
}

var index int
selectErr := prompt.Select(ctx, &index, "Select a Postgres cluster", "", options...)
if selectErr != nil {
return nil, orgSlug, selectErr
if err := prompt.Select(ctx, &index, "Select a Postgres cluster", "", options...); err != nil {
return nil, orgSlug, err
}

return clusters[index], orgSlug, nil
Expand Down
11 changes: 4 additions & 7 deletions internal/command/mpg/mpg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,11 @@ func TestClusterFromFlagOrSelect_WithFlagContext(t *testing.T) {
})

mockv1 = &mock.MpgV1Client{
ListManagedClustersFunc: func(ctx context.Context, orgSlug string, deleted bool) (mpgv1.ListManagedClustersResponse, error) {
assert.Equal(t, "test-org", orgSlug)

return mpgv1.ListManagedClustersResponse{
Data: []mpgv1.ManagedCluster{expectedCluster},
}, nil
},
GetManagedClusterByIdFunc: func(ctx context.Context, id string) (mpgv1.GetManagedClusterResponse, error) {
if id == expectedCluster.Id {
return mpgv1.GetManagedClusterResponse{Data: expectedCluster}, nil
}

return mpgv1.GetManagedClusterResponse{}, errors.New("managed postgres cluster not found")
},
}
Expand Down
1 change: 0 additions & 1 deletion internal/uiex/mpg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
// Unified type for v1 and v2 MPG clusters
type Cluster struct {
Id string
ClusterId string
Name string
Region string
Status string
Expand Down
Loading