-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from apigee/issue294
feat: adds support to deploy sf from gh #294
- Loading branch information
Showing
5 changed files
with
265 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.