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

Adding support for snapshot chain name #12481

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
6 changes: 6 additions & 0 deletions .changelog/6487.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
compute: added field `chain_name` to resource `google_compute_snapshot`
```
```release-note:enhancement
compute: added field `chain_name` to resource `google_compute_resource_policy. snapshot_properties`
```
25 changes: 25 additions & 0 deletions google/resource_compute_resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ the source disk is deleted. Default value: "KEEP_AUTO_SNAPSHOTS" Possible values
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"chain_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Creates the new snapshot in the snapshot chain labeled with the
specified name. The chain name must be 1-63 characters long and comply
with RFC1035.`,
},
"guest_flush": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -793,6 +801,8 @@ func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotProperties(v inte
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesStorageLocations(original["storageLocations"], d, config)
transformed["guest_flush"] =
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFlush(original["guestFlush"], d, config)
transformed["chain_name"] =
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(original["chainName"], d, config)
return []interface{}{transformed}
}
func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesLabels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand All @@ -810,6 +820,10 @@ func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFl
return v
}

func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeResourcePolicyGroupPlacementPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -1202,6 +1216,13 @@ func expandComputeResourcePolicySnapshotSchedulePolicySnapshotProperties(v inter
transformed["guestFlush"] = transformedGuestFlush
}

transformedChainName, err := expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(original["chain_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedChainName); val.IsValid() && !isEmptyValue(val) {
transformed["chainName"] = transformedChainName
}

return transformed, nil
}

Expand All @@ -1225,6 +1246,10 @@ func expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFlu
return v, nil
}

func expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeResourcePolicyGroupPlacementPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
55 changes: 55 additions & 0 deletions google/resource_compute_resource_policy_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,61 @@ resource "google_compute_resource_policy" "hourly" {
`, context)
}

func TestAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(context),
},
{
ResourceName: "google_compute_resource_policy.hourly",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"region"},
},
},
})
}

func testAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_resource_policy" "hourly" {
name = "policy%{random_suffix}"
region = "us-central1"
description = "chain name snapshot"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 20
start_time = "23:00"
}
}
retention_policy {
max_retention_days = 14
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
}
snapshot_properties {
labels = {
my_label = "value"
}
storage_locations = ["us"]
guest_flush = true
chain_name = "test-schedule-chain-name"
}
}
}
`, context)
}

func testAccCheckComputeResourcePolicyDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
28 changes: 28 additions & 0 deletions google/resource_compute_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ character, which cannot be a dash.`,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `A reference to the disk used to create this snapshot.`,
},
"chain_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Creates the new snapshot in the snapshot chain labeled with the
specified name. The chain name must be 1-63 characters long and
comply with RFC1035. This is an uncommon option only for advanced
service owners who needs to create separate snapshot chains, for
example, for chargeback tracking. When you describe your snapshot
resource, this field is visible only if it has a non-empty value.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -222,6 +233,12 @@ func resourceComputeSnapshotCreate(d *schema.ResourceData, meta interface{}) err
}

obj := make(map[string]interface{})
chainNameProp, err := expandComputeSnapshotChainName(d.Get("chain_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("chain_name"); !isEmptyValue(reflect.ValueOf(chainNameProp)) && (ok || !reflect.DeepEqual(v, chainNameProp)) {
obj["chainName"] = chainNameProp
}
nameProp, err := expandComputeSnapshotName(d.Get("name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -379,6 +396,9 @@ func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("disk_size_gb", flattenComputeSnapshotDiskSizeGb(res["diskSizeGb"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
if err := d.Set("chain_name", flattenComputeSnapshotChainName(res["chainName"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
if err := d.Set("name", flattenComputeSnapshotName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
Expand Down Expand Up @@ -579,6 +599,10 @@ func flattenComputeSnapshotDiskSizeGb(v interface{}, d *schema.ResourceData, con
return v // let terraform core handle it otherwise
}

func flattenComputeSnapshotChainName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeSnapshotName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -665,6 +689,10 @@ func flattenComputeSnapshotSnapshotEncryptionKeyKmsKeyServiceAccount(v interface
return v
}

func expandComputeSnapshotChainName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSnapshotName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
53 changes: 53 additions & 0 deletions google/resource_compute_snapshot_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,59 @@ resource "google_compute_disk" "persistent" {
`, context)
}

func TestAccComputeSnapshot_snapshotChainnameExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSnapshotDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeSnapshot_snapshotChainnameExample(context),
},
{
ResourceName: "google_compute_snapshot.snapshot",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"source_disk", "zone", "source_disk_encryption_key"},
},
},
})
}

func testAccComputeSnapshot_snapshotChainnameExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_snapshot" "snapshot" {
name = "tf-test-my-snapshot%{random_suffix}"
source_disk = google_compute_disk.persistent.id
zone = "us-central1-a"
chain_name = "tf-test-snapshot-chain%{random_suffix}"
labels = {
my_label = "value"
}
storage_locations = ["us-central1"]
}

data "google_compute_image" "debian" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_disk" "persistent" {
name = "tf-test-debian-disk%{random_suffix}"
image = data.google_compute_image.debian.self_link
size = 10
type = "pd-ssd"
zone = "us-central1-a"
}
`, context)
}

func testAccCheckComputeSnapshotDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
41 changes: 41 additions & 0 deletions website/docs/r/compute_resource_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,41 @@ resource "google_compute_resource_policy" "hourly" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=resource_policy_snapshot_schedule_chain_name&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Resource Policy Snapshot Schedule Chain Name


```hcl
resource "google_compute_resource_policy" "hourly" {
name = "policy"
region = "us-central1"
description = "chain name snapshot"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 20
start_time = "23:00"
}
}
retention_policy {
max_retention_days = 14
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
}
snapshot_properties {
labels = {
my_label = "value"
}
storage_locations = ["us"]
guest_flush = true
chain_name = "test-schedule-chain-name"
}
}
}
```

## Argument Reference

Expand Down Expand Up @@ -277,6 +312,12 @@ The following arguments are supported:
(Optional)
Whether to perform a 'guest aware' snapshot.

* `chain_name` -
(Optional)
Creates the new snapshot in the snapshot chain labeled with the
specified name. The chain name must be 1-63 characters long and comply
with RFC1035.

<a name="nested_group_placement_policy"></a>The `group_placement_policy` block supports:

* `vm_count` -
Expand Down
42 changes: 42 additions & 0 deletions website/docs/r/compute_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ data "google_compute_image" "debian" {
project = "debian-cloud"
}

resource "google_compute_disk" "persistent" {
name = "debian-disk"
image = data.google_compute_image.debian.self_link
size = 10
type = "pd-ssd"
zone = "us-central1-a"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=snapshot_chainname&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Snapshot Chainname


```hcl
resource "google_compute_snapshot" "snapshot" {
name = "my-snapshot"
source_disk = google_compute_disk.persistent.id
zone = "us-central1-a"
chain_name = "snapshot-chain"
labels = {
my_label = "value"
}
storage_locations = ["us-central1"]
}

data "google_compute_image" "debian" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_disk" "persistent" {
name = "debian-disk"
image = data.google_compute_image.debian.self_link
Expand Down Expand Up @@ -99,6 +132,15 @@ The following arguments are supported:
- - -


* `chain_name` -
(Optional)
Creates the new snapshot in the snapshot chain labeled with the
specified name. The chain name must be 1-63 characters long and
comply with RFC1035. This is an uncommon option only for advanced
service owners who needs to create separate snapshot chains, for
example, for chargeback tracking. When you describe your snapshot
resource, this field is visible only if it has a non-empty value.

* `description` -
(Optional)
An optional description of this resource.
Expand Down