Skip to content

Commit

Permalink
resolves #40, starting on #32
Browse files Browse the repository at this point in the history
  • Loading branch information
zambien committed Mar 26, 2020
1 parent 0b98d55 commit 57c553f
Show file tree
Hide file tree
Showing 7 changed files with 63 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
18 changes: 15 additions & 3 deletions apigee/helpers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package apigee

import (
"reflect"
"sort"

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

func flattenStringList(list []string) []interface{} {
Expand Down Expand Up @@ -60,6 +60,18 @@ func attributesFromMap(attributes map[string]interface{}) []apigee.Attribute {
return result
}

func mapFromCredentials(credentials []apigee.Credential) []interface{} {

result := make([]interface{}, 0, len(credentials))

for _, elem := range credentials {
credentialMap := structs.Map(elem)
result = append(result, credentialMap)
}

return result
}

func arraySortedEqual(a, b []string) bool {
if len(a) != len(b) {
return false
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
8 changes: 8 additions & 0 deletions apigee/resource_company_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func resourceCompanyApp() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"credentials": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
"scopes": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -108,6 +113,8 @@ func resourceCompanyAppRead(d *schema.ResourceData, meta interface{}) error {
//you might only ever have one credential... we'll see.
scopes := flattenStringList(CompanyAppData.Credentials[0].Scopes)

credentials := mapFromCredentials(CompanyAppData.Credentials)

//Apigee does not return products in the order you send them
oldApiProducts := getStringList("api_products", d)
newApiProducts := apiProductsListFromCredentials(CompanyAppData.Credentials[0].ApiProducts)
Expand All @@ -120,6 +127,7 @@ func resourceCompanyAppRead(d *schema.ResourceData, meta interface{}) error {

d.Set("name", CompanyAppData.Name)
d.Set("attributes", CompanyAppData.Attributes)
d.Set("credentials", credentials)
d.Set("scopes", scopes)
d.Set("callback_url", CompanyAppData.CallbackUrl)
d.Set("app_id", CompanyAppData.AppId)
Expand Down
8 changes: 8 additions & 0 deletions apigee/resource_developer_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func resourceDeveloperApp() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"credentials": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
"scopes": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -116,6 +121,8 @@ func resourceDeveloperAppRead(d *schema.ResourceData, meta interface{}) error {
//you might only ever have one credential... we'll see.
scopes := flattenStringList(DeveloperAppData.Credentials[0].Scopes)

credentials := mapFromCredentials(DeveloperAppData.Credentials)

//Apigee does not return products in the order you send them
oldApiProducts := getStringList("api_products", d)
newApiProducts := apiProductsListFromCredentials(DeveloperAppData.Credentials[0].ApiProducts)
Expand All @@ -128,6 +135,7 @@ func resourceDeveloperAppRead(d *schema.ResourceData, meta interface{}) error {

d.Set("name", DeveloperAppData.Name)
d.Set("attributes", DeveloperAppData.Attributes)
d.Set("credentials", credentials)
d.Set("scopes", scopes)
d.Set("callback_url", DeveloperAppData.CallbackUrl)
d.Set("app_id", DeveloperAppData.AppId)
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 57c553f

Please sign in to comment.