Skip to content

Commit

Permalink
resource/aws_glue_trigger: Add tags argument and arn attribute
Browse files Browse the repository at this point in the history
Reference: #7668

Output from acceptance testing:

```
--- PASS: TestAccAWSGlueTrigger_Basic (24.18s)
--- PASS: TestAccAWSGlueTrigger_WorkflowName (25.65s)
--- PASS: TestAccAWSGlueTrigger_Description (38.32s)
--- PASS: TestAccAWSGlueTrigger_Predicate (74.17s)
--- PASS: TestAccAWSGlueTrigger_Tags (75.60s)
--- PASS: TestAccAWSGlueTrigger_Schedule (77.46s)
--- PASS: TestAccAWSGlueTrigger_Enabled (88.06s)
--- PASS: TestAccAWSGlueTrigger_Crawler (128.09s)
```
  • Loading branch information
bflad committed Nov 21, 2019
1 parent 3990dfc commit d2e3053
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
35 changes: 35 additions & 0 deletions aws/resource_aws_glue_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"time"

"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/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsGlueTrigger() *schema.Resource {
Expand Down Expand Up @@ -52,6 +54,10 @@ func resourceAwsGlueTrigger() *schema.Resource {
},
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -136,6 +142,7 @@ func resourceAwsGlueTrigger() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"tags": tagsSchema(),
"type": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -163,6 +170,7 @@ func resourceAwsGlueTriggerCreate(d *schema.ResourceData, meta interface{}) erro
input := &glue.CreateTriggerInput{
Actions: expandGlueActions(d.Get("actions").([]interface{})),
Name: aws.String(name),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().GlueTags(),
Type: aws.String(triggerType),
}

Expand Down Expand Up @@ -248,6 +256,15 @@ func resourceAwsGlueTriggerRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting actions: %s", err)
}

triggerARN := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "glue",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("trigger/%s", d.Id()),
}.String()
d.Set("arn", triggerARN)

d.Set("description", trigger.Description)

var enabled bool
Expand All @@ -266,6 +283,17 @@ func resourceAwsGlueTriggerRead(d *schema.ResourceData, meta interface{}) error

d.Set("name", trigger.Name)
d.Set("schedule", trigger.Schedule)

tags, err := keyvaluetags.GlueListTags(conn, triggerARN)

if err != nil {
return fmt.Errorf("error listing tags for Glue Trigger (%s): %s", triggerARN, err)
}

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("type", trigger.Type)
d.Set("workflow_name", trigger.WorkflowName)

Expand Down Expand Up @@ -330,6 +358,13 @@ func resourceAwsGlueTriggerUpdate(d *schema.ResourceData, meta interface{}) erro
}
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.GlueUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return nil
}

Expand Down
82 changes: 82 additions & 0 deletions aws/resource_aws_glue_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ func TestAccAWSGlueTrigger_Basic(t *testing.T) {
testAccCheckAWSGlueTriggerExists(resourceName, &trigger),
resource.TestCheckResourceAttr(resourceName, "actions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.0.job_name", rName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "glue", fmt.Sprintf("trigger/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "predicate.#", "0"),
resource.TestCheckResourceAttr(resourceName, "schedule", ""),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "type", "ON_DEMAND"),
resource.TestCheckResourceAttr(resourceName, "workflow_name", ""),
),
Expand Down Expand Up @@ -286,6 +288,51 @@ func TestAccAWSGlueTrigger_Schedule(t *testing.T) {
})
}

func TestAccAWSGlueTrigger_Tags(t *testing.T) {
var trigger1, trigger2, trigger3 glue.Trigger

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "aws_glue_trigger.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGlueTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSGlueTriggerConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueTriggerExists(resourceName, &trigger1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSGlueTriggerConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueTriggerExists(resourceName, &trigger2),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSGlueTriggerConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGlueTriggerExists(resourceName, &trigger3),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccAWSGlueTrigger_WorkflowName(t *testing.T) {
var trigger glue.Trigger

Expand Down Expand Up @@ -507,6 +554,41 @@ resource "aws_glue_trigger" "test" {
`, testAccAWSGlueJobConfig_Required(rName), rName, schedule)
}

func testAccAWSGlueTriggerConfigTags1(rName, tagKey1, tagValue1 string) string {
return testAccAWSGlueJobConfig_Required(rName) + fmt.Sprintf(`
resource "aws_glue_trigger" "test" {
name = %[1]q
type = "ON_DEMAND"
actions {
job_name = "${aws_glue_job.test.name}"
}
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSGlueTriggerConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return testAccAWSGlueJobConfig_Required(rName) + fmt.Sprintf(`
resource "aws_glue_trigger" "test" {
name = %[1]q
type = "ON_DEMAND"
actions {
job_name = "${aws_glue_job.test.name}"
}
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccAWSGlueTriggerConfig_WorkflowName(rName string) string {
return fmt.Sprintf(`
%s
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/glue_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The following arguments are supported:
* `name` – (Required) The name of the trigger.
* `predicate` – (Optional) A predicate to specify when the new trigger should fire. Required when trigger type is `CONDITIONAL`. Defined below.
* `schedule` – (Optional) A cron expression used to specify the schedule. [Time-Based Schedules for Jobs and Crawlers](https://docs.aws.amazon.com/glue/latest/dg/monitor-data-warehouse-schedule.html)
* `tags` - (Optional) Key-value mapping of resource tags
* `type` – (Required) The type of trigger. Valid values are `CONDITIONAL`, `ON_DEMAND`, and `SCHEDULED`.
* `workflow_name` - (Optional) A workflow to which the trigger should be associated to. Every workflow graph (DAG) needs a starting trigger (`ON_DEMAND` or `SCHEDULED` type) and can contain multiple additional `CONDITIONAL` triggers.

Expand Down Expand Up @@ -140,6 +141,7 @@ The following arguments are supported:

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

* `arn` - Amazon Resource Name (ARN) of Glue Trigger
* `id` - Trigger name

## Timeouts
Expand Down

0 comments on commit d2e3053

Please sign in to comment.