From df4abe744903c98642c9159f9375159f5aecd780 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 26 Mar 2020 17:53:35 +0000 Subject: [PATCH] Virtual field for `google_cloud_run_service` revision names (#3306) * Add `autogenerate_revision_name` virtual field to `google_cloud_run_service` * Add field to examples Signed-off-by: Modular Magician --- .changelog/3306.txt | 3 +++ google-beta/resource_cloud_run_service.go | 26 +++++++++++++++++++ ...source_cloud_run_service_generated_test.go | 6 +++-- .../docs/r/cloud_run_service.html.markdown | 7 +++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 .changelog/3306.txt diff --git a/.changelog/3306.txt b/.changelog/3306.txt new file mode 100644 index 0000000000..de9ebc0040 --- /dev/null +++ b/.changelog/3306.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +cloudrun: added ability to autogenerate revision name +``` diff --git a/google-beta/resource_cloud_run_service.go b/google-beta/resource_cloud_run_service.go index ef114b6647..73f6796437 100644 --- a/google-beta/resource_cloud_run_service.go +++ b/google-beta/resource_cloud_run_service.go @@ -25,6 +25,14 @@ import ( "google.golang.org/api/googleapi" ) +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 +} + func resourceCloudRunService() *schema.Resource { return &schema.Resource{ Create: resourceCloudRunServiceCreate, @@ -42,6 +50,9 @@ func resourceCloudRunService() *schema.Resource { Delete: schema.DefaultTimeout(4 * time.Minute), }, + SchemaVersion: 1, + CustomizeDiff: revisionNameCustomizeDiff, + Schema: map[string]*schema.Schema{ "location": { Type: schema.TypeString, @@ -567,6 +578,11 @@ https://{route-hash}-{project-hash}-{cluster-level-suffix}.a.run.app`, }, }, }, + "autogenerate_revision_name": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, "project": { Type: schema.TypeString, Optional: true, @@ -693,6 +709,10 @@ func resourceCloudRunServiceRead(d *schema.ResourceData, meta interface{}) error return nil } + // Explicitly set virtual fields to default values if unset + if _, ok := d.GetOk("autogenerate_revision_name"); !ok { + d.Set("autogenerate_revision_name", false) + } if err := d.Set("project", project); err != nil { return fmt.Errorf("Error reading Service: %s", err) } @@ -809,6 +829,9 @@ func resourceCloudRunServiceImport(d *schema.ResourceData, meta interface{}) ([] } d.SetId(id) + // Explicitly set virtual fields to default values on import + d.Set("autogenerate_revision_name", false) + return []*schema.ResourceData{d}, nil } @@ -1553,6 +1576,9 @@ func expandCloudRunServiceSpecTemplateMetadataAnnotations(v interface{}, d Terra } func expandCloudRunServiceSpecTemplateMetadataName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + if d.Get("autogenerate_revision_name") == true { + return nil, nil + } return v, nil } diff --git a/google-beta/resource_cloud_run_service_generated_test.go b/google-beta/resource_cloud_run_service_generated_test.go index 14269da3c7..4a9abd2f85 100644 --- a/google-beta/resource_cloud_run_service_generated_test.go +++ b/google-beta/resource_cloud_run_service_generated_test.go @@ -92,7 +92,7 @@ func TestAccCloudRunService_cloudRunServiceSqlExample(t *testing.T) { ResourceName: "google_cloud_run_service.default", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"name", "location"}, + ImportStateVerifyIgnore: []string{"name", "location", "autogenerate_revision_name"}, }, }, }) @@ -119,6 +119,7 @@ resource "google_cloud_run_service" "default" { } } } + autogenerate_revision_name = true } resource "google_sql_database_instance" "instance" { @@ -211,7 +212,7 @@ func TestAccCloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample(t ResourceName: "google_cloud_run_service.default", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"name", "location"}, + ImportStateVerifyIgnore: []string{"name", "location", "autogenerate_revision_name"}, }, }, }) @@ -243,6 +244,7 @@ resource "google_cloud_run_service" "default" { percent = 100 latest_revision = true } + autogenerate_revision_name = true } `, context) } diff --git a/website/docs/r/cloud_run_service.html.markdown b/website/docs/r/cloud_run_service.html.markdown index 85b9340d6c..3eb269b991 100644 --- a/website/docs/r/cloud_run_service.html.markdown +++ b/website/docs/r/cloud_run_service.html.markdown @@ -99,6 +99,7 @@ resource "google_cloud_run_service" "default" { } } } + autogenerate_revision_name = true } resource "google_sql_database_instance" "instance" { @@ -181,6 +182,7 @@ resource "google_cloud_run_service" "default" { percent = 100 latest_revision = true } + autogenerate_revision_name = true } ``` @@ -487,6 +489,11 @@ The `resources` block supports: * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. +* `autogenerate_revision_name` - (Optional) 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.) The `metadata` block supports: