Skip to content

Commit

Permalink
Merge pull request #1 from f0rk/glue_crawlers_fork
Browse files Browse the repository at this point in the history
make glue crawler creation wait for role ready
  • Loading branch information
darrenhaken authored May 31, 2018
2 parents f579f8d + f43a0a2 commit 9396692
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
75 changes: 43 additions & 32 deletions aws/resource_aws_glue_catalog_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/glue"
"log"
"time"

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

Expand Down Expand Up @@ -118,46 +120,55 @@ func resourceAwsGlueCatalogCrawler() *schema.Resource {
}
}

func resourceAwsGlueCatalogCrawlerCreate(resource *schema.ResourceData, meta interface{}) error {
func resourceAwsGlueCatalogCrawlerCreate(d *schema.ResourceData, meta interface{}) error {
glueConn := meta.(*AWSClient).glueconn
name := resource.Get("name").(string)
name := d.Get("name").(string)

_, err := glueConn.CreateCrawler(createCrawlerInput(name, resource))
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := glueConn.CreateCrawler(createCrawlerInput(name, d))
if err != nil {
if isAWSErr(err, "InvalidInputException", "Service is unable to assume role") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return fmt.Errorf("error creating Glue crawler: %s", err)
}
resource.SetId(fmt.Sprintf("%s", name))
d.SetId(fmt.Sprintf("%s", name))

return resourceAwsGlueCatalogCrawlerUpdate(resource, meta)
return resourceAwsGlueCatalogCrawlerUpdate(d, meta)
}

func createCrawlerInput(crawlerName string, resource *schema.ResourceData) *glue.CreateCrawlerInput {
func createCrawlerInput(crawlerName string, d *schema.ResourceData) *glue.CreateCrawlerInput {
crawlerInput := &glue.CreateCrawlerInput{
Name: aws.String(crawlerName),
DatabaseName: aws.String(resource.Get("database_name").(string)),
Role: aws.String(resource.Get("role").(string)),
Targets: createCrawlerTargets(resource),
DatabaseName: aws.String(d.Get("database_name").(string)),
Role: aws.String(d.Get("role").(string)),
Targets: createCrawlerTargets(d),
}
//if description, ok := resource.GetOk("description"); ok {
//if description, ok := d.GetOk("description"); ok {
// crawlerInput.Description = aws.String(description.(string))
//}
//if schedule, ok := resource.GetOk("schedule"); ok {
//if schedule, ok := d.GetOk("schedule"); ok {
// crawlerInput.Description = aws.String(schedule.(string))
//}
//if classifiers, ok := resource.GetOk("classifiers"); ok {
//if classifiers, ok := d.GetOk("classifiers"); ok {
// crawlerInput.Classifiers = expandStringList(classifiers.(*schema.Set).List())
//}
//if v, ok := resource.GetOk("schema_change_policy"); ok {
//if v, ok := d.GetOk("schema_change_policy"); ok {
// crawlerInput.SchemaChangePolicy = createSchemaPolicy(v)
//}
//if tablePrefix, ok := resource.GetOk("table_prefix"); ok {
//if tablePrefix, ok := d.GetOk("table_prefix"); ok {
// crawlerInput.TablePrefix = aws.String(tablePrefix.(string))
//}
//if targets, ok := resource.GetOk("targets"); ok {
//if targets, ok := d.GetOk("targets"); ok {
// crawlerInput.Targets = createCrawlerTargets(targets)
//}
//if configuration, ok := resource.GetOk("configuration"); ok {
//if configuration, ok := d.GetOk("configuration"); ok {
// crawlerInput.Configuration = aws.String(configuration.(string))
//}
return crawlerInput
Expand Down Expand Up @@ -253,22 +264,22 @@ func expandS3Target(cfg map[string]interface{}) *glue.S3Target {
}
}

func resourceAwsGlueCatalogCrawlerUpdate(resource *schema.ResourceData, meta interface{}) error {
func resourceAwsGlueCatalogCrawlerUpdate(d *schema.ResourceData, meta interface{}) error {
glueConn := meta.(*AWSClient).glueconn
name := resource.Get("name").(string)
name := d.Get("name").(string)

crawlerInput := glue.UpdateCrawlerInput(*createCrawlerInput(name, resource))
crawlerInput := glue.UpdateCrawlerInput(*createCrawlerInput(name, d))

if _, err := glueConn.UpdateCrawler(&crawlerInput); err != nil {
return err
}

return resourceAwsGlueCatalogCrawlerRead(resource, meta)
return resourceAwsGlueCatalogCrawlerRead(d, meta)
}

func resourceAwsGlueCatalogCrawlerRead(resource *schema.ResourceData, meta interface{}) error {
func resourceAwsGlueCatalogCrawlerRead(d *schema.ResourceData, meta interface{}) error {
glueConn := meta.(*AWSClient).glueconn
name := resource.Get("name").(string)
name := d.Get("name").(string)

input := &glue.GetCrawlerInput{
Name: aws.String(name),
Expand All @@ -277,36 +288,36 @@ func resourceAwsGlueCatalogCrawlerRead(resource *schema.ResourceData, meta inter
crawlerOutput, err := glueConn.GetCrawler(input)
if err != nil {
if isAWSErr(err, glue.ErrCodeEntityNotFoundException, "") {
log.Printf("[WARN] Glue Crawler (%s) not found, removing from state", resource.Id())
resource.SetId("")
log.Printf("[WARN] Glue Crawler (%s) not found, removing from state", d.Id())
d.SetId("")
}

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

resource.Set("name", crawlerOutput.Crawler.Name)
resource.Set("database_name", crawlerOutput.Crawler.DatabaseName)
resource.Set("role", crawlerOutput.Crawler.Role)
//resource.Set("description", crawler.Description)
//resource.Set("schedule", crawler.Schedule)
d.Set("name", crawlerOutput.Crawler.Name)
d.Set("database_name", crawlerOutput.Crawler.DatabaseName)
d.Set("role", crawlerOutput.Crawler.Role)
//d.Set("description", crawler.Description)
//d.Set("schedule", crawler.Schedule)

//var classifiers []string
//if len(crawler.Classifiers) > 0 {
// for _, value := range crawler.Classifiers {
// classifiers = append(classifiers, *value)
// }
//}
//resource.Set("classifiers", crawler.Classifiers)
//d.Set("classifiers", crawler.Classifiers)

//if crawlerOutput.Crawler.SchemaChangePolicy != nil {
// schemaPolicy := map[string]string{
// "delete_behavior": *crawlerOutput.Crawler.SchemaChangePolicy.DeleteBehavior,
// "update_behavior": *crawlerOutput.Crawler.SchemaChangePolicy.UpdateBehavior,
// }
// resource.Set("schema_change_policy", schemaPolicy)
// d.Set("schema_change_policy", schemaPolicy)
//}
//
//resource.Set("table_prefix", crawlerOutput.Crawler.TablePrefix)
//d.Set("table_prefix", crawlerOutput.Crawler.TablePrefix)

return nil
}
Expand Down
12 changes: 6 additions & 6 deletions aws/resource_aws_glue_catalog_crawler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ func TestAccAWSGlueCrawler_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccGuardDutyDetectorConfigBasic,
Config: testAccGlueCrawlerConfigBasic,
Check: resource.ComposeTestCheckFunc(
checkGlueCatalogCrawlerExists("aws_glue_catalog_crawler.test", "tf-test"),
checkGlueCatalogCrawlerExists("aws_glue_catalog_crawler.test", "test"),
resource.TestCheckResourceAttr(
"aws_glue_catalog_crawler.test",
"name",
"tf-test",
"test",
),
resource.TestCheckResourceAttr(
"aws_glue_catalog_crawler.test",
"database_name",
"db-name",
"db_name",
),
resource.TestCheckResourceAttr(
"aws_glue_catalog_crawler.test",
"role",
"default",
"AWSGlueServiceRoleDefault",
),
),
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func checkGlueCatalogCrawlerExists(name string, crawlerName string) resource.Tes
}
}

const testAccGuardDutyDetectorConfigBasic = `
const testAccGlueCrawlerConfigBasic = `
resource "aws_glue_catalog_crawler" "test" {
name = "test"
database_name = "db_name"
Expand Down

0 comments on commit 9396692

Please sign in to comment.