Skip to content

Commit c605578

Browse files
committed
add redis version info
1 parent d76747f commit c605578

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

cmd/ctrlc/root/sync/aws/rds/rds.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func getNormalizedDBType(engine string) string {
269269
return engineLower
270270
}
271271
}
272+
272273
// parseEngineVersion parses the RDS engine version into semver components
273274
func parseEngineVersion(engineVersion string) (major, minor, patch string, prerelease string) {
274275
// Handle special case for Aurora MySQL which has format like "8.0.mysql_aurora.3.07.1"
@@ -278,28 +279,28 @@ func parseEngineVersion(engineVersion string) (major, minor, patch string, prere
278279
// For Aurora, use the MySQL/PostgreSQL compatibility version as the primary version
279280
major = parts[0]
280281
minor = parts[1]
281-
282+
282283
// Set Aurora version as prerelease to preserve it but prioritize DB version
283284
if len(parts) >= 4 {
284285
auroraVersion := strings.Join(parts[3:], ".")
285286
prerelease = "aurora_" + auroraVersion
286287
}
287-
288+
288289
// Default patch to 0 if not available
289290
patch = "0"
290-
291+
291292
return
292293
}
293294
}
294-
295+
295296
// For standard version strings, try to parse with semver library
296297
cleanVersion := engineVersion
297298
// Remove any non-semver compatible parts
298299
if idx := strings.Index(cleanVersion, "-"); idx > 0 {
299300
prerelease = cleanVersion[idx+1:]
300301
cleanVersion = cleanVersion[:idx]
301302
}
302-
303+
303304
// Try to parse with semver
304305
v, err := semver.NewVersion(cleanVersion)
305306
if err == nil {
@@ -311,32 +312,31 @@ func parseEngineVersion(engineVersion string) (major, minor, patch string, prere
311312
}
312313
return
313314
}
314-
315+
315316
// Fallback to manual parsing if semver fails
316317
versionParts := strings.Split(engineVersion, ".")
317-
318+
318319
if len(versionParts) >= 1 {
319320
major = versionParts[0]
320321
}
321-
322+
322323
if len(versionParts) >= 2 {
323324
minor = versionParts[1]
324325
}
325-
326+
326327
if len(versionParts) >= 3 {
327328
// Check if there's a prerelease part (e.g., "28-R1")
328329
patchParts := strings.Split(versionParts[2], "-")
329330
patch = patchParts[0]
330-
331+
331332
if len(patchParts) > 1 && prerelease == "" {
332333
prerelease = strings.Join(patchParts[1:], "-")
333334
}
334335
}
335-
336+
336337
return
337338
}
338339

339-
340340
// buildInstanceMetadata builds the metadata map for an RDS instance
341341
func buildInstanceMetadata(instance *types.DBInstance, region, host string, port int, consoleUrl string) map[string]string {
342342
// Get normalized database type

cmd/ctrlc/root/sync/google/redis/redis.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
149182
func 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

Comments
 (0)