From 43fd7d1c5652d9db8c6151a2884332389c54df34 Mon Sep 17 00:00:00 2001 From: Jieyu Tian Date: Wed, 9 Oct 2024 20:05:10 +0000 Subject: [PATCH] Preserve output type when generating metadata without --generate-output-type flag --- cli/bpmetadata/cmd.go | 3 ++ cli/bpmetadata/tfconfig.go | 19 ++++++++ cli/bpmetadata/tfconfig_test.go | 44 ++++++++++++++++++- ...faces_with_full_output_types_metadata.yaml | 17 +++++++ ...es_with_partial_output_types_metadata.yaml | 16 +++++++ ...rfaces_without_output_types_metadata.yaml} | 2 +- 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 cli/testdata/bpmetadata/metadata/interfaces_with_full_output_types_metadata.yaml create mode 100644 cli/testdata/bpmetadata/metadata/interfaces_with_partial_output_types_metadata.yaml rename cli/testdata/bpmetadata/metadata/{interfaces_without_types_metadata.yaml => interfaces_without_output_types_metadata.yaml} (86%) diff --git a/cli/bpmetadata/cmd.go b/cli/bpmetadata/cmd.go index 1a4958d9690..ab43dab1625 100644 --- a/cli/bpmetadata/cmd.go +++ b/cli/bpmetadata/cmd.go @@ -256,6 +256,9 @@ func CreateBlueprintMetadata(bpPath string, bpMetadataObj *BlueprintMetadata) (* // Merge existing connections (if any) into the newly generated interfaces mergeExistingConnections(bpMetadataObj.Spec.Interfaces, existingInterfaces) + // Merge existing output types (if any) into the newly generated interfaces + mergeExistingOutputTypes(bpMetadataObj.Spec.Interfaces, existingInterfaces) + // get blueprint requirements rolesCfgPath := path.Join(repoDetails.Source.BlueprintRootPath, tfRolesFileName) svcsCfgPath := path.Join(repoDetails.Source.BlueprintRootPath, tfServicesFileName) diff --git a/cli/bpmetadata/tfconfig.go b/cli/bpmetadata/tfconfig.go index 819feda47cd..7363f5985a5 100644 --- a/cli/bpmetadata/tfconfig.go +++ b/cli/bpmetadata/tfconfig.go @@ -522,6 +522,25 @@ func mergeExistingConnections(newInterfaces, existingInterfaces *BlueprintInterf } } +// mergeExistingOutputTypes merges existing output types from an old BlueprintInterface into a new one, +// preserving manually authored types. +func mergeExistingOutputTypes(newInterfaces, existingInterfaces *BlueprintInterface) { + if existingInterfaces == nil { + return // Nothing to merge if existingInterfaces is nil + } + + existingOutputs := make(map[string]*BlueprintOutput) + for _, output := range existingInterfaces.Outputs { + existingOutputs[output.Name] = output + } + + for i, output := range newInterfaces.Outputs { + if existingOutput, ok := existingOutputs[output.Name]; ok && existingOutput.Type != nil { + newInterfaces.Outputs[i].Type = existingOutput.Type + } + } +} + // UpdateOutputTypes generates the terraform.tfstate file, extracts output types from it, // and updates the output types in the provided BlueprintInterface. func updateOutputTypes(bpPath string, bpInterfaces *BlueprintInterface) error { diff --git a/cli/bpmetadata/tfconfig_test.go b/cli/bpmetadata/tfconfig_test.go index 123d86a95d3..7e1ea600365 100644 --- a/cli/bpmetadata/tfconfig_test.go +++ b/cli/bpmetadata/tfconfig_test.go @@ -346,6 +346,48 @@ func TestMergeExistingConnections(t *testing.T) { } } +func TestMergeExistingOutputTypes(t *testing.T) { + tests := []struct { + name string + newInterfacesFile string + existingInterfacesFile string + }{ + { + name: "No existing types", + newInterfacesFile: "interfaces_without_output_types_metadata.yaml", + existingInterfacesFile: "interfaces_without_output_types_metadata.yaml", + }, + { + name: "One complex existing type is preserved", + newInterfacesFile: "interfaces_without_output_types_metadata.yaml", + existingInterfacesFile: "interfaces_with_partial_output_types_metadata.yaml", + }, + { + name: "All existing types (both simple and complex) are preserved", + newInterfacesFile: "interfaces_without_output_types_metadata.yaml", + existingInterfacesFile: "interfaces_with_full_output_types_metadata.yaml", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Load new interfaces from file + newInterfaces, err := UnmarshalMetadata(metadataTestdataPath, tt.newInterfacesFile) + require.NoError(t, err) + + // Load existing interfaces from file + existingInterfaces, err := UnmarshalMetadata(metadataTestdataPath, tt.existingInterfacesFile) + require.NoError(t, err) + + // Perform the merge + mergeExistingOutputTypes(newInterfaces.Spec.Interfaces, existingInterfaces.Spec.Interfaces) + + // Assert that the merged interfaces match the expected outcome + assert.Equal(t, existingInterfaces.Spec.Interfaces, newInterfaces.Spec.Interfaces) + }) + } +} + func TestTFIncompleteProviderVersions(t *testing.T) { tests := []struct { name string @@ -429,7 +471,7 @@ func TestUpdateOutputTypes(t *testing.T) { { name: "Update output types from state", bpPath: "sample-module", - interfacesFile: "interfaces_without_types_metadata.yaml", + interfacesFile: "interfaces_without_output_types_metadata.yaml", stateFile: "terraform.tfstate", expectedOutputs: []*BlueprintOutput{ { diff --git a/cli/testdata/bpmetadata/metadata/interfaces_with_full_output_types_metadata.yaml b/cli/testdata/bpmetadata/metadata/interfaces_with_full_output_types_metadata.yaml new file mode 100644 index 00000000000..2359bc181a1 --- /dev/null +++ b/cli/testdata/bpmetadata/metadata/interfaces_with_full_output_types_metadata.yaml @@ -0,0 +1,17 @@ +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintMetadata +metadata: + name: terraform-google-network +spec: + interfaces: + outputs: + - name: cluster_id + description: Cluster ID + type: string + - name: endpoint + description: Cluster endpoint + type: + - list + - - object: + - host: string + port: number \ No newline at end of file diff --git a/cli/testdata/bpmetadata/metadata/interfaces_with_partial_output_types_metadata.yaml b/cli/testdata/bpmetadata/metadata/interfaces_with_partial_output_types_metadata.yaml new file mode 100644 index 00000000000..fc83b3dc8fe --- /dev/null +++ b/cli/testdata/bpmetadata/metadata/interfaces_with_partial_output_types_metadata.yaml @@ -0,0 +1,16 @@ +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintMetadata +metadata: + name: terraform-google-network +spec: + interfaces: + outputs: + - name: cluster_id + description: Cluster ID + - name: endpoint + description: Cluster endpoint + type: + - list + - - object: + - host: string + port: number \ No newline at end of file diff --git a/cli/testdata/bpmetadata/metadata/interfaces_without_types_metadata.yaml b/cli/testdata/bpmetadata/metadata/interfaces_without_output_types_metadata.yaml similarity index 86% rename from cli/testdata/bpmetadata/metadata/interfaces_without_types_metadata.yaml rename to cli/testdata/bpmetadata/metadata/interfaces_without_output_types_metadata.yaml index 50320f98b65..35e68206506 100644 --- a/cli/testdata/bpmetadata/metadata/interfaces_without_types_metadata.yaml +++ b/cli/testdata/bpmetadata/metadata/interfaces_without_output_types_metadata.yaml @@ -1,4 +1,4 @@ -# interfaces_without_types_metadata.yaml +# interfaces_without_output_types_metadata.yaml apiVersion: blueprints.cloud.google.com/v1alpha1 kind: BlueprintMetadata metadata: