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

Issue #5774 - Fix the update issues in aws_appsync_datasource #5814

Merged
merged 4 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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: 5 additions & 7 deletions aws/resource_aws_appsync_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,16 @@ func resourceAwsAppsyncDatasourceUpdate(d *schema.ResourceData, meta interface{}
input := &appsync.UpdateDataSourceInput{
ApiId: aws.String(d.Get("api_id").(string)),
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
}

if v, ok := d.GetOk("service_role_arn"); ok {
input.ServiceRoleArn = aws.String(v.(string))
}

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("service_role_arn") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given this type of change, I'm worried that all parameters must be provided during the update call. We should probably add an acceptance test that has something optional like description set, then attempts to update something else like dynamodb_config, then verifies that description is still the original value as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bflad Reverted the changes which I did for service_role_arn attr to provide the proper solution.
As you said, looks like all parameters should be provided during the update call and covered this issue with acceptance test TestAccAwsAppsyncDatasource_update, working on fix.

Copy link
Contributor Author

@saravanan30erd saravanan30erd Sep 19, 2018

Choose a reason for hiding this comment

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

@bflad Could you guide me the way how to handle this?
Since all parameters need to be provided in update call, I am thinking to use Get funcs.

input.ServiceRoleArn = aws.String(d.Get("service_role_arn").(string))
}
if d.HasChange("type") {
input.Type = aws.String(d.Get("type").(string))
}

if d.HasChange("dynamodb_config") {
ddbconfig := d.Get("dynamodb_config").([]interface{})
if len(ddbconfig) > 0 {
Expand Down
103 changes: 102 additions & 1 deletion aws/resource_aws_appsync_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestAccAwsAppsyncDatasource_lambda(t *testing.T) {
})
}

func TestAccAwsAppsyncDatasource_update(t *testing.T) {
func TestAccAwsAppsyncDatasource_updateType(t *testing.T) {
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -89,6 +89,31 @@ func TestAccAwsAppsyncDatasource_update(t *testing.T) {
})
}

func TestAccAwsAppsyncDatasource_update(t *testing.T) {
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsAppsyncDatasourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppsyncDatasourceConfig_ddb(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource version1"),
),
},
{
Config: testAccAppsyncDatasourceConfig_update_ddb(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource version2"),
),
},
},
})
}

func testAccCheckAwsAppsyncDatasourceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).appsyncconn
for _, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -188,6 +213,7 @@ resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = "tf_appsync_%s"
type = "AMAZON_DYNAMODB"
description = "appsync datasource version1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Generally its cleaner to parameterize the test configuration values when changing a single one instead of creating a mostly duplicated new test configuration, e.g.

func testAccAppsyncDatasourceConfig_ddb(rName, description string) string {
// ...
description = %q
// ...
`, rName, rName, rName, rName, rName, rName, description)
}

dynamodb_config {
region = "${data.aws_region.current.name}"
table_name = "${aws_dynamodb_table.test.name}"
Expand Down Expand Up @@ -441,3 +467,78 @@ resource "aws_appsync_datasource" "test" {
}
`, rName, rName, rName, rName, rName, rName)
}

func testAccAppsyncDatasourceConfig_update_ddb(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_appsync_graphql_api" "test" {
authentication_type = "API_KEY"
name = "tf_appsync_%s"
}

resource "aws_dynamodb_table" "test" {
name = "tf-ddb1-%s"
read_capacity = 10
write_capacity = 10
hash_key = "UserId"
attribute {
name = "UserId"
type = "S"
}
}

resource "aws_iam_role" "test" {
name = "tf-role-%s"

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

resource "aws_iam_role_policy" "test" {
name = "tf-rolepolicy-%s"
role = "${aws_iam_role.test.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"dynamodb:*"
],
"Effect": "Allow",
"Resource": [
"${aws_dynamodb_table.test.arn}"
]
}
]
}
EOF
}

resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = "tf_appsync_%s"
type = "AMAZON_DYNAMODB"
description = "appsync datasource version2"
dynamodb_config {
region = "${data.aws_region.current.name}"
table_name = "${aws_dynamodb_table.test.name}"
}
service_role_arn = "${aws_iam_role.test.arn}"
}
`, rName, rName, rName, rName, rName)
}