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

r/glue_catalog_database - add target_database argument #19371

Merged
merged 5 commits into from
May 17, 2021
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
3 changes: 3 additions & 0 deletions .changelog/19371.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_glue_catalog_database: Add `target_database` argument
```
68 changes: 67 additions & 1 deletion aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource {
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 255),
validation.StringDoesNotMatch(regexp.MustCompile(`[A-Z]`), "uppercase charcters cannot be used"),
validation.StringDoesNotMatch(regexp.MustCompile(`[A-Z]`), "uppercase characters cannot be used"),
),
},
"description": {
Expand All @@ -57,6 +57,24 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"target_database": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"catalog_id": {
Type: schema.TypeString,
Required: true,
},
"database_name": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
Expand All @@ -82,6 +100,10 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface
dbInput.Parameters = expandStringMap(v.(map[string]interface{}))
}

if v, ok := d.GetOk("target_database"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
dbInput.TargetDatabase = expandGlueDatabaseTargetDatabase(v.([]interface{})[0].(map[string]interface{}))
}

input := &glue.CreateDatabaseInput{
CatalogId: aws.String(catalogID),
DatabaseInput: dbInput,
Expand Down Expand Up @@ -177,6 +199,14 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
d.Set("location_uri", database.LocationUri)
d.Set("parameters", aws.StringValueMap(database.Parameters))

if database.TargetDatabase != nil {
if err := d.Set("target_database", []interface{}{flattenGlueDatabaseTargetDatabase(database.TargetDatabase)}); err != nil {
return fmt.Errorf("error setting target_database: %w", err)
}
} else {
d.Set("target_database", nil)
}

return nil
}

Expand Down Expand Up @@ -210,3 +240,39 @@ func createAwsGlueCatalogID(d *schema.ResourceData, accountid string) (catalogID
}
return
}

func expandGlueDatabaseTargetDatabase(tfMap map[string]interface{}) *glue.DatabaseIdentifier {
if tfMap == nil {
return nil
}

apiObject := &glue.DatabaseIdentifier{}

if v, ok := tfMap["catalog_id"].(string); ok && v != "" {
apiObject.CatalogId = aws.String(v)
}

if v, ok := tfMap["database_name"].(string); ok && v != "" {
apiObject.DatabaseName = aws.String(v)
}

return apiObject
}

func flattenGlueDatabaseTargetDatabase(apiObject *glue.DatabaseIdentifier) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.CatalogId; v != nil {
tfMap["catalog_id"] = aws.StringValue(v)
}

if v := apiObject.DatabaseName; v != nil {
tfMap["database_name"] = aws.StringValue(v)
}

return tfMap
}
50 changes: 49 additions & 1 deletion aws/resource_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
testAccCheckGlueCatalogDatabaseExists(resourceName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("database/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "description", ""), resource.TestCheckResourceAttr(resourceName, "location_uri", ""),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "location_uri", ""),
resource.TestCheckResourceAttr(resourceName, "parameters.%", "0"),
resource.TestCheckResourceAttr(resourceName, "target_database.#", "0"),
),
},
{
Expand Down Expand Up @@ -114,6 +116,35 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
})
}

func TestAccAWSGlueCatalogDatabase_targetDatabase(t *testing.T) {
resourceName := "aws_glue_catalog_database.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, glue.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckGlueDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCatalogDatabaseConfigTargetDatabase(rName),
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "target_database.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "target_database.0.catalog_id", "aws_glue_catalog_database.test2", "catalog_id"),
resource.TestCheckResourceAttrPair(resourceName, "target_database.0.database_name", "aws_glue_catalog_database.test2", "name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSGlueCatalogDatabase_disappears(t *testing.T) {
resourceName := "aws_glue_catalog_database.test"
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -190,6 +221,23 @@ resource "aws_glue_catalog_database" "test" {
`, rName, desc)
}

func testAccGlueCatalogDatabaseConfigTargetDatabase(rName string) string {
return fmt.Sprintf(`
resource "aws_glue_catalog_database" "test" {
name = %[1]q

target_database {
catalog_id = aws_glue_catalog_database.test2.catalog_id
database_name = aws_glue_catalog_database.test2.name
}
}

resource "aws_glue_catalog_database" "test2" {
name = "%[1]s-2"
}
`, rName)
}

func testAccCheckGlueCatalogDatabaseExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down
14 changes: 10 additions & 4 deletions website/docs/r/glue_catalog_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ resource "aws_glue_catalog_database" "aws_glue_catalog_database" {

The following arguments are supported:

* `name` - (Required) The name of the database. The acceptable characters are lowercase letters, numbers, and the underscore character.
* `catalog_id` - (Optional) ID of the Glue Catalog to create the database in. If omitted, this defaults to the AWS Account ID.
* `description` - (Optional) Description of the database.
* `location_uri` - (Optional) The location of the database (for example, an HDFS path).
* `parameters` - (Optional) A list of key-value pairs that define parameters and properties of the database.
* `location_uri` - (Optional) Location of the database (for example, an HDFS path).
* `name` - (Required) Name of the database. The acceptable characters are lowercase letters, numbers, and the underscore character.
* `parameters` - (Optional) List of key-value pairs that define parameters and properties of the database.
* `target_database` - (Optional) Configuration block for a target database for resource linking. See [`target_database`](#target_database) below.

### target_database

* `catalog_id` - (Required) ID of the Data Catalog in which the database resides.
* `database_name` - (Required) Name of the catalog database.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - ARN of the Glue Catalog Database.
* `id` - Catalog ID and name of the database
* `arn` - The ARN of the Glue Catalog Database.

## Import

Expand Down