-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
resource/securityhub_organization_admin_account: new resource #17501
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-resource | ||
aws_securityhub_organization_admin_account | ||
``` |
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,32 @@ | ||
package finder | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
) | ||
|
||
func AdminAccount(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
input := &securityhub.ListOrganizationAdminAccountsInput{} | ||
var result *securityhub.AdminAccount | ||
|
||
err := conn.ListOrganizationAdminAccountsPages(input, func(page *securityhub.ListOrganizationAdminAccountsOutput, lastPage bool) bool { | ||
if page == nil { | ||
return !lastPage | ||
} | ||
|
||
for _, adminAccount := range page.AdminAccounts { | ||
if adminAccount == nil { | ||
continue | ||
} | ||
|
||
if aws.StringValue(adminAccount.AccountId) == adminAccountID { | ||
result = adminAccount | ||
return false | ||
} | ||
} | ||
|
||
return !lastPage | ||
}) | ||
|
||
return result, err | ||
} |
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,33 @@ | ||
package waiter | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/finder" | ||
) | ||
|
||
const ( | ||
// AdminStatus NotFound | ||
AdminStatusNotFound = "NotFound" | ||
|
||
// AdminStatus Unknown | ||
AdminStatusUnknown = "Unknown" | ||
) | ||
|
||
// AdminAccountAdminStatus fetches the AdminAccount and its AdminStatus | ||
func AdminAccountAdminStatus(conn *securityhub.SecurityHub, adminAccountID string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
adminAccount, err := finder.AdminAccount(conn, adminAccountID) | ||
|
||
if err != nil { | ||
return nil, AdminStatusUnknown, err | ||
} | ||
|
||
if adminAccount == nil { | ||
return adminAccount, AdminStatusNotFound, nil | ||
} | ||
|
||
return adminAccount, aws.StringValue(adminAccount.Status), nil | ||
} | ||
} |
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,52 @@ | ||
package waiter | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const ( | ||
// Maximum amount of time to wait for an AdminAccount to return Enabled | ||
AdminAccountEnabledTimeout = 5 * time.Minute | ||
|
||
// Maximum amount of time to wait for an AdminAccount to return NotFound | ||
AdminAccountNotFoundTimeout = 5 * time.Minute | ||
) | ||
|
||
// AdminAccountEnabled waits for an AdminAccount to return Enabled | ||
func AdminAccountEnabled(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{AdminStatusNotFound}, | ||
Target: []string{securityhub.AdminStatusEnabled}, | ||
Refresh: AdminAccountAdminStatus(conn, adminAccountID), | ||
Timeout: AdminAccountEnabledTimeout, | ||
} | ||
|
||
outputRaw, err := stateConf.WaitForState() | ||
|
||
if output, ok := outputRaw.(*securityhub.AdminAccount); ok { | ||
return output, err | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
// AdminAccountNotFound waits for an AdminAccount to return NotFound | ||
func AdminAccountNotFound(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{securityhub.AdminStatusDisableInProgress}, | ||
Target: []string{AdminStatusNotFound}, | ||
Refresh: AdminAccountAdminStatus(conn, adminAccountID), | ||
Timeout: AdminAccountNotFoundTimeout, | ||
} | ||
|
||
outputRaw, err := stateConf.WaitForState() | ||
|
||
if output, ok := outputRaw.(*securityhub.AdminAccount); ok { | ||
return output, err | ||
} | ||
|
||
return nil, err | ||
} |
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
112 changes: 112 additions & 0 deletions
112
aws/resource_aws_securityhub_organization_admin_account.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,112 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/finder" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/waiter" | ||
) | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsSecurityHubOrganizationAdminAccountCreate, | ||
Read: resourceAwsSecurityHubOrganizationAdminAccountRead, | ||
Delete: resourceAwsSecurityHubOrganizationAdminAccountDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"admin_account_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAwsAccountId, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
adminAccountID := d.Get("admin_account_id").(string) | ||
|
||
input := &securityhub.EnableOrganizationAdminAccountInput{ | ||
AdminAccountId: aws.String(adminAccountID), | ||
} | ||
|
||
_, err := conn.EnableOrganizationAdminAccount(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error enabling Security Hub Organization Admin Account (%s): %w", adminAccountID, err) | ||
} | ||
|
||
d.SetId(adminAccountID) | ||
|
||
if _, err := waiter.AdminAccountEnabled(conn, d.Id()); err != nil { | ||
return fmt.Errorf("error waiting for Security Hub Organization Admin Account (%s) to enable: %w", d.Id(), err) | ||
} | ||
|
||
return resourceAwsSecurityHubOrganizationAdminAccountRead(d, meta) | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
adminAccount, err := finder.AdminAccount(conn, d.Id()) | ||
|
||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, securityhub.ErrCodeResourceNotFoundException) { | ||
log.Printf("[WARN] Security Hub Organization Admin Account (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
if adminAccount == nil { | ||
if d.IsNewResource() { | ||
return fmt.Errorf("error reading Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
log.Printf("[WARN] Security Hub Organization Admin Account (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("admin_account_id", adminAccount.AccountId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
input := &securityhub.DisableOrganizationAdminAccountInput{ | ||
AdminAccountId: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DisableOrganizationAdminAccount(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, securityhub.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error disabling Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
if _, err := waiter.AdminAccountNotFound(conn, d.Id()); err != nil { | ||
return fmt.Errorf("error waiting for Security Hub Organization Admin Account (%s) to disable: %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added to account for error when the resource isn't set as a dependency in a config