Skip to content

Commit

Permalink
bug: fixes api version spec updates #498 (#499)
Browse files Browse the repository at this point in the history
* bug: fixes api version spec updates #498

* bug: fixes api version spec updates #498

* bug: fixes params for deployments #498

* bug: fixes file param #498
  • Loading branch information
srinandan authored Jul 11, 2024
1 parent 4632c7d commit 4802c08
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
26 changes: 18 additions & 8 deletions internal/client/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,12 @@ func LintApiVersionSpec(apiID string, versionID string, specID string) (respBody
}

func UpdateApiVersionSpec(apiID string, versionID string, specID string, displayName string,
contents []byte, mimeType string, sourceURI string, documentation string,
contents []byte, mimeType string, sourceURI string,
) (respBody []byte, err error) {
return createOrUpdateApiVersionSpec(apiID, versionID, specID, displayName, contents, mimeType, sourceURI, documentation, UPDATE)
if contents != nil && sourceURI != "" {
return nil, fmt.Errorf("contents and sourceURI cannot be set together")
}
return createOrUpdateApiVersionSpec(apiID, versionID, specID, displayName, contents, mimeType, sourceURI, "", UPDATE)
}

func ExportApiVersionSpecs(apiID string, versionID string, folder string) (err error) {
Expand Down Expand Up @@ -507,13 +510,24 @@ func createOrUpdateApiVersionSpec(apiID string, versionID string, specID string,
contents []byte, mimeType string, sourceURI string, documentation string, action Action,
) (respBody []byte, err error) {
s := spec{}
s.DisplayName = displayName
updateMask := []string{}

if displayName != "" {
s.DisplayName = displayName
updateMask = append(updateMask, "display_name")
}
if sourceURI != "" {
s.SourceURI = sourceURI
updateMask = append(updateMask, "source_uri")
}

if documentation != "" {
s.Documentation.ExternalUri = documentation
}

if contents != nil {
s.Contents.Contents = base64.StdEncoding.EncodeToString(contents)
updateMask = append(updateMask, "contents")
}

if strings.Contains(mimeType, "yaml") || strings.Contains(mimeType, "yml") {
Expand All @@ -532,10 +546,6 @@ func createOrUpdateApiVersionSpec(apiID string, versionID string, specID string,
s.Contents.MimeType = "application/text"
}

if sourceURI != "" {
s.SourceURI = sourceURI
}

payload, err := json.Marshal(s)
if err != nil {
return nil, err
Expand All @@ -552,7 +562,7 @@ func createOrUpdateApiVersionSpec(apiID string, versionID string, specID string,
} else {
u.Path = path.Join(u.Path, "apis", apiID, "versions", versionID, "specs", specID)
q := u.Query()
q.Set("updateMask", "display_name,source_uri,documentation,contents,spec_type")
q.Set("updateMask", strings.Join(updateMask, ","))
u.RawQuery = q.Encode()
respBody, err = apiclient.HttpClient(u.String(), string(payload), "PATCH")
}
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/apihub/apis/versions/specs/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ var UpdateCmd = &cobra.Command{
cmd.SilenceUsage = true
var contents []byte

if contents, err = utils.ReadFile(apiFilePath); err != nil {
return err
if apiFilePath != "" {
if contents, err = utils.ReadFile(apiFilePath); err != nil {
return err
}
}

_, err = hub.UpdateApiVersionSpec(apiID, versionID, specID, displayName,
contents, filepath.Ext(apiFilePath), sourceURI, documentation)
contents, filepath.Ext(apiFilePath), sourceURI)
return
},
}
Expand All @@ -59,14 +62,11 @@ func init() {
nil, "API Spec attributes")
UpdateCmd.Flags().StringVarP(&sourceURI, "source-uri", "s",
"", "API Spec attributes")
UpdateCmd.Flags().StringVarP(&documentation, "documentation", "",
"", "API Spec external documentation")
UpdateCmd.Flags().StringVarP(&apiFilePath, "file", "f",
"", "Path to a file containing the API spec")

_ = UpdateCmd.MarkFlagRequired("id")
_ = UpdateCmd.MarkFlagRequired("api-id")
_ = UpdateCmd.MarkFlagRequired("version")
_ = UpdateCmd.MarkFlagRequired("display-name")
_ = UpdateCmd.MarkFlagRequired("file")
}

0 comments on commit 4802c08

Please sign in to comment.