@@ -188,8 +188,8 @@ func testSingleResource(t *testing.T, testName string, testData ResourceTestData
188188 // Compare roundtrip_config with export_config to ensure they are identical.
189189
190190 // Convert the export config to roundtrip assets and then convert the roundtrip assets back to roundtrip config
191- ancestryCache := getAncestryCache (assets )
192- roundtripAssets , roundtripConfigData , err := getRoundtripConfig (t , testName , tfDir , ancestryCache , logger , ignoredAssetFields )
191+ ancestryCache , defaultProject := getAncestryCache (assets )
192+ roundtripAssets , roundtripConfigData , err := getRoundtripConfig (t , testName , tfDir , ancestryCache , defaultProject , logger , ignoredAssetFields )
193193 if err != nil {
194194 return fmt .Errorf ("error when converting the round-trip config: %#v" , err )
195195 }
@@ -249,9 +249,10 @@ func testSingleResource(t *testing.T, testName string, testData ResourceTestData
249249 return nil
250250}
251251
252- // Gets the ancestry cache for tfplan2cai conversion
253- func getAncestryCache (assets []caiasset.Asset ) map [string ]string {
252+ // Gets the ancestry cache for tfplan2cai conversion and the default project
253+ func getAncestryCache (assets []caiasset.Asset ) ( map [string ]string , string ) {
254254 ancestryCache := make (map [string ]string , 0 )
255+ defaultProject := ""
255256
256257 for _ , asset := range assets {
257258 ancestors := asset .Ancestors
@@ -268,18 +269,29 @@ func getAncestryCache(assets []caiasset.Asset) map[string]string {
268269
269270 if _ , ok := ancestryCache [ancestors [0 ]]; ! ok {
270271 ancestryCache [ancestors [0 ]] = path
272+ if defaultProject == "" {
273+ if s , hasPrefix := strings .CutPrefix (ancestors [0 ], "projects/" ); hasPrefix {
274+ defaultProject = s
275+ }
276+ }
271277 }
272278
273279 project := utils .ParseFieldValue (asset .Name , "projects" )
274- projectKey := fmt .Sprintf ("projects/%s" , project )
275- if strings .HasPrefix (ancestors [0 ], "projects" ) && ancestors [0 ] != projectKey {
276- if _ , ok := ancestryCache [projectKey ]; ! ok {
277- ancestryCache [projectKey ] = path
280+ if project != "" {
281+ projectKey := fmt .Sprintf ("projects/%s" , project )
282+ if strings .HasPrefix (ancestors [0 ], "projects" ) && ancestors [0 ] != projectKey {
283+ if _ , ok := ancestryCache [projectKey ]; ! ok {
284+ ancestryCache [projectKey ] = path
285+ }
286+ }
287+
288+ if defaultProject == "" {
289+ defaultProject = project
278290 }
279291 }
280292 }
281293 }
282- return ancestryCache
294+ return ancestryCache , defaultProject
283295}
284296
285297// Compares HCL and finds all of the keys in map1 that are not in map2
@@ -331,7 +343,7 @@ func isIgnored(key string, ignoredFields map[string]struct{}) bool {
331343}
332344
333345// Converts a tfplan to CAI asset, and then converts the CAI asset into HCL
334- func getRoundtripConfig (t * testing.T , testName string , tfDir string , ancestryCache map [string ]string , logger * zap.Logger , ignoredAssetFields []string ) ([]caiasset.Asset , []byte , error ) {
346+ func getRoundtripConfig (t * testing.T , testName string , tfDir string , ancestryCache map [string ]string , defaultProject string , logger * zap.Logger , ignoredAssetFields []string ) ([]caiasset.Asset , []byte , error ) {
335347 fileName := fmt .Sprintf ("%s_export" , testName )
336348
337349 // Run terraform init and terraform apply to generate tfplan.json files
@@ -348,7 +360,7 @@ func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCac
348360 roundtripAssets , err := tfplan2cai .Convert (ctx , jsonPlan , & tfplan2cai.Options {
349361 ErrorLogger : logger ,
350362 Offline : true ,
351- DefaultProject : "ci-test-project-nightly-beta" ,
363+ DefaultProject : defaultProject ,
352364 DefaultRegion : "" ,
353365 DefaultZone : "" ,
354366 UserAgent : "" ,
0 commit comments