@@ -7,8 +7,10 @@ import (
77 "strings"
88
99 "github.com/MakeNowJust/heredoc/v2"
10+ "github.com/Masterminds/semver"
1011 "github.com/charmbracelet/log"
1112 "github.com/ctrlplanedev/cli/internal/api"
13+ "github.com/ctrlplanedev/cli/internal/kinds"
1214 "github.com/spf13/cobra"
1315 "github.com/spf13/viper"
1416 "google.golang.org/api/redis/v1"
@@ -145,6 +147,37 @@ func processInstance(_ context.Context, instance *redis.Instance, project string
145147 }, nil
146148}
147149
150+ // parseRedisVersion extracts the semantic version from the Redis version string
151+ func parseRedisVersion (redisVersion string ) * semver.Version {
152+ // Default version
153+ defaultVersion , _ := semver .NewVersion ("0.0.0" )
154+
155+ // Try to parse other formats
156+ if strings .HasPrefix (redisVersion , "REDIS_" ) {
157+ parts := strings .Split (strings .TrimPrefix (redisVersion , "REDIS_" ), "_" )
158+ if len (parts ) >= 1 {
159+ versionStr := parts [0 ]
160+ if len (parts ) >= 2 {
161+ versionStr += "." + parts [1 ]
162+ } else {
163+ versionStr += ".0" // Add minor version for semver compatibility
164+ }
165+ if len (parts ) >= 3 {
166+ versionStr += "." + parts [2 ]
167+ } else {
168+ versionStr += ".0" // Add patch version for semver compatibility
169+ }
170+
171+ version , err := semver .NewVersion (versionStr )
172+ if err == nil {
173+ return version
174+ }
175+ }
176+ }
177+
178+ return defaultVersion
179+ }
180+
148181// initInstanceMetadata initializes the base metadata for an instance
149182func initInstanceMetadata (instance * redis.Instance , project string ) map [string ]string {
150183 // Extract location from name
@@ -161,21 +194,26 @@ func initInstanceMetadata(instance *redis.Instance, project string) map[string]s
161194 consoleUrl := fmt .Sprintf ("https://console.cloud.google.com/memorystore/redis/locations/%s/instances/%s/details?project=%s" ,
162195 location , instanceName , project )
163196
197+ version := parseRedisVersion (instance .RedisVersion )
164198 metadata := map [string ]string {
165- "database/type" : "redis" ,
166- "database/host" : instance .Host ,
167- "database/port" : strconv .FormatInt (instance .Port , 10 ),
168- "database/version" : instance .RedisVersion ,
169- "database/region" : location ,
170- "database/tier" : instance .Tier ,
171- "database/state" : strings .ToLower (instance .State ),
172- "database/memory-size-gb" : strconv .FormatInt (instance .MemorySizeGb , 10 ),
199+ "database/type" : "redis" ,
200+ "database/host" : instance .Host ,
201+ "database/port" : strconv .FormatInt (instance .Port , 10 ),
202+ kinds .DBMetadataVersion : version .String (),
203+ kinds .DBMetadataVersionMajor : strconv .FormatUint (uint64 (version .Major ()), 10 ),
204+ kinds .DBMetadataVersionMinor : strconv .FormatUint (uint64 (version .Minor ()), 10 ),
205+ kinds .DBMetadataVersionPatch : strconv .FormatUint (uint64 (version .Patch ()), 10 ),
206+ kinds .DBMetadataVersionPrerelease : version .Prerelease (),
207+ "database/region" : location ,
208+ "database/tier" : instance .Tier ,
209+ "database/state" : strings .ToLower (instance .State ),
210+ "database/memory-size-gb" : strconv .FormatInt (instance .MemorySizeGb , 10 ),
173211
174212 "google/project" : project ,
175213 "google/instance-type" : "redis" ,
176214 "google/location" : location ,
177215 "google/state" : strings .ToLower (instance .State ),
178- "google/version" : instance . RedisVersion ,
216+ "google/version" : version . String () ,
179217 "google/tier" : instance .Tier ,
180218 "google/memory-size-gb" : strconv .FormatInt (instance .MemorySizeGb , 10 ),
181219 "google/console-url" : consoleUrl ,
0 commit comments