-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33408 from danquack/22909
Add org unit data source
- Loading branch information
Showing
10 changed files
with
219 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_organizations_organizational_unit | ||
``` |
64 changes: 64 additions & 0 deletions
64
internal/service/organizations/organizational_unit_data_source.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,64 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package organizations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/YakDriver/regexache" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/organizations" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" | ||
) | ||
|
||
// @SDKDataSource("aws_organizations_organizational_unit", name="Organizational Unit") | ||
func DataSourceOrganizationalUnit() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadWithoutTimeout: dataSourceOrganizationalUnitRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"parent_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringMatch(regexache.MustCompile("^(r-[0-9a-z]{4,32})|(ou-[0-9a-z]{4,32}-[0-9a-z]{8,32})$"), "see https://docs.aws.amazon.com/organizations/latest/APIReference/API_CreateOrganizationalUnit.html#organizations-CreateOrganizationalUnit-request-ParentId"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceOrganizationalUnitRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).OrganizationsConn(ctx) | ||
|
||
name := d.Get("name").(string) | ||
parentID := d.Get("parent_id").(string) | ||
input := &organizations.ListOrganizationalUnitsForParentInput{ | ||
ParentId: aws.String(parentID), | ||
} | ||
|
||
ou, err := findOrganizationalUnitForParent(ctx, conn, input, func(v *organizations.OrganizationalUnit) bool { | ||
return aws.StringValue(v.Name) == name | ||
}) | ||
|
||
if err != nil { | ||
return sdkdiag.AppendErrorf(diags, "reading Organizations Organizational Unit (%s/%s): %s", parentID, name, err) | ||
} | ||
|
||
d.SetId(aws.StringValue(ou.Id)) | ||
d.Set("arn", ou.Arn) | ||
|
||
return diags | ||
} |
59 changes: 59 additions & 0 deletions
59
internal/service/organizations/organizational_unit_data_source_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,59 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package organizations_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/organizations" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
) | ||
|
||
func testAccOrganizationalUnitDataSource_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
resourceName := "aws_organizations_organizational_unit.child" | ||
dataSourceName := "data.aws_organizations_organizational_unit.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckOrganizationManagementAccount(ctx, t) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, organizations.EndpointsID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccOrganizationalUnitDataSourceConfig_basic(rName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccOrganizationalUnitDataSourceConfig_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
data "aws_organizations_organization" "current" {} | ||
resource "aws_organizations_organizational_unit" "parent" { | ||
name = %[1]q | ||
parent_id = data.aws_organizations_organization.current.roots[0].id | ||
} | ||
resource "aws_organizations_organizational_unit" "child" { | ||
name = %[1]q | ||
parent_id = aws_organizations_organizational_unit.parent.id | ||
} | ||
data "aws_organizations_organizational_unit" "test" { | ||
name = aws_organizations_organizational_unit.child.name | ||
parent_id = aws_organizations_organizational_unit.parent.id | ||
} | ||
`, rName) | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
website/docs/d/organizations_organizational_unit.html.markdown
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,40 @@ | ||
--- | ||
subcategory: "Organizations" | ||
layout: "aws" | ||
page_title: "AWS: aws_organizations_organizational_unit" | ||
description: |- | ||
Terraform data source for getting an AWS Organizations Organizational Unit. | ||
--- | ||
|
||
# Data Source: aws_organizations_organizational_unit | ||
|
||
Terraform data source for getting an AWS Organizations Organizational Unit. | ||
|
||
## Example Usage | ||
|
||
### Basic Usage | ||
|
||
```terraform | ||
data "aws_organizations_organization" "org" {} | ||
data "aws_organizations_organizational_unit" "ou" { | ||
parent_id = data.aws_organizations_organization.org.roots[0].id | ||
name = "dev" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are required: | ||
|
||
* `parent_id` - (Required) Parent ID of the organizational unit. | ||
|
||
* `name` - (Required) Name of the organizational unit | ||
|
||
## Attribute Reference | ||
|
||
This data source exports the following attributes in addition to the arguments above: | ||
|
||
* `arn` - ARN of the organizational unit | ||
|
||
* `id` - ID of the organizational unit |