Skip to content

Commit

Permalink
Merge pull request #1 from zambien/master
Browse files Browse the repository at this point in the history
pull latest
  • Loading branch information
zkauker authored Nov 11, 2019
2 parents fed07f1 + fcf6e33 commit e632872
Show file tree
Hide file tree
Showing 17 changed files with 759 additions and 110 deletions.
9 changes: 1 addition & 8 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)

default: build

build: vendor fmtcheck
build: fmtcheck
CGO_ENABLED=0 go install -ldflags '-w -extldflags "-static"'

GLIDE := $(shell command -v glide 2> /dev/null)
ifndef GLIDE
$(error "glide is not available. Install using `curl https://glide.sh/get | sh`" or if on Mac `brew install glide`)
endif
vendor: glide.yaml ## Install vendor dependencies
glide update --no-recursive

test: fmtcheck
go test -i $(TEST) || exit 1
echo $(TEST) | \
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ resource "apigee_product" "helloworld_product" {
# See here: http://docs.apigee.com/api-services/content/working-scopes
scopes = ["READ"]
attributes {
attributes = {
access = "public" # this one is needed to expose the proxy. The rest of the attributes are custom attrs. Weird.
custom1 = "customval1"
Expand Down Expand Up @@ -139,7 +139,7 @@ resource "apigee_developer" "helloworld_developer" {
last_name = "thelloworld1" # required
user_name = "helloworld1" # required
attributes { # optional
attributes = { # optional
DisplayName = "my_awesome_app_updated"
Notes = "notes_for_developer_app_updated"
custom_attribute_name = "custom_attribute_value"
Expand All @@ -156,7 +156,7 @@ resource "apigee_developer_app" "helloworld_developer_app" {
callback_url = "https://www.google.com" # optional
key_expires_in = 2592000000 # optional
attributes { # optional
attributes = { # optional
DisplayName = "my_awesome_developer_app"
Notes = "notes_for_awesome_developer_app"
custom_attribute_name = "custom_attribute_value"
Expand All @@ -168,7 +168,7 @@ resource "apigee_company" "helloworld_company" {
name = "helloworld_company" # required
display_name = "some longer description for company" # optional
attributes { # optional
attributes = { # optional
DisplayName = "my-awesome-company"
}
}
Expand Down
22 changes: 18 additions & 4 deletions apigee/resource_api_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strconv"
"time"

"github.com/gofrs/uuid"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -145,10 +146,23 @@ func resourceApiProxyDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*apigee.EdgeClient)

_, _, err := client.Proxies.Delete(d.Get("name").(string))
if err != nil {
log.Printf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
//We have to handle retries in a special way here since this is a DELETE. Note this used to work fine without retries.
deleted := false
tries := 0
for !deleted && tries < 3 {
_, _, err := client.Proxies.Delete(d.Get("name").(string))
if err != nil {
//This is a race condition with Apigee APIs. Wait and try again.
if strings.Contains(err.Error(), "Undeploy the ApiProxy and try again") {
log.Printf("[ERROR] resourceApiProxyDelete proxy still exists. We will wait and try again.")
time.Sleep(5 * time.Second)
} else {
log.Printf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
}
}
deleted = true
tries += tries
}

return nil
Expand Down
17 changes: 13 additions & 4 deletions apigee/resource_api_proxy_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func resourceApiProxyDeploymentRead(d *schema.ResourceData, meta interface{}) (e
client := meta.(*apigee.EdgeClient)

found := false
latestRevision := "0"
matchedRevision := "0"

if deployments, _, err := client.Proxies.GetDeployments(d.Get("proxy_name").(string)); err != nil {
log.Printf("[ERROR] resourceApiProxyDeploymentRead error getting deployments: %s", err.Error())
Expand All @@ -105,16 +105,22 @@ func resourceApiProxyDeploymentRead(d *schema.ResourceData, meta interface{}) (e
//We don't break. Always get the last one if there are multiple deployments.
for _, revision := range environment.Revision {
log.Printf("[DEBUG] resourceApiProxyDeploymentRead checking deployed revision: %#v for expected revision: %#v\n", revision.Number.String(), d.Get("revision").(string))
latestRevision = revision.Number.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", latestRevision)
d.Set("revision", latestRevision)
log.Printf("[DEBUG] resourceApiProxyDeploymentRead - deployment found. Revision is: %#v", matchedRevision)
d.Set("revision", matchedRevision)
} else {
log.Print("[DEBUG] resourceApiProxyDeploymentRead - no deployment found")
d.SetId("")
Expand Down Expand Up @@ -198,6 +204,9 @@ func resourceApiProxyDeploymentUpdate(d *schema.ResourceData, meta interface{})
}
_, _, 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)
Expand Down
25 changes: 19 additions & 6 deletions apigee/resource_api_proxy_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func TestAccProxyDeployment_Updated(t *testing.T) {
resource.TestStep{
Config: testAccCheckProxyDeploymentConfigRequired,
Check: resource.ComposeTestCheckFunc(
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "helloworld"),
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "helloworld"),
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "org", "zambien-trial"),
resource.TestCheckResourceAttr(
Expand All @@ -32,9 +32,9 @@ func TestAccProxyDeployment_Updated(t *testing.T) {
resource.TestStep{
Config: testAccCheckProxyDeploymentConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "helloworld"),
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "helloworld"),
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "org", "zambien-trial"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -71,17 +71,30 @@ func testAccCheckProxyDeploymentExists(n string, name string) resource.TestCheck
}

const testAccCheckProxyDeploymentConfigRequired = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_api_proxy_deployment" "foo_api_proxy_deployment" {
proxy_name = "helloworld"
proxy_name = "${apigee_api_proxy.foo_api_proxy.name}"
org = "zambien-trial"
env = "test"
revision = "1"
}
`

const testAccCheckProxyDeploymentConfigUpdated = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed"
bundle = "test-fixtures/helloworld_proxy2.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy2.zip")}"
}
resource "apigee_api_proxy_deployment" "foo_api_proxy_deployment" {
proxy_name = "helloworld"
proxy_name = "${apigee_api_proxy.foo_api_proxy.name}"
org = "zambien-trial"
env = "test"
revision = "2"
Expand Down
4 changes: 2 additions & 2 deletions apigee/resource_api_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ const testAccCheckProxyConfigRequired = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${base64sha256(file("test-fixtures/helloworld_proxy.zip"))}"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
`

const testAccCheckProxyConfigUpdated = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed_updated"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${base64sha256(file("test-fixtures/helloworld_proxy.zip"))}"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
`

Expand Down
8 changes: 1 addition & 7 deletions apigee/resource_company.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func resourceCompany() *schema.Resource {
},
"apps": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"status": {
Expand Down Expand Up @@ -146,11 +146,6 @@ func setCompanyData(d *schema.ResourceData) (apigee.Company, error) {
d.Set("display_name", d.Get("name"))
}

apps := []string{""}
if d.Get("apps") != nil {
apps = getStringList("apps", d)
}

attributes := []apigee.Attribute{}
if d.Get("attributes") != nil {
attributes = attributesFromMap(d.Get("attributes").(map[string]interface{}))
Expand All @@ -161,7 +156,6 @@ func setCompanyData(d *schema.ResourceData) (apigee.Company, error) {
DisplayName: d.Get("display_name").(string),
Attributes: attributes,

Apps: apps,
Status: d.Get("status").(string),
}

Expand Down
2 changes: 1 addition & 1 deletion apigee/resource_company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const testAccCheckCompanyConfigUpdated = `
resource "apigee_company" "foo_company" {
name = "foo_company_updated"
display_name = "some longer foo description for foo company"
attributes {
attributes = {
DisplayName = "my-awesome-foo-company"
}
}
Expand Down
8 changes: 1 addition & 7 deletions apigee/resource_developer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func resourceDeveloper() *schema.Resource {
},
"apps": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"developer_id": {
Expand Down Expand Up @@ -151,11 +151,6 @@ func setDeveloperData(d *schema.ResourceData) (apigee.Developer, error) {

log.Print("[DEBUG] setDeveloperData START")

apps := []string{""}
if d.Get("apps") != nil {
apps = getStringList("apps", d)
}

attributes := []apigee.Attribute{}
if d.Get("attributes") != nil {
attributes = attributesFromMap(d.Get("attributes").(map[string]interface{}))
Expand All @@ -167,7 +162,6 @@ func setDeveloperData(d *schema.ResourceData) (apigee.Developer, error) {
LastName: d.Get("last_name").(string),
UserName: d.Get("user_name").(string),
Attributes: attributes,
Apps: apps,
}

return Developer, nil
Expand Down
2 changes: 1 addition & 1 deletion apigee/resource_developer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ resource "apigee_developer" "foo_developer" {
first_name = "foo-updated"
last_name = "test-updated"
user_name = "footest-updated"
attributes {
attributes = {
DisplayName = "my-awesome-app-updated"
Notes = "notes_for_developer_app_updated"
custom_attribute_name = "custom_attribute_value_updated"
Expand Down
18 changes: 15 additions & 3 deletions apigee/resource_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAccProduct_Updated(t *testing.T) {
resource.TestCheckResourceAttr(
"apigee_product.foo_product", "api_resources.0", "/**"),
resource.TestCheckResourceAttr(
"apigee_product.foo_product", "proxies.0", "helloworld"),
"apigee_product.foo_product", "proxies.0", "tf_helloworld"),
resource.TestCheckResourceAttr(
"apigee_product.foo_product", "quota", "1000"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -89,29 +89,41 @@ func testAccCheckProductExists(n string, name string) resource.TestCheckFunc {
}

const testAccCheckProductConfigRequired = `
resource "apigee_api_proxy" "tf_helloworld" {
name = "tf_helloworld"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_product" "foo_product" {
name = "foo_product"
approval_type = "manual"
}
`

const testAccCheckProductConfigUpdated = `
resource "apigee_api_proxy" "tf_helloworld" {
name = "tf_helloworld"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_product" "foo_product" {
name = "foo_product_updated"
display_name = "foo_product_updated_different"
description = "no one ever fills this out"
approval_type = "auto"
api_resources = ["/**"]
proxies = ["helloworld"]
proxies = ["${apigee_api_proxy.tf_helloworld.name}"]
quota = "1000"
quota_interval = "2"
quota_time_unit = "minute"
scopes = ["READ"]
attributes {
attributes = {
access = "public"
custom1 = "customval1"
Expand Down
Loading

0 comments on commit e632872

Please sign in to comment.