-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
c3b13ed
commit 19e6763
Showing
5 changed files
with
285 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
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,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 | ||
} |
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,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}" | ||
} | ||
`) | ||
} |
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
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,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. |