Skip to content

Commit d4456e1

Browse files
authored
Add instance type and replicas to MongoDB Flex instance describe pret… (#40)
* Add instance type and replicas to MongoDB Flex instance describe pretty output * Do not show instance type when it is not known
1 parent 39cba64 commit d4456e1

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

internal/cmd/mongodbflex/instance/create/create.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,8 @@ func buildRequest(ctx context.Context, service string, model *inputModel, apiCli
238238
return req, err
239239
}
240240

241-
// The number of replicas is enforced by the API according to the instance type
242-
var replicas int64
243-
if *model.Type == "Single" {
244-
replicas = 1
245-
} else if *model.Type == "Replica" {
246-
replicas = 3
247-
} else if *model.Type == "Sharded" {
248-
replicas = 9
249-
} else {
241+
replicas, err := mongodbflexUtils.GetInstanceReplicas(*model.Type)
242+
if err != nil {
250243
return req, fmt.Errorf("invalid MongoDB Flex intance type: %w", err)
251244
}
252245

internal/cmd/mongodbflex/instance/describe/describe.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"stackit/internal/pkg/examples"
1212
"stackit/internal/pkg/globalflags"
1313
"stackit/internal/pkg/services/mongodbflex/client"
14+
mongodbflexUtils "stackit/internal/pkg/services/mongodbflex/utils"
1415
"stackit/internal/pkg/tables"
1516
"stackit/internal/pkg/utils"
1617

@@ -91,6 +92,12 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex
9192
acls := *instance.Acl.Items
9293
strings.Join(acls, ",")
9394

95+
instanceType, err := mongodbflexUtils.GetInstanceType(*instance.Replicas)
96+
if err != nil {
97+
// Should never happen
98+
instanceType = ""
99+
}
100+
94101
table := tables.NewTable()
95102
table.AddRow("ID", *instance.Id)
96103
table.AddSeparator()
@@ -106,11 +113,15 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex
106113
table.AddSeparator()
107114
table.AddRow("FLAVOR DESCRIPTION", *instance.Flavor.Description)
108115
table.AddSeparator()
116+
table.AddRow("TYPE", instanceType)
117+
table.AddSeparator()
118+
table.AddRow("REPLICAS", *instance.Replicas)
119+
table.AddSeparator()
109120
table.AddRow("CPU", *instance.Flavor.Cpu)
110121
table.AddSeparator()
111122
table.AddRow("RAM", *instance.Flavor.Memory)
112123
table.AddSeparator()
113-
err := table.Display(cmd)
124+
err = table.Display(cmd)
114125
if err != nil {
115126
return fmt.Errorf("render table: %w", err)
116127
}

internal/pkg/services/mongodbflex/utils/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
1010
)
1111

12+
// The number of replicas is enforced by the API according to the instance type
13+
var instanceTypeToReplicas = map[string]int64{
14+
"Single": 1,
15+
"Replica": 3,
16+
"Sharded": 9,
17+
}
18+
1219
func ValidateFlavorId(service, flavorId string, flavors *[]mongodbflex.HandlersInfraFlavor) error {
1320
for _, f := range *flavors {
1421
if f.Id != nil && strings.EqualFold(*f.Id, flavorId) {
@@ -82,3 +89,20 @@ func GetUserName(ctx context.Context, apiClient MongoDBFlexClient, projectId, in
8289
}
8390
return *resp.Item.Username, nil
8491
}
92+
93+
func GetInstanceReplicas(instanceType string) (int64, error) {
94+
numReplicas, ok := instanceTypeToReplicas[instanceType]
95+
if !ok {
96+
return 0, fmt.Errorf("invalid instance type: %v", instanceType)
97+
}
98+
return numReplicas, nil
99+
}
100+
101+
func GetInstanceType(numReplicas int64) (string, error) {
102+
for k, v := range instanceTypeToReplicas {
103+
if v == numReplicas {
104+
return k, nil
105+
}
106+
}
107+
return "", fmt.Errorf("invalid number of replicas: %v", numReplicas)
108+
}

0 commit comments

Comments
 (0)