Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading existing organizations accounts #24899

Merged
merged 6 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/24899.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_organizations_account: Fix reading account state for existing accounts
```
10 changes: 1 addition & 9 deletions internal/service/organizations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

func ResourceAccount() *schema.Resource {
Expand Down Expand Up @@ -140,6 +139,7 @@ func resourceAccountCreate(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(aws.StringValue(output.AccountId))
d.Set("govcloud_id", output.GovCloudAccountId)
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

if v, ok := d.GetOk("parent_id"); ok {
oldParentAccountID, err := findParentAccountID(conn, d.Id())
Expand Down Expand Up @@ -196,14 +196,6 @@ func resourceAccountRead(d *schema.ResourceData, meta interface{}) error {
d.Set("parent_id", parentAccountID)
d.Set("status", account.Status)

s, err := findCreateAccountStatusByID(conn, d.Id())

if err != nil {
return names.Error(names.Organizations, "finding", "Create Account Status", d.Id(), err)
}

d.Set("govcloud_id", s.GovCloudAccountId)

tags, err := ListTags(conn, d.Id())

if err != nil {
Expand Down
43 changes: 19 additions & 24 deletions internal/service/organizations/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func testAccAccountImportStep(n string) resource.TestStep {
return resource.TestStep{
ResourceName: n,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"close_on_deletion",
"create_govcloud",
"govcloud_id",
},
}
}

func testAccAccount_basic(t *testing.T) {
key := "TEST_AWS_ORGANIZATION_ACCOUNT_EMAIL_DOMAIN"
orgsEmailDomain := os.Getenv(key)
Expand Down Expand Up @@ -48,12 +61,7 @@ func testAccAccount_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"close_on_deletion"},
},
testAccAccountImportStep(resourceName),
},
})
}
Expand Down Expand Up @@ -83,6 +91,7 @@ func testAccAccount_CloseOnDeletion(t *testing.T) {
testAccCheckAccountExists(resourceName, &v),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "email", email),
resource.TestCheckResourceAttr(resourceName, "govcloud_id", ""),
resource.TestCheckResourceAttrSet(resourceName, "joined_method"),
acctest.CheckResourceAttrRFC3339(resourceName, "joined_timestamp"),
resource.TestCheckResourceAttr(resourceName, "name", name),
Expand All @@ -91,12 +100,7 @@ func testAccAccount_CloseOnDeletion(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"close_on_deletion"},
},
testAccAccountImportStep(resourceName),
},
})
}
Expand Down Expand Up @@ -129,12 +133,7 @@ func testAccAccount_ParentID(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "parent_id", parentIdResourceName1, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"close_on_deletion"},
},
testAccAccountImportStep(resourceName),
{
Config: testAccAccountParentId2Config(name, email),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -173,12 +172,7 @@ func testAccAccount_Tags(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"close_on_deletion"},
},
testAccAccountImportStep(resourceName),
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
{
Config: testAccAccountTags2Config(name, email, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -226,6 +220,7 @@ func testAccAccount_govCloud(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "govcloud_id"),
),
},
testAccAccountImportStep(resourceName),
},
})
}
Expand Down
14 changes: 14 additions & 0 deletions internal/service/organizations/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ package organizations_test

import (
"testing"

"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func init() {
acctest.RegisterServiceErrorCheckFunc(organizations.EndpointsID, testAccErrorCheckSkip)
}

func testAccErrorCheckSkip(t *testing.T) resource.ErrorCheckFunc {
return acctest.ErrorCheckSkipMessagesContaining(t,
"MASTER_ACCOUNT_NOT_GOVCLOUD_ENABLED",
)
}

func TestAccOrganizations_serial(t *testing.T) {
testCases := map[string]map[string]func(t *testing.T){
"Organization": {
Expand Down