diff --git a/aws/provider.go b/aws/provider.go index e4c80bfcceb..2e6bb9ab60e 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -301,6 +301,7 @@ func Provider() terraform.ResourceProvider { "aws_codecommit_repository": resourceAwsCodeCommitRepository(), "aws_codecommit_trigger": resourceAwsCodeCommitTrigger(), "aws_codebuild_project": resourceAwsCodeBuildProject(), + "aws_codebuild_webhook": resourceAwsCodeBuildWebhook(), "aws_codepipeline": resourceAwsCodePipeline(), "aws_customer_gateway": resourceAwsCustomerGateway(), "aws_db_event_subscription": resourceAwsDbEventSubscription(), diff --git a/aws/resource_aws_codebuild_webhook.go b/aws/resource_aws_codebuild_webhook.go new file mode 100644 index 00000000000..07785d181d9 --- /dev/null +++ b/aws/resource_aws_codebuild_webhook.go @@ -0,0 +1,84 @@ +package aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codebuild" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsCodeBuildWebhook() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCodeBuildWebhookCreate, + Read: resourceAwsCodeBuildWebhookRead, + Delete: resourceAwsCodeBuildWebhookDelete, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsCodeBuildWebhookCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codebuildconn + + resp, err := conn.CreateWebhook(&codebuild.CreateWebhookInput{ + ProjectName: aws.String(d.Get("name").(string)), + }) + if err != nil { + return err + } + + d.SetId(d.Get("name").(string)) + d.Set("url", resp.Webhook.Url) + return nil +} + +func resourceAwsCodeBuildWebhookRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codebuildconn + + resp, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{ + Names: []*string{ + aws.String(d.Id()), + }, + }) + + if err != nil { + return err + } + + if len(resp.Projects) == 0 { + d.SetId("") + return nil + } + + project := resp.Projects[0] + d.Set("url", project.Webhook.Url) + return nil +} + +func resourceAwsCodeBuildWebhookDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codebuildconn + + _, err := conn.DeleteWebhook(&codebuild.DeleteWebhookInput{ + ProjectName: aws.String(d.Id()), + }) + + if err != nil { + if isAWSErr(err, codebuild.ErrCodeResourceNotFoundException, "") { + d.SetId("") + return nil + } + return err + } + + d.SetId("") + return nil +} diff --git a/aws/resource_aws_codebuild_webhook_test.go b/aws/resource_aws_codebuild_webhook_test.go new file mode 100644 index 00000000000..170ff3feb91 --- /dev/null +++ b/aws/resource_aws_codebuild_webhook_test.go @@ -0,0 +1,78 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codebuild" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAwsCodeBuildWebhook_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsCodeBuildWebhookDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCodeBuildWebhookConfig_basic(acctest.RandString(5)), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsCodeBuildWebhookExists("aws_codebuild_webhook.test"), + resource.TestCheckResourceAttrSet("aws_codebuild_webhook.test", "url"), + ), + }, + }, + }) +} + +func testAccCheckAwsCodeBuildWebhookDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).codebuildconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_codebuild_webhook" { + continue + } + + resp, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{ + Names: []*string{ + aws.String(rs.Primary.ID), + }, + }) + + if err != nil { + return err + } + + if len(resp.Projects) == 0 { + return nil + } + + project := resp.Projects[0] + if project.Webhook != nil && project.Webhook.Url != nil { + return fmt.Errorf("Found CodeBuild Webhook: %s", rs.Primary.ID) + } + } + return nil +} + +func testAccCheckAwsCodeBuildWebhookExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + return nil + } +} + +func testAccCodeBuildWebhookConfig_basic(rName string) string { + return fmt.Sprintf(testAccAWSCodeBuildProjectConfig_basic(rName) + ` +resource "aws_codebuild_webhook" "test" { + name = "${aws_codebuild_project.foo.name}" +} +`) +} diff --git a/website/aws.erb b/website/aws.erb index 5bf9ba7af53..758e0035f73 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -427,6 +427,10 @@ aws_codebuild_project +