Skip to content

Commit 2086ef8

Browse files
authored
tgc-revival: add google_dataproc_batch (#15378)
1 parent 0a0e3c0 commit 2086ef8

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

mmv1/products/dataproc/Batch.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async:
4444
result:
4545
resource_inside_response: false
4646
collection_url_key: 'batches'
47+
include_in_tgc_next_DO_NOT_USE: true
4748
custom_code:
4849
constants: 'templates/terraform/constants/cloud_dataproc_batch.go.tmpl'
4950
decoder: 'templates/terraform/decoders/cloud_dataproc_batch.go.tmpl'
@@ -357,6 +358,7 @@ properties:
357358
the conditions are treated as OR conditions: the workload will be terminated when it has been idle for idleTtl or
358359
when ttl has been exceeded, whichever occurs first.
359360
default_from_api: true
361+
is_missing_in_cai: true
360362
- name: 'stagingBucket'
361363
type: String
362364
description: |
@@ -456,6 +458,7 @@ properties:
456458
description: |
457459
The arguments to pass to the driver. Do not include arguments that can be set as batch
458460
properties, such as --conf, since a collision can occur that causes an incorrect batch submission.
461+
is_missing_in_cai: true
459462
item_type:
460463
type: String
461464
- name: 'pythonFileUris'
@@ -496,6 +499,7 @@ properties:
496499
properties:
497500
- name: 'args'
498501
type: Array
502+
is_missing_in_cai: true
499503
description: |
500504
The arguments to pass to the driver. Do not include arguments that can be set as batch
501505
properties, such as --conf, since a collision can occur that causes an incorrect batch submission.
@@ -552,6 +556,7 @@ properties:
552556
description: |
553557
The arguments to pass to the driver. Do not include arguments that can be set as batch
554558
properties, such as --conf, since a collision can occur that causes an incorrect batch submission.
559+
is_missing_in_cai: true
555560
item_type:
556561
type: String
557562
- name: 'fileUris'
@@ -591,3 +596,4 @@ properties:
591596
type: KeyValuePairs
592597
description: |
593598
Mapping of query variable names to values (equivalent to the Spark SQL command: SET name="value";).
599+
is_missing_in_cai: true

mmv1/templates/terraform/decoders/cloud_dataproc_batch.go.tmpl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ if obj1, ok := res["runtimeConfig"]; ok {
2323

2424
// Update properties back to original client set properties
2525
originalPropertiesCopy := make(map[string]interface{})
26-
originalProperties := d.Get("runtime_config.0.properties").(interface{}).(map[string]interface{})
27-
for k, v := range originalProperties {
28-
originalPropertiesCopy[k] = v
26+
properties := d.Get("runtime_config.0.properties")
27+
if properties != nil {
28+
originalProperties := properties.(interface{}).(map[string]interface{})
29+
for k, v := range originalProperties {
30+
originalPropertiesCopy[k] = v
31+
}
32+
rconfig["properties"] = originalPropertiesCopy
2933
}
30-
rconfig["properties"] = originalPropertiesCopy
3134
return res, nil
3235
}
3336
}

mmv1/third_party/tgc_next/test/assert_test_files.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,18 @@ func testSingleResource(t *testing.T, testName string, testData ResourceTestData
227227
}
228228

229229
if diff := cmp.Diff(string(roundtripConfigData), string(exportConfigData)); diff != "" {
230-
log.Printf("Roundtrip config is different from the export config.\nroundtrip config:\n%s\nexport config:\n%s", string(roundtripConfigData), string(exportConfigData))
231-
return fmt.Errorf("test %s got diff (-want +got): %s", testName, diff)
230+
tfFileName := fmt.Sprintf("%s_roundtrip", testName)
231+
jsonFileName := fmt.Sprintf("%s_reexport", testName)
232+
233+
reexportAssets, err := tfplan2caiConvert(t, tfFileName, jsonFileName, tfDir, ancestryCache, defaultProject, logger)
234+
if err != nil {
235+
return fmt.Errorf("error when converting the third round-trip config: %#v", err)
236+
}
237+
238+
if assestsDiff := cmp.Diff(reexportAssets, roundtripAssets); assestsDiff != "" {
239+
log.Printf("Roundtrip config is different from the export config.\nroundtrip config:\n%s\nexport config:\n%s", string(roundtripConfigData), string(exportConfigData))
240+
return fmt.Errorf("test %s got diff (-want +got): %s", testName, diff)
241+
}
232242
}
233243
log.Printf("Step 2 passes for resource %s. Roundtrip config and export config are identical", testData.ResourceAddress)
234244

@@ -377,19 +387,46 @@ func isIgnored(key string, ignoredFields map[string]any) bool {
377387
// Converts a tfplan to CAI asset, and then converts the CAI asset into HCL
378388
func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCache map[string]string, defaultProject string, logger *zap.Logger, ignoredAssetFields []string) ([]caiasset.Asset, []byte, error) {
379389
fileName := fmt.Sprintf("%s_export", testName)
390+
roundtripAssetFile := fmt.Sprintf("%s_roundtrip.json", testName)
391+
392+
roundtripAssets, err := tfplan2caiConvert(t, fileName, roundtripAssetFile, tfDir, ancestryCache, defaultProject, logger)
393+
if err != nil {
394+
return nil, nil, err
395+
}
396+
397+
var roundtripAssetsCopy []caiasset.Asset
398+
// Perform the deep copy in case the assets are transformed in cai2hcl
399+
err = DeepCopyMap(roundtripAssets, &roundtripAssetsCopy)
400+
if err != nil {
401+
fmt.Println("Error during deep copy:", err)
402+
return nil, nil, err
403+
}
404+
405+
deleteFieldsFromAssets(roundtripAssetsCopy, ignoredAssetFields)
406+
roundtripConfig, err := cai2hcl.Convert(roundtripAssetsCopy, &cai2hcl.Options{
407+
ErrorLogger: logger,
408+
})
409+
if err != nil {
410+
return nil, nil, err
411+
}
412+
413+
return roundtripAssets, roundtripConfig, nil
414+
}
380415

416+
// Converts tf file to CAI assets
417+
func tfplan2caiConvert(t *testing.T, tfFileName, jsonFileName string, tfDir string, ancestryCache map[string]string, defaultProject string, logger *zap.Logger) ([]caiasset.Asset, error) {
381418
// Run terraform init and terraform apply to generate tfplan.json files
382-
terraformWorkflow(t, tfDir, fileName)
419+
terraformWorkflow(t, tfDir, tfFileName)
383420

384-
planFile := fmt.Sprintf("%s.tfplan.json", fileName)
421+
planFile := fmt.Sprintf("%s.tfplan.json", tfFileName)
385422
planfilePath := filepath.Join(tfDir, planFile)
386423
jsonPlan, err := os.ReadFile(planfilePath)
387424
if err != nil {
388-
return nil, nil, err
425+
return nil, err
389426
}
390427

391428
ctx := context.Background()
392-
roundtripAssets, err := tfplan2cai.Convert(ctx, jsonPlan, &tfplan2cai.Options{
429+
assets, err := tfplan2cai.Convert(ctx, jsonPlan, &tfplan2cai.Options{
393430
ErrorLogger: logger,
394431
Offline: true,
395432
DefaultProject: defaultProject,
@@ -400,24 +437,14 @@ func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCac
400437
})
401438

402439
if err != nil {
403-
return nil, nil, err
440+
return nil, err
404441
}
405442

406-
deleteFieldsFromAssets(roundtripAssets, ignoredAssetFields)
407-
408443
if os.Getenv("WRITE_FILES") != "" {
409-
roundtripAssetFile := fmt.Sprintf("%s_roundtrip.json", testName)
410-
writeJSONFile(roundtripAssetFile, roundtripAssets)
411-
}
412-
413-
roundtripConfig, err := cai2hcl.Convert(roundtripAssets, &cai2hcl.Options{
414-
ErrorLogger: logger,
415-
})
416-
if err != nil {
417-
return nil, nil, err
444+
writeJSONFile(jsonFileName, assets)
418445
}
419446

420-
return roundtripAssets, roundtripConfig, nil
447+
return assets, nil
421448
}
422449

423450
// Example:

mmv1/third_party/tgc_next/test/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,18 @@ func run(t *testing.T, cmd *exec.Cmd, wantError bool) ([]byte, []byte) {
102102
}
103103
return stdout.Bytes(), stderr.Bytes()
104104
}
105+
106+
// Creates a deep copy of a source map using JSON marshalling and unmarshalling.
107+
func DeepCopyMap(source interface{}, destination interface{}) error {
108+
marshalled, err := json.Marshal(source)
109+
if err != nil {
110+
return fmt.Errorf("failed to marshal source map: %w", err)
111+
}
112+
113+
err = json.Unmarshal(marshalled, destination)
114+
if err != nil {
115+
return fmt.Errorf("failed to unmarshal JSON into destination map: %w", err)
116+
}
117+
118+
return nil
119+
}

0 commit comments

Comments
 (0)