-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'atsushi-ishibashi-media_store_container_policy'
- Loading branch information
Showing
5 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/mediastore" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsMediaStoreContainerPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsMediaStoreContainerPolicyPut, | ||
Read: resourceAwsMediaStoreContainerPolicyRead, | ||
Update: resourceAwsMediaStoreContainerPolicyPut, | ||
Delete: resourceAwsMediaStoreContainerPolicyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"container_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"policy": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateIAMPolicyJson, | ||
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsMediaStoreContainerPolicyPut(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).mediastoreconn | ||
|
||
input := &mediastore.PutContainerPolicyInput{ | ||
ContainerName: aws.String(d.Get("container_name").(string)), | ||
Policy: aws.String(d.Get("policy").(string)), | ||
} | ||
|
||
_, err := conn.PutContainerPolicy(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(d.Get("container_name").(string)) | ||
return resourceAwsMediaStoreContainerPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsMediaStoreContainerPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).mediastoreconn | ||
|
||
input := &mediastore.GetContainerPolicyInput{ | ||
ContainerName: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetContainerPolicy(input) | ||
if err != nil { | ||
if isAWSErr(err, mediastore.ErrCodeContainerNotFoundException, "") { | ||
log.Printf("[WARN] MediaContainer Policy %q not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
if isAWSErr(err, mediastore.ErrCodePolicyNotFoundException, "") { | ||
log.Printf("[WARN] MediaContainer Policy %q not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("container_name", d.Id()) | ||
d.Set("policy", resp.Policy) | ||
return nil | ||
} | ||
|
||
func resourceAwsMediaStoreContainerPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).mediastoreconn | ||
|
||
input := &mediastore.DeleteContainerPolicyInput{ | ||
ContainerName: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DeleteContainerPolicy(input) | ||
if err != nil { | ||
if isAWSErr(err, mediastore.ErrCodeContainerNotFoundException, "") { | ||
return nil | ||
} | ||
if isAWSErr(err, mediastore.ErrCodePolicyNotFoundException, "") { | ||
return nil | ||
} | ||
// if isAWSErr(err, mediastore.ErrCodeContainerInUseException, "Container must be ACTIVE in order to perform this operation") { | ||
// return nil | ||
// } | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/mediastore" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSMediaStoreContainerPolicy_basic(t *testing.T) { | ||
rname := acctest.RandString(5) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsMediaStoreContainerPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMediaStoreContainerPolicyConfig(rname, acctest.RandString(5)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsMediaStoreContainerPolicyExists("aws_media_store_container_policy.test"), | ||
resource.TestCheckResourceAttrSet("aws_media_store_container_policy.test", "container_name"), | ||
resource.TestCheckResourceAttrSet("aws_media_store_container_policy.test", "policy"), | ||
), | ||
}, | ||
{ | ||
Config: testAccMediaStoreContainerPolicyConfig(rname, acctest.RandString(5)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsMediaStoreContainerPolicyExists("aws_media_store_container_policy.test"), | ||
resource.TestCheckResourceAttrSet("aws_media_store_container_policy.test", "container_name"), | ||
resource.TestCheckResourceAttrSet("aws_media_store_container_policy.test", "policy"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSMediaStoreContainerPolicy_import(t *testing.T) { | ||
resourceName := "aws_media_store_container_policy.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsMediaStoreContainerPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccMediaStoreContainerPolicyConfig(acctest.RandString(5), acctest.RandString(5)), | ||
}, | ||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsMediaStoreContainerPolicyDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).mediastoreconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_media_store_container_policy" { | ||
continue | ||
} | ||
|
||
input := &mediastore.GetContainerPolicyInput{ | ||
ContainerName: aws.String(rs.Primary.ID), | ||
} | ||
|
||
_, err := conn.GetContainerPolicy(input) | ||
if err != nil { | ||
if isAWSErr(err, mediastore.ErrCodeContainerNotFoundException, "") { | ||
return nil | ||
} | ||
if isAWSErr(err, mediastore.ErrCodePolicyNotFoundException, "") { | ||
return nil | ||
} | ||
if isAWSErr(err, mediastore.ErrCodeContainerInUseException, "Container must be ACTIVE in order to perform this operation") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return fmt.Errorf("Expected MediaStore Container Policy to be destroyed, %s found", rs.Primary.ID) | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckAwsMediaStoreContainerPolicyExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).mediastoreconn | ||
|
||
input := &mediastore.GetContainerPolicyInput{ | ||
ContainerName: aws.String(rs.Primary.ID), | ||
} | ||
|
||
_, err := conn.GetContainerPolicy(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccMediaStoreContainerPolicyConfig(rName, sid string) string { | ||
return fmt.Sprintf(` | ||
data "aws_region" "current" {} | ||
data "aws_caller_identity" "current" {} | ||
resource "aws_media_store_container" "test" { | ||
name = "tf_mediastore_%s" | ||
} | ||
resource "aws_media_store_container_policy" "test" { | ||
container_name = "${aws_media_store_container.test.name}" | ||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [{ | ||
"Sid": "%s", | ||
"Action": [ "mediastore:*" ], | ||
"Principal": {"AWS" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"}, | ||
"Effect": "Allow", | ||
"Resource": "arn:aws:mediastore:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:container/${aws_media_store_container.test.name}/*", | ||
"Condition": { | ||
"Bool": { "aws:SecureTransport": "true" } | ||
} | ||
}] | ||
} | ||
EOF | ||
} | ||
`, rName, sid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_media_store_container_policy" | ||
sidebar_current: "docs-aws-resource-media-store-container-policy" | ||
description: |- | ||
Provides a MediaStore Container Policy. | ||
--- | ||
|
||
# aws_media_store_container_policy | ||
|
||
Provides a MediaStore Container Policy. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_region" "current" {} | ||
data "aws_caller_identity" "current" {} | ||
resource "aws_media_store_container" "example" { | ||
name = "example" | ||
} | ||
resource "aws_media_store_container_policy" "example" { | ||
container_name = "${aws_media_store_container.example.name}" | ||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [{ | ||
"Sid": "MediaStoreFullAccess", | ||
"Action": [ "mediastore:*" ], | ||
"Principal": {"AWS" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"}, | ||
"Effect": "Allow", | ||
"Resource": "arn:aws:mediastore:${data.aws_caller_identity.current.account_id}:${data.aws_region.current.name}:container/${aws_media_store_container.example.name}/*", | ||
"Condition": { | ||
"Bool": { "aws:SecureTransport": "true" } | ||
} | ||
}] | ||
} | ||
EOF | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `container_name` - (Required) The name of the container. | ||
* `policy` - (Required) The contents of the policy. | ||
|
||
## Import | ||
|
||
MediaStore Container Policy can be imported using the MediaStore Container Name, e.g. | ||
|
||
``` | ||
$ terraform import aws_media_store_container_policy.example example | ||
``` |