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

r/aws_organizations_account: Allow Import when IAM Billing is set #35662

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion internal/service/organizations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package organizations
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -34,7 +36,7 @@ func ResourceAccount() *schema.Resource {
UpdateWithoutTimeout: resourceAccountUpdate,
DeleteWithoutTimeout: resourceAccountDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourceAccountImportState,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -261,6 +263,22 @@ func resourceAccountDelete(ctx context.Context, d *schema.ResourceData, meta int
return diags
}

func resourceAccountImportState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {

if strings.Contains(d.Id(), "_") {
parts := strings.Split(d.Id(), "_")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected <account_id>_<IAM User Access Status> or <account_id>", d.Id())
}
d.SetId(parts[0])
d.Set("iam_user_access_to_billing", parts[1])
} else {
d.SetId(d.Id())
}

return []*schema.ResourceData{d}, nil
}

func createAccount(ctx context.Context, conn *organizations.Organizations, name, email string, iamUserAccessToBilling, roleName *string, tags []*organizations.Tag, govCloud bool) (*organizations.CreateAccountStatus, error) {
if govCloud {
input := &organizations.CreateGovCloudAccountInput{
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/organizations_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Using `terraform import`, import the AWS member account using the `account_id`.
% terraform import aws_organizations_account.my_account 111111111111
```

To import accounts that have set iam_user_access_to_billing, use the following:

```console
% terraform import aws_organizations_account.my_account 111111111111_ALLOW
```

Certain resource arguments, like `role_name`, do not have an Organizations API method for reading the information after account creation. If the argument is set in the Terraform configuration on an imported resource, Terraform will always show a difference. To workaround this behavior, either omit the argument from the Terraform configuration or use [`ignore_changes`](https://www.terraform.io/docs/configuration/meta-arguments/lifecycle.html#ignore_changes) to hide the difference. For example:

```terraform
Expand Down
Loading