From 9b8894d8e117403b504f889d1ff8589b3ba68764 Mon Sep 17 00:00:00 2001 From: Anshul Sharma Date: Thu, 8 Sep 2016 14:24:54 +0530 Subject: [PATCH] Added Codecommit Trigger --- builtin/providers/aws/provider.go | 1 + .../aws/resource_aws_codecommit_repository.go | 7 - .../aws/resource_aws_codecommit_trigger.go | 165 ++++++++++++++++++ .../resource_aws_codecommit_trigger_test.go | 160 +++++++++++++++++ .../aws/r/code_commit_trigger.html.markdown | 36 ++++ website/source/layouts/aws.erb | 4 + 6 files changed, 366 insertions(+), 7 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_codecommit_trigger.go create mode 100644 builtin/providers/aws/resource_aws_codecommit_trigger_test.go create mode 100644 website/source/docs/providers/aws/r/code_commit_trigger.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index e7b23fd615aa..b21689a2161d 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -200,6 +200,7 @@ func Provider() terraform.ResourceProvider { "aws_codedeploy_app": resourceAwsCodeDeployApp(), "aws_codedeploy_deployment_group": resourceAwsCodeDeployDeploymentGroup(), "aws_codecommit_repository": resourceAwsCodeCommitRepository(), + "aws_codecommit_trigger": resourceAwsCodeCommitTrigger(), "aws_customer_gateway": resourceAwsCustomerGateway(), "aws_db_event_subscription": resourceAwsDbEventSubscription(), "aws_db_instance": resourceAwsDbInstance(), diff --git a/builtin/providers/aws/resource_aws_codecommit_repository.go b/builtin/providers/aws/resource_aws_codecommit_repository.go index 83d32f42bdea..c9b2bd2dc1ba 100644 --- a/builtin/providers/aws/resource_aws_codecommit_repository.go +++ b/builtin/providers/aws/resource_aws_codecommit_repository.go @@ -74,13 +74,6 @@ func resourceAwsCodeCommitRepository() *schema.Resource { func resourceAwsCodeCommitRepositoryCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).codecommitconn - region := meta.(*AWSClient).region - - // This is a temporary thing - we need to ensure that CodeCommit is only being run against us-east-1 - // As this is the only place that AWS currently supports it - if region != "us-east-1" { - return fmt.Errorf("CodeCommit can only be used with us-east-1. You are trying to use it on %s", region) - } input := &codecommit.CreateRepositoryInput{ RepositoryName: aws.String(d.Get("repository_name").(string)), diff --git a/builtin/providers/aws/resource_aws_codecommit_trigger.go b/builtin/providers/aws/resource_aws_codecommit_trigger.go new file mode 100644 index 000000000000..e1f8f0cd14e5 --- /dev/null +++ b/builtin/providers/aws/resource_aws_codecommit_trigger.go @@ -0,0 +1,165 @@ +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, + }, + "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).codecommitconnr + + // 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", resp) + + d.SetId(d.Get("repository_name").(string)) + + 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("[INFO] CodeCommit Trigger READ %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)), + } + + if raw, ok := data["branches"]; ok { + branches := make([]*string, len(raw.([]interface{}))) + for i, vv := range raw.([]interface{}) { + str := vv.(string) + branches[i] = aws.String(str) + } + if len(branches) > 0 { + t.Branches = branches + } + } + + if raw, ok := data["events"]; ok { + events := make([]*string, len(raw.([]interface{}))) + for i, vv := range raw.([]interface{}) { + str := vv.(string) + events[i] = aws.String(str) + } + if len(events) > 0 { + t.Events = events + } + } + + } + return triggers +} diff --git a/builtin/providers/aws/resource_aws_codecommit_trigger_test.go b/builtin/providers/aws/resource_aws_codecommit_trigger_test.go new file mode 100644 index 000000000000..eba2c8e165fe --- /dev/null +++ b/builtin/providers/aws/resource_aws_codecommit_trigger_test.go @@ -0,0 +1,160 @@ +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.794783505.name", "noname"), + ), + }, + 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.807285978.name", "noname_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 + } + + log.Printf("[INFO] TriggerMetadata %s", rs.Primary.ID) + + if *out.Triggers[0].Name != rs.Primary.ID { + return fmt.Errorf("CodeCommit Trigger Mismatch - existing: %q, state: %q", + *out.Triggers[0].Name, rs.Primary.ID) + } + + return nil + } +} + +const testAccCodeCommitTrigger_basic = ` +provider "aws" { + region = "us-east-1" +} +resource "aws_sns_topic" "test" { + name = "my-test-topic" +} +resource "aws_codecommit_repository" "test" { + repository_name = "my_test_repository" + description = "This is a test description" +} +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}" + } + } +` + +const testAccCodeCommitTrigger_withChanges = ` +provider "aws" { + region = "us-east-1" +} +resource "aws_sns_topic" "test" { + name = "my-test-topic" +} +resource "aws_codecommit_repository" "test" { + repository_name = "my_test_repository" + description = "This is a test description" +} +resource "aws_codecommit_trigger" "test" { + depends_on = ["aws_codecommit_repository.test"] + repository_name = "my_test_repository" + trigger { + name = "noname_withChanges" + events = ["all"] + destination_arn = "${aws_sns_topic.test.arn}" + } + } +` diff --git a/website/source/docs/providers/aws/r/code_commit_trigger.html.markdown b/website/source/docs/providers/aws/r/code_commit_trigger.html.markdown new file mode 100644 index 000000000000..14dc62390c1b --- /dev/null +++ b/website/source/docs/providers/aws/r/code_commit_trigger.html.markdown @@ -0,0 +1,36 @@ +--- +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. + +## 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 description of the repository. This needs to be less than 1000 characters +* `destination_arn` - (Required) The default branch of the repository. The branch specified here needs to exist. +* `custom_data` - (Optional) The default branch of the repository. The branch specified here needs to exist. +* `branches` - (Optional) The default branch of the repository. The branch specified here needs to exist. +* `events` - (Required) The default branch of the repository. The branch specified here needs to exist. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index ef0fe5d0cf76..fca31abd60db 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -177,6 +177,10 @@ > aws_codecommit_repository + + > + aws_codecommit_trigger +