Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushi-ishibashi committed Dec 31, 2017
1 parent c3b13ed commit 19e6763
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
84 changes: 84 additions & 0 deletions aws/resource_aws_codebuild_webhook.go
Original file line number Diff line number Diff line change
@@ -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
}
78 changes: 78 additions & 0 deletions aws/resource_aws_codebuild_webhook_test.go
Original file line number Diff line number Diff line change
@@ -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}"
}
`)
}
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@
<a href="/docs/providers/aws/r/codebuild_project.html">aws_codebuild_project</a>
</li>

<li<%= sidebar_current("docs-aws-resource-codebuild-webhook") %>>
<a href="/docs/providers/aws/r/codebuild_webhook.html">aws_codebuild_webhook</a>
</li>

</ul>
</li>

Expand Down
118 changes: 118 additions & 0 deletions website/docs/r/codebuild_webhook.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
layout: "aws"
page_title: "AWS: aws_codebuild_webhook"
sidebar_current: "docs-aws-resource-codebuild-webhook"
description: |-
Provides a CodeBuild Webhook resource.
---

# aws_codebuild_webhook

Provides a CodeBuild Webhook resource.

## Example Usage

```hcl
resource "aws_iam_role" "codebuild_role" {
name = "codebuild-role-"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_policy" "codebuild_policy" {
name = "codebuild-policy"
path = "/service-role/"
description = "Policy used in trust relationship with CodeBuild"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Resource": [
"*"
],
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
}
]
}
POLICY
}
resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
name = "codebuild-policy-attachment"
policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
roles = ["${aws_iam_role.codebuild_role.id}"]
}
resource "aws_codebuild_project" "foo" {
name = "test-project"
description = "test_codebuild_project"
build_timeout = "5"
service_role = "${aws_iam_role.codebuild_role.arn}"
artifacts {
type = "NO_ARTIFACTS"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
environment_variable {
"name" = "SOME_KEY1"
"value" = "SOME_VALUE1"
}
environment_variable {
"name" = "SOME_KEY2"
"value" = "SOME_VALUE2"
}
}
source {
type = "GITHUB"
location = "https://github.com/mitchellh/packer.git"
}
tags {
"Environment" = "Test"
}
}
resource "aws_codebuild_webhook" "foo" {
name = "${aws_codebuild_project.foo.name}"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the build project.

## Attributes Reference

The following attributes are exported:

* `id` - The name of the build project.
* `url` - The URL to the webhook.

0 comments on commit 19e6763

Please sign in to comment.