Skip to content

Commit

Permalink
Merge pull request #298 from apigee/issue294
Browse files Browse the repository at this point in the history
feat: adds support to deploy sf from gh #294
  • Loading branch information
ssvaidyanathan authored Sep 19, 2023
2 parents 8991204 + df04201 commit 5137053
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 123 deletions.
6 changes: 3 additions & 3 deletions cmd/apis/ghcrtapis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ var GhCreateCmd = &cobra.Command{
if os.Getenv("GITHUB_TOKEN") == "" {
clilog.Debug.Println("github token is not set as an env var. Running unauthenticated")
}
if err = proxybundle.GitHubImportBundle(ghOwner, ghRepo, ghPath); err != nil {
proxybundle.CleanUp()
if err = proxybundle.GitHubImportBundle(ghOwner, ghRepo, ghPath, false); err != nil {
proxybundle.ProxyCleanUp()
return err
}
_, err = apis.CreateProxy(name, bundleName)
proxybundle.CleanUp()
proxybundle.ProxyCleanUp()
return err
},
}
Expand Down
93 changes: 93 additions & 0 deletions cmd/sharedflows/bundlecrtsf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 sharedflows

import (
"fmt"
"os"
"path"
"path/filepath"

"internal/apiclient"

"internal/bundlegen/proxybundle"

"internal/client/sharedflows"

"github.com/spf13/cobra"
)

// BundleCreateCmd to create shared flow
var BundleCreateCmd = &cobra.Command{
Use: "bundle",
Short: "Creates a sharedflow in an Apigee Org",
Long: "Creates a sharedflow in an Apigee Org",
Args: func(cmd *cobra.Command, args []string) (err error) {
apiclient.SetApigeeEnv(env)
if sfZip != "" && sfFolder != "" {
return fmt.Errorf("sharedflow bundle (zip) and folder to a sharedflow cannot be combined")
}
if sfZip == "" && sfFolder == "" {
return fmt.Errorf("either sharedflow bundle (zip) or folder must be specified, not both")
}
if sfFolder != "" {
if _, err := os.Stat(sfFolder); os.IsNotExist(err) {
return err
}
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if sfZip != "" {
_, err = sharedflows.Create(name, sfZip)
} else if sfFolder != "" {
if stat, err := os.Stat(folder); err == nil && !stat.IsDir() {
return fmt.Errorf("supplied path is not a folder")
}
if filepath.Base(sfFolder) != "sharedflowbundle" {
return fmt.Errorf("--sf-folder or -p must be a path to sharedflowbundle folder")
}
tmpDir, err := os.MkdirTemp("", "sf")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

sfBundlePath := path.Join(tmpDir, name+".zip")

if err = proxybundle.GenerateArchiveBundle(sfFolder, sfBundlePath); err != nil {
return err
}
if _, err = sharedflows.Create(name, sfBundlePath); err != nil {
return err
}
return os.Remove(sfBundlePath)
}
return err
},
}

var sfZip, sfFolder string

func init() {
BundleCreateCmd.Flags().StringVarP(&name, "name", "n",
"", "Sharedflow name")
BundleCreateCmd.Flags().StringVarP(&sfZip, "sf-zip", "p",
"", "Path to the Sharedflow bundle/zip file")
BundleCreateCmd.Flags().StringVarP(&sfFolder, "sf-folder", "f",
"", "Path to the Sharedflow Bundle; ex: ./test/sharedflowbundle")

_ = BundleCreateCmd.MarkFlagRequired("name")
}
72 changes: 5 additions & 67 deletions cmd/sharedflows/crtsf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,17 @@
package sharedflows

import (
"fmt"
"os"
"path"
"path/filepath"

"internal/apiclient"

"internal/bundlegen/proxybundle"

"internal/client/sharedflows"

"github.com/spf13/cobra"
)

// CreateCmd to create shared flow
// CreateCmd to create sharedflow
var CreateCmd = &cobra.Command{
Use: "create",
Short: "Creates a sharedflow in an Apigee Org",
Long: "Creates a sharedflow in an Apigee Org",
Args: func(cmd *cobra.Command, args []string) (err error) {
apiclient.SetApigeeEnv(env)
if sfZip != "" && sfFolder != "" {
return fmt.Errorf("sharedflow bundle (zip) and folder to a sharedflow cannot be combined")
}
if sfZip == "" && sfFolder == "" {
return fmt.Errorf("either sharedflow bundle (zip) or folder must be specified, not both")
}
if sfFolder != "" {
if _, err := os.Stat(sfFolder); os.IsNotExist(err) {
return err
}
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if sfZip != "" {
_, err = sharedflows.Create(name, sfZip)
} else if sfFolder != "" {
if stat, err := os.Stat(folder); err == nil && !stat.IsDir() {
return fmt.Errorf("supplied path is not a folder")
}
if filepath.Base(sfFolder) != "sharedflowbundle" {
return fmt.Errorf("--sf-folder or -p must be a path to sharedflowbundle folder")
}
tmpDir, err := os.MkdirTemp("", "sf")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

sfBundlePath := path.Join(tmpDir, name+".zip")

if err = proxybundle.GenerateArchiveBundle(sfFolder, sfBundlePath); err != nil {
return err
}
if _, err = sharedflows.Create(name, sfBundlePath); err != nil {
return err
}
return os.Remove(sfBundlePath)
}
return err
},
Short: "Creates a Sharedflow in an Apigee Org",
Long: "Creates a Sharedflow in an Apigee Org",
}

var sfZip, sfFolder string

func init() {
CreateCmd.Flags().StringVarP(&name, "name", "n",
"", "Sharedflow name")
CreateCmd.Flags().StringVarP(&sfZip, "sf-zip", "p",
"", "Path to the Sharedflow bundle/zip file")
CreateCmd.Flags().StringVarP(&sfFolder, "sf-folder", "f",
"", "Path to the Sharedflow Bundle; ex: ./test/sharedflowbundle")

_ = CreateCmd.MarkFlagRequired("name")
CreateCmd.AddCommand(GhCreateCmd)
CreateCmd.AddCommand(BundleCreateCmd)
}
79 changes: 79 additions & 0 deletions cmd/sharedflows/ghcrtsf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2021 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 sharedflows

import (
"fmt"
"os"
"regexp"

"internal/apiclient"
"internal/client/sharedflows"

"internal/clilog"

proxybundle "internal/bundlegen/proxybundle"

"github.com/spf13/cobra"
)

// GhCreateCmd create an api from a github repo
var GhCreateCmd = &cobra.Command{
Use: "github",
Aliases: []string{"gh"},
Short: "Creates a sharedflow from a GitHub repo",
Long: "Creates a sharedflow from a GitHub repo",
Args: func(cmd *cobra.Command, args []string) (err error) {
re := regexp.MustCompile(`(\w+)?\/sharedflowbundle$`)
if ok := re.Match([]byte(ghPath)); !ok {
return fmt.Errorf("github path must end with /sharedflowbundle")
}

return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if os.Getenv("GITHUB_TOKEN") == "" {
clilog.Debug.Println("github token is not set as an env var. Running unauthenticated")
}
if err = proxybundle.GitHubImportBundle(ghOwner, ghRepo, ghPath, true); err != nil {
proxybundle.SharedflowCleanUp()
return err
}

_, err = sharedflows.Create(name, bundleName)
proxybundle.SharedflowCleanUp()
return err
},
}

const bundleName = "sharedflowbundle.zip"

var ghOwner, ghRepo, ghPath string

func init() {
GhCreateCmd.Flags().StringVarP(&name, "name", "n",
"", "Sharedflow name")
GhCreateCmd.Flags().StringVarP(&ghOwner, "owner", "u",
"", "The github organization or username. ex: In https://github.com/apigee, apigee is the owner name")
GhCreateCmd.Flags().StringVarP(&ghRepo, "repo", "r",
"", "The github repo name. ex: https://github.com/apigee/api-platform-samples, api-platform-samples is the repo")
GhCreateCmd.Flags().StringVarP(&ghPath, "sf-path", "p",
"", "The path in the repo to the sharedflowbundle folder. ex: sample-proxies/security/sharedflowbundle")

_ = GhCreateCmd.MarkFlagRequired("name")
_ = GhCreateCmd.MarkFlagRequired("owner")
_ = GhCreateCmd.MarkFlagRequired("repo")
_ = GhCreateCmd.MarkFlagRequired("sf-path")
}
Loading

0 comments on commit 5137053

Please sign in to comment.