Skip to content

Commit c311e92

Browse files
Move functions to the verify package (#7846) (#5583)
* Move functions to the verity package * Move validation_test.go to the verify package * Move common_diff_suppression.go to the verify package * Move common_diff_suppression_test.go to the verify package * Replace the functions calls * Add Deprecated doc * Move common_diff_suppression.go and common_diff_suppression_test.go to the tpgresource package Signed-off-by: Modular Magician <[email protected]>
1 parent c52b662 commit c311e92

File tree

229 files changed

+1827
-1158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+1827
-1158
lines changed

.changelog/7846.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google-beta/appengine_operation.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"time"
88

99
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
10+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
1011

1112
"google.golang.org/api/appengine/v1"
1213
)
1314

1415
var (
15-
appEngineOperationIdRegexp = regexp.MustCompile(fmt.Sprintf("apps/%s/operations/(.*)", ProjectRegex))
16+
appEngineOperationIdRegexp = regexp.MustCompile(fmt.Sprintf("apps/%s/operations/(.*)", verify.ProjectRegex))
1617
)
1718

1819
type AppEngineOperationWaiter struct {

google-beta/common_diff_suppress.go

Lines changed: 69 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,199 +3,135 @@
33
package google
44

55
import (
6-
"crypto/sha256"
7-
"encoding/hex"
8-
"log"
96
"net"
107
"reflect"
11-
"regexp"
12-
"strconv"
13-
"strings"
14-
"time"
158

169
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
1711
)
1812

13+
// Deprecated: For backward compatibility OptionalPrefixSuppress is still working,
14+
// but all new code should use OptionalPrefixSuppress in the tpgresource package instead.
1915
func OptionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc {
20-
return func(k, old, new string, d *schema.ResourceData) bool {
21-
return prefix+old == new || prefix+new == old
22-
}
16+
return tpgresource.OptionalPrefixSuppress(prefix)
2317
}
2418

19+
// Deprecated: For backward compatibility IgnoreMissingKeyInMap is still working,
20+
// but all new code should use IgnoreMissingKeyInMap in the tpgresource package instead.
2521
func IgnoreMissingKeyInMap(key string) schema.SchemaDiffSuppressFunc {
26-
return func(k, old, new string, d *schema.ResourceData) bool {
27-
log.Printf("[DEBUG] - suppressing diff %q with old %q, new %q", k, old, new)
28-
if strings.HasSuffix(k, ".%") {
29-
oldNum, err := strconv.Atoi(old)
30-
if err != nil {
31-
log.Printf("[ERROR] could not parse %q as number, no longer attempting diff suppress", old)
32-
return false
33-
}
34-
newNum, err := strconv.Atoi(new)
35-
if err != nil {
36-
log.Printf("[ERROR] could not parse %q as number, no longer attempting diff suppress", new)
37-
return false
38-
}
39-
return oldNum+1 == newNum
40-
} else if strings.HasSuffix(k, "."+key) {
41-
return old == ""
42-
}
43-
return false
44-
}
22+
return tpgresource.IgnoreMissingKeyInMap(key)
4523
}
4624

25+
// Deprecated: For backward compatibility OptionalSurroundingSpacesSuppress is still working,
26+
// but all new code should use OptionalSurroundingSpacesSuppress in the tpgresource package instead.
4727
func OptionalSurroundingSpacesSuppress(k, old, new string, d *schema.ResourceData) bool {
48-
return strings.TrimSpace(old) == strings.TrimSpace(new)
28+
return tpgresource.OptionalSurroundingSpacesSuppress(k, old, new, d)
4929
}
5030

31+
// Deprecated: For backward compatibility EmptyOrDefaultStringSuppress is still working,
32+
// but all new code should use EmptyOrDefaultStringSuppress in the tpgresource package instead.
5133
func EmptyOrDefaultStringSuppress(defaultVal string) schema.SchemaDiffSuppressFunc {
52-
return func(k, old, new string, d *schema.ResourceData) bool {
53-
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
54-
}
34+
return tpgresource.EmptyOrDefaultStringSuppress(defaultVal)
5535
}
5636

37+
// Deprecated: For backward compatibility IpCidrRangeDiffSuppress is still working,
38+
// but all new code should use IpCidrRangeDiffSuppress in the tpgresource package instead.
5739
func IpCidrRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
58-
// The range may be a:
59-
// A) single IP address (e.g. 10.2.3.4)
60-
// B) CIDR format string (e.g. 10.1.2.0/24)
61-
// C) netmask (e.g. /24)
62-
//
63-
// For A) and B), no diff to suppress, they have to match completely.
64-
// For C), The API picks a network IP address and this creates a diff of the form:
65-
// network_interface.0.alias_ip_range.0.ip_cidr_range: "10.128.1.0/24" => "/24"
66-
// We should only compare the mask portion for this case.
67-
if len(new) > 0 && new[0] == '/' {
68-
oldNetmaskStartPos := strings.LastIndex(old, "/")
69-
70-
if oldNetmaskStartPos != -1 {
71-
oldNetmask := old[strings.LastIndex(old, "/"):]
72-
if oldNetmask == new {
73-
return true
74-
}
75-
}
76-
}
77-
78-
return false
40+
return tpgresource.IpCidrRangeDiffSuppress(k, old, new, d)
7941
}
8042

