Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using in repo configuration for cloudbuild trigger #1557

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions google/resource_cloudbuild_build_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func resourceCloudBuildTrigger() *schema.Resource {
Computed: true,
ForceNew: true,
},
"filename": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"build"},
},
"build": {
Type: schema.TypeList,
Description: "Contents of the build template.",
Expand Down Expand Up @@ -142,7 +148,12 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface
buildTrigger.Description = v.(string)
}

buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
if v, ok := d.GetOk("filename"); ok {
buildTrigger.Filename = v.(string)
} else {
buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
}

buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)

tstr, err := json.Marshal(buildTrigger)
Expand Down Expand Up @@ -179,7 +190,10 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{}
if buildTrigger.TriggerTemplate != nil {
d.Set("trigger_template", flattenCloudbuildBuildTriggerTemplate(d, config, buildTrigger.TriggerTemplate))
}
if buildTrigger.Build != nil {

if buildTrigger.Filename != "" {
d.Set("filename", buildTrigger.Filename)
} else if buildTrigger.Build != nil {
d.Set("build", flattenCloudbuildBuildTriggerBuild(d, config, buildTrigger.Build))
}

Expand Down
109 changes: 100 additions & 9 deletions google/resource_cloudbuild_build_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func TestAccCloudBuildTrigger_basic(t *testing.T) {
Expand Down Expand Up @@ -37,24 +38,80 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
})
}

func TestAccCloudBuildTrigger_filename(t *testing.T) {
t.Parallel()

projectID := "terraform-" + acctest.RandString(10)
projectOrg := getTestOrgFromEnv(t)
projectBillingAccount := getTestBillingAccountFromEnv(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGoogleCloudBuildTriggerVersionsDestroyed,
Steps: []resource.TestStep{
resource.TestStep{
Config: testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleCloudFilenameConfig("google_cloudbuild_trigger.filename_build_trigger"),
),
},
resource.TestStep{
Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleCloudBuildTriggerWasRemovedFromState("google_cloudbuild_trigger.filename_build_trigger"),
),
},
},
})

}

func testAccGetBuildTrigger(s *terraform.State, resourceName string) (*cloudbuild.BuildTrigger, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return nil, fmt.Errorf("Resource not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return nil, fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)
project := rs.Primary.Attributes["project"]

trigger, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do()
if err != nil {
return nil, fmt.Errorf("Trigger does not exist")
}

return trigger, nil
}

func testAccCheckGoogleCloudBuildTriggerExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, err := testAccGetBuildTrigger(s, resourceName)

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Resource not found: %s", resourceName)
if err != nil {
return fmt.Errorf("Trigger does not exist")
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
project := rs.Primary.Attributes["project"]
return nil
}
}

func testAccCheckGoogleCloudFilenameConfig(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
trigger, err := testAccGetBuildTrigger(s, resourceName)

_, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do()
if err != nil {
return fmt.Errorf("Trigger does not exist")
}

if trigger.Filename != "cloudbuild.yaml" {
return fmt.Errorf("Config filename mismatch: %s", trigger.Filename)
}

return nil
}
}
Expand Down Expand Up @@ -147,6 +204,40 @@ resource "google_cloudbuild_trigger" "build_trigger" {
`, projectID, projectID, projectOrg, projectBillingAccount)
}

func testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
name = "%s"
project_id = "%s"
org_id = "%s"
billing_account = "%s"
}

resource "google_project_services" "acceptance" {
project = "${google_project.acceptance.project_id}"

services = [
"cloudbuild.googleapis.com",
"containerregistry.googleapis.com",
"logging.googleapis.com",
"pubsub.googleapis.com",
"storage-api.googleapis.com",
]
}

resource "google_cloudbuild_trigger" "filename_build_trigger" {
project = "${google_project_services.acceptance.project}"
description = "acceptance test build trigger"
trigger_template {
branch_name = "master"
project = "${google_project_services.acceptance.project}"
repo_name = "some-repo"
}
filename = "cloudbuild.yaml"
}
`, projectID, projectID, projectOrg, projectBillingAccount)
}

func testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
Expand Down
19 changes: 19 additions & 0 deletions website/docs/r/cloudbuild_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ resource "google_cloudbuild_trigger" "build_trigger" {
}
```

OR

```hcl
resource "google_cloudbuild_trigger" "build_trigger" {
project = "my-project"
trigger_template {
branch_name = "master"
project = "my-project"
repo_name = "some-repo"
}
filename = "cloudbuild.yaml"
}
```


## Argument Reference

(Argument descriptions sourced from https://godoc.org/google.golang.org/api/cloudbuild/v1#BuildTrigger)
Expand All @@ -59,6 +74,10 @@ will be expanded when the build is created:
or resolved from the specified branch or tag.
* `$SHORT_SHA`: first 7 characters of `$REVISION_ID` or `$COMMIT_SHA`.

* `filename` - (Optional) Specify the path to a Cloud Build configuration file
in the Git repo. This is mutually exclusive with `build`. This is typically
`cloudbuild.yaml` however it can be specified by the user.

---

The `trigger_template` block supports:
Expand Down