Skip to content

Commit 29fc5fc

Browse files
authored
tgc-revival: use testresource data for cai2hcl (#15391)
1 parent 84bceb8 commit 29fc5fc

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

docs/content/convert/add-new-resource-tgc.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ To resolve integration test failures, you need to apply the correct override con
304304

305305
If the default Terraform encoding applied during conversion is causing issues, disable it by adding `tgc_ignore_terraform_encoder: true` to the Resource.yaml file.
306306

307+
* Ignore Default Terraform Decoder
308+
309+
If the default Terraform decoding applied during conversion is causing issues, disable it by adding `tgc_ignore_terraform_decoder: true` to the Resource.yaml file.
310+
307311
* Custom Decoder (CAI → GET API object Mismatch)
308312

309313
This is used when the value of a field in a CAI asset is different from the value required in the final API object during cai2hcl.

mmv1/api/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ type TGCResource struct {
386386
// If true, the Terraform custom encoder is not applied during tfplan2cai
387387
TGCIgnoreTerraformEncoder bool `yaml:"tgc_ignore_terraform_encoder,omitempty"`
388388

389+
// If true, the Terraform custom decoder is not applied during cai2hcl
390+
TGCIgnoreTerraformDecoder bool `yaml:"tgc_ignore_terraform_decoder,omitempty"`
391+
389392
// [Optional] The parameter that uniquely identifies the resource.
390393
// Generally, it shouldn't be set when the identity can be decided.
391394
// Otherswise, it should be set.

mmv1/products/dataproc/Batch.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async:
4545
resource_inside_response: false
4646
collection_url_key: 'batches'
4747
include_in_tgc_next_DO_NOT_USE: true
48+
tgc_ignore_terraform_decoder: true
4849
custom_code:
4950
constants: 'templates/terraform/constants/cloud_dataproc_batch.go.tmpl'
5051
decoder: 'templates/terraform/decoders/cloud_dataproc_batch.go.tmpl'
@@ -289,6 +290,8 @@ properties:
289290
type: KeyValuePairs
290291
description: |
291292
A mapping of property names to values, which are used to configure workload execution.
293+
custom_tgc_expand: 'templates/tgc_next/custom_expand/dataproc_batch.go.tmpl'
294+
custom_tgc_flatten: 'templates/tgc_next/custom_flatten/dataproc_batch.go.tmpl'
292295
- name: 'effective_properties'
293296
type: KeyValuePairs
294297
description: |

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ func (c *{{ $.ResourceName -}}Cai2hclConverter) convertResourceData(asset caiass
7979
var err error
8080
res := asset.Resource.Data
8181
config := transport.NewConfig()
82-
d := &schema.ResourceData{}
82+
83+
// This is a fake resource used to get fake d
84+
// d.Get will return empty map, instead of nil
85+
fakeResource := &schema.Resource{
86+
Schema: c.schema,
87+
}
88+
d := fakeResource.TestResourceData()
8389

8490
{{ if $.TgcHclBlockName -}}
8591
hclBlockName := res["{{ $.TgcHclBlockName -}}"].(string)
@@ -98,7 +104,7 @@ func (c *{{ $.ResourceName -}}Cai2hclConverter) convertResourceData(asset caiass
98104
}
99105
{{ end}}
100106

101-
{{ if $.CustomCode.Decoder -}}
107+
{{ if and $.CustomCode.Decoder (not $.TGCIgnoreTerraformDecoder) -}}
102108
res, err = resource{{ $.ResourceName -}}Decoder(d, config, res)
103109
if err != nil {
104110
return nil, err
@@ -145,7 +151,7 @@ func resource{{ $.ResourceName -}}TgcDecoder(d *schema.ResourceData, meta interf
145151
}
146152
{{- end }}
147153

148-
{{- if $.CustomCode.Decoder }}
154+
{{- if and $.CustomCode.Decoder (not $.TGCIgnoreTerraformDecoder) }}
149155
func resource{{ $.ResourceName -}}Decoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
150156
{{ $.CustomTemplate $.CustomCode.Decoder false -}}
151157
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2+
if v == nil {
3+
return map[string]string{}, nil
4+
}
5+
m := make(map[string]string)
6+
for k, val := range v.(map[string]interface{}) {
7+
// In CAI asset, the properties have prefix "spark:" (e.g. spark:spark.dynamicAllocation.enabled)
8+
modifiedK := fmt.Sprintf("spark:%s", k)
9+
m[modifiedK] = val.(string)
10+
}
11+
return m, nil
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2+
if properties, ok := v.(map[string]interface{}); ok {
3+
propertiesCopy := make(map[string]interface{})
4+
for k, v := range properties {
5+
// Remove the prefix "spark:" from the properties. Otherwise, terraform apply will fail with the error from API.
6+
modifiedK := strings.TrimPrefix(k, "spark:")
7+
propertiesCopy[modifiedK] = v
8+
}
9+
return propertiesCopy
10+
}
11+
12+
13+
return v
14+
}

0 commit comments

Comments
 (0)