-
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/aws_wafregional_size_constraint_set #3796
resource/aws_wafregional_size_constraint_set #3796
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.
Thanks for the PR, this overall looks good, just a few comments.
👍 for decoupling all the helper functions for diffing etc.
I have mixed feelings about decoupling the schema though. It's not that many LOC and the downside of moving it away from the CRUD is that it becomes much more difficult to build the mind-map of relationships/context between schema and CRUD.
Sometimes duplication is more than abstraction 😉
It's not a big deal though - certainly not a blocker.
|
||
name := d.Get("name").(string) | ||
|
||
log.Printf("[INFO] Creating SizeConstraintSet: %s", name) |
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: All of the log messages should probably say "WAF Regional Size Constraint Set" to prevent mismatching those with logs from the global resource.
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.
👍
|
||
resp, err := conn.GetSizeConstraintSet(params) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" { |
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: Do you mind replacing this logic with isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "")
?
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.
👍
oldS, newS := o.(*schema.Set).List(), n.(*schema.Set).List() | ||
|
||
if err := updateRegionalSizeConstraintSetResource(d.Id(), oldS, newS, client.wafregionalconn, client.region); err != nil { | ||
return errwrap.Wrapf("[ERROR] Error updating SizeConstraintSet: {{err}}", 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.
Nitpick: there's no value in using errwrap
. I know it's sprinkled all around the codebase atm, but in most cases for no particular reason.
The goal of the library is to allow you to wrap errors so you can later check the original error type. In most cases in AWS provider though - and it includes this snippet of code - there is no "later" when we'd ever care about the original error type, because the error is just returned from CRUD and bubbles up to the UI/CLI. In the UI user only ever cares about strings, not error types.
TL;DR fmt.Errorf
would do the job equally well 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.
👍
|
||
if d.HasChange("size_constraints") { | ||
o, n := d.GetChange("size_constraints") | ||
oldS, newS := o.(*schema.Set).List(), n.(*schema.Set).List() |
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: What's the idea behind oldS
- what does "S" mean in this context? Can't we call it e.g. oldConstraints, newConstraints
?
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 was copypasta I will change it in waf global as well
} | ||
|
||
func TestAccAWSWafRegionalSizeConstraintSet_noConstraints(t *testing.T) { | ||
var ipset waf.SizeConstraintSet |
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.
Copy-pasta error? 👀 🙂
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... again it is present in waf global also, will fix both?
} | ||
|
||
func TestAccAWSWafRegionalSizeConstraintSet_disappears(t *testing.T) { | ||
var v waf.SizeConstraintSet |
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: What did you mean by v
as the name of the argument here? Can't we give it more descriptive name?
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.
👍
} | ||
} | ||
|
||
func testAccCheckAWSWafRegionalSizeConstraintSetExists(n string, v *waf.SizeConstraintSet) 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.
Nitpick: What did you mean by v
as the name of the argument here? Can't we give it more descriptive name?
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.
👍
|
||
func testAccCheckAWSWafRegionalSizeConstraintSetDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_wafregional_byte_match_set" { |
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.
Copy-pasta error? 👀
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'm not sure.... again was present in waf global
|
||
// Return nil if the SizeConstraintSet is already destroyed | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
if awsErr.Code() == "WAFNonexistentItemException" { |
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.
Do you mind replacing this with the helper function? i.e.
isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "")
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.
👍
See [docs](http://docs.aws.amazon.com/waf/latest/APIReference/API_FieldToMatch.html) | ||
for all supported values. | ||
|
||
## Remarks |
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.
Extra header 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.
👍
begin moving helpers/common code to helper file
@radeksimko I addressed all the comments. If it's fine with you I would leave the schema in the helper, if a need arises to separate them again I think it should be trivial to do so, unless you feel strongly otherwise (and I do see your point!) |
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.
LGTM. Feel free to merge once the two comments in tests are addressed (and acceptance tests are still passing). 😉
@@ -258,9 +258,6 @@ func testAccCheckAWSWafSizeConstraintSetExists(n string, v *waf.SizeConstraintSe | |||
|
|||
func testAccCheckAWSWafSizeConstraintSetDestroy(s *terraform.State) error { | |||
for _, rs := range s.RootModule().Resources { | |||
if rs.Type != "aws_waf_byte_match_set" { |
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.
So the conditional should IMO stay here, it should be just changed to if rs.Type != "aws_waf_size_constraint_set" {
. Otherwise this would cause confusing errors if there was more than 1 resource in the state after deletion and we'd use the ID of that resource for looking up Size Constraint Set (even though it may be a completely different resource).
|
||
func testAccCheckAWSWafRegionalSizeConstraintSetDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
|
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.
As mentioned above we should keep the conditional in place, my comment was more about the resource name we were checking here. Sorry for not making it clear.
ForceNew: true, | ||
}, | ||
|
||
"size_constraints": &schema.Schema{ |
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.
Somehow I didn't notice this in my previous code review, but we tend to use singular names for TypeList/TypeSet fields with non-primitive nested types. This is because such fields are commonly represented as
size_constraint {
...
}
size_constraint {
...
}
size_constraint {
...
}
unlike TypeList/TypeSet with primitive (e.g. TypeString) field:
availability_zones = ["us-east-1a", "us-east-1b"]
However I'm 👌 with keeping it as is for this initial implementation. We can deprecate later - which we'll need to do anyway for some other WAF resources.
This has been released in version 1.12.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! |
This implements size constraints for WAF regional. Full disclosure, I tried my hardest to understand the API/how things worked, but this is mostly copy paste of size constraint set from global WAF. As a result this PR doesn't try and change anything really between the resources (regional vs global).
I did notice there are some validations missing from the schema (ranges, max sizes, enum's of allowable strings), that could be present. I've refactored the schema to a common helper and could add the validations but does that require more tests/docs that I need to replicate to waf and wafregional? Let me know and I am happy to implement them but it does lend to a larger discussion below.
The following is a discussion/commentary on the overall waf/wafregional codebase:
IMO the best course of action is to merge the less "robust" PR like so (implementing any feedback of course), but make note that WAF and WAF regional are very much 1 API with different client conns and could use some harmonizing. There is code that is shareable, for instance the docs is complete copypaste since the internal types/rules are the same between waf and wafregional. I suspect we could almost half the LOCs and account for the region/vs global differences with a special flag/conditional code, but this would make the 2 sets of resources completely coupled.
On the other hand if Amazon changes/evolves the service we would have to rip everything apart again. Something tells me that when it comes to provider code it's better to have copypaste/duplication for the sake of not modelling something that we don't control (and could break later). Feedback from more experienced developers on this would be greatly appreciated.... sorry for long write up
Tests: