66 "strings"
77
88 "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
9+ "golang.org/x/mod/semver"
910
1011 "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
1112)
@@ -17,14 +18,6 @@ var instanceTypeToReplicas = map[string]int64{
1718 "Sharded" : 9 ,
1819}
1920
20- func GetUserName (ctx context.Context , apiClient MongoDBFlexClient , projectId , instanceId , userId string ) (string , error ) {
21- resp , err := apiClient .GetUserExecute (ctx , projectId , instanceId , userId )
22- if err != nil {
23- return "" , fmt .Errorf ("get MongoDBFlex user: %w" , err )
24- }
25- return * resp .Item .Username , nil
26- }
27-
2821func AvailableInstanceTypes () []string {
2922 instanceTypes := make ([]string , len (instanceTypeToReplicas ))
3023 i := 0
@@ -118,14 +111,45 @@ func LoadFlavorId(cpu, ram int64, flavors *[]mongodbflex.HandlersInfraFlavor) (*
118111}
119112
120113type MongoDBFlexClient interface {
114+ ListVersionsExecute (ctx context.Context , projectId string ) (* mongodbflex.ListVersionsResponse , error )
121115 GetInstanceExecute (ctx context.Context , projectId , instanceId string ) (* mongodbflex.GetInstanceResponse , error )
122116 GetUserExecute (ctx context.Context , projectId , instanceId , userId string ) (* mongodbflex.GetUserResponse , error )
123117}
124118
119+ func GetLatestMongoDBVersion (ctx context.Context , apiClient MongoDBFlexClient , projectId string ) (string , error ) {
120+ resp , err := apiClient .ListVersionsExecute (ctx , projectId )
121+ if err != nil {
122+ return "" , fmt .Errorf ("get MongoDB versions: %w" , err )
123+ }
124+ versions := * resp .Versions
125+
126+ latestVersion := "0"
127+ for i := range versions {
128+ oldSemVer := fmt .Sprintf ("v%s" , latestVersion )
129+ newSemVer := fmt .Sprintf ("v%s" , versions [i ])
130+ if semver .Compare (newSemVer , oldSemVer ) != 1 {
131+ continue
132+ }
133+ latestVersion = versions [i ]
134+ }
135+ if latestVersion == "0" {
136+ return "" , fmt .Errorf ("no MongoDB versions found" )
137+ }
138+ return latestVersion , nil
139+ }
140+
125141func GetInstanceName (ctx context.Context , apiClient MongoDBFlexClient , projectId , instanceId string ) (string , error ) {
126142 resp , err := apiClient .GetInstanceExecute (ctx , projectId , instanceId )
127143 if err != nil {
128144 return "" , fmt .Errorf ("get MongoDBFlex instance: %w" , err )
129145 }
130146 return * resp .Item .Name , nil
131147}
148+
149+ func GetUserName (ctx context.Context , apiClient MongoDBFlexClient , projectId , instanceId , userId string ) (string , error ) {
150+ resp , err := apiClient .GetUserExecute (ctx , projectId , instanceId , userId )
151+ if err != nil {
152+ return "" , fmt .Errorf ("get MongoDBFlex user: %w" , err )
153+ }
154+ return * resp .Item .Username , nil
155+ }
0 commit comments