Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/content/convert/add-new-resource-tgc.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ To resolve integration test failures, you need to apply the correct override con

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.

* Ignore Default Terraform Decoder

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.

* Custom Decoder (CAI → GET API object Mismatch)

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.
Expand Down
3 changes: 3 additions & 0 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ type TGCResource struct {
// If true, the Terraform custom encoder is not applied during tfplan2cai
TGCIgnoreTerraformEncoder bool `yaml:"tgc_ignore_terraform_encoder,omitempty"`

// If true, the Terraform custom decoder is not applied during cai2hcl
TGCIgnoreTerraformDecoder bool `yaml:"tgc_ignore_terraform_decoder,omitempty"`

// [Optional] The parameter that uniquely identifies the resource.
// Generally, it shouldn't be set when the identity can be decided.
// Otherswise, it should be set.
Expand Down
3 changes: 3 additions & 0 deletions mmv1/products/dataproc/Batch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async:
resource_inside_response: false
collection_url_key: 'batches'
include_in_tgc_next_DO_NOT_USE: true
tgc_ignore_terraform_decoder: true
custom_code:
constants: 'templates/terraform/constants/cloud_dataproc_batch.go.tmpl'
decoder: 'templates/terraform/decoders/cloud_dataproc_batch.go.tmpl'
Expand Down Expand Up @@ -289,6 +290,8 @@ properties:
type: KeyValuePairs
description: |
A mapping of property names to values, which are used to configure workload execution.
custom_tgc_expand: 'templates/tgc_next/custom_expand/dataproc_batch.go.tmpl'
custom_tgc_flatten: 'templates/tgc_next/custom_flatten/dataproc_batch.go.tmpl'
- name: 'effective_properties'
type: KeyValuePairs
description: |
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/pubsub/Topic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
- 'avro_format'
- 'pubsub_avro_format'
properties:
# Meant to be an empty object with no properties.

Check warning on line 294 in mmv1/products/pubsub/Topic.yaml

View workflow job for this annotation

GitHub Actions / lint-yaml

294:2 [comments-indentation] comment not indented like content
[]
- name: 'pubsubAvroFormat'
type: NestedObject
Expand All @@ -307,7 +307,7 @@
- 'avro_format'
- 'pubsub_avro_format'
properties:
# Meant to be an empty object with no properties.

Check warning on line 310 in mmv1/products/pubsub/Topic.yaml

View workflow job for this annotation

GitHub Actions / lint-yaml

310:2 [comments-indentation] comment not indented like content
[]
- name: 'minimumObjectCreateTime'
type: String
Expand Down Expand Up @@ -469,6 +469,7 @@
description: |
Javascript User Defined Function. If multiple Javascript UDFs are specified on a resource,
each one must have a unique `function_name`.
is_missing_in_cai: true
properties:
- name: functionName
type: String
Expand Down
12 changes: 9 additions & 3 deletions mmv1/templates/tgc_next/cai2hcl/resource_converter.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ func (c *{{ $.ResourceName -}}Cai2hclConverter) convertResourceData(asset caiass
var err error
res := asset.Resource.Data
config := transport.NewConfig()
d := &schema.ResourceData{}

// This is a fake resource used to get fake d
// d.Get will return empty map, instead of nil
fakeResource := &schema.Resource{
Schema: c.schema,
}
d := fakeResource.TestResourceData()

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

{{ if $.CustomCode.Decoder -}}
{{ if and $.CustomCode.Decoder (not $.TGCIgnoreTerraformDecoder) -}}
res, err = resource{{ $.ResourceName -}}Decoder(d, config, res)
if err != nil {
return nil, err
Expand Down Expand Up @@ -145,7 +151,7 @@ func resource{{ $.ResourceName -}}TgcDecoder(d *schema.ResourceData, meta interf
}
{{- end }}

{{- if $.CustomCode.Decoder }}
{{- if and $.CustomCode.Decoder (not $.TGCIgnoreTerraformDecoder) }}
func resource{{ $.ResourceName -}}Decoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
{{ $.CustomTemplate $.CustomCode.Decoder false -}}
}
Expand Down
12 changes: 12 additions & 0 deletions mmv1/templates/tgc_next/custom_expand/dataproc_batch.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
// In CAI asset, the properties have prefix "spark:" (e.g. spark:spark.dynamicAllocation.enabled)
modifiedK := fmt.Sprintf("spark:%s", k)
m[modifiedK] = val.(string)
}
return m, nil
}
14 changes: 14 additions & 0 deletions mmv1/templates/tgc_next/custom_flatten/dataproc_batch.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
func flatten{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if properties, ok := v.(map[string]interface{}); ok {
propertiesCopy := make(map[string]interface{})
for k, v := range properties {
// Remove the prefix "spark:" from the properties. Otherwise, terraform apply will fail with the error from API.
modifiedK := strings.TrimPrefix(k, "spark:")
propertiesCopy[modifiedK] = v
}
return propertiesCopy
}


return v
}
Loading