Skip to content

Commit

Permalink
Merge pull request #21178 from hashicorp/b/storage-account-ip-rule-va…
Browse files Browse the repository at this point in the history
…lidation

storage: fixing the validation for `ip_rules` to include the value in the error
  • Loading branch information
tombuildsstuff authored Mar 30, 2023
2 parents ffb0aeb + 18d5af8 commit 42dd928
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func resourceStorageAccountNetworkRulesSchema() map[string]*pluginsdk.Schema {
Computed: true,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Type: pluginsdk.TypeString,
ValidateFunc: validate.StorageAccountIpRule,
},
Set: pluginsdk.HashString,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/services/storage/validate/storage_account_ip_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func StorageAccountIpRule(v interface{}, k string) (warnings []string, errors []
value := v.(string)

if !regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(/([0-9]|[1-2][0-9]|30))?$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must start with IPV4 address and/or slash, number of bits (0-30) as prefix. Example: 23.45.1.0/30.", k))
errors = append(errors, fmt.Errorf("%q must start with IPV4 address and/or slash, number of bits (0-30) as prefix. Example: 23.45.1.0/30 but got %q.", k, value))
return warnings, errors
}

ipParts := strings.Split(v.(string), ".")
firstIPPart := ipParts[0]
secondIPPart, _ := strconv.Atoi(ipParts[1])
if (firstIPPart == "10") || (firstIPPart == "172" && secondIPPart >= 16 && secondIPPart <= 31) || (firstIPPart == "192" && secondIPPart == 168) {
errors = append(errors, fmt.Errorf("%q must be public ip address", k))
errors = append(errors, fmt.Errorf("%q must be public ip address but got %q", k, value))
return warnings, errors
}

Expand Down

0 comments on commit 42dd928

Please sign in to comment.