Skip to content

Commit

Permalink
deploy existing but undeployed proxies in the org (#142)
Browse files Browse the repository at this point in the history
* deploy existing but undeployed proxies in the org

* fixed typo in comment
  • Loading branch information
rockspore authored Jan 5, 2021
1 parent 87355e0 commit 60dc389
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
38 changes: 38 additions & 0 deletions cmd/provision/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"}
Expand Down
57 changes: 35 additions & 22 deletions cmd/provision/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 60dc389

Please sign in to comment.