Skip to content

Commit

Permalink
prevent permadiff from IP address
Browse files Browse the repository at this point in the history
add the validation function
  • Loading branch information
emilymye authored and chrisst committed Nov 8, 2019
1 parent 06cae48 commit 96aab32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
32 changes: 14 additions & 18 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
default_from_api: true
IPAddress: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
validation: !ruby/object:Provider::Terraform::Validation
function: 'validateIpAddress'
description: |
The IP address that this forwarding rule is serving on behalf of.
Expand All @@ -554,15 +556,11 @@ overrides: !ruby/object:Overrides::ResourceOverrides
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
~> **NOTE** The address should be specified as a literal IP address,
e.g. `100.1.2.3` to avoid a permanent diff, as the server returns the
IP address regardless of the input value.
The server accepts a literal IP address or a URL reference to an existing
Address resource. The following examples are all valid but only the first
will prevent a permadiff. If you are using `google_compute_address` or
similar, interpolate using `.address` instead of `.self_link` or similar
to prevent a diff on re-apply.
An address must be specified by a literal IP address. ~> **NOTE**: While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.
IPProtocol: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'caseDiffSuppress'
default_from_api: true
Expand Down Expand Up @@ -637,6 +635,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
exclude: true
IPAddress: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
validation: !ruby/object:Provider::Terraform::Validation
function: 'validateIpAddress'
description: |
The IP address that this forwarding rule is serving on behalf of.
Expand All @@ -657,15 +657,11 @@ overrides: !ruby/object:Overrides::ResourceOverrides
internal IP address will be automatically allocated from the IP range
of the subnet or network configured for this forwarding rule.
~> **NOTE** The address should be specified as a literal IP address,
e.g. `100.1.2.3` to avoid a permanent diff, as the server returns the
IP address regardless of the input value.
The server accepts a literal IP address or a URL reference to an existing
Address resource. The following examples are all valid but only the first
will prevent a permadiff. If you are using `google_compute_address` or
similar, interpolate using `.address` instead of `.self_link` or similar
to prevent a diff on re-apply.
An address must be specified by a literal IP address. ~> **NOTE**: While
the API allows you to specify various resource paths for an address resource
instead, Terraform requires this to specifically be an IP address to
avoid needing to fetching the IP address from resource paths on refresh
or unnecessary diffs.
IPProtocol: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'caseDiffSuppress'
default_from_api: true
Expand Down
8 changes: 8 additions & 0 deletions third_party/terraform/utils/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func validateNonNegativeDuration() schema.SchemaValidateFunc {
}
}

func validateIpAddress(i interface{}, val string) ([]string, []error) {
ip := net.ParseIP(i.(string))
if ip == nil {
return nil, []error{fmt.Errorf("could not parse %q to IP address", val)}
}
return nil, nil
}

// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and that it matches none of the element in the invalid slice.
// if ignorecase is true, case is ignored.
Expand Down

0 comments on commit 96aab32

Please sign in to comment.