Skip to content

Commit 76541c0

Browse files
Adding support for snapshot chain name (#6487) (#4660)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 6d25190 commit 76541c0

7 files changed

+250
-0
lines changed

.changelog/6487.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
compute: added field `chain_name` to resource `google_compute_snapshot`
3+
```
4+
```release-note:enhancement
5+
compute: added field `chain_name` to resource `google_compute_resource_policy. snapshot_properties`
6+
```

google-beta/resource_compute_resource_policy.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ the source disk is deleted. Default value: "KEEP_AUTO_SNAPSHOTS" Possible values
301301
MaxItems: 1,
302302
Elem: &schema.Resource{
303303
Schema: map[string]*schema.Schema{
304+
"chain_name": {
305+
Type: schema.TypeString,
306+
Optional: true,
307+
ForceNew: true,
308+
Description: `Creates the new snapshot in the snapshot chain labeled with the
309+
specified name. The chain name must be 1-63 characters long and comply
310+
with RFC1035.`,
311+
},
304312
"guest_flush": {
305313
Type: schema.TypeBool,
306314
Optional: true,
@@ -793,6 +801,8 @@ func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotProperties(v inte
793801
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesStorageLocations(original["storageLocations"], d, config)
794802
transformed["guest_flush"] =
795803
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFlush(original["guestFlush"], d, config)
804+
transformed["chain_name"] =
805+
flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(original["chainName"], d, config)
796806
return []interface{}{transformed}
797807
}
798808
func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesLabels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -810,6 +820,10 @@ func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFl
810820
return v
811821
}
812822

823+
func flattenComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
824+
return v
825+
}
826+
813827
func flattenComputeResourcePolicyGroupPlacementPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
814828
if v == nil {
815829
return nil
@@ -1202,6 +1216,13 @@ func expandComputeResourcePolicySnapshotSchedulePolicySnapshotProperties(v inter
12021216
transformed["guestFlush"] = transformedGuestFlush
12031217
}
12041218

1219+
transformedChainName, err := expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(original["chain_name"], d, config)
1220+
if err != nil {
1221+
return nil, err
1222+
} else if val := reflect.ValueOf(transformedChainName); val.IsValid() && !isEmptyValue(val) {
1223+
transformed["chainName"] = transformedChainName
1224+
}
1225+
12051226
return transformed, nil
12061227
}
12071228

@@ -1225,6 +1246,10 @@ func expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesGuestFlu
12251246
return v, nil
12261247
}
12271248

1249+
func expandComputeResourcePolicySnapshotSchedulePolicySnapshotPropertiesChainName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1250+
return v, nil
1251+
}
1252+
12281253
func expandComputeResourcePolicyGroupPlacementPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
12291254
l := v.([]interface{})
12301255
if len(l) == 0 || l[0] == nil {

google-beta/resource_compute_resource_policy_generated_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,61 @@ resource "google_compute_resource_policy" "hourly" {
200200
`, context)
201201
}
202202

