-
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
New resource: aws_macie_s3_bucket_association #5201
Conversation
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.
Hi @ewbankkit 👋 Thanks for submitting this! I left some initial feedback below. Can you please take a look or let us know if you do not have time to implement it?
return nil | ||
} | ||
|
||
func macieS3BucketAssociationId(d *schema.ResourceData) string { |
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.
Nitpick: This indirection is unnecessary as its only used once for a one-liner. It seems like we should also include the member account ID so we can easily support passthrough import.
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.
OK, I'll fix.
} | ||
if oneTime { | ||
return macie.S3OneTimeClassificationTypeFull | ||
} else { |
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.
Nitpick: The else
nesting here is extraneous as the if
has a return statement.
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.
👍
prefix := d.Get("prefix") | ||
var res *macie.S3ResourceClassification | ||
for { | ||
resp, err := conn.ListS3Resources(req) |
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.
We should prefer to use the available SDK paginated function: https://docs.aws.amazon.com/sdk-for-go/api/service/macie/#Macie.ListS3ResourcesPages
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.
👍
return nil | ||
} | ||
|
||
m := map[string]interface{}{} |
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.
To prevent potential panics, we should perform a nil
check for res.ClassificationType
before dereferencing.
We also generally prefer to move this type of logic into a "flatten" function, e.g.
func flattenMacieClassificationType(classificationType *macie.ClassificationType) []map[string]interface{} {
if classificationType == nil {
return []map[string]interface{}{}
}
m := map[string]interface{}{
"one_time": false,
}
if aws.StringValue(res.ClassificationType.OneTime) == macie.S3OneTimeClassificationTypeFull {
m["one_time"] = true
}
return []map[string]interface{}{m}
}
We should also provide a helpful error message when d.Set()
fails, e.g.
if err := d.Set("classification_type", flattenMacieClassificationType(res.ClassificationType)); err != nil {
return fmt.Errorf("error setting classification_type: %s", err)
}
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.
👍
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"one_time": { | ||
Type: schema.TypeBool, |
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.
We might want to consider following the SDK and using a TypeString
with the constant values in case they introduce additional types. This will also simplify the logic to remove the conditional handling.
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.
Agreed, trying to second guess what might come in the future is guaranteed compatibility pain.
I'll also add in the continuous
attribute of TypeString
.
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSMacieS3BucketAssociation_basic(t *testing.T) { |
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.
Is there a way to programatically enable/disable Macie?
=== RUN TestAccAWSMacieS3BucketAssociation_basic
--- FAIL: TestAccAWSMacieS3BucketAssociation_basic (6.07s)
testing.go:518: Step 0 error: Error applying: 1 error(s) occurred:
* aws_macie_s3_bucket_association.test: 1 error(s) occurred:
* aws_macie_s3_bucket_association.test: Error creating Macie S3 bucket association: InvalidInputException: The request was rejected. Macie is not enabled for this AWS account *******.
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.
I'll see if AssociateMemberAccount
with the current account ID does that.
I just manually enabled Macie via the console.
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.
No, it looks like Macie cannot programatically be enabled:
$ aws --region us-east-1 macie list-member-accounts
An error occurred (InvalidInputException) when calling the ListMemberAccounts operation: The request was rejected. Macie is not enabled for this AWS account 999999999999.
$ aws --region us-east-1 macie associate-member-account --member-account-id 999999999999
An error occurred (InvalidInputException) when calling the AssociateMemberAccount operation: The request was rejected. Macie is not enabled for this AWS account 999999999999.
I'll add a link to the manual setup instructions to the documentation.
} | ||
|
||
for { | ||
resp, err := conn.ListS3Resources(req) |
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.
We should prefer to also use the paginated method here 👍
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.
👍
return nil | ||
} | ||
|
||
func testAccCheckAWSMacieS3BucketAssociationExists(name string) resource.TestCheckFunc { |
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 function should perform similar logic as the destroy check above to ensure the S3 resources are appropriately allocated in Macie.
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.
👍
Yes, should get time to address these today. |
Acceptance tests after code review changes: $ make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSMacieS3BucketAssociation_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws/ -v -run=TestAccAWSMacieS3BucketAssociation_ -timeout 120m
=== RUN TestAccAWSMacieS3BucketAssociation_basic
--- PASS: TestAccAWSMacieS3BucketAssociation_basic (65.16s)
=== RUN TestAccAWSMacieS3BucketAssociation_accountIdAndPrefix
--- PASS: TestAccAWSMacieS3BucketAssociation_accountIdAndPrefix (72.33s)
PASS
ok github.com/terraform-providers/terraform-provider-aws/aws 138.118s |
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.
Looks great, thanks @ewbankkit! 🚀
2 tests passed (all tests)
=== RUN TestAccAWSMacieS3BucketAssociation_basic
--- PASS: TestAccAWSMacieS3BucketAssociation_basic (23.34s)
=== RUN TestAccAWSMacieS3BucketAssociation_accountIdAndPrefix
--- PASS: TestAccAWSMacieS3BucketAssociation_accountIdAndPrefix (28.21s)
This has been released in version 1.28.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Fixes #4992.
This is the initial Amazon Macie resource, associating S3 resources with Amazon Macie for monitoring and data classification.
Acceptance tests: