forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_source_google_cloudfunctions2_function.go
52 lines (39 loc) · 1.35 KB
/
data_source_google_cloudfunctions2_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}