Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add purpose field to google compute address #2284

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ objects:
following characters must be a dash, lowercase letter, or digit,
except the last character, which cannot be a dash.
required: true
- !ruby/object:Api::Type::Enum
name: purpose
description: |
The purpose of this resource, which can be one of the following values:

- GCE_ENDPOINT for addresses that are used by VM instances, alias IP ranges, internal load balancers, and similar resources.

This should only be set when using an Internal address.
values:
- :GCE_ENDPOINT
- !ruby/object:Api::Type::Enum
name: 'networkTier'
description: |
Expand Down
8 changes: 8 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ overrides: !ruby/object:Overrides::ResourceOverrides
address_name: "my-internal-address"
network_name: "my-network"
subnetwork_name: "my-subnet"
- !ruby/object:Provider::Terraform::Examples
name: "address_with_gce_endpoint"
primary_resource_id: "internal_with_gce_endpoint"
vars:
address_name: "my-internal-address-"
# TODO(rileykarson): Remove this example when instance is supported
- !ruby/object:Provider::Terraform::Examples
name: "instance_with_ip"
Expand All @@ -40,6 +45,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
default_from_api: true
addressType: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: 'templates/terraform/custom_flatten/default_if_empty.erb'
purpose: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: addressDiffSuppress
id: !ruby/object:Overrides::Terraform::PropertyOverride
exclude: true
labelFingerprint: !ruby/object:Overrides::Terraform::PropertyOverride
Expand All @@ -63,6 +70,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
* `address` - The IP of the created resource.
custom_code: !ruby/object:Provider::Terraform::CustomCode
post_create: templates/terraform/post_create/labels.erb
constants: templates/terraform/constants/address.go.erb
Autoscaler: !ruby/object:Overrides::Terraform::ResourceOverride
id_format: "{{zone}}/{{name}}"
examples:
Expand Down
14 changes: 14 additions & 0 deletions templates/terraform/constants/address.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// All internal addresses default to a purpose of `GCE_ENDPOINT`, but it is not
// allowed on external addresses, so we can't set the default on the field.
func addressDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could implement this as a CustomizeDiff as well I think, setting the new value to the default when address_type is internal. It's little nicer than a DSF imo, because Terraform shows what it will do in plan, but isn't necessary. (this is very much a stretch thing, and feel free to ignore it. Historically we've been a little squeamish about CD, so we don't have great examples of using it so I over-suggest it on PRs sometimes.)

if k == "purpose" {
addressType := d.Get("address_type")

if addressType == "INTERNAL" && old == "GCE_ENDPOINT" &&
new == "" {
return true
}
}

return false
}
5 changes: 5 additions & 0 deletions templates/terraform/examples/address_with_gce_endpoint.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "google_compute_address" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['address_name'] %>"
address_type = "INTERNAL"
purpose = "GCE_ENDPOINT"
}