Skip to content

Commit 1748b95

Browse files
updated datasource generation to handle certain other code blocks (#14800)
1 parent ebcea8f commit 1748b95

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

mmv1/api/resource.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ type Resource struct {
227227
ExcludeImport bool `yaml:"exclude_import,omitempty"`
228228

229229
// If true, resource should be autogenerated as a data source
230-
GenerateDatasource bool `yaml:"generate_datasource,omitempty"`
230+
Datasource *resource.Datasource `yaml:"datasource,omitempty"`
231231

232232
// If true, skip sweeper generation for this resource
233233
ExcludeSweeper bool `yaml:"exclude_sweeper,omitempty"`
@@ -2078,8 +2078,31 @@ func urlContainsOnlyAllowedKeys(templateURL string, allowedKeys []string) bool {
20782078
return true
20792079
}
20802080

2081-
func (r Resource) ShouldGenerateSingularDataSource() bool {
2082-
return r.GenerateDatasource
2081+
func (r *Resource) ShouldGenerateSingularDataSource() bool {
2082+
2083+
if r.Datasource == nil {
2084+
return false
2085+
}
2086+
2087+
return r.Datasource.Generate
2088+
}
2089+
2090+
func (r Resource) ShouldDatasourceSetLabels() bool {
2091+
for _, p := range r.Properties {
2092+
if p.Name == "labels" && p.Type == "KeyValueLabels" {
2093+
return true
2094+
}
2095+
}
2096+
return false
2097+
}
2098+
2099+
func (r Resource) ShouldDatasourceSetAnnotations() bool {
2100+
for _, p := range r.Properties {
2101+
if p.Name == "annotations" && p.Type == "KeyValueAnnotations" {
2102+
return true
2103+
}
2104+
}
2105+
return false
20832106
}
20842107

20852108
// DatasourceOptionalFields returns a list of fields from the resource's URI

mmv1/api/resource/datasource.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 Google Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package resource
15+
16+
type Datasource struct {
17+
// boolean to determine whether the datasource file should be generated
18+
Generate bool `yaml:"generate"`
19+
}

mmv1/products/iap/Client.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ self_link: '{{brand}}/identityAwareProxyClients/{{client_id}}'
3333
immutable: true
3434
import_format:
3535
- '{{brand}}/identityAwareProxyClients/{{client_id}}'
36-
generate_datasource: true
36+
datasource:
37+
generate: true
3738
timeouts:
3839
insert_minutes: 20
3940
update_minutes: 20

mmv1/templates/terraform/datasource.go.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func dataSource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{
8484
return err
8585
}
8686

87+
{{if $.ShouldDatasourceSetLabels}}
88+
if err := tpgresource.SetDataSourceLabels(d); err != nil {
89+
return err
90+
}
91+
{{end}}
92+
93+
{{if $.ShouldDatasourceSetAnnotations}}
94+
if err := tpgresource.SetDataSourceAnnotations(d); err != nil {
95+
return err
96+
}
97+
{{end}}
98+
8799
if d.Id() == "" {
88100
return fmt.Errorf("%s not found", id)
89101
}

0 commit comments

Comments
 (0)