Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add policy attribute to aws_api_gateway_rest_api #4211

Merged
merged 10 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 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 @@ -32,6 +33,13 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Optional: true,
},

"policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
},

"binary_media_types": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -77,6 +85,10 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}
Description: description,
}

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

binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
if binaryMediaTypesOk {
params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
Expand Down Expand Up @@ -151,6 +163,18 @@ func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{})

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

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 Expand Up @@ -183,6 +207,14 @@ func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*api
})
}

if d.HasChange("policy") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/policy"),
Value: aws.String(d.Get("policy").(string)),
})
}

if d.HasChange("minimum_compression_size") {
minimumCompressionSize := d.Get("minimum_compression_size").(int)
var value string
Expand Down
74 changes: 74 additions & 0 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
})
}

func TestAccAWSAPIGatewayRestApi_policy(t *testing.T) {
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,
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayRestAPIConfigWithPolicy,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "policy", expectedPolicyText),
),
},
Copy link
Contributor

@bflad bflad Apr 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add test steps that attempt to:

  • update the policy
  • remove the policy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test steps for update and removal have been added

{
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", ""),
),
},
},
})
}

func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) {
var conf apigateway.RestApi

Expand Down Expand Up @@ -298,6 +328,50 @@ resource "aws_api_gateway_rest_api" "test" {
}
`

const testAccAWSAPIGatewayRestAPIConfigWithPolicy = `
resource "aws_api_gateway_rest_api" "test" {
name = "bar"
minimum_compression_size = 0
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "execute-api:Invoke",
"Resource": "*"
}
]
}
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
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The following arguments are supported:
* `binary_media_types` - (Optional) The list of binary media types supported by the RestApi. By default, the RestApi supports only UTF-8-encoded text payloads.
* `minimum_compression_size` - (Optional) Minimum response size to compress for the REST API. Integer between -1 and 10485760 (10MB). Setting a value greater than -1 will enable compression, -1 disables compression (default).
* `body` - (Optional) An OpenAPI specification that defines the set of routes and integrations to create as part of the REST API.
* `policy` - (Optional) JSON formatted policy document that controls access to the API Gateway

__Note__: If the `body` argument is provided, the OpenAPI specification will be used to configure the resources, methods and integrations for the Rest API. If this argument is provided, the following resources should not be managed as separate ones, as updates may cause manual resource updates to be overwritten:

Expand Down