203+
func TestAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(t *testing.T) {
204+
t.Parallel()
205+
206+
context := map[string]interface{}{
207+
"random_suffix": randString(t, 10),
208+
}
209+
210+
vcrTest(t, resource.TestCase{
211+
PreCheck: func() { testAccPreCheck(t) },
212+
Providers: testAccProviders,
213+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
214+
Steps: []resource.TestStep{
215+
{
216+
Config: testAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(context),
217+
},
218+
{
219+
ResourceName: "google_compute_resource_policy.hourly",
220+
ImportState: true,
221+
ImportStateVerify: true,
222+
ImportStateVerifyIgnore: []string{"region"},
223+
},
224+
},
225+
})
226+
}
227+
228+
func testAccComputeResourcePolicy_resourcePolicySnapshotScheduleChainNameExample(context map[string]interface{}) string {
229+
return Nprintf(`
230+
resource "google_compute_resource_policy" "hourly" {
231+
name = "policy%{random_suffix}"
232+
region = "us-central1"
233+
description = "chain name snapshot"
234+
snapshot_schedule_policy {
235+
schedule {
236+
hourly_schedule {
237+
hours_in_cycle = 20
238+
start_time = "23:00"
239+
}
240+
}
241+
retention_policy {
242+
max_retention_days = 14
243+
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
244+
}
245+
snapshot_properties {
246+
labels = {
247+
my_label = "value"
248+
}
249+
storage_locations = ["us"]
250+
guest_flush = true
251+
chain_name = "test-schedule-chain-name"
252+
}
253+
}
254+
}
255+
`, context)
256+
}
257+
203258
func testAccCheckComputeResourcePolicyDestroyProducer(t *testing.T) func(s *terraform.State) error {
204259
return func(s *terraform.State) error {
205260
for name, rs := range s.RootModule().Resources {

google-beta/resource_compute_snapshot.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ character, which cannot be a dash.`,
6262
DiffSuppressFunc: compareSelfLinkOrResourceName,
6363
Description: `A reference to the disk used to create this snapshot.`,
6464
},
65+
"chain_name": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
ForceNew: true,
69+
Description: `Creates the new snapshot in the snapshot chain labeled with the
70+
specified name. The chain name must be 1-63 characters long and
71+
comply with RFC1035. This is an uncommon option only for advanced
72+
service owners who needs to create separate snapshot chains, for
73+
example, for chargeback tracking. When you describe your snapshot
74+
resource, this field is visible only if it has a non-empty value.`,
75+
},
6576
"description": {
6677
Type: schema.TypeString,
6778
Optional: true,
@@ -222,6 +233,12 @@ func resourceComputeSnapshotCreate(d *schema.ResourceData, meta interface{}) err
222233
}
223234

224235
obj := make(map[string]interface{})
236+
chainNameProp, err := expandComputeSnapshotChainName(d.Get("chain_name"), d, config)
237+
if err != nil {
238+
return err
239+
} else if v, ok := d.GetOkExists("chain_name"); !isEmptyValue(reflect.ValueOf(chainNameProp)) && (ok || !reflect.DeepEqual(v, chainNameProp)) {
240+
obj["chainName"] = chainNameProp
241+
}
225242
nameProp, err := expandComputeSnapshotName(d.Get("name"), d, config)
226243
if err != nil {
227244
return err
@@ -379,6 +396,9 @@ func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error
379396
if err := d.Set("disk_size_gb", flattenComputeSnapshotDiskSizeGb(res["diskSizeGb"], d, config)); err != nil {
380397
return fmt.Errorf("Error reading Snapshot: %s", err)
381398
}
399+
if err := d.Set("chain_name", flattenComputeSnapshotChainName(res["chainName"], d, config)); err != nil {
400+
return fmt.Errorf("Error reading Snapshot: %s", err)
401+
}
382402
if err := d.Set("name", flattenComputeSnapshotName(res["name"], d, config)); err != nil {
383403
return fmt.Errorf("Error reading Snapshot: %s", err)
384404
}
@@ -579,6 +599,10 @@ func flattenComputeSnapshotDiskSizeGb(v interface{}, d *schema.ResourceData, con
579599
return v // let terraform core handle it otherwise
580600
}
581601

602+
func flattenComputeSnapshotChainName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
603+
return v
604+
}
605+
582606
func flattenComputeSnapshotName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
583607
return v
584608
}
@@ -665,6 +689,10 @@ func flattenComputeSnapshotSnapshotEncryptionKeyKmsKeyServiceAccount(v interface
665689
return v
666690
}
667691

692+
func expandComputeSnapshotChainName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
693+
return v, nil
694+
}
695+
668696
func expandComputeSnapshotName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
669697
return v, nil
670698
}

google-beta/resource_compute_snapshot_generated_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,59 @@ resource "google_compute_disk" "persistent" {
7575
`, context)
7676
}
7777

78+
func TestAccComputeSnapshot_snapshotChainnameExample(t *testing.T) {
79+
t.Parallel()
80+
81+
context := map[string]interface{}{
82+
"random_suffix": randString(t, 10),
83+
}
84+
85+
vcrTest(t, resource.TestCase{
86+
PreCheck: func() { testAccPreCheck(t) },
87+
Providers: testAccProviders,
88+
CheckDestroy: testAccCheckComputeSnapshotDestroyProducer(t),
89+
Steps: []resource.TestStep{
90+
{
91+
Config: testAccComputeSnapshot_snapshotChainnameExample(context),
92+
},
93+
{
94+
ResourceName: "google_compute_snapshot.snapshot",
95+
ImportState: true,
96+
ImportStateVerify: true,
97+
ImportStateVerifyIgnore: []string{"source_disk", "zone", "source_disk_encryption_key"},
98+
},
99+
},
100+
})
101+
}
102+
103+
func testAccComputeSnapshot_snapshotChainnameExample(context map[string]interface{}) string {
104+
return Nprintf(`
105+
resource "google_compute_snapshot" "snapshot" {
106+
name = "tf-test-my-snapshot%{random_suffix}"
107+
source_disk = google_compute_disk.persistent.id
108+
zone = "us-central1-a"
109+
chain_name = "tf-test-snapshot-chain%{random_suffix}"
110+
labels = {
111+
my_label = "value"
112+
}
113+
storage_locations = ["us-central1"]
114+
}
115+
116+
data "google_compute_image" "debian" {
117+
family = "debian-11"
118+
project = "debian-cloud"
119+
}
120+
121+
resource "google_compute_disk" "persistent" {
122+
name = "tf-test-debian-disk%{random_suffix}"
123+
image = data.google_compute_image.debian.self_link
124+
size = 10
125+
type = "pd-ssd"
126+
zone = "us-central1-a"
127+
}
128+
`, context)
129+
}
130+
78131
func testAccCheckComputeSnapshotDestroyProducer(t *testing.T) func(s *terraform.State) error {
79132
return func(s *terraform.State) error {
80133
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_resource_policy.html.markdown

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ resource "google_compute_resource_policy" "hourly" {
121121
}
122122
}
123123
```
124+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
125+
<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">
126+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
127+
</a>
128+
</div>
129+
## Example Usage - Resource Policy Snapshot Schedule Chain Name
130+
131+
132+
```hcl
133+
resource "google_compute_resource_policy" "hourly" {
134+
name = "policy"
135+
region = "us-central1"
136+
description = "chain name snapshot"
137+
snapshot_schedule_policy {
138+
schedule {
139+
hourly_schedule {
140+
hours_in_cycle = 20
141+
start_time = "23:00"
142+
}
143+
}
144+
retention_policy {
145+
max_retention_days = 14
146+
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
147+
}
148+
snapshot_properties {
149+
labels = {
150+
my_label = "value"
151+
}
152+
storage_locations = ["us"]
153+
guest_flush = true
154+
chain_name = "test-schedule-chain-name"
155+
}
156+
}
157+
}
158+
```
124159

125160
## Argument Reference
126161

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

315+
* `chain_name` -
316+
(Optional)
317+
Creates the new snapshot in the snapshot chain labeled with the
318+
specified name. The chain name must be 1-63 characters long and comply
319+
with RFC1035.
320+
280321
<a name="nested_group_placement_policy"></a>The `group_placement_policy` block supports:
281322

282323
* `vm_count` -

website/docs/r/compute_snapshot.html.markdown

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ data "google_compute_image" "debian" {
6767
project = "debian-cloud"
6868
}
6969
70+
resource "google_compute_disk" "persistent" {
71+
name = "debian-disk"
72+
image = data.google_compute_image.debian.self_link
73+
size = 10
74+
type = "pd-ssd"
75+
zone = "us-central1-a"
76+
}
77+
```
78+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
79+
<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">
80+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
81+
</a>
82+
</div>
83+
## Example Usage - Snapshot Chainname
84+
85+
86+
```hcl
87+
resource "google_compute_snapshot" "snapshot" {
88+
name = "my-snapshot"
89+
source_disk = google_compute_disk.persistent.id
90+
zone = "us-central1-a"
91+
chain_name = "snapshot-chain"
92+
labels = {
93+
my_label = "value"
94+
}
95+
storage_locations = ["us-central1"]
96+
}
97+
98+
data "google_compute_image" "debian" {
99+
family = "debian-11"
100+
project = "debian-cloud"
101+
}
102+
70103
resource "google_compute_disk" "persistent" {
71104
name = "debian-disk"
72105
image = data.google_compute_image.debian.self_link
@@ -99,6 +132,15 @@ The following arguments are supported:
99132
- - -
100133

101134

135+
* `chain_name` -
136+
(Optional)
137+
Creates the new snapshot in the snapshot chain labeled with the
138+
specified name. The chain name must be 1-63 characters long and
139+
comply with RFC1035. This is an uncommon option only for advanced
140+
service owners who needs to create separate snapshot chains, for
141+
example, for chargeback tracking. When you describe your snapshot
142+
resource, this field is visible only if it has a non-empty value.
143+
102144
* `description` -
103145
(Optional)
104146
An optional description of this resource.

0 commit comments

Comments
 (0)