-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
3b80066
commit fdf2ef0
Showing
6 changed files
with
186 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-datasource | ||
`google_cloudfunctions2_function` | ||
``` |
41 changes: 41 additions & 0 deletions
41
google-beta/data_source_google_cloudfunctions2_function.go
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,41 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleCloudFunctions2Function() *schema.Resource { | ||
// Generate datasource schema from resource | ||
dsSchema := datasourceSchemaFromResourceSchema(resourceCloudfunctions2function().Schema) | ||
|
||
// Set 'Required' schema elements | ||
addRequiredFieldsToSchema(dsSchema, "name", "location") | ||
|
||
// Set 'Optional' schema elements | ||
addOptionalFieldsToSchema(dsSchema, "project") | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceGoogleCloudFunctions2FunctionRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func dataSourceGoogleCloudFunctions2FunctionRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(fmt.Sprintf("projects/%s/locations/%s/functions/%s", project, d.Get("location").(string), d.Get("name").(string))) | ||
|
||
err = resourceCloudfunctions2functionRead(d, meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
75 changes: 75 additions & 0 deletions
75
google-beta/data_source_google_cloudfunctions2_function_test.go
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,75 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGoogleCloudFunctions2Function_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
funcDataNameHttp := "data.google_cloudfunctions2_function.function_http_v2" | ||
functionName := fmt.Sprintf("tf-test-%s", randString(t, 10)) | ||
bucketName := fmt.Sprintf("tf-test-bucket-%d", randInt(t)) | ||
zipFilePath := "./test-fixtures/cloudfunctions2/function-source.zip" | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudfunctions2functionDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleCloudFunctions2FunctionConfig(functionName, | ||
bucketName, zipFilePath), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkDataSourceStateMatchesResourceStateWithIgnores(funcDataNameHttp, | ||
"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": {}}), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleCloudFunctions2FunctionConfig(functionName, bucketName, zipFilePath string) string { | ||
return fmt.Sprintf(` | ||
resource "google_storage_bucket" "bucket" { | ||
name = "%s" | ||
location = "US" | ||
} | ||
resource "google_storage_bucket_object" "object" { | ||
name = "function-source.zip" | ||
bucket = google_storage_bucket.bucket.name | ||
source = "%s" | ||
} | ||
resource "google_cloudfunctions2_function" "function_http_v2" { | ||
name = "%s" | ||
location = "us-central1" | ||
description = "a new function" | ||
build_config { | ||
runtime = "nodejs12" | ||
entry_point = "helloHttp" | ||
source { | ||
storage_source { | ||
bucket = google_storage_bucket.bucket.name | ||
object = google_storage_bucket_object.object.name | ||
} | ||
} | ||
} | ||
service_config { | ||
max_instance_count = 1 | ||
available_memory = "256Mi" | ||
timeout_seconds = 60 | ||
} | ||
} | ||
data "google_cloudfunctions2_function" "function_http_v2" { | ||
name = google_cloudfunctions2_function.function_http_v2.name | ||
location = "us-central1" | ||
} | ||
`, bucketName, zipFilePath, functionName) | ||
} |
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
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,38 @@ | ||
--- | ||
subcategory: "Cloud Functions (2nd gen)" | ||
page_title: "Google: google_cloudfunctions2_function" | ||
description: |- | ||
Get information about a Google Cloud Function (2nd gen). | ||
--- | ||
|
||
# google\_cloudfunctions2\_function | ||
|
||
Get information about a Google Cloud Function (2nd gen). For more information see: | ||
|
||
* [API documentation](https://cloud.google.com/functions/docs/reference/rest/v2beta/projects.locations.functions). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "google_cloudfunctions2_function" "my-function" { | ||
name = "function" | ||
location = "us-central1" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of a Cloud Function (2nd gen). | ||
|
||
* `location` - (Required) The location in which the resource belongs. | ||
|
||
- - - | ||
|
||
* `project` - (Optional) The project in which the resource belongs. If it | ||
is not provided, the provider project is used. | ||
|
||
## Attributes Reference | ||
|
||
See [google_cloudfunctions2_function](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions2_function) resource for details of all the available attributes. |