43+
// Deprecated: For backward compatibility Sha256DiffSuppress is still working,
44+
// but all new code should use Sha256DiffSuppress in the tpgresource package instead.
8145
// Sha256DiffSuppress
8246
// if old is the hex-encoded sha256 sum of new, treat them as equal
83-
func Sha256DiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
84-
return hex.EncodeToString(sha256.New().Sum([]byte(old))) == new
47+
func Sha256DiffSuppress(k, old, new string, d *schema.ResourceData) bool {
48+
return tpgresource.Sha256DiffSuppress(k, old, new, d)
8549
}
8650

87-
func CaseDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
88-
return strings.ToUpper(old) == strings.ToUpper(new)
51+
// Deprecated: For backward compatibility CaseDiffSuppress is still working,
52+
// but all new code should use CaseDiffSuppress in the tpgresource package instead.
53+
func CaseDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
54+
return tpgresource.CaseDiffSuppress(k, old, new, d)
8955
}
9056

57+
// Deprecated: For backward compatibility PortRangeDiffSuppress is still working,
58+
// but all new code should use PortRangeDiffSuppress in the tpgresource package instead.
9159
// Port range '80' and '80-80' is equivalent.
9260
// `old` is read from the server and always has the full range format (e.g. '80-80', '1024-2048').
9361
// `new` can be either a single port or a port range.
9462
func PortRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
95-
return old == new+"-"+new
63+
return tpgresource.PortRangeDiffSuppress(k, old, new, d)
9664
}
9765

66+
// Deprecated: For backward compatibility Rfc3339TimeDiffSuppress is still working,
67+
// but all new code should use Rfc3339TimeDiffSuppress in the tpgresource package instead.
9868
// Single-digit hour is equivalent to hour with leading zero e.g. suppress diff 1:00 => 01:00.
9969
// Assume either value could be in either format.
10070
func Rfc3339TimeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
101-
if (len(old) == 4 && "0"+old == new) || (len(new) == 4 && "0"+new == old) {
102-
return true
103-
}
104-
return false
71+
return tpgresource.Rfc3339TimeDiffSuppress(k, old, new, d)
10572
}
10673

