Skip to content

Commit 367bba5

Browse files
authored
Revert "tgc-revival: Set the fields to state and then read state to hcl data during cai2hcl" (#15999)
1 parent 2e26cb9 commit 367bba5

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

mmv1/products/compute/BackendService.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ custom_code:
5555
decoder: 'templates/terraform/decoders/backend_service.go.tmpl'
5656
post_create: 'templates/terraform/post_create/compute_backend_service_security_policy.go.tmpl'
5757
post_update: 'templates/terraform/post_create/compute_backend_service_security_policy.go.tmpl'
58+
tgc_decoder: 'templates/tgc_next/decoders/compute_backend_service.go.tmpl'
5859
schema_version: 1
5960
examples:
6061
- name: 'backend_service_basic'

mmv1/templates/tgc_next/cai2hcl/resource_converter.go.tmpl

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func (c *{{ $.ResourceName -}}Cai2hclConverter) convertResourceData(asset caiass
143143
}
144144
{{ end}}
145145

146+
{{/* Attempt to parse all self-link parameters from asset name. */}}
147+
outputFields := {{ $.OutputFieldSetStr }}
148+
utils.ParseUrlParamValuesFromAssetName(asset.Name, "{{ $.Cai2hclAssetNameTemplate }}", outputFields, hclData)
146149
{{ range $prop := $.ReadPropertiesForTgc }}
147150
{{ if $prop.FlattenObject -}}
148151
if flattenedProp := flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config); flattenedProp != nil {
@@ -151,22 +154,10 @@ func (c *{{ $.ResourceName -}}Cai2hclConverter) convertResourceData(asset caiass
151154
}
152155
}
153156
{{- else -}}
154-
if err := d.Set("{{ underscore $prop.Name -}}", flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config)); err != nil {
155-
return nil, fmt.Errorf("Error reading {{ $.Name -}}: %s", err)
156-
}
157+
hclData["{{ underscore $prop.Name -}}"] = flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config)
157158
{{- end}}
158159
{{- end}}
159160

160-
for key, sch := range c.schema {
161-
if val, ok := d.GetOk(key); ok || sch.Required {
162-
hclData[key] = val
163-
}
164-
}
165-
166-
{{/* Attempt to parse all self-link parameters from asset name. */}}
167-
outputFields := {{ $.OutputFieldSetStr }}
168-
utils.ParseUrlParamValuesFromAssetName(asset.Name, "{{ $.Cai2hclAssetNameTemplate }}", outputFields, hclData)
169-
170161
ctyVal, err := utils.MapToCtyValWithSchema(hclData, c.schema)
171162
if err != nil {
172163
return nil, err
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// billing_project is the required url_param_only property, but is not in CAI asset name or data
22
// TODO: handle it in a generic way
3-
d.Set("billing_project", "null")
3+
hclData["billing_project"] = "null"
44
return res, hclData, nil
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{/*
2+
The license inside this block applies to this file
3+
Copyright 2025 Google Inc.
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 http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/ -}}
13+
14+
if v, ok := res["backends"]; ok {
15+
backends := v.([]interface{})
16+
for _, vBackend := range backends {
17+
backend := vBackend.(map[string]interface{})
18+
if vCms, ok := backend["customMetrics"]; ok {
19+
cms := vCms.([]interface{})
20+
for _, vCm := range cms {
21+
cm := vCm.(map[string]interface{})
22+
if vMu, ok := cm["maxUtilization"]; ok {
23+
mu := vMu.(float64)
24+
cm["maxUtilization"] = fmt.Sprintf("%.1f", mu)
25+
}
26+
}
27+
}
28+
}
29+
}
30+
31+
return res, hclData, nil

mmv1/templates/tgc_next/decoders/compute_route.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
if v, ok := res["nextHopInstance"]; ok {
2+
val, err := tpgresource.ParseZonalFieldValue("instances", v.(string), "project", "next_hop_instance_zone", d, meta.(*transport_tpg.Config), true)
3+
if err != nil {
4+
return nil, nil, err
5+
}
6+
hclData["next_hop_instance_zone"] = val.Zone
7+
res["nextHopInstance"] = val.RelativeLink()
8+
}
9+
110
// next_hop_ip is Computed + Optional and conflict with next_hop_ilb
211
if _, ok := res["nextHopIp"]; ok {
312
if _, ok := res["nextHopIlb"]; ok {

0 commit comments

Comments
 (0)