Skip to content

Commit d5c0cb2

Browse files
Data source for CloudBuild Trigger (#7003) (#13329)
* added data source for cloud build trigger * added the data source function in provider.go * added doc * addressing the review changes * minor fix * updating the docs * fixing the linting errors * fixing the linting errors Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 8552c05 commit d5c0cb2

5 files changed

+134
-0
lines changed

.changelog/7003.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_cloudbuild_trigger`
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGoogleCloudBuildTrigger() *schema.Resource {
11+
12+
dsSchema := datasourceSchemaFromResourceSchema(resourceCloudBuildTrigger().Schema)
13+
14+
addRequiredFieldsToSchema(dsSchema, "trigger_id", "location")
15+
addOptionalFieldsToSchema(dsSchema, "project")
16+
17+
return &schema.Resource{
18+
Read: dataSourceGoogleCloudBuildTriggerRead,
19+
Schema: dsSchema,
20+
}
21+
22+
}
23+
24+
func dataSourceGoogleCloudBuildTriggerRead(d *schema.ResourceData, meta interface{}) error {
25+
config := meta.(*Config)
26+
27+
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/triggers/{{trigger_id}}")
28+
if err != nil {
29+
return fmt.Errorf("Error constructing id: %s", err)
30+
}
31+
32+
id = strings.ReplaceAll(id, "/locations/global/", "/")
33+
34+
d.SetId(id)
35+
return resourceCloudBuildTriggerRead(d, meta)
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGoogleCloudBuildTrigger_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"random_suffix": randString(t, 10),
14+
}
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckCloudRunServiceDestroyProducer(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataSourceGoogleCloudBuildTrigger_basic(context),
23+
Check: resource.ComposeTestCheckFunc(
24+
checkDataSourceStateMatchesResourceState("data.google_cloudbuild_trigger.foo", "google_cloudbuild_trigger.test-trigger"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceGoogleCloudBuildTrigger_basic(context map[string]interface{}) string {
32+
return Nprintf(`
33+
resource "google_cloudbuild_trigger" "test-trigger" {
34+
location = "us-central1"
35+
name = "manual-build%{random_suffix}"
36+
trigger_template {
37+
branch_name = "main"
38+
repo_name = "my-repo"
39+
}
40+
41+
substitutions = {
42+
_FOO = "bar"
43+
_BAZ = "qux"
44+
}
45+
46+
filename = "cloudbuild.yaml"
47+
}
48+
49+
data "google_cloudbuild_trigger" "foo" {
50+
location = google_cloudbuild_trigger.test-trigger.location
51+
trigger_id = google_cloudbuild_trigger.test-trigger.trigger_id
52+
}`, context)
53+
54+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ func Provider() *schema.Provider {
828828
"google_bigquery_default_service_account": dataSourceGoogleBigqueryDefaultServiceAccount(),
829829
"google_client_config": dataSourceGoogleClientConfig(),
830830
"google_client_openid_userinfo": dataSourceGoogleClientOpenIDUserinfo(),
831+
"google_cloudbuild_trigger": dataSourceGoogleCloudBuildTrigger(),
831832
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
832833
"google_cloudfunctions2_function": dataSourceGoogleCloudFunctions2Function(),
833834
"google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Cloud Build"
3+
page_title: "Google: google_cloudbuild_trigger"
4+
description: |-
5+
Get information about a Google CloudBuild Trigger.
6+
---
7+
8+
# google\_cloudbuild\_trigger
9+
10+
To get more information about Cloudbuild Trigger, see:
11+
12+
* [API documentation](https://cloud.google.com/build/docs/api/reference/rest/v1/projects.triggers)
13+
* How-to Guides
14+
* [Official Documentation](https://cloud.google.com/build/docs/automating-builds/create-manage-triggers)
15+
16+
## Example Usage
17+
18+
```hcl
19+
data "google_cloudbuild_trigger" "name" {
20+
project = "your-project-id"
21+
trigger_id = google_cloudbuild_trigger.filename-trigger.trigger_id
22+
location = "location of trigger build"
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
The following arguments are supported:
29+
30+
* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
31+
32+
* `trigger_id` - (Required) The unique identifier for the trigger..
33+
34+
* `location` - (Required) The Cloud Build location for the trigger.
35+
36+
- - -
37+
38+
## Attributes Reference
39+
40+
See [google_cloudbuild_trigger](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudbuild_trigger#project) resource for details of the available attributes.

0 commit comments

Comments
 (0)