Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support to set description #448

Merged
merged 4 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions internal/bundlegen/proxybundle/proxybundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"errors"
"fmt"
"internal/bundlegen/apiproxydef"
"io"
"net/http"
"net/url"
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions internal/bundlegen/sharedflowdef/sharedflowdef.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion internal/cmd/apis/oascrtapisv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "",
Expand Down
22 changes: 21 additions & 1 deletion internal/cmd/sharedflows/bundlecrtsf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"internal/client/sharedflows"

cp "github.com/otiai10/copy"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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",
Expand All @@ -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",
"", "Sharedflow description; Appends to the description if the description already exists")

_ = BundleCreateCmd.MarkFlagRequired("name")
}
Loading