From 2b5303825d72454bb3934d324eae0ebce3ade8a3 Mon Sep 17 00:00:00 2001 From: Bianca Lisle <40155621+blva@users.noreply.github.com> Date: Fri, 7 Feb 2025 09:53:08 +0000 Subject: [PATCH] CLOUDP-297221: Add preview support to split command (#410) --- tools/cli/internal/apiversion/version.go | 69 ++++++++++++++----- tools/cli/internal/apiversion/version_test.go | 30 ++++++++ tools/cli/internal/cli/versions/versions.go | 4 +- 3 files changed, 83 insertions(+), 20 deletions(-) diff --git a/tools/cli/internal/apiversion/version.go b/tools/cli/internal/apiversion/version.go index 1450b318c4..72fe4b8a99 100644 --- a/tools/cli/internal/apiversion/version.go +++ b/tools/cli/internal/apiversion/version.go @@ -18,6 +18,7 @@ import ( "fmt" "log" "regexp" + "strings" "time" "github.com/getkin/kin-openapi/openapi3" @@ -31,8 +32,8 @@ type APIVersion struct { const ( dateFormat = "2006-01-02" - StableStabilityLevel = "STABLE" - PreviewStabilityLevel = "PREVIEW" + StableStabilityLevel = "stable" + PreviewStabilityLevel = "preview" ) var contentPattern = regexp.MustCompile(`application/vnd\.atlas\.((\d{4})-(\d{2})-(\d{2})|preview)\+(.+)`) @@ -51,6 +52,17 @@ func New(opts ...Option) (*APIVersion, error) { return version, nil } +func (v *APIVersion) newVersion(version string, date time.Time) { + v.version = version + v.stabilityVersion = StableStabilityLevel + v.versionDate = date + + if IsPreviewSabilityLevel(version) { + v.versionDate = time.Now().AddDate(10, 0, 0) // set preview date to the future + v.stabilityVersion = PreviewStabilityLevel + } +} + // WithVersion sets the version on the APIVersion. func WithVersion(version string) Option { return func(v *APIVersion) error { @@ -59,8 +71,7 @@ func WithVersion(version string) Option { return err } - v.version = version - v.versionDate = versionDate + v.newVersion(version, versionDate) return nil } } @@ -68,9 +79,7 @@ func WithVersion(version string) Option { // WithDate sets the version on the APIVersion. func WithDate(date time.Time) Option { return func(v *APIVersion) error { - v.version = date.Format(dateFormat) - v.versionDate = date - v.stabilityVersion = StableStabilityLevel + v.newVersion(date.Format(dateFormat), date) return nil } } @@ -83,22 +92,20 @@ func WithContent(contentType string) Option { return err } - v.version = version - v.stabilityVersion = StableStabilityLevel - if version == PreviewStabilityLevel { - v.stabilityVersion = PreviewStabilityLevel - return nil - } - - v.versionDate, err = DateFromVersion(version) + versionDate, err := DateFromVersion(version) if err != nil { return err } + + v.newVersion(version, versionDate) return nil } } func DateFromVersion(version string) (time.Time, error) { + if IsPreviewSabilityLevel(version) { + return time.Now(), nil + } return time.Parse(dateFormat, version) } @@ -130,6 +137,26 @@ func (v *APIVersion) Date() time.Time { return v.versionDate } +func (v *APIVersion) StabilityLevel() string { + return v.stabilityVersion +} + +func (v *APIVersion) ExactMatchOnly() bool { + return v.IsPreview() +} + +func (v *APIVersion) IsPreview() bool { + return IsPreviewSabilityLevel(v.version) +} + +func IsPreviewSabilityLevel(value string) bool { + return strings.EqualFold(value, PreviewStabilityLevel) +} + +func IsStableSabilityLevel(value string) bool { + return strings.EqualFold(value, StableStabilityLevel) +} + func FindMatchesFromContentType(contentType string) []string { return contentPattern.FindStringSubmatch(contentType) } @@ -160,6 +187,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A op response: "200": content: application/vnd.atlas.2023-01-01+json + content: application/vnd.atlas.preview+json "201": content: application/vnd.atlas.2023-12-01+json content: application/vnd.atlas.2025-01-01+json @@ -181,14 +209,19 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A log.Printf("Ignoring invalid content type: %q", contentType) continue } - if contentVersion.GreaterThan(requestedVersion) { - continue - } if contentVersion.Equal(requestedVersion) { return contentVersion } + if contentVersion.ExactMatchOnly() || requestedVersion.ExactMatchOnly() { + continue + } + + if contentVersion.GreaterThan(requestedVersion) { + continue + } + if latestVersionMatch == nil || contentVersion.GreaterThan(latestVersionMatch) { latestVersionMatch = contentVersion } diff --git a/tools/cli/internal/apiversion/version_test.go b/tools/cli/internal/apiversion/version_test.go index fbde00ece7..d518949dc6 100644 --- a/tools/cli/internal/apiversion/version_test.go +++ b/tools/cli/internal/apiversion/version_test.go @@ -48,6 +48,24 @@ func TestParseVersion(t *testing.T) { expectedMatch: "2030-02-20", wantErr: false, }, + { + name: "preview_json", + contentType: "application/vnd.atlas.preview+json", + expectedMatch: "preview", + wantErr: false, + }, + { + name: "preview_yaml", + contentType: "application/vnd.atlas.preview+yaml", + expectedMatch: "preview", + wantErr: false, + }, + { + name: "preview_csv", + contentType: "application/vnd.atlas.preview+csv", + expectedMatch: "preview", + wantErr: false, + }, { name: "invalid", contentType: "application/vnd.test.2023-01-01", @@ -94,6 +112,12 @@ func TestNewAPIVersionFromContentType(t *testing.T) { expectedMatch: "2030-02-20", wantErr: false, }, + { + name: "preview", + contentType: "application/vnd.atlas.preview+json", + expectedMatch: "preview", + wantErr: false, + }, { name: "invalid", contentType: "application/vnd.test.2023-01-01", @@ -416,6 +440,11 @@ func TestFindLatestContentVersionMatched(t *testing.T) { targetVersion: "2023-01-01", expectedMatch: "2023-01-01", }, + { + name: "exact match preview", + targetVersion: "preview", + expectedMatch: "preview", + }, { name: "exact match 2023-11-15", targetVersion: "2023-11-15", @@ -470,6 +499,7 @@ func oasOperationAllVersions() *openapi3.Operation { responses.Set("200", &openapi3.ResponseRef{ Value: &openapi3.Response{ Content: map[string]*openapi3.MediaType{ + "application/vnd.atlas.preview+json": {}, "application/vnd.atlas.2023-01-01+json": {}, "application/vnd.atlas.2023-01-01+csv": {}, "application/vnd.atlas.2023-02-01+json": {}, diff --git a/tools/cli/internal/cli/versions/versions.go b/tools/cli/internal/cli/versions/versions.go index 311e16468f..c09b3235e2 100644 --- a/tools/cli/internal/cli/versions/versions.go +++ b/tools/cli/internal/cli/versions/versions.go @@ -76,11 +76,11 @@ func (o *Opts) filterStabilityLevelVersions(apiVersions []string) []string { var out []string for _, v := range apiVersions { - if o.stabilityLevel == apiversion.PreviewStabilityLevel && strings.Contains(v, "preview") { + if (apiversion.IsStableSabilityLevel(o.stabilityLevel)) && !apiversion.IsPreviewSabilityLevel(v) { out = append(out, v) } - if o.stabilityLevel == apiversion.StableStabilityLevel && !strings.Contains(v, "preview") { + if (apiversion.IsPreviewSabilityLevel(o.stabilityLevel)) && apiversion.IsPreviewSabilityLevel(v) { out = append(out, v) } }