generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add CIDR Validator that matches backend validator #214
Merged
+331
−13
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24fdf6b
Add HCP arch image and documentation link
efgold 41dddcc
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
efgold f3c5008
Add CIDR Validation to validators
efgold 97a2767
Add tests
efgold 2e869de
Remove extra space
efgold 3ac8582
Move validation to resource rather than data source
efgold c80e6c1
Capitalize CIDR
efgold 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,3 +391,245 @@ func Test_validateVaultClusterTier(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func Test_validateCIDRBlock(t *testing.T) { | ||
tcs := map[string]struct { | ||
input string | ||
expected diag.Diagnostics | ||
}{ | ||
"blank input string": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit on test case organization: I would start with all of the valid test cases together followed by the invalid test cases. and probably worth throwing in a comment line to break them up |
||
input: "", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid cidr notation": { | ||
input: "192.168.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid characters": { | ||
input: "someString", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"valid cidr block": { | ||
input: "10.0.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 2": { | ||
input: "10.255.255.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"invalid cidr notation 2": { | ||
input: "10.256.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid cidr notation 3": { | ||
input: "10.0.256.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid characters 2": { | ||
input: "10.255.255asdfasdfaqsd.250", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"valid cidr block 3": { | ||
input: "192.168.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 4": { | ||
input: "192.168.255.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"invalid cidr notation 4": { | ||
input: "192.168.256.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "unable to parse string as CIDR notation IP address", | ||
Detail: "unable to parse string as CIDR notation IP address", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern": { | ||
input: "192.0.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"valid cidr block 5": { | ||
input: "172.16.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 6": { | ||
input: "172.17.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 7": { | ||
input: "172.18.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 8": { | ||
input: "172.30.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 9": { | ||
input: "172.20.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"valid cidr block 10": { | ||
input: "172.31.0.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
"invalid pattern 2": { | ||
input: "172.15.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern 3": { | ||
input: "172.32.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern 4": { | ||
input: "172.192.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern 5": { | ||
input: "172.255.0.0/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern 6": { | ||
input: "10.0.0.0/7", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern 7": { | ||
input: "192.168.0.0/15", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid pattern and invalid range": { | ||
input: "87.70.141.1/22", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
Detail: "must match pattern of 10.*.*.* with prefix greater than /8,or 172.[16-31].*.* with prefix greater than /12, or 192.168.*.* with prefix greater than /16; where * is any number from [0-255]", | ||
AttributePath: nil, | ||
}, | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "invalid CIDR range start 87.70.141.1, should have been 87.70.140.0", | ||
Detail: "invalid CIDR range start 87.70.141.1, should have been 87.70.140.0", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"invalid range": { | ||
input: "192.168.255.255/24", | ||
expected: diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "invalid CIDR range start 192.168.255.255, should have been 192.168.255.0", | ||
Detail: "invalid CIDR range start 192.168.255.255, should have been 192.168.255.0", | ||
AttributePath: nil, | ||
}, | ||
}, | ||
}, | ||
"valid cidr block 11": { | ||
input: "172.25.16.0/24", | ||
expected: diag.Diagnostics(nil), | ||
}, | ||
} | ||
|
||
for n, tc := range tcs { | ||
t.Run(n, func(t *testing.T) { | ||
r := require.New(t) | ||
result := validateCIDRBlock(tc.input, nil) | ||
r.Equal(tc.expected, result) | ||
}) | ||
} | ||
} |
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.
I'd add a comment here clarifying what rules are being enforced here (so, the first two documented here: https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/resources/hvn)