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

New resource for Macie2 Account #19069

Merged
merged 18 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 17 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/19069.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_macie2_account
```
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ func Provider() *schema.Provider {
"aws_load_balancer_backend_server_policy": resourceAwsLoadBalancerBackendServerPolicies(),
"aws_load_balancer_listener_policy": resourceAwsLoadBalancerListenerPolicies(),
"aws_lb_ssl_negotiation_policy": resourceAwsLBSSLNegotiationPolicy(),
"aws_macie2_account": resourceAwsMacie2Account(),
"aws_macie_member_account_association": resourceAwsMacieMemberAccountAssociation(),
"aws_macie_s3_bucket_association": resourceAwsMacieS3BucketAssociation(),
"aws_main_route_table_association": resourceAwsMainRouteTableAssociation(),
Expand Down
159 changes: 159 additions & 0 deletions aws/resource_aws_macie2_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package aws

import (
"context"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/macie2"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceAwsMacie2Account() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceMacie2AccountCreate,
ReadWithoutTimeout: resourceMacie2AccountRead,
UpdateWithoutTimeout: resourceMacie2AccountUpdate,
DeleteWithoutTimeout: resourceMacie2AccountDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"finding_publishing_frequency": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice(macie2.FindingPublishingFrequency_Values(), false),
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice(macie2.MacieStatus_Values(), false),
},
"service_role": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceMacie2AccountCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).macie2conn

input := &macie2.EnableMacieInput{
ClientToken: aws.String(resource.UniqueId()),
}

if v, ok := d.GetOk("finding_publishing_frequency"); ok {
input.FindingPublishingFrequency = aws.String(v.(string))
}
if v, ok := d.GetOk("status"); ok {
input.Status = aws.String(v.(string))
}

var err error
err = resource.RetryContext(ctx, 4*time.Minute, func() *resource.RetryError {
_, err = conn.EnableMacieWithContext(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, macie2.ErrorCodeClientError) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

return nil
})

if isResourceTimeoutError(err) {
_, err = conn.EnableMacieWithContext(ctx, input)
}

if err != nil {
return diag.FromErr(fmt.Errorf("error enabling Macie Account: %w", err))
}

d.SetId(meta.(*AWSClient).accountid)

return resourceMacie2AccountRead(ctx, d, meta)
}

func resourceMacie2AccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).macie2conn

input := &macie2.GetMacieSessionInput{}

resp, err := conn.GetMacieSessionWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, macie2.ErrCodeAccessDeniedException) {
gdavison marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[WARN] Macie not enabled for AWS account (%s), removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.FromErr(fmt.Errorf("error reading Macie Account (%s): %w", d.Id(), err))
}

d.Set("status", resp.Status)
d.Set("finding_publishing_frequency", resp.FindingPublishingFrequency)
d.Set("service_role", resp.ServiceRole)
d.Set("created_at", aws.TimeValue(resp.CreatedAt).Format(time.RFC3339))
d.Set("updated_at", aws.TimeValue(resp.UpdatedAt).Format(time.RFC3339))

return nil
}

func resourceMacie2AccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).macie2conn

input := &macie2.UpdateMacieSessionInput{}

if d.HasChange("finding_publishing_frequency") {
input.FindingPublishingFrequency = aws.String(d.Get("finding_publishing_frequency").(string))
}

if d.HasChange("status") {
input.Status = aws.String(d.Get("status").(string))
}

_, err := conn.UpdateMacieSessionWithContext(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("error updating Macie Account (%s): %w", d.Id(), err))
}

return resourceMacie2AccountRead(ctx, d, meta)
}

func resourceMacie2AccountDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).macie2conn

input := &macie2.DisableMacieInput{}

_, err := conn.DisableMacieWithContext(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, macie2.ErrCodeResourceNotFoundException) ||
tfawserr.ErrMessageContains(err, macie2.ErrCodeAccessDeniedException, "Macie is not enabled") {
return nil
}
return diag.FromErr(fmt.Errorf("error disabling Macie Account (%s): %w", d.Id(), err))
}
return nil
}
Loading