74+
// Deprecated: For backward compatibility EmptyOrUnsetBlockDiffSuppress is still working,
75+
// but all new code should use EmptyOrUnsetBlockDiffSuppress in the tpgresource package instead.
10776
// Suppress diffs for blocks where one version is completely unset and the other is set
10877
// to an empty block. This might occur in situations where removing a block completely
10978
// is impossible (if it's computed or part of an AtLeastOneOf), so instead the user sets
11079
// its values to empty.
11180
// NOTE: Using Optional + Computed is *strongly* preferred to this DSF, as it's
11281
// more well understood and resilient to API changes.
11382
func EmptyOrUnsetBlockDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
114-
o, n := d.GetChange(strings.TrimSuffix(k, ".#"))
115-
return EmptyOrUnsetBlockDiffSuppressLogic(k, old, new, o, n)
83+
return tpgresource.EmptyOrUnsetBlockDiffSuppress(k, old, new, d)
11684
}
11785

86+
// Deprecated: For backward compatibility EmptyOrUnsetBlockDiffSuppressLogic is still working,
87+
// but all new code should use EmptyOrUnsetBlockDiffSuppressLogic in the tpgresource package instead.
11888
// The core logic for EmptyOrUnsetBlockDiffSuppress, in a format that is more conducive
11989
// to unit testing.
12090
func EmptyOrUnsetBlockDiffSuppressLogic(k, old, new string, o, n interface{}) bool {
121-
if !strings.HasSuffix(k, ".#") {
122-
return false
123-
}
124-
var l []interface{}
125-
if old == "0" && new == "1" {
126-
l = n.([]interface{})
127-
} else if new == "0" && old == "1" {
128-
l = o.([]interface{})
129-
} else {
130-
// we don't have one set and one unset, so don't suppress the diff
131-
return false
132-
}
133-
134-
contents, ok := l[0].(map[string]interface{})
135-
if !ok {
136-
return false
137-
}
138-
for _, v := range contents {
139-
if !isEmptyValue(reflect.ValueOf(v)) {
140-
return false
141-
}
142-
}
143-
return true
91+
return tpgresource.EmptyOrUnsetBlockDiffSuppressLogic(k, old, new, o, n)
14492
}
14593

94+
// Deprecated: For backward compatibility LocationDiffSuppress is still working,
95+
// but all new code should use LocationDiffSuppress in the tpgresource package instead.
14696
// Suppress diffs for values that are equivalent except for their use of the words "location"
14797
// compared to "region" or "zone"
14898
func LocationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
149-
return locationDiffSuppressHelper(old, new) || locationDiffSuppressHelper(new, old)
99+
return tpgresource.LocationDiffSuppress(k, old, new, d)
150100
}
151101

102+
// Deprecated: For backward compatibility locationDiffSuppressHelper is still working,
103+
// but all new code should use LocationDiffSuppressHelper in the tpgresource package instead.
152104
func locationDiffSuppressHelper(a, b string) bool {
153-
return strings.Replace(a, "/locations/", "/regions/", 1) == b ||
154-
strings.Replace(a, "/locations/", "/zones/", 1) == b
105+
return tpgresource.LocationDiffSuppressHelper(a, b)
155106
}
156107

108+
// Deprecated: For backward compatibility AbsoluteDomainSuppress is still working,
109+
// but all new code should use AbsoluteDomainSuppress in the tpgresource package instead.
157110
// For managed SSL certs, if new is an absolute FQDN (trailing '.') but old isn't, treat them as equals.
158-
func AbsoluteDomainSuppress(k, old, new string, _ *schema.ResourceData) bool {
159-
if strings.HasPrefix(k, "managed.0.domains.") {
160-
return old == strings.TrimRight(new, ".") || new == strings.TrimRight(old, ".")
161-
}
162-
return false
111+
func AbsoluteDomainSuppress(k, old, new string, d *schema.ResourceData) bool {
112+
return tpgresource.AbsoluteDomainSuppress(k, old, new, d)
163113
}
164114

115+
// Deprecated: For backward compatibility TimestampDiffSuppress is still working,
116+
// but all new code should use TimestampDiffSuppress in the tpgresource package instead.
165117
func TimestampDiffSuppress(format string) schema.SchemaDiffSuppressFunc {
166-
return func(_, old, new string, _ *schema.ResourceData) bool {
167-
oldT, err := time.Parse(format, old)
168-
if err != nil {
169-
return false
170-
}
171-
172-
newT, err := time.Parse(format, new)
173-
if err != nil {
174-
return false
175-
}
176-
177-
return oldT == newT
178-
}
118+
return tpgresource.TimestampDiffSuppress(format)
179119
}
180120

121+
// Deprecated: For backward compatibility InternalIpDiffSuppress is still working,
122+
// but all new code should use InternalIpDiffSuppress in the tpgresource package instead.
181123
// suppress diff when saved is Ipv4 format while new is required a reference
182124
// this happens for an internal ip for Private Services Connect
183-
func InternalIpDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
184-
return (net.ParseIP(old) != nil) && (net.ParseIP(new) == nil)
125+
func InternalIpDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
126+
return tpgresource.InternalIpDiffSuppress(k, old, new, d)
185127
}
186128

