forked from GoogleCloudPlatform/cloud-foundation-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate type info for output in metadata file
- Loading branch information
Showing
5 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/golang/protobuf/jsonpb" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
// ParseOutputTypesFromState parses the terraform.tfstate file and extracts the output types | ||
func ParseOutputTypesFromState(stateData []byte) (map[string]*structpb.Value, error) { // Change return type | ||
var state struct { | ||
Outputs map[string]struct { | ||
Type interface{} `json:"type"` | ||
} `json:"outputs"` | ||
} | ||
|
||
if err := json.Unmarshal(stateData, &state); err != nil { | ||
return nil, err | ||
} | ||
|
||
outputTypes := make(map[string]*structpb.Value) // Change map type | ||
for outputName, outputData := range state.Outputs { | ||
outputType, err := convertInterfaceToValue(outputData.Type) // Use convertInterfaceToValue | ||
if err != nil { | ||
return nil, err | ||
} | ||
outputTypes[outputName] = outputType | ||
} | ||
|
||
return outputTypes, nil | ||
} | ||
|
||
// convertInterfaceToValue converts an interface{} to a structpb.Value | ||
func convertInterfaceToValue(v interface{}) (*structpb.Value, error) { | ||
// Marshal the interface{} to JSON | ||
jsonData, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Unmarshal the JSON into a structpb.Value | ||
var val structpb.Value | ||
if err := jsonpb.UnmarshalString(string(jsonData), &val); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &val, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
// MockT implements the testing.TB interface with minimal functionality | ||
type MockT struct { | ||
*testing.T | ||
} | ||
|
||
// Logf is required by testing.TB but does nothing in this mock | ||
func (m *MockT) Logf(format string, args ...any) { | ||
fmt.Printf("MockT.Logf called with format: %s, args: %v\n", format, args) | ||
} | ||
|
||
// Errorf is required by testing.TB but does nothing in this mock | ||
func (m *MockT) Errorf(format string, args ...any) { | ||
fmt.Printf("MockT.Errorf called with format: %s, args: %v\n", format, args) | ||
} | ||
|
||
// Fatal is required by testing.TB but does nothing in this mock | ||
func (m *MockT) Fatal(args ...any) { | ||
fmt.Printf("MockT.Fatal called with args: %v\n", args) | ||
panic(fmt.Sprint(args...)) // Call panic to stop execution | ||
} | ||
|
||
// Fatalf is required by testing.TB but does nothing in this mock | ||
func (m *MockT) Fatalf(format string, args ...any) { | ||
fmt.Printf("MockT.Fatalf called with format: %s, args: %v\n", format, args) | ||
panic(fmt.Sprintf(format, args...)) // Call panic to stop execution | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils" | ||
) | ||
|
||
func TestMockT(t *testing.T) { | ||
mockT := &utils.MockT{T: t} | ||
|
||
// Test Logf | ||
mockT.Logf("test logf with %s", "argument") | ||
// Check the output (you might need to capture it for a more robust test) | ||
|
||
// Test Errorf | ||
mockT.Errorf("test errorf with %d", 123) | ||
// Check the output | ||
|
||
// Test Fatal | ||
// Note: Fatal will stop execution, so use a subtest or defer to recover | ||
t.Run("Test Fatal", func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("mockT.Fatal did not panic") | ||
} | ||
}() | ||
mockT.Fatal("test fatal") | ||
}) | ||
|
||
// Test Fatalf | ||
t.Run("Test Fatalf", func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("mockT.Fatalf did not panic") | ||
} | ||
}() | ||
mockT.Fatalf("test fatalf with %s", "argument") | ||
}) | ||
} |