Skip to content

Commit

Permalink
Merge pull request #59 from zambien/bugs/40
Browse files Browse the repository at this point in the history
Bugs/40
  • Loading branch information
zambien authored Apr 3, 2020
2 parents 0b98d55 + d811245 commit e082ca6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 49 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,25 +260,3 @@ goreleaser # actually create the release
You can read more about goreleaser here:

https://goreleaser.com/

## Important Known Issues

Right now if you rev your proxy bundle then apply your deployment will not update automatically if you reference that proxy rev (as in the example above).

To work around the issue you can apply twice:
```
terraform apply && terraform apply
```

Or manually change the revision number in a variable or in the script...
```
resource "apigee_api_proxy_deployment" "helloworld_proxy_deployment" {
proxy_name = "${apigee_api_proxy.helloworld_proxy.name}"
org = "${var.org}"
env = "${var.env}"
revision = 4 # the known next revision number
}
```

This is happening due to a known issue in Terraform that should be fixed soon:
https://github.com/hashicorp/terraform/issues/15857
5 changes: 2 additions & 3 deletions apigee/helpers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package apigee

import (
"reflect"
"sort"

"github.com/hashicorp/terraform/helper/schema"
"github.com/zambien/go-apigee-edge"
"reflect"
"sort"
)

func flattenStringList(list []string) []interface{} {
Expand Down
50 changes: 26 additions & 24 deletions apigee/resource_api_proxy_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,26 @@ func resourceApiProxyDeploymentRead(d *schema.ResourceData, meta interface{}) (e
if environment.Name == d.Get("env").(string) {
//We don't break. Always get the last one if there are multiple deployments.
for _, revision := range environment.Revision {
found = true
log.Printf("[DEBUG] resourceApiProxyDeploymentRead checking deployed revision: %#v for expected revision: %#v\n", revision.Number.String(), d.Get("revision").(string))
if d.Get("revision").(string) != "latest" && d.Get("revision").(string) == revision.Number.String() {
matchedRevision = revision.Number.String()
found = true
break
} else {
matchedRevision = revision.Number.String()
}
found = true
}
}
}
}

if found {
log.Printf("[DEBUG] resourceApiProxyDeploymentRead - deployment found. Revision is: %#v", matchedRevision)
d.Set("revision", matchedRevision)
if d.Get("revision").(string) == "latest" {
d.SetId(matchedRevision)
} else {
d.Set("revision", matchedRevision)
}
log.Printf("[DEBUG] resourceApiProxyDeploymentRead - deployment found. Revision is: %#v", d.Get("revision").(string))
} else {
log.Print("[DEBUG] resourceApiProxyDeploymentRead - no deployment found")
d.SetId("")
Expand All @@ -143,16 +146,11 @@ func resourceApiProxyDeploymentCreate(d *schema.ResourceData, meta interface{})

if d.Get("revision").(string) == "latest" {
// deploy latest
rev, err := getLatestRevision(client, proxy_name)
rev_int, err := getLatestRevision(client, proxy_name)
rev = apigee.Revision(rev_int)
if err != nil {
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentUpdate error getting latest revision: %v", err)
}
_, _, err = client.Proxies.Deploy(proxy_name, env, apigee.Revision(rev), delay, override)
if err != nil {
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentUpdate error deploying: %v", err)
}
log.Printf("[DEBUG] resourceApiProxyDeploymentUpdate Deployed revision %d of %s", rev, proxy_name)
return resourceApiProxyDeploymentRead(d, meta)
}

proxyDep, _, err := client.Proxies.Deploy(proxy_name, env, rev, delay, override)
Expand All @@ -174,6 +172,7 @@ func resourceApiProxyDeploymentCreate(d *schema.ResourceData, meta interface{})
d.SetId(id.String())
d.Set("revision", proxyDep.Revision.String())

log.Printf("[DEBUG] resourceApiProxyDeploymentUpdate Deployed revision %d of %s", rev, proxy_name)
return resourceApiProxyDeploymentRead(d, meta)
}

