| // Copyright (c) HashiCorp, Inc. |
| // SPDX-License-Identifier: MPL-2.0 |
| package cloudfunctions2 |
| |
| import ( |
| "fmt" |
| |
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| ) |
| |
| func DataSourceGoogleCloudFunctions2Function() *schema.Resource { |
| // Generate datasource schema from resource |
| dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceCloudfunctions2function().Schema) |
| |
| // Set 'Required' schema elements |
| tpgresource.AddRequiredFieldsToSchema(dsSchema, "name", "location") |
| |
| // Set 'Optional' schema elements |
| tpgresource.AddOptionalFieldsToSchema(dsSchema, "project") |
| |
| return &schema.Resource{ |
| Read: dataSourceGoogleCloudFunctions2FunctionRead, |
| Schema: dsSchema, |
| } |
| } |
| |
| func dataSourceGoogleCloudFunctions2FunctionRead(d *schema.ResourceData, meta interface{}) error { |
| config := meta.(*transport_tpg.Config) |
| |
| project, err := tpgresource.GetProject(d, config) |
| if err != nil { |
| return err |
| } |
| |
| id := fmt.Sprintf("projects/%s/locations/%s/functions/%s", project, d.Get("location").(string), d.Get("name").(string)) |
| d.SetId(id) |
| |
| err = resourceCloudfunctions2functionRead(d, meta) |
| if err != nil { |
| return err |
| } |
| |
| if err := tpgresource.SetDataSourceLabels(d); err != nil { |
| return err |
| } |
| |
| if d.Id() == "" { |
| return fmt.Errorf("%s not found", id) |
| } |
| |
| return nil |
| } |