From 60dc389aa49864cd8c71573e6b93218684b1ab3e Mon Sep 17 00:00:00 2001 From: Luwei Ge Date: Tue, 5 Jan 2021 12:59:30 -0500 Subject: [PATCH] deploy existing but undeployed proxies in the org (#142) * deploy existing but undeployed proxies in the org * fixed typo in comment --- cmd/provision/provision_test.go | 38 ++++++++++++++++++++++ cmd/provision/proxy.go | 57 ++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/cmd/provision/provision_test.go b/cmd/provision/provision_test.go index 14554b2..bad3c89 100644 --- a/cmd/provision/provision_test.go +++ b/cmd/provision/provision_test.go @@ -250,6 +250,34 @@ func serveMux(t *testing.T) *http.ServeMux { } } }) + m.HandleFunc("/v1/organizations/saas/environments/dev/apis/remote-service/deployments", func(w http.ResponseWriter, r *http.Request) { + res := apigee.EnvironmentDeployment{ + Name: "remote-service", + Revision: []apigee.RevisionDeployment{ + { + Number: 3, + State: "undeployed", + }, + { + Number: 2, + State: "undeployed", + }, + { + Number: 1, + State: "undeployed", + }, + }, + } + switch r.Method { + default: + t.Fatalf("%s to %s not allowed", r.Method, r.URL.Path) + case http.MethodGet: + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(res); err != nil { + t.Fatalf("want no error %v", err) + } + } + }) m.HandleFunc("/v1/organizations/saas/apis/remote-service", func(w http.ResponseWriter, r *http.Request) { res := apigee.Proxy{ Name: "remote-service", @@ -477,6 +505,16 @@ data: t.Fatalf("want no error: %v", err) } + // deploying existing but undeployed proxies + rootArgs = &shared.RootArgs{} + flags = []string{"provision", "-o", "saas", "-e", "dev", "-u", "me", "-p", "password", "--legacy"} + rootCmd = cmd.GetRootCmd(flags, print.Printf) + shared.AddCommandWithFlags(rootCmd, rootArgs, testCmd(rootArgs, print.Printf, func(r *shared.RootArgs) { setTestUrls(r, ts.URL) })) + + if err := rootCmd.Execute(); err != nil { + t.Fatalf("want no error: %v", err) + } + // error on having rotate > 0 on saas rootArgs = &shared.RootArgs{} flags = []string{"provision", "-o", "saas", "-e", "test", "-u", "me", "-p", "password", "-f", "--legacy", "--rotate", "1"} diff --git a/cmd/provision/proxy.go b/cmd/provision/proxy.go index 8ee64d0..962a7a7 100644 --- a/cmd/provision/proxy.go +++ b/cmd/provision/proxy.go @@ -97,37 +97,50 @@ func (p *provision) checkAndDeployProxy(name, file string, printf shared.FormatF func (p *provision) importAndDeployProxy(name string, proxy *apigee.Proxy, oldRev *apigee.Revision, file string, printf shared.FormatFn) error { var newRev apigee.Revision = 1 + var latestRev apigee.Revision = 0 if proxy != nil && len(proxy.Revisions) > 0 { sort.Sort(apigee.RevisionSlice(proxy.Revisions)) - newRev = proxy.Revisions[len(proxy.Revisions)-1] + 1 - printf("proxy %s exists. highest revision is: %d", name, newRev-1) + latestRev = proxy.Revisions[len(proxy.Revisions)-1] + if p.forceProxyInstall { + // Always increment newRev if forceProxyInstall is true + // regardless of deployment status of the latestRev in the specified environment. + newRev = latestRev + 1 + } else { + // This is the case where proxy exists in the organization + // but is not deployed to the specified environment. + // If oldRev != nil, the whole function will not be invoked without forceProxyInstall being true. + newRev = latestRev + } + printf("proxy %s exists. highest revision is: %d", name, latestRev) } - // create a new client to avoid dumping the proxy binary to stdout during Import - noDebugClient := p.ApigeeClient - if p.Verbose { - opts := *p.ClientOpts - opts.Debug = false - var err error - noDebugClient, err = apigee.NewEdgeClient(&opts) - if err != nil { - return err + if newRev > latestRev { // only import if newRev is larger than the latest revision, 0 if not present, in the organization + // create a new client to avoid dumping the proxy binary to stdout during Import + noDebugClient := p.ApigeeClient + if p.Verbose { + opts := *p.ClientOpts + opts.Debug = false + var err error + noDebugClient, err = apigee.NewEdgeClient(&opts) + if err != nil { + return err + } } - } - printf("creating new proxy %s revision: %d...", name, newRev) - _, res, err := noDebugClient.Proxies.Import(name, file) - if res != nil { - defer res.Body.Close() - } - if err != nil { - return errors.Wrapf(err, "importing proxy %s", name) + printf("creating new proxy %s revision: %d...", name, newRev) + _, res, err := noDebugClient.Proxies.Import(name, file) + if res != nil { + defer res.Body.Close() + } + if err != nil { + return errors.Wrapf(err, "importing proxy %s", name) + } } if oldRev != nil && !p.IsGCPManaged { // it's not necessary to undeploy first with GCP printf("undeploying proxy %s revision %d on env %s...", name, oldRev, p.Env) - _, res, err = p.ApigeeClient.Proxies.Undeploy(name, p.Env, *oldRev) + _, res, err := p.ApigeeClient.Proxies.Undeploy(name, p.Env, *oldRev) if res != nil { defer res.Body.Close() } @@ -140,7 +153,7 @@ func (p *provision) importAndDeployProxy(name string, proxy *apigee.Proxy, oldRe cache := apigee.Cache{ Name: cacheName, } - res, err = p.ApigeeClient.CacheService.Create(cache) + res, err := p.ApigeeClient.CacheService.Create(cache) if err != nil && (res == nil || res.StatusCode != http.StatusConflict) { // http.StatusConflict == already exists return err } @@ -155,7 +168,7 @@ func (p *provision) importAndDeployProxy(name string, proxy *apigee.Proxy, oldRe } printf("deploying proxy %s revision %d to env %s...", name, newRev, p.Env) - _, res, err = p.ApigeeClient.Proxies.Deploy(name, p.Env, newRev) + _, res, err := p.ApigeeClient.Proxies.Deploy(name, p.Env, newRev) if res != nil { defer res.Body.Close() }