-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
ip.go
116 lines (96 loc) · 3.31 KB
/
ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package validators
import (
"context"
"errors"
"net/netip"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
// ipv4AddressValidator validates that a string Attribute's value is a valid IPv4 address.
type ipv4AddressValidator struct{}
// Description describes the validation in plain text formatting.
func (validator ipv4AddressValidator) Description(_ context.Context) string {
return "value must be a valid IPv4 address"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (validator ipv4AddressValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}
// Validate performs the validation.
func (validator ipv4AddressValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
if err := validateIPv4Address(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
validator.Description(ctx),
request.ConfigValue.ValueString(),
))
return
}
}
// IPv4Address returns a string validator which ensures that any configured
// attribute value:
//
// - Is a string, which represents a valid IPv4 address.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func IPv4Address() validator.String {
return ipv4AddressValidator{}
}
func validateIPv4Address(value string) error {
addr, err := netip.ParseAddr(value)
if err != nil {
return err
}
if !addr.Is4() {
return errors.New("invalid IPv4 address")
}
return nil
}
// ipv6AddressValidator validates that a string Attribute's value is a valid IPv6 address.
type ipv6AddressValidator struct{}
// Description describes the validation in plain text formatting.
func (validator ipv6AddressValidator) Description(_ context.Context) string {
return "value must be a valid IPv6 address"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (validator ipv6AddressValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}
// Validate performs the validation.
func (validator ipv6AddressValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
if err := validateIPv6Address(request.ConfigValue.ValueString()); err != nil {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
validator.Description(ctx),
request.ConfigValue.ValueString(),
))
return
}
}
// IPv6Address returns a string validator which ensures that any configured
// attribute value:
//
// - Is a string, which represents a valid IPv6 address.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func IPv6Address() validator.String {
return ipv6AddressValidator{}
}
func validateIPv6Address(value string) error {
addr, err := netip.ParseAddr(value)
if err != nil {
return err
}
if !addr.Is6() {
return errors.New("invalid IPv6 address")
}
return nil
}