Skip to content

Commit a5198b9

Browse files
Add disk resource policies field to the compute instance (#11527)
[upstream:c2e2bf70c45e1afe0d214bca4eb51ee03507a09c] Signed-off-by: Modular Magician <[email protected]>
1 parent 6841180 commit a5198b9

7 files changed

+407
-0
lines changed

.changelog/11527.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added support for `boot_disk.initialize_params.resource_policies` in `google_compute_instance` and `google_instance_template`
3+
```

google-beta/services/compute/resource_compute_instance.go

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var (
7171
"boot_disk.0.initialize_params.0.provisioned_throughput",
7272
"boot_disk.0.initialize_params.0.enable_confidential_compute",
7373
"boot_disk.0.initialize_params.0.storage_pool",
74+
"boot_disk.0.initialize_params.0.resource_policies",
7475
}
7576

7677
schedulingKeys = []string{
@@ -305,6 +306,17 @@ func ResourceComputeInstance() *schema.Resource {
305306
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.`,
306307
},
307308

309+
"resource_policies": {
310+
Type: schema.TypeList,
311+
Elem: &schema.Schema{Type: schema.TypeString},
312+
Optional: true,
313+
ForceNew: true,
314+
AtLeastOneOf: initializeParamsKeys,
315+
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
316+
MaxItems: 1,
317+
Description: `A list of self_links of resource policies to attach to the instance's boot disk. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported.`,
318+
},
319+
308320
"provisioned_iops": {
309321
Type: schema.TypeInt,
310322
Optional: true,
@@ -3010,6 +3022,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
30103022
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.resource_manager_tags")
30113023
}
30123024

3025+
if _, ok := d.GetOk("boot_disk.0.initialize_params.0.resource_policies"); ok {
3026+
disk.InitializeParams.ResourcePolicies = tpgresource.ConvertStringArr(d.Get("boot_disk.0.initialize_params.0.resource_policies").([]interface{}))
3027+
}
3028+
30133029
if v, ok := d.GetOk("boot_disk.0.initialize_params.0.storage_pool"); ok {
30143030
disk.InitializeParams.StoragePool = v.(string)
30153031
}
@@ -3055,6 +3071,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
30553071
"size": diskDetails.SizeGb,
30563072
"labels": diskDetails.Labels,
30573073
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),
3074+
"resource_policies": diskDetails.ResourcePolicies,
30583075
"provisioned_iops": diskDetails.ProvisionedIops,
30593076
"provisioned_throughput": diskDetails.ProvisionedThroughput,
30603077
"enable_confidential_compute": diskDetails.EnableConfidentialCompute,

google-beta/services/compute/resource_compute_instance_from_template.go

+9
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *transport_t
248248
// only have the name (since they're global).
249249
disk.InitializeParams.DiskType = fmt.Sprintf("zones/%s/diskTypes/%s", zone.Name, dt)
250250
}
251+
if rp := disk.InitializeParams.ResourcePolicies; len(rp) > 0 {
252+
// Instances need a URL for the resource policy, but instance templates
253+
// only have the name (since they're global).
254+
for i := range rp {
255+
rp[i], _ = parseUniqueId(rp[i]) // in some cases the API translation doesn't work and returns entire url when only name is provided. And allows for id to be passed as well
256+
rp[i] = fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, regionFromUrl(zone.Region), rp[i])
257+
}
258+
disk.InitializeParams.ResourcePolicies = rp
259+
}
251260
}
252261
disks = append(disks, disk)
253262
break

google-beta/services/compute/resource_compute_instance_from_template_test.go

+188
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,38 @@ func TestAccComputeInstanceFromTemplateWithOverride_localSsdRecoveryTimeout(t *t
162162
})
163163
}
164164

