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

Virtual field for google_cloud_run_service revision names #3306

Merged
merged 2 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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,