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

resource/aws_ssm_document: Fix Deletion of Privately Shared Documents #5668

Merged
merged 2 commits into from
Aug 27, 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
11 changes: 10 additions & 1 deletion aws/resource_aws_ssm_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,19 @@ func deleteDocumentPermissions(d *schema.ResourceData, meta interface{}) error {

log.Printf("[INFO] Removing permissions from document: %s", d.Id())

permission := d.Get("permissions").(map[string]interface{})
var accountsToRemove []*string
if permission["account_ids"] != nil {
accountsToRemove = aws.StringSlice([]string{permission["account_ids"].(string)})
if strings.Contains(permission["account_ids"].(string), ",") {
accountsToRemove = aws.StringSlice(strings.Split(permission["account_ids"].(string), ","))
}
}

permInput := &ssm.ModifyDocumentPermissionInput{
Name: aws.String(d.Get("name").(string)),
PermissionType: aws.String("Share"),
AccountIdsToRemove: aws.StringSlice(strings.Split("all", ",")),
AccountIdsToRemove: accountsToRemove,
}

_, err := ssmconn.ModifyDocumentPermission(permInput)
Expand Down
60 changes: 57 additions & 3 deletions aws/resource_aws_ssm_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func TestAccAWSSSMDocument_update(t *testing.T) {
})
}

func TestAccAWSSSMDocument_permission(t *testing.T) {
func TestAccAWSSSMDocument_permission_public(t *testing.T) {
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMDocumentPermissionConfig(name),
Config: testAccAWSSSMDocumentPublicPermissionConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
resource.TestCheckResourceAttr(
Expand All @@ -88,6 +88,26 @@ func TestAccAWSSSMDocument_permission(t *testing.T) {
})
}

func TestAccAWSSSMDocument_permission_private(t *testing.T) {
name := acctest.RandString(10)
ids := "123456789012"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMDocumentPrivatePermissionConfig(name, ids),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
resource.TestCheckResourceAttr(
"aws_ssm_document.foo", "permissions.type", "Share"),
),
},
},
})
}

func TestAccAWSSSMDocument_params(t *testing.T) {
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -371,7 +391,7 @@ DOC
`, rName)
}

func testAccAWSSSMDocumentPermissionConfig(rName string) string {
func testAccAWSSSMDocumentPublicPermissionConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
Expand Down Expand Up @@ -405,6 +425,40 @@ DOC
`, rName)
}

func testAccAWSSSMDocumentPrivatePermissionConfig(rName string, rIds string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
document_type = "Command"

permissions = {
type = "Share"
account_ids = "%s"
}

content = <<DOC
{
"schemaVersion": "1.2",
"description": "Check ip configuration of a Linux instance.",
"parameters": {

},
"runtimeConfig": {
"aws:runShellScript": {
"properties": [
{
"id": "0.aws:runShellScript",
"runCommand": ["ifconfig"]
}
]
}
}
}
DOC
}
`, rName, rIds)
}

func testAccAWSSSMDocumentParamConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
Expand Down