From 0ef1ca996034d33be379adf4fc3ebbfa08283e11 Mon Sep 17 00:00:00 2001 From: Srinandan Sridhar <13950006+srinandan@users.noreply.github.com> Date: Wed, 20 Sep 2023 10:46:26 -0700 Subject: [PATCH] feat: adds wait option for sf deploys #300 (#301) * feat: adds wait option for sf deploys #300 * chore: fix linting issues #300 --- cmd/sharedflows/depsf.go | 43 ++++++++++++++++++- internal/bundlegen/proxybundle/proxybundle.go | 7 +-- internal/client/appgroups/appgroups.go | 2 +- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/cmd/sharedflows/depsf.go b/cmd/sharedflows/depsf.go index 996b242fb..578b8fa28 100644 --- a/cmd/sharedflows/depsf.go +++ b/cmd/sharedflows/depsf.go @@ -15,7 +15,11 @@ package sharedflows import ( + "encoding/json" + "time" + "internal/apiclient" + "internal/clilog" "internal/client/sharedflows" @@ -38,15 +42,49 @@ var DepCmd = &cobra.Command{ } } _, err = sharedflows.Deploy(name, revision, overrides, serviceAccountName) + apiclient.DisableCmdPrintHttpResponse() + + if wait { + clilog.Info.Printf("Checking deployment status in %d seconds\n", interval) + + stop := apiclient.Every(interval*time.Second, func(time.Time) bool { + var respBody []byte + respMap := make(map[string]interface{}) + if respBody, err = sharedflows.ListRevisionDeployments(name, revision); err != nil { + clilog.Error.Printf("Error fetching sharedflow revision status: %v", err) + return false + } + + if err = json.Unmarshal(respBody, &respMap); err != nil { + return true + } + + if respMap["state"] == "PROGRESSING" { + clilog.Info.Printf("Sharedflow deployment status is: %s. Waiting %d seconds.\n", respMap["state"], interval) + return true + } else if respMap["state"] == "READY" { + clilog.Info.Println("Sharedflow deployment completed with status: ", respMap["state"]) + return false + } else { + clilog.Info.Println("Sharedflow deployment failed with status: ", respMap["state"]) + return false + } + }) + + <-stop + } + return err }, } var ( - overrides bool + overrides, wait bool serviceAccountName string ) +const interval = 10 + func init() { DepCmd.Flags().StringVarP(&name, "name", "n", "", "Sharedflow name") @@ -56,6 +94,9 @@ func init() { -1, "Sharedflow revision. If not set, the highest revision is used") DepCmd.Flags().BoolVarP(&overrides, "ovr", "r", false, "Forces deployment of the new revision") + DepCmd.Flags().BoolVarP(&wait, "wait", "", + false, "Waits for the deployment to finish, with success or error") + DepCmd.Flags().StringVarP(&serviceAccountName, "sa", "s", "", "The format must be {ACCOUNT_ID}@{PROJECT}.iam.gserviceaccount.com.") diff --git a/internal/bundlegen/proxybundle/proxybundle.go b/internal/bundlegen/proxybundle/proxybundle.go index f524e7769..f300ccd9e 100644 --- a/internal/bundlegen/proxybundle/proxybundle.go +++ b/internal/bundlegen/proxybundle/proxybundle.go @@ -43,8 +43,10 @@ import ( "golang.org/x/oauth2" ) -var proxyRootDir = "apiproxy" -var sfRootDir = "sharedflowbundle" +var ( + proxyRootDir = "apiproxy" + sfRootDir = "sharedflowbundle" +) func GenerateAPIProxyBundleFromOAS(name string, content string, @@ -692,7 +694,6 @@ func archiveBundle(pathToZip, destinationPath string, sharedflow bool) (err erro } func GitHubImportBundle(owner string, repo string, repopath string, sharedflow bool) (err error) { - var rootDir string if sharedflow { diff --git a/internal/client/appgroups/appgroups.go b/internal/client/appgroups/appgroups.go index 6802f54d8..db9d5de93 100644 --- a/internal/client/appgroups/appgroups.go +++ b/internal/client/appgroups/appgroups.go @@ -136,7 +136,7 @@ func Update(name string, channelURI string, channelID string, displayName string developerDetailsAttribute.Value = getDeveloperDetails(devs) } - //clear existing attributes + // clear existing attributes a.Attributes = nil if len(attrs) != 0 {