diff --git a/products/cloudrun/terraform.yaml b/products/cloudrun/terraform.yaml index 2af6c6abb520..1969381dfc56 100644 --- a/products/cloudrun/terraform.yaml +++ b/products/cloudrun/terraform.yaml @@ -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" @@ -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 @@ -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 diff --git a/templates/terraform/constants/cloud_run_service.go.erb b/templates/terraform/constants/cloud_run_service.go.erb new file mode 100644 index 000000000000..bb3520a774bd --- /dev/null +++ b/templates/terraform/constants/cloud_run_service.go.erb @@ -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 +} diff --git a/templates/terraform/custom_expand/cloud_run_service_revision_name.erb b/templates/terraform/custom_expand/cloud_run_service_revision_name.erb new file mode 100644 index 000000000000..ebcd33a9b4f3 --- /dev/null +++ b/templates/terraform/custom_expand/cloud_run_service_revision_name.erb @@ -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 +} diff --git a/templates/terraform/examples/cloud_run_service_multiple_environment_variables.tf.erb b/templates/terraform/examples/cloud_run_service_multiple_environment_variables.tf.erb index 567da4bbaa3f..5f7bb2db4444 100644 --- a/templates/terraform/examples/cloud_run_service_multiple_environment_variables.tf.erb +++ b/templates/terraform/examples/cloud_run_service_multiple_environment_variables.tf.erb @@ -22,4 +22,5 @@ resource "google_cloud_run_service" "<%= ctx[:primary_resource_id] %>" { percent = 100 latest_revision = true } + autogenerate_revision_name = true } diff --git a/templates/terraform/examples/cloud_run_service_sql.tf.erb b/templates/terraform/examples/cloud_run_service_sql.tf.erb index 400a3daa38bf..42ab5d8b4f93 100644 --- a/templates/terraform/examples/cloud_run_service_sql.tf.erb +++ b/templates/terraform/examples/cloud_run_service_sql.tf.erb @@ -17,6 +17,7 @@ resource "google_cloud_run_service" "<%= ctx[:primary_resource_id] %>" { } } } + autogenerate_revision_name = true } resource "google_sql_database_instance" "instance" { diff --git a/templates/terraform/resource_definition/cloud_run_service.go.erb b/templates/terraform/resource_definition/cloud_run_service.go.erb new file mode 100644 index 000000000000..28783a8a8ad8 --- /dev/null +++ b/templates/terraform/resource_definition/cloud_run_service.go.erb @@ -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,