|
8 | 8 | "fmt"
|
9 | 9 | "testing"
|
10 | 10 |
|
| 11 | + "github.com/YakDriver/regexache" |
11 | 12 | "github.com/aws/aws-sdk-go/service/glue"
|
12 | 13 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
13 | 14 | sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
@@ -194,6 +195,38 @@ func TestAccGlueCatalogDatabase_targetDatabaseWithRegion(t *testing.T) {
|
194 | 195 | })
|
195 | 196 | }
|
196 | 197 |
|
| 198 | +func TestAccGlueCatalogDatabase_federatedDatabase(t *testing.T) { |
| 199 | + ctx := acctest.Context(t) |
| 200 | + resourceName := "aws_glue_catalog_database.test" |
| 201 | + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) |
| 202 | + |
| 203 | + resource.ParallelTest(t, resource.TestCase{ |
| 204 | + PreCheck: func() { acctest.PreCheck(ctx, t) }, |
| 205 | + ErrorCheck: acctest.ErrorCheck(t, glue.EndpointsID), |
| 206 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, |
| 207 | + CheckDestroy: testAccCheckDatabaseDestroy(ctx), |
| 208 | + Steps: []resource.TestStep{ |
| 209 | + { |
| 210 | + Config: testAccCatalogDatabaseConfig_federatedDatabase(rName), |
| 211 | + Destroy: false, |
| 212 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 213 | + testAccCheckCatalogDatabaseExists(ctx, resourceName), |
| 214 | + resource.TestCheckResourceAttr(resourceName, "name", rName), |
| 215 | + acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("database/%s", rName)), |
| 216 | + resource.TestCheckResourceAttr(resourceName, "federated_database.#", "1"), |
| 217 | + resource.TestCheckResourceAttr(resourceName, "federated_database.0.connection_name", "aws:redshift"), |
| 218 | + acctest.MatchResourceAttrRegionalARN(resourceName, "federated_database.0.identifier", "redshift", regexache.MustCompile(`datashare:+.`)), |
| 219 | + ), |
| 220 | + }, |
| 221 | + { |
| 222 | + ResourceName: resourceName, |
| 223 | + ImportState: true, |
| 224 | + ImportStateVerify: true, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }) |
| 228 | +} |
| 229 | + |
197 | 230 | func TestAccGlueCatalogDatabase_tags(t *testing.T) {
|
198 | 231 | ctx := acctest.Context(t)
|
199 | 232 | resourceName := "aws_glue_catalog_database.test"
|
@@ -320,6 +353,90 @@ resource "aws_glue_catalog_database" "test" {
|
320 | 353 | `, rName, desc)
|
321 | 354 | }
|
322 | 355 |
|
| 356 | +func testAccCatalogDatabaseConfig_federatedDatabase(rName string) string { |
| 357 | + return acctest.ConfigCompose( |
| 358 | + fmt.Sprintf(` |
| 359 | +data "aws_region" "current" {} |
| 360 | +data "aws_partition" "current" {} |
| 361 | +data "aws_caller_identity" "current" {} |
| 362 | +
|
| 363 | +resource "aws_redshiftserverless_namespace" "test" { |
| 364 | + namespace_name = %[1]q |
| 365 | + db_name = "test" |
| 366 | +} |
| 367 | +
|
| 368 | +resource "aws_redshiftserverless_workgroup" "test" { |
| 369 | + namespace_name = aws_redshiftserverless_namespace.test.namespace_name |
| 370 | + workgroup_name = %[1]q |
| 371 | +} |
| 372 | +
|
| 373 | +resource "aws_redshiftdata_statement" "test_create" { |
| 374 | + workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name |
| 375 | + database = aws_redshiftserverless_namespace.test.db_name |
| 376 | + sql = "CREATE DATASHARE tfacctest;" |
| 377 | +} |
| 378 | +`, rName), |
| 379 | + // Split this resource into a string literal so the terraform `format` function |
| 380 | + // interpolates properly |
| 381 | + ` |
| 382 | +resource "aws_redshiftdata_statement" "test_grant_usage" { |
| 383 | + depends_on = [aws_redshiftdata_statement.test_create] |
| 384 | + workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name |
| 385 | + database = aws_redshiftserverless_namespace.test.db_name |
| 386 | + sql = format("GRANT USAGE ON DATASHARE tfacctest TO ACCOUNT '%s' VIA DATA CATALOG;", data.aws_caller_identity.current.account_id) |
| 387 | +} |
| 388 | +
|
| 389 | +locals { |
| 390 | + # Data share ARN is not returned from the GRANT USAGE statement, so must be |
| 391 | + # composed manually. |
| 392 | + # Ref: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonredshift.html#amazonredshift-resources-for-iam-policies |
| 393 | + data_share_arn = format("arn:%s:redshift:%s:%s:datashare:%s/%s", |
| 394 | + data.aws_partition.current.id, |
| 395 | + data.aws_region.current.name, |
| 396 | + data.aws_caller_identity.current.account_id, |
| 397 | + aws_redshiftserverless_namespace.test.namespace_id, |
| 398 | + "tfacctest", |
| 399 | + ) |
| 400 | +} |
| 401 | +
|
| 402 | +resource "aws_redshift_data_share_authorization" "test" { |
| 403 | + depends_on = [aws_redshiftdata_statement.test_grant_usage] |
| 404 | +
|
| 405 | + data_share_arn = local.data_share_arn |
| 406 | + consumer_identifier = format("DataCatalog/%s", data.aws_caller_identity.current.account_id) |
| 407 | +} |
| 408 | +
|
| 409 | +resource "aws_redshift_data_share_consumer_association" "test" { |
| 410 | + depends_on = [aws_redshift_data_share_authorization.test] |
| 411 | +
|
| 412 | + data_share_arn = local.data_share_arn |
| 413 | + consumer_arn = format("arn:%s:glue:%s:%s:catalog", |
| 414 | + data.aws_partition.current.id, |
| 415 | + data.aws_region.current.name, |
| 416 | + data.aws_caller_identity.current.account_id, |
| 417 | + ) |
| 418 | +} |
| 419 | +
|
| 420 | +resource "aws_lakeformation_resource" "test" { |
| 421 | + depends_on = [aws_redshift_data_share_consumer_association.test] |
| 422 | +
|
| 423 | + arn = local.data_share_arn |
| 424 | + use_service_linked_role = false |
| 425 | +} |
| 426 | +`, |
| 427 | + fmt.Sprintf(` |
| 428 | +resource "aws_glue_catalog_database" "test" { |
| 429 | + depends_on = [aws_lakeformation_resource.test] |
| 430 | +
|
| 431 | + name = %[1]q |
| 432 | + federated_database { |
| 433 | + connection_name = "aws:redshift" |
| 434 | + identifier = local.data_share_arn |
| 435 | + } |
| 436 | +} |
| 437 | +`, rName)) |
| 438 | +} |
| 439 | + |
323 | 440 | func testAccCatalogDatabaseConfig_target(rName string) string {
|
324 | 441 | return fmt.Sprintf(`
|
325 | 442 | resource "aws_glue_catalog_database" "test" {
|
|
0 commit comments