Skip to content

Commit

Permalink
Move diff suppress funcs used by a single services into that service'…
Browse files Browse the repository at this point in the history
…s package (#9962)
  • Loading branch information
BBBmau authored Jun 25, 2024
1 parent c8d4f42 commit 167ad5f
Show file tree
Hide file tree
Showing 68 changed files with 793 additions and 753 deletions.
2 changes: 1 addition & 1 deletion mmv1/products/cloudscheduler/Job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ properties:
description: |
The full URI path that the request will be sent to.
required: true
diff_suppress_func: 'tpgresource.LastSlashDiffSuppress'
diff_suppress_func: 'LastSlashDiffSuppress'
- !ruby/object:Api::Type::String
name: httpMethod
description: |
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/compute/Disk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ properties:
disk interfaces are automatically determined on attachment.
description: |
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
diff_suppress_func: 'tpgresource.AlwaysDiffSuppress'
diff_suppress_func: AlwaysDiffSuppress
- !ruby/object:Api::Type::String
name: 'sourceDisk'
description: |
Expand Down
4 changes: 2 additions & 2 deletions mmv1/products/compute/ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ properties:
When reading an `IPAddress`, the API always returns the IP
address number.
default_from_api: true
diff_suppress_func: 'tpgresource.InternalIpDiffSuppress'
diff_suppress_func: InternalIpDiffSuppress
- !ruby/object:Api::Type::Enum
name: 'IPProtocol'
description: |
Expand Down Expand Up @@ -430,7 +430,7 @@ properties:
cannot have overlapping `portRange`s.
@pattern: \d+(?:-\d+)?
diff_suppress_func: 'tpgresource.PortRangeDiffSuppress'
diff_suppress_func: PortRangeDiffSuppress
default_from_api: true
- !ruby/object:Api::Type::Array
name: 'ports'
Expand Down
4 changes: 2 additions & 2 deletions mmv1/products/compute/GlobalForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ properties:
When reading an `IPAddress`, the API always returns the IP
address number.
default_from_api: true
diff_suppress_func: 'tpgresource.InternalIpDiffSuppress'
diff_suppress_func: InternalIpDiffSuppress
- !ruby/object:Api::Type::Enum
name: 'IPProtocol'
description: |
Expand Down Expand Up @@ -441,7 +441,7 @@ properties:
cannot have overlapping `portRange`s.
@pattern: \d+(?:-\d+)?
diff_suppress_func: 'tpgresource.PortRangeDiffSuppress'
diff_suppress_func: PortRangeDiffSuppress
# This is a multi-resource resource reference (TargetHttp(s)Proxy,
# TargetSslProxy, TargetTcpProxy, TargetVpnGateway, TargetPool,
# TargetInstance)
Expand Down
4 changes: 3 additions & 1 deletion mmv1/products/compute/ManagedSslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ docs: !ruby/object:Provider::Terraform::Docs
certificates may entail some downtime while the certificate provisions.
In conclusion: Be extremely cautious.
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/compute_managed_ssl_certificate.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'managed_ssl_certificate_basic'
Expand Down Expand Up @@ -131,7 +133,7 @@ properties:
there can be up to 100 domains in this list.
max_size: 100
required: true
diff_suppress_func: 'tpgresource.AbsoluteDomainSuppress'
diff_suppress_func: 'AbsoluteDomainSuppress'
item_type: Api::Type::String
- !ruby/object:Api::Type::Enum
name: 'type'
Expand Down
4 changes: 3 additions & 1 deletion mmv1/products/compute/NetworkEndpointGroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ async: !ruby/object:Api::OpAsync
error: !ruby/object:Api::OpAsync::Error
path: 'error/errors'
message: 'message'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/compute_network_endpoint_group.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'network_endpoint_group'
Expand Down Expand Up @@ -139,7 +141,7 @@ properties:
imports: 'selfLink'
description: |
Optional subnetwork to which all network endpoints in the NEG belong.
diff_suppress_func: 'tpgresource.CompareOptionalSubnet'
diff_suppress_func: 'compareOptionalSubnet'
custom_expand: 'templates/terraform/custom_expand/resourceref_with_validation.go.erb'
- !ruby/object:Api::Type::Integer
name: 'defaultPort'
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/compute/RegionDisk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ properties:
disk interfaces are automatically determined on attachment.
description: |
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
diff_suppress_func: 'tpgresource.AlwaysDiffSuppress'
diff_suppress_func: AlwaysDiffSuppress
- !ruby/object:Api::Type::String
name: 'sourceDisk'
description: |
Expand Down
3 changes: 2 additions & 1 deletion mmv1/products/compute/RegionSslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ examples:
ignore_read_extra:
- 'name_prefix'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/go/compute_certificate.go.tmpl
extra_schema_entry: templates/terraform/extra_schema_entry/ssl_certificate.erb
parameters:
- !ruby/object:Api::Type::ResourceRef
Expand Down Expand Up @@ -141,4 +142,4 @@ properties:
sensitive: true
ignore_read: true
custom_flatten: 'templates/terraform/custom_flatten/sha256.erb'
diff_suppress_func: 'tpgresource.Sha256DiffSuppress'
diff_suppress_func: 'sha256DiffSuppress'
3 changes: 2 additions & 1 deletion mmv1/products/compute/Route.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ examples:
backend_name: 'compute-backend'
route_name: 'route-ilb'
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/compute_route.go.erb
decoder: templates/terraform/decoders/route.erb
extra_schema_entry: templates/terraform/extra_schema_entry/route.erb
docs: !ruby/object:Provider::Terraform::Docs
Expand Down Expand Up @@ -252,4 +253,4 @@ properties:
- next_hop_ip
- next_hop_vpn_tunnel
- next_hop_ilb
diff_suppress_func: 'tpgresource.CompareIpAddressOrSelfLinkOrResourceName'
diff_suppress_func: 'CompareIpAddressOrSelfLinkOrResourceName'
2 changes: 1 addition & 1 deletion mmv1/products/compute/SslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ properties:
sensitive: true
ignore_read: true
custom_flatten: 'templates/terraform/custom_flatten/sha256.erb'
diff_suppress_func: 'tpgresource.Sha256DiffSuppress'
diff_suppress_func: 'sha256DiffSuppress'
2 changes: 1 addition & 1 deletion mmv1/products/compute/go_Disk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ properties:
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
min_version: 'beta'
url_param_only: true
diff_suppress_func: 'tpgresource.AlwaysDiffSuppress'
diff_suppress_func: AlwaysDiffSuppress
default_value: SCSI
deprecation_message: '`interface` is deprecated and will be removed in a future major release. This field is no longer used and can be safely removed from your configurations; disk interfaces are automatically determined on attachment.'
- name: 'sourceDisk'
Expand Down
4 changes: 2 additions & 2 deletions mmv1/products/compute/go_ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ properties:
When reading an `IPAddress`, the API always returns the IP
address number.
default_from_api: true
diff_suppress_func: 'tpgresource.InternalIpDiffSuppress'
diff_suppress_func: InternalIpDiffSuppress
- name: 'IPProtocol'
type: Enum
description: |
Expand Down Expand Up @@ -412,7 +412,7 @@ properties:
@pattern: \d+(?:-\d+)?
default_from_api: true
diff_suppress_func: 'tpgresource.PortRangeDiffSuppress'
diff_suppress_func: PortRangeDiffSuppress
- name: 'ports'
type: Array
description: |
Expand Down
4 changes: 2 additions & 2 deletions mmv1/products/compute/go_GlobalForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ properties:
When reading an `IPAddress`, the API always returns the IP
address number.
default_from_api: true
diff_suppress_func: 'tpgresource.InternalIpDiffSuppress'
diff_suppress_func: InternalIpDiffSuppress
- name: 'IPProtocol'
type: Enum
description: |
Expand Down Expand Up @@ -433,7 +433,7 @@ properties:
cannot have overlapping `portRange`s.
@pattern: \d+(?:-\d+)?
diff_suppress_func: 'tpgresource.PortRangeDiffSuppress'
diff_suppress_func: PortRangeDiffSuppress
- name: 'subnetwork'
type: ResourceRef
description: |
Expand Down
5 changes: 3 additions & 2 deletions mmv1/products/compute/go_ManagedSslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ async:
path: 'error/errors'
message: 'message'
collection_url_key: 'items'
custom_code:
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/compute_managed_ssl_certificate.go.erb
examples:
- name: 'managed_ssl_certificate_basic'
primary_resource_id: 'default'
Expand Down Expand Up @@ -120,7 +121,7 @@ properties:
Domains for which a managed SSL certificate will be valid. Currently,
there can be up to 100 domains in this list.
required: true
diff_suppress_func: 'tpgresource.AbsoluteDomainSuppress'
diff_suppress_func: 'AbsoluteDomainSuppress'
item_type:
type: String
max_size: 100
Expand Down
5 changes: 3 additions & 2 deletions mmv1/products/compute/go_NetworkEndpointGroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ async:
path: 'error/errors'
message: 'message'
collection_url_key: 'items'
custom_code:
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/compute_network_endpoint_group.go.erb
examples:
- name: 'network_endpoint_group'
primary_resource_id: 'neg'
Expand Down Expand Up @@ -137,7 +138,7 @@ properties:
type: ResourceRef
description: |
Optional subnetwork to which all network endpoints in the NEG belong.
diff_suppress_func: 'tpgresource.CompareOptionalSubnet'
diff_suppress_func: 'compareOptionalSubnet'
custom_expand: 'templates/terraform/custom_expand/go/resourceref_with_validation.go.tmpl'
resource: 'Subnetwork'
imports: 'selfLink'
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/compute/go_RegionDisk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ properties:
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
min_version: 'beta'
url_param_only: true
diff_suppress_func: 'tpgresource.AlwaysDiffSuppress'
diff_suppress_func: AlwaysDiffSuppress
default_value: SCSI
deprecation_message: '`interface` is deprecated and will be removed in a future major release. This field is no longer used and can be safely removed from your configurations; disk interfaces are automatically determined on attachment.'
- name: 'sourceDisk'
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/compute/go_RegionSslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ properties:
immutable: true
ignore_read: true
sensitive: true
diff_suppress_func: 'tpgresource.Sha256DiffSuppress'
diff_suppress_func: 'sha256DiffSuppress'
custom_flatten: 'templates/terraform/custom_flatten/go/sha256.tmpl'
3 changes: 2 additions & 1 deletion mmv1/products/compute/go_Route.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async:
message: 'message'
collection_url_key: 'items'
custom_code:
constants: templates/terraform/constants/compute_route.go.erb
extra_schema_entry: 'templates/terraform/extra_schema_entry/go/route.tmpl'
decoder: 'templates/terraform/decoders/go/route.tmpl'
error_retry_predicates:
Expand Down Expand Up @@ -253,4 +254,4 @@ properties:
- 'next_hop_ip'
- 'next_hop_vpn_tunnel'
- 'next_hop_ilb'
diff_suppress_func: 'tpgresource.CompareIpAddressOrSelfLinkOrResourceName'
diff_suppress_func: 'CompareIpAddressOrSelfLinkOrResourceName'
2 changes: 1 addition & 1 deletion mmv1/products/compute/go_SslCertificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ properties:
immutable: true
ignore_read: true
sensitive: true
diff_suppress_func: 'tpgresource.Sha256DiffSuppress'
diff_suppress_func: 'sha256DiffSuppress'
custom_flatten: 'templates/terraform/custom_flatten/go/sha256.tmpl'
2 changes: 1 addition & 1 deletion mmv1/products/pubsub/Subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ properties:
- v1beta1: uses the push format defined in the v1beta1 Pub/Sub API.
- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API.
diff_suppress_func: 'tpgresource.IgnoreMissingKeyInMap("x-goog-version")'
diff_suppress_func: 'IgnoreMissingKeyInMap("x-goog-version")'
- !ruby/object:Api::Type::NestedObject
name: 'noWrapper'
custom_flatten: 'templates/terraform/custom_flatten/pubsub_no_wrapper_write_metadata_flatten.go.erb'
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/pubsub/go_Subscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ properties:
- v1beta1: uses the push format defined in the v1beta1 Pub/Sub API.
- v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API.
diff_suppress_func: 'tpgresource.IgnoreMissingKeyInMap("x-goog-version")'
diff_suppress_func: 'IgnoreMissingKeyInMap("x-goog-version")'
- name: 'noWrapper'
type: NestedObject
description: |
Expand Down
5 changes: 5 additions & 0 deletions mmv1/templates/terraform/constants/compute_certificate.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// sha256DiffSuppress
// if old is the hex-encoded sha256 sum of new, treat them as equal
func sha256DiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
return hex.EncodeToString(sha256.New().Sum([]byte(old))) == new
}
51 changes: 51 additions & 0 deletions mmv1/templates/terraform/constants/compute_forwarding_rule.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,54 @@ func forwardingRuleCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v
}
return nil
}

