From 6764f64a4a24d1b788df7bc78db9d0b062be2ce4 Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:44:36 -0700 Subject: [PATCH 1/4] bug: fix descrption for flag --- internal/cmd/apis/oascrtapisv2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/apis/oascrtapisv2.go b/internal/cmd/apis/oascrtapisv2.go index 276285387..874a3b428 100644 --- a/internal/cmd/apis/oascrtapisv2.go +++ b/internal/cmd/apis/oascrtapisv2.go @@ -165,7 +165,7 @@ func init() { OasCreatev2Cmd.Flags().StringVarP(&oasURI, "oas-base-uri", "u", "", "Open API Specification URI Base location") OasCreatev2Cmd.Flags().StringVarP(&specName, "oas-name", "", - "", "Open API 3.0/3.1 Specification Name; Used with oas-base-filepath or oas-base-uri") + "", "Open API 3.0/3.1 Specification Name; Used with oas-base-folderpath or oas-base-uri") OasCreatev2Cmd.Flags().StringVarP(&oasGoogleAcessTokenScopeLiteral, "google-accesstoken-scope-literal", "", "", "Generate Google Access token with target endpoint and set scope") OasCreatev2Cmd.Flags().StringVarP(&oasGoogleIDTokenAudLiteral, "google-idtoken-aud-literal", "", From 887e81bf7128540dc8b532cd0d741c6b87f664f6 Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:55:15 -0700 Subject: [PATCH 2/4] feat: adds support to set description #447 --- internal/bundlegen/proxybundle/proxybundle.go | 24 +++++++- .../bundlegen/sharedflowdef/sharedflowdef.go | 61 +++++++++++++++++++ internal/cmd/sharedflows/bundlecrtsf.go | 22 ++++++- 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 internal/bundlegen/sharedflowdef/sharedflowdef.go diff --git a/internal/bundlegen/proxybundle/proxybundle.go b/internal/bundlegen/proxybundle/proxybundle.go index d9c02a14d..3a3cc726c 100644 --- a/internal/bundlegen/proxybundle/proxybundle.go +++ b/internal/bundlegen/proxybundle/proxybundle.go @@ -19,7 +19,6 @@ import ( "context" "errors" "fmt" - "internal/bundlegen/apiproxydef" "io" "net/http" "net/url" @@ -37,6 +36,7 @@ import ( apiproxy "internal/bundlegen/apiproxydef" policies "internal/bundlegen/policies" proxies "internal/bundlegen/proxies" + sf "internal/bundlegen/sharedflowdef" "internal/bundlegen/targets" target "internal/bundlegen/targets" @@ -773,13 +773,33 @@ func SetDescription(apiProxyFolder string, name string, description string) (err } userFile.Close() - byteValue, err = apiproxydef.SetDescriptionWithMarshal(byteValue, description) + byteValue, err = apiproxy.SetDescriptionWithMarshal(byteValue, description) if err != nil { return err } return apiclient.WriteByteArrayToFile(path.Join(apiProxyFolder, name+".xml"), false, byteValue) } +func SetSharedFlowDescription(sfFolder string, name string, description string) (err error) { + userFile, err := os.Open(path.Join(sfFolder, name+".xml")) + if err != nil { + return err + } + + byteValue, err := io.ReadAll(userFile) + if err != nil { + return err + } + userFile.Close() + + byteValue, err = sf.SetDescriptionWithMarshal(byteValue, description) + if err != nil { + return err + } + return apiclient.WriteByteArrayToFile(path.Join(sfFolder, name+".xml"), false, byteValue) + +} + func downloadProxyFromRepo(client *github.Client, ctx context.Context, owner string, repo string, repopath string, sharedflow bool) (err error) { var fileContent *github.RepositoryContent var directoryContents []*github.RepositoryContent diff --git a/internal/bundlegen/sharedflowdef/sharedflowdef.go b/internal/bundlegen/sharedflowdef/sharedflowdef.go new file mode 100644 index 000000000..cb437b280 --- /dev/null +++ b/internal/bundlegen/sharedflowdef/sharedflowdef.go @@ -0,0 +1,61 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sharedflowdef + +import "encoding/xml" + +type sharedFlowDef struct { + XMLName xml.Name `xml:"SharedFlowBundle"` + Name string `xml:"name,attr"` + Revision string `xml:"revision,attr"` + CreatedAt string `xml:"CreatedAt,omitempty"` + Description string `xml:"Description,omitempty"` + DisplayName string `xml:"DisplayName,omitempty"` + LastModifiedAt string `xml:"LastModifiedAt,omitempty"` + Policies policiesDef `xml:"Policies,omitempty"` + SharedFlows sharedflowsDef `xml:"SharedFlows,omitempty"` + Resources resourcesDef `xml:"Resources,omitempty"` + SubType string `xml:"subType,omitempty"` +} + +type resourcesDef struct { + Resource []string `xml:"Resource,omitempty"` +} + +type policiesDef struct { + Policy []string `xml:"Policy,omitempty"` +} + +type sharedflowsDef struct { + SharedFlow []string `xml:"SharedFlow,omitempty"` +} + +func SetDescriptionWithMarshal(contents []byte, description string) (resp []byte, err error) { + s := sharedFlowDef{} + err = xml.Unmarshal(contents, &s) + if err != nil { + return nil, err + } + if s.Description != "" { + s.Description = s.Description + " " + description + } else { + s.Description = description + } + resp, err = xml.MarshalIndent(s, "", " ") + if err != nil { + return nil, err + } + return resp, nil +} diff --git a/internal/cmd/sharedflows/bundlecrtsf.go b/internal/cmd/sharedflows/bundlecrtsf.go index 907aa85be..eb2ffa7a1 100644 --- a/internal/cmd/sharedflows/bundlecrtsf.go +++ b/internal/cmd/sharedflows/bundlecrtsf.go @@ -27,6 +27,8 @@ import ( "internal/client/sharedflows" + cp "github.com/otiai10/copy" + "github.com/spf13/cobra" ) @@ -73,6 +75,22 @@ var BundleCreateCmd = &cobra.Command{ sfBundlePath := path.Join(tmpDir, name+".zip") + if desc != "" { + tmpProxyFolder := path.Join(tmpDir, "sharedflowbundle") + // copy the apiproxy folder to a temporary location + err = cp.Copy(sfFolder, tmpProxyFolder) + if err != nil { + return err + } + // set the description in the xml file + err = proxybundle.SetSharedFlowDescription(tmpProxyFolder, name, desc) + if err != nil { + return err + } + // use the apiproxy folder in the tmp location to create archive + sfFolder = tmpProxyFolder + } + if err = proxybundle.GenerateArchiveBundle(sfFolder, sfBundlePath, true); err != nil { return err } @@ -99,7 +117,7 @@ var BundleCreateCmd = &cobra.Command{ }, } -var sfZip, sfFolder string +var sfZip, sfFolder, desc string func init() { BundleCreateCmd.Flags().StringVarP(&name, "name", "n", @@ -116,6 +134,8 @@ func init() { false, "Waits for the deployment to finish, with success or error") BundleCreateCmd.Flags().StringVarP(&serviceAccountName, "sa", "s", "", "The format must be {ACCOUNT_ID}@{PROJECT}.iam.gserviceaccount.com.") + BundleCreateCmd.Flags().StringVarP(&desc, "desc", "d", + "", "API Proxy description; Appends to the description if the description already exists") _ = BundleCreateCmd.MarkFlagRequired("name") } From dbf9ec8a7304b5f36ded9a375f20fbbef16c789b Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:56:47 -0700 Subject: [PATCH 3/4] bug: fix description --- internal/cmd/sharedflows/bundlecrtsf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/sharedflows/bundlecrtsf.go b/internal/cmd/sharedflows/bundlecrtsf.go index eb2ffa7a1..380d3d00b 100644 --- a/internal/cmd/sharedflows/bundlecrtsf.go +++ b/internal/cmd/sharedflows/bundlecrtsf.go @@ -135,7 +135,7 @@ func init() { BundleCreateCmd.Flags().StringVarP(&serviceAccountName, "sa", "s", "", "The format must be {ACCOUNT_ID}@{PROJECT}.iam.gserviceaccount.com.") BundleCreateCmd.Flags().StringVarP(&desc, "desc", "d", - "", "API Proxy description; Appends to the description if the description already exists") + "", "Shareflow description; Appends to the description if the description already exists") _ = BundleCreateCmd.MarkFlagRequired("name") } From f0f096f5427a14d816d50bf12d866bb62c26feee Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:03:04 -0700 Subject: [PATCH 4/4] bug: fix description --- internal/cmd/sharedflows/bundlecrtsf.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/sharedflows/bundlecrtsf.go b/internal/cmd/sharedflows/bundlecrtsf.go index 380d3d00b..8dbde4670 100644 --- a/internal/cmd/sharedflows/bundlecrtsf.go +++ b/internal/cmd/sharedflows/bundlecrtsf.go @@ -135,7 +135,7 @@ func init() { BundleCreateCmd.Flags().StringVarP(&serviceAccountName, "sa", "s", "", "The format must be {ACCOUNT_ID}@{PROJECT}.iam.gserviceaccount.com.") BundleCreateCmd.Flags().StringVarP(&desc, "desc", "d", - "", "Shareflow description; Appends to the description if the description already exists") + "", "Sharedflow description; Appends to the description if the description already exists") _ = BundleCreateCmd.MarkFlagRequired("name") }