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 arn + disappears test #13452

Merged
merged 5 commits into from
Jun 23, 2020
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
30 changes: 22 additions & 8 deletions aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"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-plugin-sdk/helper/schema"
Expand All @@ -22,6 +23,10 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"catalog_id": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -51,7 +56,7 @@ func resourceAwsGlueCatalogDatabase() *schema.Resource {
}

func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
glueconn := meta.(*AWSClient).glueconn
conn := meta.(*AWSClient).glueconn
catalogID := createAwsGlueCatalogID(d, meta.(*AWSClient).accountid)
name := d.Get("name").(string)

Expand All @@ -62,7 +67,7 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface
},
}

_, err := glueconn.CreateDatabase(input)
_, err := conn.CreateDatabase(input)
if err != nil {
return fmt.Errorf("Error creating Catalog Database: %s", err)
}
Expand All @@ -73,7 +78,7 @@ func resourceAwsGlueCatalogDatabaseCreate(d *schema.ResourceData, meta interface
}

func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
glueconn := meta.(*AWSClient).glueconn
conn := meta.(*AWSClient).glueconn

catalogID, name, err := readAwsGlueCatalogID(d.Id())
if err != nil {
Expand Down Expand Up @@ -108,7 +113,7 @@ func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface
dbUpdateInput.DatabaseInput = dbInput

if d.HasChanges("description", "location_uri", "parameters") {
if _, err := glueconn.UpdateDatabase(dbUpdateInput); err != nil {
if _, err := conn.UpdateDatabase(dbUpdateInput); err != nil {
return err
}
}
Expand All @@ -117,7 +122,7 @@ func resourceAwsGlueCatalogDatabaseUpdate(d *schema.ResourceData, meta interface
}

func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}) error {
glueconn := meta.(*AWSClient).glueconn
conn := meta.(*AWSClient).glueconn

catalogID, name, err := readAwsGlueCatalogID(d.Id())
if err != nil {
Expand All @@ -129,7 +134,7 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
Name: aws.String(name),
}

out, err := glueconn.GetDatabase(input)
out, err := conn.GetDatabase(input)
if err != nil {

if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
Expand All @@ -141,6 +146,15 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error reading Glue Catalog Database: %s", err.Error())
}

databaseArn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "glue",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
Resource: fmt.Sprintf("database/%s", aws.StringValue(out.Database.Name)),
}.String()
d.Set("arn", databaseArn)

d.Set("name", out.Database.Name)
d.Set("catalog_id", catalogID)
d.Set("description", out.Database.Description)
Expand All @@ -158,14 +172,14 @@ func resourceAwsGlueCatalogDatabaseRead(d *schema.ResourceData, meta interface{}
}

func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
glueconn := meta.(*AWSClient).glueconn
conn := meta.(*AWSClient).glueconn
catalogID, name, err := readAwsGlueCatalogID(d.Id())
if err != nil {
return err
}

log.Printf("[DEBUG] Glue Catalog Database: %s:%s", catalogID, name)
_, err = glueconn.DeleteDatabase(&glue.DeleteDatabaseInput{
_, err = conn.DeleteDatabase(&glue.DeleteDatabaseInput{
Name: aws.String(name),
})
if err != nil {
Expand Down
106 changes: 18 additions & 88 deletions aws/resource_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,10 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
resource.TestCheckResourceAttr(
resourceName,
"name",
rName,
),
resource.TestCheckResourceAttr(
resourceName,
"description",
"",
),
resource.TestCheckResourceAttr(
resourceName,
"location_uri",
"",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.%",
"0",
),
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, "parameters.%", "0"),
),
},
{
Expand All @@ -58,69 +42,29 @@ func TestAccAWSGlueCatalogDatabase_full(t *testing.T) {
Destroy: false,
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
resource.TestCheckResourceAttr(
resourceName,
"description",
"A test catalog from terraform",
),
resource.TestCheckResourceAttr(
resourceName,
"location_uri",
"my-location",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param1",
"value1",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param2",
"true",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param3",
"50",
),
resource.TestCheckResourceAttr(resourceName, "description", "A test catalog from terraform"),
resource.TestCheckResourceAttr(resourceName, "location_uri", "my-location"),
resource.TestCheckResourceAttr(resourceName, "parameters.param1", "value1"),
resource.TestCheckResourceAttr(resourceName, "parameters.param2", "true"),
resource.TestCheckResourceAttr(resourceName, "parameters.param3", "50"),
),
},
{
Config: testAccGlueCatalogDatabase_full(rName, "An updated test catalog from terraform"),
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
resource.TestCheckResourceAttr(
resourceName,
"description",
"An updated test catalog from terraform",
),
resource.TestCheckResourceAttr(
resourceName,
"location_uri",
"my-location",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param1",
"value1",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param2",
"true",
),
resource.TestCheckResourceAttr(
resourceName,
"parameters.param3",
"50",
),
resource.TestCheckResourceAttr(resourceName, "description", "An updated test catalog from terraform"),
resource.TestCheckResourceAttr(resourceName, "location_uri", "my-location"),
resource.TestCheckResourceAttr(resourceName, "parameters.param1", "value1"),
resource.TestCheckResourceAttr(resourceName, "parameters.param2", "true"),
resource.TestCheckResourceAttr(resourceName, "parameters.param3", "50"),
),
},
},
})
}

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

Expand All @@ -133,23 +77,9 @@ func TestAccAWSGlueCatalogDatabase_recreates(t *testing.T) {
Config: testAccGlueCatalogDatabase_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGlueCatalogDatabaseExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsGlueCatalogDatabase(), resourceName),
),
},
{
// Simulate deleting the database outside Terraform
PreConfig: func() {
conn := testAccProvider.Meta().(*AWSClient).glueconn
input := &glue.DeleteDatabaseInput{
Name: aws.String(rName),
}
_, err := conn.DeleteDatabase(input)
if err != nil {
t.Fatalf("error deleting Glue Catalog Database: %s", err)
}
},
Config: testAccGlueCatalogDatabase_basic(rName),
ExpectNonEmptyPlan: true,
PlanOnly: true,
},
},
})
Expand Down Expand Up @@ -239,9 +169,9 @@ func testAccCheckGlueCatalogDatabaseExists(name string) resource.TestCheckFunc {
return fmt.Errorf("No Glue Database Found")
}

if *out.Database.Name != dbName {
if aws.StringValue(out.Database.Name) != dbName {
return fmt.Errorf("Glue Database Mismatch - existing: %q, state: %q",
*out.Database.Name, dbName)
aws.StringValue(out.Database.Name), dbName)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ for more information about connecting to alternate AWS endpoints or AWS compatib
- [`aws_elasticache_cluster` resource](/docs/providers/aws/r/elasticache_cluster.html)
- [`aws_elb` data source](/docs/providers/aws/d/elb.html)
- [`aws_elb` resource](/docs/providers/aws/r/elb.html)
- [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html)
- [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html)
- [`aws_glue_catalog_database` resource](/docs/providers/aws/r/glue_catalog_database.html)
- [`aws_glue_catalog_table` resource](/docs/providers/aws/r/glue_catalog_table.html)
- [`aws_glue_connection` resource](/docs/providers/aws/r/glue_connection.html)
- [`aws_glue_crawler` resource](/docs/providers/aws/r/glue_crawler.html)
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/glue_catalog_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ The following arguments are supported:
* `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.

## Attributes Reference

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

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

## Import

Glue Catalog Databases can be imported using the `catalog_id:name`. If you have not set a Catalog ID specify the AWS Account ID that the database is in, e.g.
Expand Down