// Port range '80' and '80-80' is equivalent.
// `old` is read from the server and always has the full range format (e.g. '80-80', '1024-2048').
// `new` can be either a single port or a port range.
func PortRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return old == new+"-"+new
}

// Suppresses diff for IPv4 and IPv6 different formats.
// It also suppresses diffs if an IP is changing to a reference.
func InternalIpDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
addr_equality := false
netmask_equality := false

addr_netmask_old := strings.Split(old, "/")
addr_netmask_new := strings.Split(new, "/")

// Check if old or new are IPs (with or without netmask)
var addr_old net.IP
if net.ParseIP(addr_netmask_old[0]) == nil {
addr_old = net.ParseIP(old)
} else {
addr_old = net.ParseIP(addr_netmask_old[0])
}
var addr_new net.IP
if net.ParseIP(addr_netmask_new[0]) == nil {
addr_new = net.ParseIP(new)
} else {
addr_new = net.ParseIP(addr_netmask_new[0])
}

if addr_old != nil {
if addr_new == nil {
// old is an IP and new is a reference
addr_equality = true
} else {
// old and new are IP addresses
addr_equality = bytes.Equal(addr_old, addr_new)
}
}

// If old and new both have a netmask compare them, otherwise suppress
// This is not technically correct but prevents the permadiff described in https://github.com/hashicorp/terraform-provider-google/issues/16400
if (len(addr_netmask_old)) == 2 && (len(addr_netmask_new) == 2) {
netmask_equality = addr_netmask_old[1] == addr_netmask_new[1]
} else {
netmask_equality = true
}

