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 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
37 changes: 15 additions & 22 deletions aws/resource_aws_appsync_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,35 +194,28 @@ 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 d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("service_role_arn") {
input.ServiceRoleArn = aws.String(d.Get("service_role_arn").(string))
if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}
if d.HasChange("type") {
input.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("dynamodb_config") {
ddbconfig := d.Get("dynamodb_config").([]interface{})
if len(ddbconfig) > 0 {
input.DynamodbConfig = expandAppsyncDynamodbDataSourceConfig(ddbconfig[0].(map[string]interface{}))
}
ddbconfig := d.Get("dynamodb_config").([]interface{})
if len(ddbconfig) > 0 {
input.DynamodbConfig = expandAppsyncDynamodbDataSourceConfig(ddbconfig[0].(map[string]interface{}))
}
if d.HasChange("elasticsearch_config") {
esconfig := d.Get("elasticsearch_config").([]interface{})
if len(esconfig) > 0 {
input.ElasticsearchConfig = expandAppsyncElasticsearchDataSourceConfig(esconfig[0].(map[string]interface{}))
}
esconfig := d.Get("elasticsearch_config").([]interface{})
if len(esconfig) > 0 {
input.ElasticsearchConfig = expandAppsyncElasticsearchDataSourceConfig(esconfig[0].(map[string]interface{}))
}
if d.HasChange("lambda_config") {
lambdaconfig := d.Get("lambda_config").([]interface{})
if len(lambdaconfig) > 0 {
input.LambdaConfig = expandAppsyncLambdaDataSourceConfig(lambdaconfig[0].(map[string]interface{}))
}
lambdaconfig := d.Get("lambda_config").([]interface{})
if len(lambdaconfig) > 0 {
input.LambdaConfig = expandAppsyncLambdaDataSourceConfig(lambdaconfig[0].(map[string]interface{}))
}

_, err := conn.UpdateDataSource(input)
Expand Down
125 changes: 34 additions & 91 deletions aws/resource_aws_appsync_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ func TestAccAwsAppsyncDatasource_es(t *testing.T) {
}

func TestAccAwsAppsyncDatasource_lambda(t *testing.T) {
Desc := "appsync datasource"
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, starting variable names with a capital letter is special in Go (makes them exportable). While it shouldn't matter in this case, its generally best practice to lowerCamelCase unless otherwise necessary.

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsAppsyncDatasourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppsyncDatasourceConfig_lambda(acctest.RandString(5)),
Config: testAccAppsyncDatasourceConfig_lambda(acctest.RandString(5), Desc),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttrSet("aws_appsync_datasource.test", "arn"),
Expand All @@ -75,14 +76,41 @@ func TestAccAwsAppsyncDatasource_update(t *testing.T) {
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttrSet("aws_appsync_datasource.test", "arn"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "type", "AMAZON_DYNAMODB"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource"),
),
},
{
Config: testAccAppsyncDatasourceConfig_update_lambda(rName),
Config: testAccAppsyncDatasourceConfig_lambda(rName, "appsync datasource"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttrSet("aws_appsync_datasource.test", "arn"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "type", "AWS_LAMBDA"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource"),
),
},
},
})
}

func TestAccAwsAppsyncDatasource_updateDescription(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_lambda(rName, "appsync datasource"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource"),
),
},
{
Config: testAccAppsyncDatasourceConfig_lambda(rName, "appsync datasource v1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncDatasourceExists("aws_appsync_datasource.test"),
resource.TestCheckResourceAttr("aws_appsync_datasource.test", "description", "appsync datasource v1"),
),
},
},
Expand Down Expand Up @@ -188,6 +216,7 @@ resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = "tf_appsync_%s"
type = "AMAZON_DYNAMODB"
description = "appsync datasource"
dynamodb_config {
region = "${data.aws_region.current.name}"
table_name = "${aws_dynamodb_table.test.name}"
Expand Down Expand Up @@ -268,94 +297,7 @@ resource "aws_appsync_datasource" "test" {
`, rName, rName, rName, rName, rName)
}

func testAccAppsyncDatasourceConfig_lambda(rName string) string {
return fmt.Sprintf(`
resource "aws_appsync_graphql_api" "test" {
authentication_type = "API_KEY"
name = "tf_appsync_%s"
}

resource "aws_iam_role" "test_lambda" {
name = "tf-lambdarole-%s"

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

resource "aws_lambda_function" "test" {
function_name = "tf-lambda-%s"
filename = "test-fixtures/lambdatest.zip"
role = "${aws_iam_role.test_lambda.arn}"
handler = "exports.test"
runtime = "nodejs6.10"
}

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": [
"lambda:*"
],
"Effect": "Allow",
"Resource": [
"${aws_lambda_function.test.arn}"
]
}
]
}
EOF
}

resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = "tf_appsync_%s"
type = "AWS_LAMBDA"
lambda_config {
function_arn = "${aws_lambda_function.test.arn}"
}
service_role_arn = "${aws_iam_role.test.arn}"
}
`, rName, rName, rName, rName, rName, rName)
}

func testAccAppsyncDatasourceConfig_update_lambda(rName string) string {
func testAccAppsyncDatasourceConfig_lambda(rName, Desc string) string {
return fmt.Sprintf(`
resource "aws_appsync_graphql_api" "test" {
authentication_type = "API_KEY"
Expand Down Expand Up @@ -434,10 +376,11 @@ resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = "tf_appsync_%s"
type = "AWS_LAMBDA"
description = "%s"
lambda_config {
function_arn = "${aws_lambda_function.test.arn}"
}
service_role_arn = "${aws_iam_role.test_applambda.arn}"
}
`, rName, rName, rName, rName, rName, rName)
`, rName, rName, rName, rName, rName, rName, Desc)
}