-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mcaf_aws_all_organizational_units data resource
Currently there is no way to recursively find all OUs in an AWS Organisation via a data resource and doesn't look like hashicorp/terraform-provider-aws#24350 will get merged any time soon, so mcaf_aws_all_organizational_units fills this gap until such a resource exists in the community AWS provider. Signed-off-by: Stephen Hoekstra <shoekstra@schubergphilis.com>
- Loading branch information
Showing
12 changed files
with
391 additions
and
9 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
9 changes: 9 additions & 0 deletions
9
examples/data-sources/mcaf_aws_all_organizations_organizational_units/data-source.tf
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,9 @@ | ||
provider "mcaf" { | ||
aws {} | ||
} | ||
|
||
data "mcaf_aws_all_organizational_units" "example" {} | ||
|
||
output "mcaf_aws_all_organizational_units" { | ||
value = data.mcaf_aws_all_organizational_units.example.organizational_units | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/data-sources/mcaf_aws_all_organizations_organizational_units/versions.tf
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,7 @@ | ||
terraform { | ||
required_providers { | ||
mcaf = { | ||
source = "schubergphilis/mcaf" | ||
} | ||
} | ||
} |
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
147 changes: 147 additions & 0 deletions
147
internal/mcaf/data_source_aws_all_organizational_units.go
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,147 @@ | ||
package mcaf | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/organizations" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type OrganizationalUnit struct { | ||
OrganizationalUnit *organizations.OrganizationalUnit | ||
|
||
Path *string | ||
} | ||
|
||
func dataSourceAwsAllOrganizationalUnits() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: checkProvider("aws", dataSourceAwsAllOrganizationalUnitsRead), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"organizational_units": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsAllOrganizationalUnitsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*Client).AWSClient.orgsconn | ||
|
||
roots, err := listRoots(conn) | ||
if err != nil { | ||
return err | ||
} | ||
root_id := aws.StringValue(roots[0].Id) | ||
|
||
var ous []*OrganizationalUnit | ||
ous, err = listOrganizationalUnitsForParentPagesRecursive(conn, "Root", root_id, ous) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(root_id) | ||
|
||
if err := d.Set("organizational_units", flattenOrganizationsOrganizationalUnits(ous)); err != nil { | ||
return fmt.Errorf("Error setting organizational_units: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func flattenOrganizationsOrganizationalUnits(ous []*OrganizationalUnit) []map[string]interface{} { | ||
if len(ous) == 0 { | ||
return nil | ||
} | ||
var result []map[string]interface{} | ||
for _, ou := range ous { | ||
result = append(result, map[string]interface{}{ | ||
"arn": aws.StringValue(ou.OrganizationalUnit.Arn), | ||
"id": aws.StringValue(ou.OrganizationalUnit.Id), | ||
"name": aws.StringValue(ou.OrganizationalUnit.Name), | ||
"path": aws.StringValue(ou.Path), | ||
}) | ||
} | ||
return result | ||
} | ||
|
||
func listOrganizationalUnitsForParentPagesRecursive(conn *organizations.Organizations, parentPath, parentId string, ous []*OrganizationalUnit) ([]*OrganizationalUnit, error) { | ||
// Control Tower supports a maximum of 5 levels of nested OUs. | ||
parentPathSplit := strings.Split(parentPath, "/") | ||
if len(parentPathSplit) == 5 { | ||
log.Printf("[INFO] Maximum number of levels of nested OUs reached. Skipping %s (%s)", parentPath, parentId) | ||
return ous, nil | ||
} | ||
|
||
input := &organizations.ListOrganizationalUnitsForParentInput{ | ||
ParentId: aws.String(parentId), | ||
} | ||
|
||
log.Printf("[DEBUG] Listing OUs under parent: %s (%s)", parentPath, parentId) | ||
err := conn.ListOrganizationalUnitsForParentPages(input, func(page *organizations.ListOrganizationalUnitsForParentOutput, lastPage bool) bool { | ||
for _, ou := range page.OrganizationalUnits { | ||
ouPath := fmt.Sprintf("%s/%s", parentPath, aws.StringValue(ou.Name)) | ||
ous = append(ous, &OrganizationalUnit{ | ||
OrganizationalUnit: ou, | ||
Path: aws.String(ouPath), | ||
}) | ||
|
||
var err error | ||
ous, err = listOrganizationalUnitsForParentPagesRecursive(conn, ouPath, aws.StringValue(ou.Id), ous) | ||
if err != nil { | ||
log.Printf("[ERROR] Error listing OUs for %s (%s): %s", ouPath, aws.StringValue(ou.Id), err) | ||
} | ||
} | ||
|
||
return !lastPage | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error listing Organization Units for parent (%s): %s", parentId, err) | ||
} | ||
|
||
return ous, nil | ||
} | ||
|
||
func listRoots(conn *organizations.Organizations) ([]*organizations.Root, error) { | ||
var roots []*organizations.Root | ||
err := conn.ListRootsPages(&organizations.ListRootsInput{}, func(page *organizations.ListRootsOutput, lastPage bool) bool { | ||
if page == nil { | ||
return !lastPage | ||
} | ||
|
||
roots = append(roots, page.Roots...) | ||
|
||
return !lastPage | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error listing Organization Roots: %s", err) | ||
} | ||
|
||
return roots, nil | ||
} |
35 changes: 35 additions & 0 deletions
35
internal/mcaf/data_source_aws_all_organizational_units_test.go
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,35 @@ | ||
package mcaf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsAllOrganizationalUnits_basic(t *testing.T) { | ||
dataSourceName := "data.mcaf_aws_all_organizational_units.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccAwsPreCheck(t) | ||
}, | ||
ProviderFactories: testAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsAllOrganizationalUnitsConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "organizational_units.#", "5"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccDataSourceAwsAllOrganizationalUnitsConfig = ` | ||
provider "mcaf" { | ||
aws {} | ||
} | ||
data "mcaf_aws_all_organizational_units" "test" {} | ||
` |
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
Oops, something went wrong.