Skip to content

Commit 2b53038

Browse files
authored
CLOUDP-297221: Add preview support to split command (#410)
1 parent 32f1658 commit 2b53038

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

tools/cli/internal/apiversion/version.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"regexp"
21+
"strings"
2122
"time"
2223

2324
"github.com/getkin/kin-openapi/openapi3"
@@ -31,8 +32,8 @@ type APIVersion struct {
3132

3233
const (
3334
dateFormat = "2006-01-02"
34-
StableStabilityLevel = "STABLE"
35-
PreviewStabilityLevel = "PREVIEW"
35+
StableStabilityLevel = "stable"
36+
PreviewStabilityLevel = "preview"
3637
)
3738

3839
var contentPattern = regexp.MustCompile(`application/vnd\.atlas\.((\d{4})-(\d{2})-(\d{2})|preview)\+(.+)`)
@@ -51,6 +52,17 @@ func New(opts ...Option) (*APIVersion, error) {
5152
return version, nil
5253
}
5354

55+
func (v *APIVersion) newVersion(version string, date time.Time) {
56+
v.version = version
57+
v.stabilityVersion = StableStabilityLevel
58+
v.versionDate = date
59+
60+
if IsPreviewSabilityLevel(version) {
61+
v.versionDate = time.Now().AddDate(10, 0, 0) // set preview date to the future
62+
v.stabilityVersion = PreviewStabilityLevel
63+
}
64+
}
65+
5466
// WithVersion sets the version on the APIVersion.
5567
func WithVersion(version string) Option {
5668
return func(v *APIVersion) error {
@@ -59,18 +71,15 @@ func WithVersion(version string) Option {
5971
return err
6072
}
6173

62-
v.version = version
63-
v.versionDate = versionDate
74+
v.newVersion(version, versionDate)
6475
return nil
6576
}
6677
}
6778

6879
// WithDate sets the version on the APIVersion.
6980
func WithDate(date time.Time) Option {
7081
return func(v *APIVersion) error {
71-
v.version = date.Format(dateFormat)
72-
v.versionDate = date
73-
v.stabilityVersion = StableStabilityLevel
82+
v.newVersion(date.Format(dateFormat), date)
7483
return nil
7584
}
7685
}
@@ -83,22 +92,20 @@ func WithContent(contentType string) Option {
8392
return err
8493
}
8594

86-
v.version = version
87-
v.stabilityVersion = StableStabilityLevel
88-
if version == PreviewStabilityLevel {
89-
v.stabilityVersion = PreviewStabilityLevel
90-
return nil
91-
}
92-
93-
v.versionDate, err = DateFromVersion(version)
95+
versionDate, err := DateFromVersion(version)
9496
if err != nil {
9597
return err
9698
}
99+
100+
v.newVersion(version, versionDate)
97101
return nil
98102
}
99103
}
100104

101105
func DateFromVersion(version string) (time.Time, error) {
106+
if IsPreviewSabilityLevel(version) {
107+
return time.Now(), nil
108+
}
102109
return time.Parse(dateFormat, version)
103110
}
104111

@@ -130,6 +137,26 @@ func (v *APIVersion) Date() time.Time {
130137
return v.versionDate
131138
}
132139

140+
func (v *APIVersion) StabilityLevel() string {
141+
return v.stabilityVersion
142+
}
143+
144+
func (v *APIVersion) ExactMatchOnly() bool {
145+
return v.IsPreview()
146+
}
147+
148+
func (v *APIVersion) IsPreview() bool {
149+
return IsPreviewSabilityLevel(v.version)
150+
}
151+
152+
func IsPreviewSabilityLevel(value string) bool {
153+
return strings.EqualFold(value, PreviewStabilityLevel)
154+
}
155+
156+
func IsStableSabilityLevel(value string) bool {
157+
return strings.EqualFold(value, StableStabilityLevel)
158+
}
159+
133160
func FindMatchesFromContentType(contentType string) []string {
134161
return contentPattern.FindStringSubmatch(contentType)
135162
}
@@ -160,6 +187,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
160187
op response:
161188
"200":
162189
content: application/vnd.atlas.2023-01-01+json
190+
content: application/vnd.atlas.preview+json
163191
"201":
164192
content: application/vnd.atlas.2023-12-01+json
165193
content: application/vnd.atlas.2025-01-01+json
@@ -181,14 +209,19 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
181209
log.Printf("Ignoring invalid content type: %q", contentType)
182210
continue
183211
}
184-
if contentVersion.GreaterThan(requestedVersion) {
185-
continue
186-
}
187212

