1+ {{/* The license inside this block applies to this file
2+ Copyright 2024 Google LLC. All Rights Reserved.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License. */ -}}
15+ // Copyright (c) HashiCorp, Inc.
16+ // SPDX-License-Identifier: MPL-2.0
17+
18+ {{$ .CodeHeader TemplatePath}}
19+ package {{ lower $ .ProductMetadata.Name }}
20+
21+ import (
22+
23+ " fmt"
24+ " log"
25+ " net/http"
26+ " reflect"
27+ {{- if $ .SupportsIndirectUserProjectOverride }}
28+ " regexp"
29+ {{- end }}
30+ {{- if or (and (not $ .Immutable ) ($ .UpdateMask )) $ .LegacyLongFormProject }}
31+ " strings"
32+ {{- end }}
33+ " time"
34+
35+ {{/* # We list all the v2 imports here, because we run 'goimports' to guess the correct */ }}
36+ {{/* # set of imports, which will never guess the major version correctly. */ }}
37+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
38+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
39+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
40+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
41+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
42+ " github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
43+ " github.com/hashicorp/terraform-plugin-sdk/v2/diag"
44+ " github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
45+ " github.com/hashicorp/go-cty/cty"
46+
47+ " {{ $.ImportPath }}/tpgresource"
48+ transport_tpg " {{ $.ImportPath }}/transport"
49+ " {{ $.ImportPath }}/verify"
50+
51+ {{ if $ .FlattenedProperties }}
52+ " google.golang.org/api/googleapi"
53+ {{- end }}
54+ )
55+
56+ func DataSource{{ .ResourceName -}}() *schema.Resource {
57+ rs := Resource{{ .ResourceName -}}().Schema
58+
59+ dsSchema := tpgresource.DatasourceSchemaFromResourceSchema (rs)
60+
61+ // Set 'Required' schema elements
62+ tpgresource.AddRequiredFieldsToSchema (dsSchema, {{range $index , $field := .DatasourceRequiredFields }}{{if gt $index 0}}, {{end }}{{printf " %q " $field }}{{end }})
63+
64+ // Set 'Optional' schema elements
65+ tpgresource.AddOptionalFieldsToSchema (dsSchema, {{range $index , $field := .DatasourceOptionalFields }}{{if gt $index 0}}, {{end }}{{printf " %q " $field }}{{end }})
66+
67+ return &schema.Resource {
68+ Read: dataSource{{ $ .ResourceName -}}Read,
69+ Schema: dsSchema,
70+ }
71+ }
72+
73+ func dataSource{{ $ .ResourceName -}}Read(d *schema.ResourceData , meta interface{}) error {
74+ config := meta. (*transport_tpg.Config )
75+
76+ id, err := tpgresource.ReplaceVars {{if $ .LegacyLongFormProject -}}ForId{{ end -}}(d, config, " {{$.SelfLinkUri}}{{$.ReadQueryParams}}" )
77+ if err != nil {
78+ return err
79+ }
80+ d.SetId (id)
81+
82+ err = resource{{ $ .ResourceName -}}Read(d, meta)
83+ if err != nil {
84+ return err
85+ }
86+
87+ if d.Id () == " " {
88+ return fmt.Errorf (" %s not found" , id)
89+ }
90+
91+ return nil
92+ }
0 commit comments