Skip to content

Commit

Permalink
feat: Preserve output types in bpmetadata interface (GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
  • Loading branch information
tjy9206 authored Oct 17, 2024
1 parent c6e0375 commit d5872d9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cli/bpmetadata/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions cli/bpmetadata/tfconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,28 @@ 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 output.Type != nil {
continue
}
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 {
Expand Down
58 changes: 57 additions & 1 deletion cli/bpmetadata/tfconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,62 @@ func TestMergeExistingConnections(t *testing.T) {
}
}

func TestMergeExistingOutputTypes(t *testing.T) {
tests := []struct {
name string
newInterfacesFile string
existingInterfacesFile string
expectedInterfacesFile string
}{
{
name: "No existing types",
newInterfacesFile: "interfaces_without_output_types_metadata.yaml",
existingInterfacesFile: "interfaces_without_output_types_metadata.yaml",
expectedInterfacesFile: "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",
expectedInterfacesFile: "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",
expectedInterfacesFile: "interfaces_with_full_output_types_metadata.yaml",
},
{
name: "Previous types are not overwriting newly generated types",
newInterfacesFile: "interfaces_with_new_output_types_metadata.yaml",
existingInterfacesFile: "interfaces_with_partial_output_types_metadata.yaml",
expectedInterfacesFile: "interfaces_with_new_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)

// Load expected interfaces from file
expectedInterfaces, err := UnmarshalMetadata(metadataTestdataPath, tt.expectedInterfacesFile)
require.NoError(t, err)

// Assert that the merged interfaces match the expected outcome
assert.Equal(t, expectedInterfaces.Spec.Interfaces, newInterfaces.Spec.Interfaces)
})
}
}

func TestTFIncompleteProviderVersions(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -429,7 +485,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{
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
type: string
- name: endpoint
description: Cluster endpoint
type:
- tuple
- string
number
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# interfaces_without_types_metadata.yaml
# interfaces_without_output_types_metadata.yaml
apiVersion: blueprints.cloud.google.com/v1alpha1
kind: BlueprintMetadata
metadata:
Expand Down

0 comments on commit d5872d9

Please sign in to comment.