129+
// Deprecated: For backward compatibility DurationDiffSuppress is still working,
130+
// but all new code should use DurationDiffSuppress in the tpgresource package instead.
187131
// Suppress diffs for duration format. ex "60.0s" and "60s" same
188132
// https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#duration
189133
func DurationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
190-
oDuration, err := time.ParseDuration(old)
191-
if err != nil {
192-
return false
193-
}
194-
nDuration, err := time.ParseDuration(new)
195-
if err != nil {
196-
return false
197-
}
198-
return oDuration == nDuration
134+
return tpgresource.DurationDiffSuppress(k, old, new, d)
199135
}
200136

201137
// Use this method when the field accepts either an IP address or a
@@ -211,43 +147,36 @@ func CompareIpAddressOrSelfLinkOrResourceName(_, old, new string, _ *schema.Reso
211147
return compareSelfLinkOrResourceName("", old, new, nil)
212148
}
213149

150+
// Deprecated: For backward compatibility AlwaysDiffSuppress is still working,
151+
// but all new code should use AlwaysDiffSuppress in the tpgresource package instead.
214152
// Suppress all diffs, used for Disk.Interface which is a nonfunctional field
215-
func AlwaysDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool {
216-
return true
153+
func AlwaysDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
154+
return tpgresource.AlwaysDiffSuppress(k, old, new, d)
217155
}
218156

219157
// Use this method when subnet is optioanl and auto_create_subnetworks = true
220158
// API sometimes choose a subnet so the diff needs to be ignored
221159
func CompareOptionalSubnet(_, old, new string, _ *schema.ResourceData) bool {
222-
if isEmptyValue(reflect.ValueOf(new)) {
160+
if tpgresource.IsEmptyValue(reflect.ValueOf(new)) {
223161
return true
224162
}
225163
// otherwise compare as self links
226164
return compareSelfLinkOrResourceName("", old, new, nil)
227165
}
228166

167+
// Deprecated: For backward compatibility LastSlashDiffSuppress is still working,
168+
// but all new code should use LastSlashDiffSuppress in the tpgresource package instead.
229169
// Suppress diffs in below cases
230170
// "https://hello-rehvs75zla-uc.a.run.app/" -> "https://hello-rehvs75zla-uc.a.run.app"
231171
// "https://hello-rehvs75zla-uc.a.run.app" -> "https://hello-rehvs75zla-uc.a.run.app/"
232-
func LastSlashDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
233-
if last := len(new) - 1; last >= 0 && new[last] == '/' {
234-
new = new[:last]
235-
}
236-
237-
if last := len(old) - 1; last >= 0 && old[last] == '/' {
238-
old = old[:last]
239-
}
240-
return new == old
172+
func LastSlashDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
173+
return tpgresource.LastSlashDiffSuppress(k, old, new, d)
241174
}
242175

