Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Changes to ancestrymanager logic to parse "parent" field for project, folder and organization asset types. #1450

Merged
merged 16 commits into from
Mar 20, 2023
Merged
7 changes: 5 additions & 2 deletions ancestrymanager/ancestrymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ func (m *manager) fetchAncestors(config *resources.Config, tfData resources.Terr

switch cai.Type {
case "cloudresourcemanager.googleapis.com/Folder":
if !folderOK {
if folderOK {
key = folderKey
} else if orgOK {
key = orgKey
} else {
return []string{"organizations/unknown"}, nil
}
key = folderKey
case "cloudresourcemanager.googleapis.com/Organization":
if !orgOK {
return nil, fmt.Errorf("organization id not found in terraform data")
Expand Down
103 changes: 103 additions & 0 deletions ancestrymanager/ancestrymanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,109 @@ func TestGetAncestors(t *testing.T) {
wantOnlineError: true,
wantOfflineError: true,
},
{
name: "Org policy v2 on Project",
data: tfdata.NewFakeResourceData(
"google_org_policy_policy",
p.ResourcesMap["google_org_policy_policy"].Schema,
map[string]interface{}{
"parent": "projects/foo",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Project",
},
want: []string{"projects/foo", "folders/bar", "organizations/qux"},
wantParent: "//cloudresourcemanager.googleapis.com/folders/bar",
},
{
name: "Org policy v2 on Folder",
data: tfdata.NewFakeResourceData(
"google_org_policy_policy",
p.ResourcesMap["google_org_policy_policy"].Schema,
map[string]interface{}{
"parent": "folders/bar",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Folder",
},
want: []string{"folders/bar", "organizations/qux"},
wantParent: "//cloudresourcemanager.googleapis.com/organizations/qux",
},
{
name: "Org policy v2 on Organization",
data: tfdata.NewFakeResourceData(
"google_org_policy_policy",
p.ResourcesMap["google_org_policy_policy"].Schema,
map[string]interface{}{
"parent": "organizations/qux",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Organization",
},
want: []string{"organizations/qux"},
},
{
name: "Google folder with organizations/ as {parent}",
data: tfdata.NewFakeResourceData(
"google_folder",
p.ResourcesMap["google_folder"].Schema,
map[string]interface{}{
"parent": "organizations/qux",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Folder",
},
want: []string{"organizations/qux"},
wantParent: "//cloudresourcemanager.googleapis.com/organizations/qux",
},
{
name: "Google folder with folders/ as {parent}",
data: tfdata.NewFakeResourceData(
"google_folder",
p.ResourcesMap["google_folder"].Schema,
map[string]interface{}{
"parent": "folders/bar",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Folder",
},
want: []string{"folders/bar", "organizations/qux"},
wantParent: "//cloudresourcemanager.googleapis.com/organizations/qux",
},
{
name: "Google folder with both folder_id and parent fields present",
data: tfdata.NewFakeResourceData(
"google_folder",
p.ResourcesMap["google_folder"].Schema,
map[string]interface{}{
"folder_id": "bar",
"parent": "organizations/qux",
},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Folder",
},
want: []string{"folders/bar", "organizations/qux"},
wantParent: "//cloudresourcemanager.googleapis.com/organizations/qux",
},
{
name: "Google folder with missing parent field",
data: tfdata.NewFakeResourceData(
"google_folder",
p.ResourcesMap["google_folder"].Schema,
map[string]interface{}{},
),
asset: &resources.Asset{
Type: "cloudresourcemanager.googleapis.com/Folder",
},
want: []string{"organizations/unknown"},
wantParent: "//cloudresourcemanager.googleapis.com/organizations/unknown",
},
}
for _, c := range cases {
for _, offline := range []bool{true, false} {
Expand Down
15 changes: 14 additions & 1 deletion ancestrymanager/ancestryutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func getProjectFromSchema(projectSchemaField string, d resources.TerraformResour
if ok && projectSchemaField != "" {
return res.(string), nil
}
res, ok = d.GetOk("parent")
if ok && strings.HasPrefix(res.(string), "projects/") {
return res.(string), nil
}
if config.Project != "" {
return config.Project, nil
}
Expand All @@ -91,10 +95,14 @@ func getOrganizationFromResource(tfData resources.TerraformResourceData) (string
if ok {
return orgID.(string), ok
}
orgID, ok = tfData.GetOk("parent")
if ok && strings.HasPrefix(orgID.(string), "organizations/") {
return orgID.(string), ok
}
return "", false
}

// getFolderFromResource reads folder_id or folder field from terraform data.
// getFolderFromResource reads folder_id, folder, parent field from terraform data.
func getFolderFromResource(tfData resources.TerraformResourceData) (string, bool) {
folderID, ok := tfData.GetOk("folder_id")
jashan-g marked this conversation as resolved.
Show resolved Hide resolved
if ok {
Expand All @@ -104,6 +112,11 @@ func getFolderFromResource(tfData resources.TerraformResourceData) (string, bool
if ok {
return folderID.(string), ok
}

folderID, ok = tfData.GetOk("parent")
if ok && strings.HasPrefix(folderID.(string), "folders/") {
return folderID.(string), ok
}
return "", false
}

Expand Down