Skip to content

Commit b216734

Browse files
Added datasource files for cloudfunctions2_function (#6523) (#12673)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 1e3c865 commit b216734

6 files changed

+186
-2
lines changed

.changelog/6523.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_cloudfunctions2_function`
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceGoogleCloudFunctions2Function() *schema.Resource {
10+
// Generate datasource schema from resource
11+
dsSchema := datasourceSchemaFromResourceSchema(resourceCloudfunctions2function().Schema)
12+
13+
// Set 'Required' schema elements
14+
addRequiredFieldsToSchema(dsSchema, "name", "location")
15+
16+
// Set 'Optional' schema elements
17+
addOptionalFieldsToSchema(dsSchema, "project")
18+
19+
return &schema.Resource{
20+
Read: dataSourceGoogleCloudFunctions2FunctionRead,
21+
Schema: dsSchema,
22+
}
23+
}
24+
25+
func dataSourceGoogleCloudFunctions2FunctionRead(d *schema.ResourceData, meta interface{}) error {
26+
config := meta.(*Config)
27+
28+
project, err := getProject(d, config)
29+
if err != nil {
30+
return err
31+
}
32+
33+
d.SetId(fmt.Sprintf("projects/%s/locations/%s/functions/%s", project, d.Get("location").(string), d.Get("name").(string)))
34+
35+
err = resourceCloudfunctions2functionRead(d, meta)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccDataSourceGoogleCloudFunctions2Function_basic(t *testing.T) {
11+
t.Parallel()
12+
13+
funcDataNameHttp := "data.google_cloudfunctions2_function.function_http_v2"
14+
functionName := fmt.Sprintf("tf-test-%s", randString(t, 10))
15+
bucketName := fmt.Sprintf("tf-test-bucket-%d", randInt(t))
16+
zipFilePath := "./test-fixtures/cloudfunctions2/function-source.zip"
17+
18+
vcrTest(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCheckCloudfunctions2functionDestroyProducer(t),
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceGoogleCloudFunctions2FunctionConfig(functionName,
25+
bucketName, zipFilePath),
26+
Check: resource.ComposeTestCheckFunc(
27+
checkDataSourceStateMatchesResourceStateWithIgnores(funcDataNameHttp,
28+
"google_cloudfunctions2_function.function_http_v2", map[string]struct{}{"build_config.0.source.0.storage_source.0.bucket": {}, "build_config.0.source.0.storage_source.0.object": {}}),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccDataSourceGoogleCloudFunctions2FunctionConfig(functionName, bucketName, zipFilePath string) string {
36+
return fmt.Sprintf(`
37+
resource "google_storage_bucket" "bucket" {
38+
name = "%s"
39+
location = "US"
40+
}
41+
42+
resource "google_storage_bucket_object" "object" {
43+
name = "function-source.zip"
44+
bucket = google_storage_bucket.bucket.name
45+
source = "%s"
46+
}
47+
48+
resource "google_cloudfunctions2_function" "function_http_v2" {
49+
name = "%s"
50+
location = "us-central1"
51+
description = "a new function"
52+
53+
build_config {
54+
runtime = "nodejs12"
55+
entry_point = "helloHttp"
56+
source {
57+
storage_source {
58+
bucket = google_storage_bucket.bucket.name
59+
object = google_storage_bucket_object.object.name
60+
}
61+
}
62+
}
63+
64+
service_config {
65+
max_instance_count = 1
66+
available_memory = "256Mi"
67+
timeout_seconds = 60
68+
}
69+
}
70+
data "google_cloudfunctions2_function" "function_http_v2" {
71+
name = google_cloudfunctions2_function.function_http_v2.name
72+
location = "us-central1"
73+
}
74+
`, bucketName, zipFilePath, functionName)
75+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ func Provider() *schema.Provider {
780780
"google_client_config": dataSourceGoogleClientConfig(),
781781
"google_client_openid_userinfo": dataSourceGoogleClientOpenIDUserinfo(),
782782
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
783+
"google_cloudfunctions2_function": dataSourceGoogleCloudFunctions2Function(),
783784
"google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(),
784785
"google_cloud_identity_group_memberships": dataSourceGoogleCloudIdentityGroupMemberships(),
785786
"google_cloud_run_locations": dataSourceGoogleCloudRunLocations(),

google/resource_cloudfunctions2_function.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,37 @@ func flattenCloudfunctions2functionBuildConfigSourceStorageSource(v interface{},
905905
}
906906

907907
func flattenCloudfunctions2functionBuildConfigSourceStorageSourceBucket(v interface{}, d *schema.ResourceData, config *Config) interface{} {
908-
return d.Get("build_config.0.source.0.storage_source.0.bucket")
908+
// This flatten function is shared between the resource and the datasource.
909+
// TF Input format: {bucket-name}
910+
// GET Response format: gcf-v2-sources-{Project-number}-{location}
911+
// As TF Input and GET response values have different format,
912+
// we will return TF Input value to prevent state drift.
913+
914+
if bVal, ok := d.GetOk("build_config.0.source.0.storage_source.0.bucket"); ok {
915+
return bVal
916+
}
917+
918+
// For the datasource, there is no prior TF Input for this attribute.
919+
// Hence, GET Response value is returned.
920+
921+
return v
909922
}
910923

911924
func flattenCloudfunctions2functionBuildConfigSourceStorageSourceObject(v interface{}, d *schema.ResourceData, config *Config) interface{} {
912-
return d.Get("build_config.0.source.0.storage_source.0.object")
925+
// This flatten function is shared between the resource and the datasource.
926+
// TF Input format: {object-name}
927+
// GET Response format: {function-name}/{object-name}
928+
// As TF Input and GET response values have different format,
929+
// we will return TF Input value to prevent state drift.
930+
931+
if ObjVal, ok := d.GetOk("build_config.0.source.0.storage_source.0.object"); ok {
932+
return ObjVal
933+
}
934+
935+
// For the datasource, there is no prior TF Input for this attribute.
936+
// Hence, GET Response value is returned.
937+
938+
return v
913939
}
914940

915941
func flattenCloudfunctions2functionBuildConfigSourceStorageSourceGeneration(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
subcategory: "Cloud Functions (2nd gen)"
3+
page_title: "Google: google_cloudfunctions2_function"
4+
description: |-
5+
Get information about a Google Cloud Function (2nd gen).
6+
---
7+
8+
# google\_cloudfunctions2\_function
9+
10+
Get information about a Google Cloud Function (2nd gen). For more information see:
11+
12+
* [API documentation](https://cloud.google.com/functions/docs/reference/rest/v2beta/projects.locations.functions).
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "google_cloudfunctions2_function" "my-function" {
18+
name = "function"
19+
location = "us-central1"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `name` - (Required) The name of a Cloud Function (2nd gen).
28+
29+
* `location` - (Required) The location in which the resource belongs.
30+
31+
- - -
32+
33+
* `project` - (Optional) The project in which the resource belongs. If it
34+
is not provided, the provider project is used.
35+
36+
## Attributes Reference
37+
38+
See [google_cloudfunctions2_function](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions2_function) resource for details of all the available attributes.

0 commit comments

Comments
 (0)