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_glue_crawler: Suppress role difference when using ARN #6293

Merged
merged 2 commits into from
Oct 30, 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
12 changes: 12 additions & 0 deletions aws/resource_aws_glue_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/glue"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -38,6 +40,16 @@ func resourceAwsGlueCrawler() *schema.Resource {
"role": {
Type: schema.TypeString,
Required: true,
// Glue API always returns name
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
newARN, err := arn.Parse(new)

if err != nil {
return false
}

return old == strings.TrimPrefix(newARN.Resource, "role/")
},
},
"description": {
Type: schema.TypeString,
Expand Down
191 changes: 191 additions & 0 deletions aws/resource_aws_glue_crawler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,85 @@ func TestAccAWSGlueCrawler_Description(t *testing.T) {
})
}

func TestAccAWSGlueCrawler_Role_ARN_NoPath(t *testing.T) {
var crawler glue.Crawler
iamRoleResourceName := "aws_iam_role.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_glue_crawler.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGlueCrawlerDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCrawlerConfig_Role_ARN_NoPath(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueCrawlerExists(resourceName, &crawler),
resource.TestCheckResourceAttrPair(resourceName, "role", iamRoleResourceName, "name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSGlueCrawler_Role_ARN_Path(t *testing.T) {
var crawler glue.Crawler
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_glue_crawler.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGlueCrawlerDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCrawlerConfig_Role_ARN_Path(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueCrawlerExists(resourceName, &crawler),
resource.TestCheckResourceAttr(resourceName, "role", fmt.Sprintf("path/%s", rName)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSGlueCrawler_Role_Name_Path(t *testing.T) {
var crawler glue.Crawler
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_glue_crawler.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGlueCrawlerDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCrawlerConfig_Role_Name_Path(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueCrawlerExists(resourceName, &crawler),
resource.TestCheckResourceAttr(resourceName, "role", fmt.Sprintf("path/%s", rName)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSGlueCrawler_Schedule(t *testing.T) {
var crawler glue.Crawler
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -1031,6 +1110,118 @@ resource "aws_glue_crawler" "test" {
`, rName, rName, rName, path1, path2)
}

func testAccGlueCrawlerConfig_Role_ARN_NoPath(rName string) string {
return testAccGlueCrawlerConfig_Base(rName) + fmt.Sprintf(`
resource "aws_glue_catalog_database" "test" {
name = %q
}

resource "aws_glue_crawler" "test" {
depends_on = ["aws_iam_role_policy_attachment.test-AWSGlueServiceRole"]

database_name = "${aws_glue_catalog_database.test.name}"
name = %q
role = "${aws_iam_role.test.arn}"

s3_target {
path = "s3://bucket-name"
}
}
`, rName, rName)
}

func testAccGlueCrawlerConfig_Role_ARN_Path(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %q
path = "/path/"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "glue.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "test-AWSGlueServiceRole" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
role = "${aws_iam_role.test.name}"
}

resource "aws_glue_catalog_database" "test" {
name = %q
}

resource "aws_glue_crawler" "test" {
depends_on = ["aws_iam_role_policy_attachment.test-AWSGlueServiceRole"]

database_name = "${aws_glue_catalog_database.test.name}"
name = %q
role = "${aws_iam_role.test.arn}"

s3_target {
path = "s3://bucket-name"
}
}
`, rName, rName, rName)
}

func testAccGlueCrawlerConfig_Role_Name_Path(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %q
path = "/path/"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "glue.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "test-AWSGlueServiceRole" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
role = "${aws_iam_role.test.name}"
}

resource "aws_glue_catalog_database" "test" {
name = %q
}

resource "aws_glue_crawler" "test" {
depends_on = ["aws_iam_role_policy_attachment.test-AWSGlueServiceRole"]

database_name = "${aws_glue_catalog_database.test.name}"
name = %q
role = "${replace(aws_iam_role.test.path, "/^\\//", "")}${aws_iam_role.test.name}"

s3_target {
path = "s3://bucket-name"
}
}
`, rName, rName, rName)
}

func testAccGlueCrawlerConfig_S3Target(rName, path string) string {
return testAccGlueCrawlerConfig_Base(rName) + fmt.Sprintf(`
resource "aws_glue_catalog_database" "test" {
Expand Down