Expand All @@ -196,25 +195,17 @@ func resourceApiProxyDeploymentUpdate(d *schema.ResourceData, meta interface{})
override = true
}

rev_int, _ := strconv.Atoi(d.Get("revision").(string))
rev := apigee.Revision(rev_int)
if d.Get("revision").(string) == "latest" {
// deploy latest
rev, err := getLatestRevision(client, proxy_name)
rev_int, err := getLatestRevision(client, proxy_name)
rev = apigee.Revision(rev_int)
if err != nil {
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentUpdate error getting latest revision: %v", err)
}
_, _, err = client.Proxies.ReDeploy(proxy_name, env, apigee.Revision(rev), delay, override)
if err != nil {
if strings.Contains(err.Error(), " is already deployed ") {
return resourceApiProxyDeploymentRead(d, meta)
}
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentUpdate error deploying: %v", err)
}
log.Printf("[DEBUG] resourceApiProxyDeploymentUpdate Deployed revision %d of %s", rev, proxy_name)
return resourceApiProxyDeploymentRead(d, meta)
}

rev_int, _ := strconv.Atoi(d.Get("revision").(string))
rev := apigee.Revision(rev_int)
_, _, err := client.Proxies.ReDeploy(proxy_name, env, rev, delay, override)

if err != nil {
Expand All @@ -225,6 +216,7 @@ func resourceApiProxyDeploymentUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentUpdate error redeploying: %s", err.Error())
}

log.Printf("[DEBUG] resourceApiProxyDeploymentUpdate Deployed revision %d of %s", rev, proxy_name)
return resourceApiProxyDeploymentRead(d, meta)
}

Expand All @@ -236,16 +228,26 @@ func resourceApiProxyDeploymentDelete(d *schema.ResourceData, meta interface{})

proxy_name := d.Get("proxy_name").(string)
env := d.Get("env").(string)

rev_int, _ := strconv.Atoi(d.Get("revision").(string))
rev := apigee.Revision(rev_int)
if d.Get("revision").(string) == "latest" {
// deploy latest
rev_int, err := getLatestRevision(client, proxy_name)
rev = apigee.Revision(rev_int)
if err != nil {
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentDelete error getting latest revision: %v", err)
}
}

_, _, err := client.Proxies.Undeploy(proxy_name, env, rev)
if err != nil {
log.Printf("[ERROR] resourceApiProxyDeploymentDelete error undeploying: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDeploymentDelete error undeploying: %s", err.Error())
}
log.Printf("[DEBUG] resourceApiProxyDeploymentDelete Deleted revision %d of %s", rev, proxy_name)

return nil
return resourceApiProxyDeploymentRead(d, meta)
}

func getLatestRevision(client *apigee.EdgeClient, proxyName string) (int, error) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/zambien/terraform-provider-apigee
go 1.13

require (
github.com/17media/structs v0.0.0-20200317074636-7872972ebe57
github.com/fatih/structs v1.1.0 // indirect
github.com/gofrs/uuid v3.2.0+incompatible
github.com/hashicorp/terraform v0.12.13
github.com/sethgrid/pester v0.0.0-20190127155807-68a33a018ad0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl
dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBrvjyP0v+ecvNYvCpyZgu5/xkfAUhi6wJj28eUfSU=
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
github.com/17media/structs v0.0.0-20200317074636-7872972ebe57 h1:37pgIfOnYep7tssdIxFq51FH9DwETjTcYH8bkAm2Y8U=
github.com/17media/structs v0.0.0-20200317074636-7872972ebe57/go.mod h1:OYgQ/75ljAEp1bzyw70wLZ7QC6H3Hm2Li941u3rSXG0=
github.com/Azure/azure-sdk-for-go v21.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-autorest v10.15.4+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-ntlmssp v0.0.0-20180810175552-4a21cbd618b4/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
Expand Down Expand Up @@ -96,6 +98,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down

0 comments on commit e082ca6

Please sign in to comment.