-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
New Resource: aws_auditmanager_account_registration
#28314
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9988e33
r/aws_auditmanager_account_registration: resource implementation
jar-b 7ae5441
r/aws_auditmanager_account_registration: tests
jar-b dcb2c85
r/aws_auditmanager_account_registration: docs
jar-b 833f75b
chore: add changelog entry
jar-b c7c2ad0
r/aws_auditmanager_account_registration: use only region for id
jar-b 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_auditmanager_account_registration | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package auditmanager | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/auditmanager" | ||
awstypes "github.com/aws/aws-sdk-go-v2/service/auditmanager/types" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/create" | ||
"github.com/hashicorp/terraform-provider-aws/internal/flex" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func init() { | ||
registerFrameworkResourceFactory(newResourceAccountRegistration) | ||
} | ||
|
||
func newResourceAccountRegistration(_ context.Context) (resource.ResourceWithConfigure, error) { | ||
return &resourceAccountRegistration{}, nil | ||
} | ||
|
||
const ( | ||
ResNameAccountRegistration = "AccountRegistration" | ||
) | ||
|
||
type resourceAccountRegistration struct { | ||
framework.ResourceWithConfigure | ||
} | ||
|
||
func (r *resourceAccountRegistration) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { | ||
response.TypeName = "aws_auditmanager_account_registration" | ||
} | ||
|
||
func (r *resourceAccountRegistration) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"delegated_admin_account": schema.StringAttribute{ | ||
Optional: true, | ||
}, | ||
"deregister_on_destroy": schema.BoolAttribute{ | ||
Optional: true, | ||
}, | ||
"kms_key": schema.StringAttribute{ | ||
Optional: true, | ||
}, | ||
"id": framework.IDAttribute(), | ||
"status": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *resourceAccountRegistration) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
conn := r.Meta().AuditManagerClient | ||
// Registration is applied per region, so use this as the ID | ||
id := r.Meta().Region | ||
|
||
var plan resourceAccountRegistrationData | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
in := auditmanager.RegisterAccountInput{} | ||
if !plan.DelegatedAdminAccount.IsNull() { | ||
in.DelegatedAdminAccount = aws.String(plan.DelegatedAdminAccount.ValueString()) | ||
} | ||
if !plan.KmsKey.IsNull() { | ||
in.KmsKey = aws.String(plan.KmsKey.ValueString()) | ||
} | ||
out, err := conn.RegisterAccount(ctx, &in) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.AuditManager, create.ErrActionCreating, ResNameAccountRegistration, id, nil), | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
state := plan | ||
state.ID = types.StringValue(id) | ||
state.Status = flex.StringValueToFramework(ctx, out.Status) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) | ||
} | ||
|
||
func (r *resourceAccountRegistration) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
conn := r.Meta().AuditManagerClient | ||
|
||
var state resourceAccountRegistrationData | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// There is no API to get account registration attributes like delegated admin account | ||
// and KMS key. Read will instead call the GetAccountStatus API to confirm an active | ||
// account status. | ||
out, err := conn.GetAccountStatus(ctx, &auditmanager.GetAccountStatusInput{}) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.AuditManager, create.ErrActionReading, ResNameAccountRegistration, state.ID.String(), nil), | ||
err.Error(), | ||
) | ||
return | ||
} | ||
if out.Status == awstypes.AccountStatusInactive { | ||
resp.State.RemoveResource(ctx) | ||
return | ||
} | ||
|
||
state.Status = flex.StringValueToFramework(ctx, out.Status) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (r *resourceAccountRegistration) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
conn := r.Meta().AuditManagerClient | ||
|
||
var plan, state resourceAccountRegistrationData | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if !plan.DelegatedAdminAccount.Equal(state.DelegatedAdminAccount) || | ||
!plan.KmsKey.Equal(state.KmsKey) { | ||
in := auditmanager.RegisterAccountInput{} | ||
if !plan.DelegatedAdminAccount.IsNull() { | ||
in.DelegatedAdminAccount = aws.String(plan.DelegatedAdminAccount.ValueString()) | ||
} | ||
if !plan.KmsKey.IsNull() { | ||
in.KmsKey = aws.String(plan.KmsKey.ValueString()) | ||
} | ||
out, err := conn.RegisterAccount(ctx, &in) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.AuditManager, create.ErrActionUpdating, ResNameAccountRegistration, state.ID.String(), nil), | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
state.DelegatedAdminAccount = plan.DelegatedAdminAccount | ||
state.KmsKey = plan.KmsKey | ||
state.Status = flex.StringValueToFramework(ctx, out.Status) | ||
} | ||
|
||
if !plan.DeregisterOnDestroy.Equal(state.DeregisterOnDestroy) { | ||
state.DeregisterOnDestroy = plan.DeregisterOnDestroy | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
func (r *resourceAccountRegistration) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
conn := r.Meta().AuditManagerClient | ||
|
||
var state resourceAccountRegistrationData | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if state.DeregisterOnDestroy.ValueBool() { | ||
_, err := conn.DeregisterAccount(ctx, &auditmanager.DeregisterAccountInput{}) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.AuditManager, create.ErrActionDeleting, ResNameAccountRegistration, state.ID.String(), nil), | ||
err.Error(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
func (r *resourceAccountRegistration) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} | ||
|
||
type resourceAccountRegistrationData struct { | ||
DelegatedAdminAccount types.String `tfsdk:"delegated_admin_account"` | ||
DeregisterOnDestroy types.Bool `tfsdk:"deregister_on_destroy"` | ||
KmsKey types.String `tfsdk:"kms_key"` | ||
ID types.String `tfsdk:"id"` | ||
Status types.String `tfsdk:"status"` | ||
} |
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.
What happens if
RegisterAccount
is called when the account is already registered?Given the
deregister_on_destroy
flag there is a possibility that this will happen even if everything is managed by Terraform.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.
This functions like a typical
Put*
API. Calling it when the account is already active just sets the KMS key and delegated account ID values to the new input and keeps the status asACTIVE
.