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
11 changes: 2 additions & 9 deletions internal/cmd/mongodbflex/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,8 @@ func buildRequest(ctx context.Context, service string, model *inputModel, apiCli
return req, err
}

// The number of replicas is enforced by the API according to the instance type
var replicas int64
if *model.Type == "Single" {
replicas = 1
} else if *model.Type == "Replica" {
replicas = 3
} else if *model.Type == "Sharded" {
replicas = 9
} else {
replicas, err := mongodbflexUtils.GetInstanceReplicas(*model.Type)
if err != nil {
return req, fmt.Errorf("invalid MongoDB Flex intance type: %w", err)
}

Expand Down
13 changes: 12 additions & 1 deletion internal/cmd/mongodbflex/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"stackit/internal/pkg/examples"
"stackit/internal/pkg/globalflags"
"stackit/internal/pkg/services/mongodbflex/client"
mongodbflexUtils "stackit/internal/pkg/services/mongodbflex/utils"
"stackit/internal/pkg/tables"
"stackit/internal/pkg/utils"

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

instanceType, err := mongodbflexUtils.GetInstanceType(*instance.Replicas)
if err != nil {
// Should never happen
instanceType = ""
}

table := tables.NewTable()
table.AddRow("ID", *instance.Id)
table.AddSeparator()
Expand All @@ -106,11 +113,15 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *mongodbflex
table.AddSeparator()
table.AddRow("FLAVOR DESCRIPTION", *instance.Flavor.Description)
table.AddSeparator()
table.AddRow("TYPE", instanceType)
table.AddSeparator()
table.AddRow("REPLICAS", *instance.Replicas)
table.AddSeparator()
table.AddRow("CPU", *instance.Flavor.Cpu)
table.AddSeparator()
table.AddRow("RAM", *instance.Flavor.Memory)
table.AddSeparator()
err := table.Display(cmd)
err = table.Display(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}
Expand Down
24 changes: 24 additions & 0 deletions internal/pkg/services/mongodbflex/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
)

// The number of replicas is enforced by the API according to the instance type
var instanceTypeToReplicas = map[string]int64{
"Single": 1,
"Replica": 3,
"Sharded": 9,
}

func ValidateFlavorId(service, flavorId string, flavors *[]mongodbflex.HandlersInfraFlavor) error {
for _, f := range *flavors {
if f.Id != nil && strings.EqualFold(*f.Id, flavorId) {
Expand Down Expand Up @@ -82,3 +89,20 @@ func GetUserName(ctx context.Context, apiClient MongoDBFlexClient, projectId, in
}
return *resp.Item.Username, nil
}

func GetInstanceReplicas(instanceType string) (int64, error) {
numReplicas, ok := instanceTypeToReplicas[instanceType]
if !ok {
return 0, fmt.Errorf("invalid instance type: %v", instanceType)
}
return numReplicas, nil
}

func GetInstanceType(numReplicas int64) (string, error) {
for k, v := range instanceTypeToReplicas {
if v == numReplicas {
return k, nil
}
}
return "", fmt.Errorf("invalid number of replicas: %v", numReplicas)
}