Skip to content

Commit

Permalink
Add tests steps for update / remove policy, condesne policy setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Wittman committed Apr 17, 2018
1 parent df094cb commit c953fde
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
23 changes: 16 additions & 7 deletions aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"net/url"
"strconv"
"time"

Expand Down Expand Up @@ -79,15 +80,13 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}
description = aws.String(d.Get("description").(string))
}

var policy *string
if d.Get("policy").(string) != "" {
policy = aws.String(d.Get("policy").(string))
}

params := &apigateway.CreateRestApiInput{
Name: aws.String(d.Get("name").(string)),
Description: description,
Policy: policy,
}

if v, ok := d.GetOk("policy"); ok && v.(string) != "" {
params.Policy = aws.String(v.(string))
}

binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
Expand Down Expand Up @@ -164,8 +163,18 @@ func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{})

d.Set("name", api.Name)
d.Set("description", api.Description)
d.Set("policy", api.Policy)

if api.Policy != nil {
policy, err := url.QueryUnescape(*api.Policy)
log.Printf("[DEBUG] Decoded Policy: %s", policy)
if err != nil {
return err
}
if err := d.Set("policy", policy); err != nil {
return err
}
}
log.Printf("[DEBUG] Api Policy %s", d.Get("policy"))
d.Set("binary_media_types", api.BinaryMediaTypes)
if api.MinimumCompressionSize == nil {
d.Set("minimum_compression_size", -1)
Expand Down
38 changes: 36 additions & 2 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
}

func TestAccAWSAPIGatewayRestApi_policy(t *testing.T) {
expectedPolicyText := fmt.Sprintf(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"execute-api:Invoke","Resource":"*"}]}`)

expectedPolicyText := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"execute-api:Invoke","Resource":"*"}]}`
expectedUpdatePolicyText := `{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":{"AWS":"*"},"Action":"execute-api:Invoke","Resource":"*"}]}`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -141,6 +141,18 @@ func TestAccAWSAPIGatewayRestApi_policy(t *testing.T) {
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "policy", expectedPolicyText),
),
},
{
Config: testAccAWSAPIGatewayRestAPIConfigUpdatePolicy,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "policy", expectedUpdatePolicyText),
),
},
{
Config: testAccAWSAPIGatewayRestAPIConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "policy", ""),
),
},
},
})
}
Expand Down Expand Up @@ -338,6 +350,28 @@ EOF
}
`

const testAccAWSAPIGatewayRestAPIConfigUpdatePolicy = `
resource "aws_api_gateway_rest_api" "test" {
name = "bar"
minimum_compression_size = 0
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "execute-api:Invoke",
"Resource": "*"
}
]
}
EOF
}
`

const testAccAWSAPIGatewayRestAPIUpdateConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
Expand Down

0 comments on commit c953fde

Please sign in to comment.