Skip to content

Commit

Permalink
Add service_account to google_cloudbuild_trigger (#5237) (#10159)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 24, 2021
1 parent 7efe98e commit e64f9e9
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5237.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudbuild: added field `service_account` to `google_cloudbuild_trigger`
```
34 changes: 34 additions & 0 deletions google/resource_cloudbuild_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,17 @@ Only populated on get requests.`,
},
ExactlyOneOf: []string{"trigger_template", "github", "pubsub_config", "webhook_config"},
},
"service_account": {
Type: schema.TypeString,
Optional: true,
Description: `The service account used for all user-controlled operations including
triggers.patch, triggers.run, builds.create, and builds.cancel.
If no service account is set, then the standard Cloud Build service account
([PROJECT_NUM]@system.gserviceaccount.com) will be used instead.
Format: projects/{PROJECT_ID}/serviceAccounts/{ACCOUNT_ID_OR_EMAIL}`,
},
"substitutions": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -963,6 +974,12 @@ func resourceCloudBuildTriggerCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("substitutions"); !isEmptyValue(reflect.ValueOf(substitutionsProp)) && (ok || !reflect.DeepEqual(v, substitutionsProp)) {
obj["substitutions"] = substitutionsProp
}
serviceAccountProp, err := expandCloudBuildTriggerServiceAccount(d.Get("service_account"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("service_account"); !isEmptyValue(reflect.ValueOf(serviceAccountProp)) && (ok || !reflect.DeepEqual(v, serviceAccountProp)) {
obj["serviceAccount"] = serviceAccountProp
}
filenameProp, err := expandCloudBuildTriggerFilename(d.Get("filename"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1120,6 +1137,9 @@ func resourceCloudBuildTriggerRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("substitutions", flattenCloudBuildTriggerSubstitutions(res["substitutions"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
if err := d.Set("service_account", flattenCloudBuildTriggerServiceAccount(res["serviceAccount"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
if err := d.Set("filename", flattenCloudBuildTriggerFilename(res["filename"], d, config)); err != nil {
return fmt.Errorf("Error reading Trigger: %s", err)
}
Expand Down Expand Up @@ -1194,6 +1214,12 @@ func resourceCloudBuildTriggerUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("substitutions"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, substitutionsProp)) {
obj["substitutions"] = substitutionsProp
}
serviceAccountProp, err := expandCloudBuildTriggerServiceAccount(d.Get("service_account"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("service_account"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, serviceAccountProp)) {
obj["serviceAccount"] = serviceAccountProp
}
filenameProp, err := expandCloudBuildTriggerFilename(d.Get("filename"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1352,6 +1378,10 @@ func flattenCloudBuildTriggerSubstitutions(v interface{}, d *schema.ResourceData
return v
}

func flattenCloudBuildTriggerServiceAccount(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudBuildTriggerFilename(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -2055,6 +2085,10 @@ func expandCloudBuildTriggerSubstitutions(v interface{}, d TerraformResourceData
return m, nil
}

func expandCloudBuildTriggerServiceAccount(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerFilename(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
56 changes: 56 additions & 0 deletions google/resource_cloudbuild_trigger_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,62 @@ resource "google_cloudbuild_trigger" "build-trigger" {
`, context)
}

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

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudBuildTriggerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccCloudBuildTrigger_cloudbuildTriggerServiceAccountExample(context),
},
{
ResourceName: "google_cloudbuild_trigger.service-account-trigger",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCloudBuildTrigger_cloudbuildTriggerServiceAccountExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloudbuild_trigger" "service-account-trigger" {
trigger_template {
branch_name = "master"
repo_name = "my-repo"
}
service_account = google_service_account.cloudbuild_service_account.id
filename = "cloudbuild.yaml"
depends_on = [
google_project_iam_member.act_as,
google_project_iam_member.logs_writer
]
}
resource "google_service_account" "cloudbuild_service_account" {
account_id = "my-service-account"
}
resource "google_project_iam_member" "act_as" {
role = "roles/iam.serviceAccountUser"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}
resource "google_project_iam_member" "logs_writer" {
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}
`, context)
}

func testAccCheckCloudBuildTriggerDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
45 changes: 45 additions & 0 deletions website/docs/r/cloudbuild_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,43 @@ resource "google_cloudbuild_trigger" "build-trigger" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=cloudbuild_trigger_service_account&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Cloudbuild Trigger Service Account


```hcl
resource "google_cloudbuild_trigger" "service-account-trigger" {
trigger_template {
branch_name = "master"
repo_name = "my-repo"
}
service_account = google_service_account.cloudbuild_service_account.id
filename = "cloudbuild.yaml"
depends_on = [
google_project_iam_member.act_as,
google_project_iam_member.logs_writer
]
}
resource "google_service_account" "cloudbuild_service_account" {
account_id = "my-service-account"
}
resource "google_project_iam_member" "act_as" {
role = "roles/iam.serviceAccountUser"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}
resource "google_project_iam_member" "logs_writer" {
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}
```

## Argument Reference

Expand Down Expand Up @@ -154,6 +191,14 @@ The following arguments are supported:
(Optional)
Substitutions data for Build resource.

* `service_account` -
(Optional)
The service account used for all user-controlled operations including
triggers.patch, triggers.run, builds.create, and builds.cancel.
If no service account is set, then the standard Cloud Build service account
([PROJECT_NUM]@system.gserviceaccount.com) will be used instead.
Format: projects/{PROJECT_ID}/serviceAccounts/{ACCOUNT_ID_OR_EMAIL}

* `filename` -
(Optional)
Path, from the source root, to a file whose contents is used for the template. Either a filename or build template must be provided.
Expand Down

0 comments on commit e64f9e9

Please sign in to comment.