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

New Resource: AWS Glue Catalog Database #2175

Merged
merged 48 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6afe6ea
Updated vendor folder to include aws glue sdk
Nov 4, 2017
4c79647
Added initial test cases
Nov 4, 2017
29286c5
Added support resources
Nov 4, 2017
cb12806
Implemented glue service and database creation
Nov 4, 2017
da164c4
Expanded test cases to include parameters, description, and location_uri
Nov 5, 2017
b3de1ca
Implemented parameters attribute in database creation
Nov 5, 2017
57a1230
Added documentation
Nov 5, 2017
19ab44c
Added import test
Nov 5, 2017
bb596b8
Fixed import not importing AWS Glue db parameters
Nov 5, 2017
4bfee75
Fixed formatting
Nov 5, 2017
dbfe0e7
Addded missing import documentation
Nov 5, 2017
3fc9052
Removed custom function
Nov 5, 2017
5a603d6
Merge branch 'master' into f-aws-glue
Nov 5, 2017
9a5ed37
Merge branch 'master' into f-aws-glue
Nov 6, 2017
5d717d7
Merge branch 'master' into f-aws-glue
drewsonne Nov 12, 2017
4ebc382
Removed merge artifacts
drewsonne Nov 12, 2017
5fec001
Fixed an error where the incorrect sha was attached to the iam vendor
drewsonne Nov 12, 2017
8744d9b
Removed duplicate glue vendor entry
drewsonne Nov 12, 2017
a0b825d
Merge remote-tracking branch 'origin/master' into f-aws-glue
drewsonne Dec 19, 2017
ec0527e
Removed redundant escapes
drewsonne Dec 19, 2017
5ffc1b0
Removed redundant types
drewsonne Dec 19, 2017
d6de489
Fixed typos
drewsonne Dec 19, 2017
b276648
Use built in terraform err checker
Dec 25, 2017
27d0cb7
Merge branch 'master' into f-aws-glue
Dec 25, 2017
e70c722
Added catalogId field
Dec 26, 2017
886b5ed
Attempted to use a second test step to ensure diff
Dec 26, 2017
429d2a7
Trying to test diff
Dec 26, 2017
6d35a39
Updated docs
Dec 26, 2017
2975924
Merge branch 'master' into f-aws-glue
Dec 26, 2017
935fcf4
Made sure all attributes are set during update
Dec 26, 2017
20faedc
Updated docs
Dec 26, 2017
af18cc6
Fixed inconsistent `ID`
Dec 26, 2017
e5305c3
Fixed incorrect test name
Dec 26, 2017
7511705
Grouped imports ala. gofmt
Dec 26, 2017
efbc83a
Fixed bad fmt
Dec 26, 2017
b8fd125
Test all values of the resource
drewsonne Dec 26, 2017
bb60b99
Fixed bug after changing test helper names
Dec 26, 2017
482db05
Confirm no parameters exist
Dec 26, 2017
d27cc24
Reduce the interface of `createAwsGlueCatalogID`
drewsonne Jan 10, 2018
7701582
Make sure Glue Catalog Database only updates params which are provided.
drewsonne Jan 10, 2018
878627a
Use the common syntac type as noted by @radeksimko
drewsonne Jan 10, 2018
8cdcebc
Check if the DB has been deleted and remove it from the state
drewsonne Jan 10, 2018
419eaa0
Merge branch 'master' into f-aws-glue
drewsonne Jan 10, 2018
7d6e0dd
Merge branch 'master' into f-aws-glue
drewsonne Jan 10, 2018
853efbd
Use American spelling
drewsonne Jan 10, 2018
f285890
Remove un-used Computed attributes for schema vars
Jan 11, 2018
46302bf
Merge branch 'master' into f-aws-glue
Jan 11, 2018
15c87e0
Merge branch 'master' into f-aws-glue
Jan 11, 2018
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 aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/aws/aws-sdk-go/service/emr"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/glacier"
"github.com/aws/aws-sdk-go/service/glue"
"github.com/aws/aws-sdk-go/service/guardduty"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/inspector"
Expand Down Expand Up @@ -198,6 +199,7 @@ type AWSClient struct {
wafregionalconn *wafregional.WAFRegional
iotconn *iot.IoT
batchconn *batch.Batch
glueconn *glue.Glue
athenaconn *athena.Athena
dxconn *directconnect.DirectConnect
mediastoreconn *mediastore.MediaStore
Expand Down Expand Up @@ -445,6 +447,7 @@ func (c *Config) Client() (interface{}, error) {
client.wafconn = waf.New(sess)
client.wafregionalconn = wafregional.New(sess)
client.batchconn = batch.New(sess)
client.glueconn = glue.New(sess)
client.athenaconn = athena.New(sess)
client.dxconn = directconnect.New(sess)
client.mediastoreconn = mediastore.New(sess)
Expand Down
29 changes: 29 additions & 0 deletions aws/import_aws_glue_catalog_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSGlueCatalogDatabase_importBasic(t *testing.T) {
resourceName := "aws_glue_catalog_database.test"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGlueDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlueCatalogDatabase_full(rInt, "A test catalog from terraform"),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func Provider() terraform.ResourceProvider {
"aws_emr_security_configuration": resourceAwsEMRSecurityConfiguration(),
"aws_flow_log": resourceAwsFlowLog(),
"aws_glacier_vault": resourceAwsGlacierVault(),
"aws_glue_catalog_database": resourceAwsGlueCatalogDatabase(),
"aws_guardduty_detector": resourceAwsGuardDutyDetector(),
"aws_iam_access_key": resourceAwsIamAccessKey(),
"aws_iam_account_alias": resourceAwsIamAccountAlias(),
Expand Down
193 changes: 193 additions & 0 deletions aws/resource_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/glue"

"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsGlueCatalogDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceAwsGlueCatalogDatabaseCreate,
Read: resourceAwsGlueCatalogDatabaseRead,
Update: resourceAwsGlueCatalogDatabaseUpdate,
Delete: resourceAwsGlueCatalogDatabaseDelete,
Exists: resourceAwsGlueCatalogDatabaseExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"catalog_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"location_uri": {
Type: schema.TypeString,
Optional: true,
},
"parameters": {
Type: schema.TypeMap,
Elem: schema.TypeString,
Optional: true,
},
},
}
}

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

input := &glue.CreateDatabaseInput{
CatalogId: aws.String(catalogID),
DatabaseInput: &glue.DatabaseInput{
Name: aws.String(name),
},
}

_, err := glueconn.CreateDatabase(input)
if err != nil {
return fmt.Errorf("Error creating Catalog Database: %s", err)
}

d.SetId(fmt.Sprintf("%s:%s", catalogID, name))

return resourceAwsGlueCatalogDatabaseUpdate(d, meta)
}

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

catalogID, name := readAwsGlueCatalogID(d.Id())

dbUpdateInput := &glue.UpdateDatabaseInput{
CatalogId: aws.String(catalogID),
Name: aws.String(name),
}

dbInput := &glue.DatabaseInput{
Name: aws.String(name),
}

if desc, ok := d.GetOk("description"); ok {
dbInput.Description = aws.String(desc.(string))
}

if loc, ok := d.GetOk("location_uri"); ok {
dbInput.LocationUri = aws.String(loc.(string))
}

if params, ok := d.GetOk("parameters"); ok {
parametersInput := make(map[string]*string)
for key, value := range params.(map[string]interface{}) {
parametersInput[key] = aws.String(value.(string))
}
dbInput.Parameters = parametersInput
}

dbUpdateInput.DatabaseInput = dbInput

if d.HasChange("description") || d.HasChange("location_uri") || d.HasChange("parameters") {
if _, err := glueconn.UpdateDatabase(dbUpdateInput); err != nil {
return err
}
}

return resourceAwsGlueCatalogDatabaseRead(d, meta)
}

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

catalogID, name := readAwsGlueCatalogID(d.Id())

input := &glue.GetDatabaseInput{
CatalogId: aws.String(catalogID),
Name: aws.String(name),
}

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

if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
log.Printf("[WARN] Glue Catalog Database (%s) not found, removing from state", d.Id())
d.SetId("")
}

return fmt.Errorf("Error reading Glue Catalog Database: %s", err.Error())
}

d.Set("name", out.Database.Name)
d.Set("catalog_id", catalogID)
d.Set("description", out.Database.Description)
d.Set("location_uri", out.Database.LocationUri)

dParams := make(map[string]string)
if len(out.Database.Parameters) > 0 {
for key, value := range out.Database.Parameters {
dParams[key] = *value
}
}
d.Set("parameters", dParams)

return nil
}

func resourceAwsGlueCatalogDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
glueconn := meta.(*AWSClient).glueconn
catalogID, name := readAwsGlueCatalogID(d.Id())

log.Printf("[DEBUG] Glue Catalog Database: %s:%s", catalogID, name)
_, err := glueconn.DeleteDatabase(&glue.DeleteDatabaseInput{
Name: aws.String(name),
})
if err != nil {
return fmt.Errorf("Error deleting Glue Catalog Database: %s", err.Error())
}
return nil
}

func resourceAwsGlueCatalogDatabaseExists(d *schema.ResourceData, meta interface{}) (bool, error) {
glueconn := meta.(*AWSClient).glueconn
catalogID, name := readAwsGlueCatalogID(d.Id())

input := &glue.GetDatabaseInput{
CatalogId: aws.String(catalogID),
Name: aws.String(name),
}

_, err := glueconn.GetDatabase(input)
return err == nil, err
}

func readAwsGlueCatalogID(id string) (catalogID string, name string) {
idParts := strings.Split(id, ":")
return idParts[0], idParts[1]
}

func createAwsGlueCatalogID(d *schema.ResourceData, accountid string) (catalogID string) {
if rawCatalogID, ok := d.GetOkExists("catalog_id"); ok {
catalogID = rawCatalogID.(string)
} else {
catalogID = accountid
}
return
}
Loading