Skip to content

Commit f7ebeda

Browse files
CLOUDP-333549: --output is required when downloading logs, but this is not documented
1 parent 234834c commit f7ebeda

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

tools/cli/internal/openapi/filter/code_sample.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,21 @@ func (f *CodeSampleFilter) Apply() error {
6565
return nil
6666
}
6767

68-
func (f *CodeSampleFilter) newDigestCurlCodeSamplesForOperation(pathName, opMethod string) codeSample {
68+
func (f *CodeSampleFilter) newDigestCurlCodeSamplesForOperation(pathName, opMethod, format string) codeSample {
6969
version := apiVersion(f.metadata.targetVersion)
7070
source := "curl --user \"${PUBLIC_KEY}:${PRIVATE_KEY}\" \\\n --digest \\\n " +
71-
"--header \"Accept: application/vnd.atlas." + version + "+json\" \\\n "
71+
"--header \"Accept: application/vnd.atlas." + version + "+" + format + "\" \\\n "
7272

7373
switch opMethod {
7474
case "GET":
75-
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName + "?pretty=true\""
75+
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName
76+
if format == "gzip" {
77+
source += "\" \\\n "
78+
source += "--output \"file_name." + format + "\""
79+
} else {
80+
source += "?pretty=true\""
81+
}
82+
7683
case "DELETE":
7784
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName + "\""
7885
case "POST", "PATCH", "PUT":
@@ -88,14 +95,20 @@ func (f *CodeSampleFilter) newDigestCurlCodeSamplesForOperation(pathName, opMeth
8895
}
8996
}
9097

91-
func (f *CodeSampleFilter) newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod string) codeSample {
98+
func (f *CodeSampleFilter) newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod, format string) codeSample {
9299
version := apiVersion(f.metadata.targetVersion)
93100
source := "curl --header \"Authorization: Bearer ${ACCESS_TOKEN}\" \\\n " +
94-
"--header \"Accept: application/vnd.atlas." + version + "+json\" \\\n "
101+
"--header \"Accept: application/vnd.atlas." + version + "+" + format + "\" \\\n "
95102

96103
switch opMethod {
97104
case "GET":
98-
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName + "?pretty=true\""
105+
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName
106+
if format == "gzip" {
107+
source += "\" \\\n "
108+
source += "--output \"file_name." + format + "\""
109+
} else {
110+
source += "?pretty=true\""
111+
}
99112
case "DELETE":
100113
source += "-X " + opMethod + " \"https://cloud.mongodb.com" + pathName + "\""
101114
case "POST", "PATCH", "PUT":
@@ -193,10 +206,46 @@ func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod str
193206
codeSamples = append(codeSamples, *sdkSample)
194207
}
195208

209+
supportedFormat := getSupportedFormat(op)
196210
codeSamples = append(
197211
codeSamples,
198-
f.newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod),
199-
f.newDigestCurlCodeSamplesForOperation(pathName, opMethod))
212+
f.newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod, supportedFormat),
213+
f.newDigestCurlCodeSamplesForOperation(pathName, opMethod, supportedFormat))
200214
op.Extensions[codeSampleExtensionName] = codeSamples
201215
return nil
202216
}
217+
218+
// getSupportedFormat inspects the response content types of a given OpenAPI operation,
219+
// looking for a content type string in the format "application/vnd.atlas.<api_version>+<supported_format>".
220+
// It splits the content type on the '+' character and returns the last part, which represents the supported format (e.g., "json").
221+
// If no such content type is found, it defaults to returning "json".
222+
func getSupportedFormat(op *openapi3.Operation) string {
223+
responseMap := successResponseExtensions(op.Responses.Map())
224+
for k := range responseMap {
225+
// k is a string with the format "application/vnd.atlas.<api_version>+<supported_format>"
226+
parts := strings.Split(k, "+")
227+
if len(parts) > 1 {
228+
return parts[len(parts)-1]
229+
}
230+
}
231+
return "json"
232+
}
233+
234+
// successResponseExtensions returns the Content object of the first successful HTTP response (status 200, 201, 202, or 204)
235+
// found in the provided responses map.
236+
func successResponseExtensions(responsesMap map[string]*openapi3.ResponseRef) openapi3.Content {
237+
if val, ok := responsesMap["200"]; ok {
238+
return val.Value.Content
239+
}
240+
if val, ok := responsesMap["201"]; ok {
241+
return val.Value.Content
242+
}
243+
if val, ok := responsesMap["202"]; ok {
244+
return val.Value.Content
245+
}
246+
if val, ok := responsesMap["204"]; ok {
247+
return val.Value.Content
248+
}
249+
250+
return nil
251+
}

0 commit comments

Comments
 (0)