176+
// Deprecated: For backward compatibility ProjectNumberDiffSuppress is still working,
177+
// but all new code should use ProjectNumberDiffSuppress in the tpgresource package instead.
243178
// Suppress diffs when the value read from api
244179
// has the project number instead of the project name
245-
func ProjectNumberDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
246-
var a2, b2 string
247-
reN := regexp.MustCompile("projects/\\d+")
248-
re := regexp.MustCompile("projects/[^/]+")
249-
replacement := []byte("projects/equal")
250-
a2 = string(reN.ReplaceAll([]byte(old), replacement))
251-
b2 = string(re.ReplaceAll([]byte(new), replacement))
252-
return a2 == b2
180+
func ProjectNumberDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
181+
return tpgresource.ProjectNumberDiffSuppress(k, old, new, d)
253182
}

google-beta/data_source_google_project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
77
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
8+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
89
)
910

1011
func DataSourceGoogleProject() *schema.Resource {
@@ -13,7 +14,7 @@ func DataSourceGoogleProject() *schema.Resource {
1314

1415
addOptionalFieldsToSchema(dsSchema, "project_id")
1516

16-
dsSchema["project_id"].ValidateFunc = validateDSProjectID()
17+
dsSchema["project_id"].ValidateFunc = verify.ValidateDSProjectID()
1718
return &schema.Resource{
1819
Read: datasourceGoogleProjectRead,
1920
Schema: dsSchema,

google-beta/data_source_google_service_account_access_token.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func DataSourceGoogleServiceAccountAccessToken() *schema.Resource {
2121
"target_service_account": {
2222
Type: schema.TypeString,
2323
Required: true,
24-
ValidateFunc: verify.ValidateRegexp("(" + strings.Join(PossibleServiceAccountNames, "|") + ")"),
24+
ValidateFunc: verify.ValidateRegexp("(" + strings.Join(verify.PossibleServiceAccountNames, "|") + ")"),
2525
},
2626
"access_token": {
2727
Type: schema.TypeString,
@@ -44,13 +44,13 @@ func DataSourceGoogleServiceAccountAccessToken() *schema.Resource {
4444
Optional: true,
4545
Elem: &schema.Schema{
4646
Type: schema.TypeString,
47-
ValidateFunc: verify.ValidateRegexp(ServiceAccountLinkRegex),
47+
ValidateFunc: verify.ValidateRegexp(verify.ServiceAccountLinkRegex),
4848
},
4949
},
5050
"lifetime": {
5151
Type: schema.TypeString,
5252
Optional: true,
53-
ValidateFunc: validateDuration(), // duration <=3600s; TODO: support validateDuration(min,max)
53+
ValidateFunc: verify.ValidateDuration(), // duration <=3600s; TODO: support validateDuration(min,max)
5454
Default: "3600s",
5555
},
5656
},

google-beta/data_source_google_service_account_id_token.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
88
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
9-
109
iamcredentials "google.golang.org/api/iamcredentials/v1"
1110
"google.golang.org/api/idtoken"
1211
"google.golang.org/api/option"
@@ -31,14 +30,14 @@ func DataSourceGoogleServiceAccountIdToken() *schema.Resource {
3130
"target_service_account": {
3231
Type: schema.TypeString,
3332
Optional: true,
34-
ValidateFunc: verify.ValidateRegexp("(" + strings.Join(PossibleServiceAccountNames, "|") + ")"),
33+
ValidateFunc: verify.ValidateRegexp("(" + strings.Join(verify.PossibleServiceAccountNames, "|") + ")"),
3534
},
3635
"delegates": {
3736
Type: schema.TypeSet,
3837
Optional: true,
3938
Elem: &schema.Schema{
4039
Type: schema.TypeString,
41-
ValidateFunc: verify.ValidateRegexp(ServiceAccountLinkRegex),
40+
ValidateFunc: verify.ValidateRegexp(verify.ServiceAccountLinkRegex),
4241
},
4342
},
4443
"include_email": {

0 commit comments

Comments
 (0)