Skip to content

Commit

Permalink
Virtual field for google_cloud_run_service revision names (#3306)
Browse files Browse the repository at this point in the history
* Add `autogenerate_revision_name` virtual field to `google_cloud_run_service`

* Add field to examples
  • Loading branch information
c2thorn authored Mar 26, 2020
1 parent 40fe6be commit ef13586
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions products/cloudrun/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
cloud_run_sql_name: "cloudrun-sql"
test_env_vars:
project: :PROJECT_NAME
ignore_read_extra:
- "autogenerate_revision_name"
- !ruby/object:Provider::Terraform::Examples
name: "cloud_run_service_noauth"
primary_resource_id: "default"
Expand All @@ -98,9 +100,22 @@ overrides: !ruby/object:Overrides::ResourceOverrides
cloud_run_service_name: "cloudrun-srv"
test_env_vars:
project: :PROJECT_NAME
ignore_read_extra:
- "autogenerate_revision_name"
virtual_fields:
- !ruby/object:Provider::Terraform::VirtualFields
name: 'autogenerate_revision_name'
description: |
If set to `true`, the revision name (template.metadata.name) will be omitted and
autogenerated by Cloud Run. This cannot be set to `true` while `template.metadata.name`
is also set.
(For legacy support, if `template.metadata.name` is unset in state while
this field is set to false, the revision name will still autogenerate.)
custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: 'templates/terraform/encoders/cloud_run_service.go.erb'
decoder: 'templates/terraform/decoders/cloud_run.go.erb'
constants: templates/terraform/constants/cloud_run_service.go.erb
resource_definition: templates/terraform/resource_definition/cloud_run_service.go.erb
properties:
name: !ruby/object:Overrides::Terraform::PropertyOverride
required: true
Expand All @@ -116,6 +131,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
spec.template.metadata: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
spec.template.metadata.name: !ruby/object:Overrides::Terraform::PropertyOverride
custom_expand: 'templates/terraform/custom_expand/cloud_run_service_revision_name.erb'
default_from_api: true
spec.template.metadata.annotations: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
Expand Down
7 changes: 7 additions & 0 deletions templates/terraform/constants/cloud_run_service.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func revisionNameCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
autogen := diff.Get("autogenerate_revision_name").(bool)
if autogen && diff.HasChange("template.0.metadata.0.name") {
return fmt.Errorf("google_cloud_run_service: `template.metadata.name` cannot be set while `autogenerate_revision_name` is true. Please remove the field or set `autogenerate_revision_name` to false.")
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
if d.Get("autogenerate_revision_name") == true {
return nil, nil
}
return v, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ resource "google_cloud_run_service" "<%= ctx[:primary_resource_id] %>" {
percent = 100
latest_revision = true
}
autogenerate_revision_name = true
}
1 change: 1 addition & 0 deletions templates/terraform/examples/cloud_run_service_sql.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ resource "google_cloud_run_service" "<%= ctx[:primary_resource_id] %>" {
}
}
}
autogenerate_revision_name = true
}

resource "google_sql_database_instance" "instance" {
Expand Down
16 changes: 16 additions & 0 deletions templates/terraform/resource_definition/cloud_run_service.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%# The license inside this block applies to this file.
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
SchemaVersion: 1,
CustomizeDiff: revisionNameCustomizeDiff,

0 comments on commit ef13586

Please sign in to comment.