188213
if contentVersion.Equal(requestedVersion) {
189214
return contentVersion
190215
}
191216

217+
if contentVersion.ExactMatchOnly() || requestedVersion.ExactMatchOnly() {
218+
continue
219+
}
220+
221+
if contentVersion.GreaterThan(requestedVersion) {
222+
continue
223+
}
224+
192225
if latestVersionMatch == nil || contentVersion.GreaterThan(latestVersionMatch) {
193226
latestVersionMatch = contentVersion
194227
}

tools/cli/internal/apiversion/version_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ func TestParseVersion(t *testing.T) {
4848
expectedMatch: "2030-02-20",
4949
wantErr: false,
5050
},
51+
{
52+
name: "preview_json",
53+
contentType: "application/vnd.atlas.preview+json",
54+
expectedMatch: "preview",
55+
wantErr: false,
56+
},
57+
{
58+
name: "preview_yaml",
59+
contentType: "application/vnd.atlas.preview+yaml",
60+
expectedMatch: "preview",
61+
wantErr: false,
62+
},
63+
{
64+
name: "preview_csv",
65+
contentType: "application/vnd.atlas.preview+csv",
66+
expectedMatch: "preview",
67+
wantErr: false,
68+
},
5169
{
5270
name: "invalid",
5371
contentType: "application/vnd.test.2023-01-01",
@@ -94,6 +112,12 @@ func TestNewAPIVersionFromContentType(t *testing.T) {
94112
expectedMatch: "2030-02-20",
95113
wantErr: false,
96114
},
115+
{
116+
name: "preview",
117+
contentType: "application/vnd.atlas.preview+json",
118+
expectedMatch: "preview",
119+
wantErr: false,
120+
},
97121
{
98122
name: "invalid",
99123
contentType: "application/vnd.test.2023-01-01",
@@ -416,6 +440,11 @@ func TestFindLatestContentVersionMatched(t *testing.T) {
416440
targetVersion: "2023-01-01",
417441
expectedMatch: "2023-01-01",
418442
},
443+
{
444+
name: "exact match preview",
445+
targetVersion: "preview",
446+
expectedMatch: "preview",
447+
},
419448
{
420449
name: "exact match 2023-11-15",
421450
targetVersion: "2023-11-15",
@@ -470,6 +499,7 @@ func oasOperationAllVersions() *openapi3.Operation {
470499
responses.Set("200", &openapi3.ResponseRef{
471500
Value: &openapi3.Response{
472501
Content: map[string]*openapi3.MediaType{
502+
"application/vnd.atlas.preview+json": {},
473503
"application/vnd.atlas.2023-01-01+json": {},
474504
"application/vnd.atlas.2023-01-01+csv": {},
475505
"application/vnd.atlas.2023-02-01+json": {},

tools/cli/internal/cli/versions/versions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func (o *Opts) filterStabilityLevelVersions(apiVersions []string) []string {
7676

7777
var out []string
7878
for _, v := range apiVersions {
79-
if o.stabilityLevel == apiversion.PreviewStabilityLevel && strings.Contains(v, "preview") {
79+
if (apiversion.IsStableSabilityLevel(o.stabilityLevel)) && !apiversion.IsPreviewSabilityLevel(v) {
8080
out = append(out, v)
8181
}
8282

83-
if o.stabilityLevel == apiversion.StableStabilityLevel && !strings.Contains(v, "preview") {
83+
if (apiversion.IsPreviewSabilityLevel(o.stabilityLevel)) && apiversion.IsPreviewSabilityLevel(v) {
8484
out = append(out, v)
8585
}
8686
}

0 commit comments

Comments
 (0)