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 #5987

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
3 changes: 3 additions & 0 deletions .changelog/3306.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudrun: added ability to autogenerate revision name
```
26 changes: 26 additions & 0 deletions google/resource_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions google/resource_cloud_run_service_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
})
Expand All @@ -119,6 +119,7 @@ resource "google_cloud_run_service" "default" {
}
}
}
autogenerate_revision_name = true
}

resource "google_sql_database_instance" "instance" {
Expand Down Expand Up @@ -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"},
},
},
})
Expand Down Expand Up @@ -243,6 +244,7 @@ resource "google_cloud_run_service" "default" {
percent = 100
latest_revision = true
}
autogenerate_revision_name = true
}
`, context)
}
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/cloud_run_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ resource "google_cloud_run_service" "default" {
}
}
}
autogenerate_revision_name = true
}

resource "google_sql_database_instance" "instance" {
Expand Down Expand Up @@ -181,6 +182,7 @@ resource "google_cloud_run_service" "default" {
percent = 100
latest_revision = true
}
autogenerate_revision_name = true
}
```

Expand Down Expand Up @@ -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:

Expand Down