Skip to content

Commit 4cf8df0

Browse files
zli82016trodge
andauthored
tgc-revival: add google_apigee_instance (#14924)
Co-authored-by: Thomas Rodgers <thomasrodgers@google.com>
1 parent 63cb195 commit 4cf8df0

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

mmv1/products/apigee/Instance.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ custom_code:
4747
error_retry_predicates:
4848
- 'transport_tpg.IsApigeeRetryableError'
4949
exclude_sweeper: true
50+
include_in_tgc_next_DO_NOT_USE: true
5051
examples:
5152
- name: 'apigee_instance_basic'
5253
vars:

mmv1/third_party/tgc_next/pkg/tfplan2cai/ancestrymanager/ancestrymanager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ func (m *manager) fetchAncestors(config *transport_tpg.Config, tfData tpgresourc
196196
return []string{unknownOrg}, nil
197197
}
198198
key = projectKey
199-
199+
case "apigee.googleapis.com/Instance":
200+
// Project is used to find the ancestors.
201+
// org_id in resource `google_apigee_instance` is the apigee org id under a project.
202+
if projectKey == "" {
203+
return []string{unknownOrg}, nil
204+
}
205+
key = projectKey
200206
default:
201207
switch {
202208
case orgOK:

mmv1/third_party/tgc_next/test/assert_test_files.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)