return addr_equality && netmask_equality
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// For managed SSL certs, if new is an absolute FQDN (trailing '.') but old isn't, treat them as equals.
func AbsoluteDomainSuppress(k, old, new string, _ *schema.ResourceData) bool {
if strings.HasPrefix(k, "managed.0.domains.") {
return old == strings.TrimRight(new, ".") || new == strings.TrimRight(old, ".")
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Use this method when subnet is optioanl and auto_create_subnetworks = true
// API sometimes choose a subnet so the diff needs to be ignored
func compareOptionalSubnet(_, old, new string, _ *schema.ResourceData) bool {
if tpgresource.IsEmptyValue(reflect.ValueOf(new)) {
return true
}
// otherwise compare as self links
return tpgresource.CompareSelfLinkOrResourceName("", old, new, nil)
}
12 changes: 12 additions & 0 deletions mmv1/templates/terraform/constants/compute_route.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Use this method when the field accepts either an IP address or a
// self_link referencing a resource (such as google_compute_route's
// next_hop_ilb)
func CompareIpAddressOrSelfLinkOrResourceName(_, old, new string, _ *schema.ResourceData) bool {
// if we can parse `new` as an IP address, then compare as strings
if net.ParseIP(new) != nil {
return new == old
}

// otherwise compare as self links
return tpgresource.CompareSelfLinkOrResourceName("", old, new, nil)
}
10 changes: 10 additions & 0 deletions mmv1/templates/terraform/constants/dataproc_cluster.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Suppress diffs for values that are equivalent except for their use of the words "location"
// compared to "region" or "zone"
func locationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return locationDiffSuppressHelper(old, new) || locationDiffSuppressHelper(new, old)
}

func locationDiffSuppressHelper(a, b string) bool {
return strings.Replace(a, "/locations/", "/regions/", 1) == b ||
strings.Replace(a, "/locations/", "/zones/", 1) == b
}
7 changes: 7 additions & 0 deletions mmv1/templates/terraform/constants/disk.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func hyperDiskIopsUpdateDiffSupress(_ context.Context, d *schema.ResourceDiff, m
}
<% end -%>

<% unless version == "ga" -%>
// Suppress all diffs, used for Disk.Interface which is a nonfunctional field
func AlwaysDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool {
return true
}
<% end -%>

// diffsupress for beta and to check change in source_disk attribute
func sourceDiskDiffSupress(_, old, new string, _ *schema.ResourceData) bool {
s1 := strings.TrimPrefix(old, "https://www.googleapis.com/compute/beta")
Expand Down
Loading

0 comments on commit 167ad5f

Please sign in to comment.