-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
068585b
commit 497063a
Showing
5 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
builtin/providers/aws/resource_aws_codecommit_trigger.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/codecommit" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsCodeCommitTrigger() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsCodeCommitTriggerCreate, | ||
Read: resourceAwsCodeCommitTriggerRead, | ||
Delete: resourceAwsCodeCommitTriggerDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"configuration_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"trigger": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
ForceNew: true, | ||
Required: true, | ||
MaxItems: 10, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"destination_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"custom_data": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"branches": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"events": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Required: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsCodeCommitTriggerCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).codecommitconn | ||
|
||
// Expand the "trigger" set to aws-sdk-go compat []*codecommit.RepositoryTrigger | ||
triggers := expandAwsCodeCommitTriggers(d.Get("trigger").(*schema.Set).List()) | ||
|
||
input := &codecommit.PutRepositoryTriggersInput{ | ||
RepositoryName: aws.String(d.Get("repository_name").(string)), | ||
Triggers: triggers, | ||
} | ||
|
||
resp, err := conn.PutRepositoryTriggers(input) | ||
if err != nil { | ||
return fmt.Errorf("Error creating CodeCommit Trigger: %s", err) | ||
} | ||
|
||
log.Printf("[INFO] Code Commit Trigger Created %s input %s", resp, input) | ||
|
||
d.SetId(d.Get("repository_name").(string)) | ||
d.Set("configuration_id", resp.ConfigurationId) | ||
|
||
return resourceAwsCodeCommitTriggerRead(d, meta) | ||
} | ||
|
||
func resourceAwsCodeCommitTriggerRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).codecommitconn | ||
|
||
input := &codecommit.GetRepositoryTriggersInput{ | ||
RepositoryName: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetRepositoryTriggers(input) | ||
if err != nil { | ||
return fmt.Errorf("Error reading CodeCommit Trigger: %s", err.Error()) | ||
} | ||
|
||
log.Printf("[DEBUG] CodeCommit Trigger: %s", resp) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsCodeCommitTriggerDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
||
conn := meta.(*AWSClient).codecommitconn | ||
|
||
log.Printf("[DEBUG] Deleting Trigger: %q", d.Id()) | ||
|
||
input := &codecommit.PutRepositoryTriggersInput{ | ||
RepositoryName: aws.String(d.Get("repository_name").(string)), | ||
Triggers: []*codecommit.RepositoryTrigger{}, | ||
} | ||
|
||
_, err := conn.PutRepositoryTriggers(input) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandAwsCodeCommitTriggers(configured []interface{}) []*codecommit.RepositoryTrigger { | ||
triggers := make([]*codecommit.RepositoryTrigger, 0, len(configured)) | ||
// Loop over our configured triggers and create | ||
// an array of aws-sdk-go compatabile objects | ||
for _, lRaw := range configured { | ||
data := lRaw.(map[string]interface{}) | ||
t := &codecommit.RepositoryTrigger{ | ||
CustomData: aws.String(data["custom_data"].(string)), | ||
DestinationArn: aws.String(data["destination_arn"].(string)), | ||
Name: aws.String(data["name"].(string)), | ||
} | ||
|
||
branches := make([]*string, len(data["events"].([]interface{}))) | ||
for i, vv := range data["branches"].([]interface{}) { | ||
str := vv.(string) | ||
branches[i] = aws.String(str) | ||
} | ||
t.Branches = branches | ||
|
||
events := make([]*string, len(data["events"].([]interface{}))) | ||
for i, vv := range data["events"].([]interface{}) { | ||
str := vv.(string) | ||
events[i] = aws.String(str) | ||
} | ||
t.Events = events | ||
|
||
triggers = append(triggers, t) | ||
} | ||
return triggers | ||
} |
157 changes: 157 additions & 0 deletions
157
builtin/providers/aws/resource_aws_codecommit_trigger_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/codecommit" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSCodeCommitTrigger_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCodeCommitTriggerDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCodeCommitTrigger_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCodeCommitTriggerExists("aws_codecommit_trigger.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_trigger.test", "trigger.#", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCodeCommitTrigger_withChanges(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCodeCommitTriggerDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCodeCommitTrigger_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCodeCommitRepositoryExists("aws_codecommit_trigger.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_trigger.test", "trigger.#", "1"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_trigger.test", "trigger.2511662502.name", "tf-test-trigger"), | ||
), | ||
}, | ||
resource.TestStep{ | ||
Config: testAccCodeCommitTrigger_withChanges, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCodeCommitRepositoryExists("aws_codecommit_trigger.test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_trigger.test", "trigger.#", "1"), | ||
resource.TestCheckResourceAttr( | ||
"aws_codecommit_trigger.test", "trigger.229487722.name", "tf-test-trigger_withChanges"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCodeCommitTriggerDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).codecommitconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_codecommit_trigger" { | ||
continue | ||
} | ||
|
||
_, err := conn.GetRepositoryTriggers(&codecommit.GetRepositoryTriggersInput{ | ||
RepositoryName: aws.String(rs.Primary.ID), | ||
}) | ||
|
||
if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" { | ||
continue | ||
} | ||
if err == nil { | ||
return fmt.Errorf("Trigger still exists: %s", rs.Primary.ID) | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckCodeCommitTriggerExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn | ||
out, err := codecommitconn.GetRepositoryTriggers(&codecommit.GetRepositoryTriggersInput{ | ||
RepositoryName: aws.String(rs.Primary.ID), | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(out.Triggers) == 0 { | ||
return fmt.Errorf("CodeCommit Trigger Failed: %q", out) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccCodeCommitTrigger_basic = ` | ||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
resource "aws_sns_topic" "test" { | ||
name = "tf-test-topic" | ||
} | ||
resource "aws_codecommit_repository" "test" { | ||
repository_name = "tf_test_repository" | ||
description = "This is a test description" | ||
} | ||
resource "aws_codecommit_trigger" "test" { | ||
depends_on = ["aws_codecommit_repository.test"] | ||
repository_name = "tf_test_repository" | ||
trigger { | ||
name = "tf-test-trigger" | ||
events = ["all"] | ||
destination_arn = "${aws_sns_topic.test.arn}" | ||
} | ||
} | ||
` | ||
|
||
const testAccCodeCommitTrigger_withChanges = ` | ||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
resource "aws_sns_topic" "test" { | ||
name = "tf-test-topic" | ||
} | ||
resource "aws_codecommit_repository" "test" { | ||
repository_name = "tf_test_repository" | ||
description = "This is a test description" | ||
} | ||
resource "aws_codecommit_trigger" "test" { | ||
depends_on = ["aws_codecommit_repository.test"] | ||
repository_name = "tf_test_repository" | ||
trigger { | ||
name = "tf-test-trigger_withChanges" | ||
events = ["all"] | ||
destination_arn = "${aws_sns_topic.test.arn}" | ||
} | ||
} | ||
` |
40 changes: 40 additions & 0 deletions
40
website/source/docs/providers/aws/r/code_commit_trigger.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_codecommit_trigger" | ||
sidebar_current: "docs-aws-resource-codecommit-trigger" | ||
description: |- | ||
Provides a CodeCommit Trigger Resource. | ||
--- | ||
|
||
# aws\_codecommit\_trigger | ||
|
||
Provides a CodeCommit Trigger Resource. | ||
|
||
~> **NOTE on CodeCommit**: The CodeCommit is not yet rolled out | ||
in all regions - available regions are listed | ||
[the AWS Docs](https://docs.aws.amazon.com/general/latest/gr/rande.html#codecommit_region). | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_codecommit_trigger" "test" { | ||
depends_on = ["aws_codecommit_repository.test"] | ||
repository_name = "my_test_repository" | ||
trigger { | ||
name = "noname" | ||
events = ["all"] | ||
destination_arn = "${aws_sns_topic.test.arn}" | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. | ||
* `name` - (Required) The name of the trigger. | ||
* `destination_arn` - (Required) The ARN of the resource that is the target for a trigger. For example, the ARN of a topic in Amazon Simple Notification Service (SNS). | ||
* `custom_data` - (Optional) Any custom data associated with the trigger that will be included in the information sent to the target of the trigger. | ||
* `branches` - (Optional) The branches that will be included in the trigger configuration. If no branches are specified, the trigger will apply to all branches. | ||
* `events` - (Required) The repository events that will cause the trigger to run actions in another service, such as sending a notification through Amazon Simple Notification Service (SNS). If no events are specified, the trigger will run for all repository events. Event types include: `all`, `updateReference`, `createReference`, `deleteReference`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters