33package google
44
55import (
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.
1915func 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.
2521func 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.
4727func 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.
5133func 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.
5739func 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.
9462func 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.
10070func 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.
11382func 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.
12090func 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"
14898func 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.
152104func 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.
165117func 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
189133func 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
221159func 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}
0 commit comments