165+
func TestAccComputeInstanceFromTemplate_diskResourcePolicies(t *testing.T) {
166+
t.Parallel()
167+
168+
var instance compute.Instance
169+
templateName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
170+
suffix := acctest.RandString(t, 10)
171+
172+
acctest.VcrTest(t, resource.TestCase{
173+
PreCheck: func() { acctest.AccTestPreCheck(t) },
174+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
175+
CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroyProducer(t),
176+
Steps: []resource.TestStep{
177+
{
178+
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesCreate(suffix, templateName),
179+
Check: resource.ComposeTestCheckFunc(
180+
testAccCheckComputeInstanceExists(t, "google_compute_instance_from_template.foobar", &instance),
181+
),
182+
},
183+
{
184+
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesUpdate(suffix, templateName),
185+
Check: resource.ComposeTestCheckFunc(
186+
testAccCheckComputeInstanceExists(t, "google_compute_instance_from_template.foobar", &instance),
187+
),
188+
},
189+
{
190+
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesTwoPolicies(suffix, templateName),
191+
ExpectError: regexp.MustCompile("Too many list items"),
192+
},
193+
},
194+
})
195+
}
196+
165197
func TestAccComputeInstanceFromTemplate_partnerMetadata(t *testing.T) {
166198
t.Parallel()
167199

@@ -1834,3 +1866,159 @@ resource "google_compute_instance_from_template" "foobar" {
18341866
}
18351867
`, template, instance, template, instance)
18361868
}
1869+
1870+
func testAccComputeInstanceFromTemplate_diskResourcePoliciesCreate(suffix, template string) string {
1871+
return fmt.Sprintf(`
1872+
resource "google_compute_resource_policy" "test-snapshot-policy" {
1873+
name = "test-policy-%s"
1874+
snapshot_schedule_policy {
1875+
schedule {
1876+
hourly_schedule {
1877+
hours_in_cycle = 1
1878+
start_time = "11:00"
1879+
}
1880+
}
1881+
}
1882+
}
1883+
1884+
resource "google_compute_resource_policy" "test-snapshot-policy2" {
1885+
name = "test-policy2-%s"
1886+
snapshot_schedule_policy {
1887+
schedule {
1888+
hourly_schedule {
1889+
hours_in_cycle = 1
1890+
start_time = "22:00"
1891+
}
1892+
}
1893+
}
1894+
}
1895+
1896+
data "google_compute_image" "my_image" {
1897+
family = "debian-11"
1898+
project = "debian-cloud"
1899+
}
1900+
1901+
resource "google_compute_region_instance_template" "foobar" {
1902+
name = "%s"
1903+
region = "us-central1"
1904+
machine_type = "n1-standard-1"
1905+
disk {
1906+
resource_policies = [ google_compute_resource_policy.test-snapshot-policy.name ]
1907+
source_image = data.google_compute_image.my_image.self_link
1908+
}
1909+
network_interface {
1910+
network = "default"
1911+
}
1912+
}
1913+
1914+
resource "google_compute_instance_from_template" "foobar" {
1915+
name = "%s"
1916+
zone = "us-central1-a"
1917+
source_instance_template = google_compute_region_instance_template.foobar.id
1918+
}
1919+
`, suffix, suffix, template, template)
1920+
}
1921+
1922+
func testAccComputeInstanceFromTemplate_diskResourcePoliciesUpdate(suffix, template string) string {
1923+
return fmt.Sprintf(`
1924+
resource "google_compute_resource_policy" "test-snapshot-policy" {
1925+
name = "test-policy-%s"
1926+
snapshot_schedule_policy {
1927+
schedule {
1928+
hourly_schedule {
1929+
hours_in_cycle = 1
1930+
start_time = "11:00"
1931+
}
1932+
}
1933+
}
1934+
}
1935+
1936+
resource "google_compute_resource_policy" "test-snapshot-policy2" {
1937+
name = "test-policy2-%s"
1938+
snapshot_schedule_policy {
1939+
schedule {
1940+
hourly_schedule {
1941+
hours_in_cycle = 1
1942+
start_time = "22:00"
1943+
}
1944+
}
1945+
}
1946+
}
1947+
1948+
data "google_compute_image" "my_image" {
1949+
family = "debian-11"
1950+
project = "debian-cloud"
1951+
}
1952+
1953+
resource "google_compute_region_instance_template" "foobar" {
1954+
name = "%s"
1955+
region = "us-central1"
1956+
machine_type = "n1-standard-1"
1957+
disk {
1958+
resource_policies = [ google_compute_resource_policy.test-snapshot-policy2.name ]
1959+
source_image = data.google_compute_image.my_image.self_link
1960+
}
1961+
network_interface {
1962+
network = "default"
1963+
}
1964+
}
1965+
1966+
resource "google_compute_instance_from_template" "foobar" {
1967+
name = "%s"
1968+
zone = "us-central1-a"
1969+
source_instance_template = google_compute_region_instance_template.foobar.id
1970+
}
1971+
`, suffix, suffix, template, template)
1972+
}
1973+
1974+
func testAccComputeInstanceFromTemplate_diskResourcePoliciesTwoPolicies(suffix, template string) string {
1975+
return fmt.Sprintf(`
1976+
resource "google_compute_resource_policy" "test-snapshot-policy" {
1977+
name = "test-policy-%s"
1978+
snapshot_schedule_policy {
1979+
schedule {
1980+
hourly_schedule {
1981+
hours_in_cycle = 1
1982+
start_time = "11:00"
1983+
}
1984+
}
1985+
}
1986+
}
1987+
1988+
resource "google_compute_resource_policy" "test-snapshot-policy2" {
1989+
name = "test-policy2-%s"
1990+
snapshot_schedule_policy {
1991+
schedule {
1992+
hourly_schedule {
1993+
hours_in_cycle = 1
1994+
start_time = "22:00"
1995+
}
1996+
}
1997+
}
1998+
}
1999+
2000+
data "google_compute_image" "my_image" {
2001+
family = "debian-11"
2002+
project = "debian-cloud"
2003+
}
2004+
2005+
resource "google_compute_region_instance_template" "foobar" {
2006+
name = "%s"
2007+
region = "us-central1"
2008+
machine_type = "n1-standard-1"
2009+
disk {
2010+
resource_policies = [ google_compute_resource_policy.test-snapshot-policy.name, google_compute_resource_policy.test-snapshot-policy2.name ]
2011+
source_image = data.google_compute_image.my_image.self_link
2012+
}
2013+
network_interface {
2014+
network = "default"
2015+
}
2016+
}
2017+
2018+
resource "google_compute_instance_from_template" "foobar" {
2019+
name = "%s"
2020+
zone = "us-central1-a"
2021+
source_instance_template = google_compute_region_instance_template.foobar.id
2022+
}
2023+
`, suffix, suffix, template, template)
2024+
}

0 commit comments

Comments
 (0)