Skip to content

Commit 90791ec

Browse files
Feature gap: Add workload_policy field (#13341) (#9599)
[upstream:a47bdc51b413d33c8d872b186b9f96d8a7dd64da] Signed-off-by: Modular Magician <[email protected]>
1 parent 6725dd0 commit 90791ec

5 files changed

+324
-0
lines changed

.changelog/13341.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `workload_policy.type`, `workload_policy.max_topology_distance` and `workload_policy.accelerator_topology` fields to `google_compute_resource_policy` resource
3+
```

google-beta/services/compute/resource_compute_resource_policy.go

+123
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,40 @@ with RFC1035.`,
365365
},
366366
ConflictsWith: []string{"group_placement_policy", "instance_schedule_policy", "disk_consistency_group_policy"},
367367
},
368+
"workload_policy": {
369+
Type: schema.TypeList,
370+
Optional: true,
371+
Description: `Represents the workload policy.`,
372+
MaxItems: 1,
373+
Elem: &schema.Resource{
374+
Schema: map[string]*schema.Schema{
375+
"type": {
376+
Type: schema.TypeString,
377+
Required: true,
378+
ForceNew: true,
379+
ValidateFunc: verify.ValidateEnum([]string{"HIGH_AVAILABILITY", "HIGH_THROUGHPUT"}),
380+
Description: `The type of workload policy. Possible values: ["HIGH_AVAILABILITY", "HIGH_THROUGHPUT"]`,
381+
},
382+
"accelerator_topology": {
383+
Type: schema.TypeString,
384+
Optional: true,
385+
ForceNew: true,
386+
Description: `The accelerator topology. This field can be set only when the workload policy type is HIGH_THROUGHPUT
387+
and cannot be set if max topology distance is set.`,
388+
ConflictsWith: []string{"workload_policy.0.max_topology_distance"},
389+
},
390+
"max_topology_distance": {
391+
Type: schema.TypeString,
392+
Optional: true,
393+
ForceNew: true,
394+
ValidateFunc: verify.ValidateEnum([]string{"BLOCK", "CLUSTER", "SUBBLOCK", ""}),
395+
Description: `The maximum topology distance. This field can be set only when the workload policy type is HIGH_THROUGHPUT
396+
and cannot be set if accelerator topology is set. Possible values: ["BLOCK", "CLUSTER", "SUBBLOCK"]`,
397+
ConflictsWith: []string{"workload_policy.0.accelerator_topology"},
398+
},
399+
},
400+
},
401+
},
368402
"project": {
369403
Type: schema.TypeString,
370404
Optional: true,
@@ -443,6 +477,12 @@ func resourceComputeResourcePolicyCreate(d *schema.ResourceData, meta interface{
443477
} else if v, ok := d.GetOkExists("disk_consistency_group_policy"); ok || !reflect.DeepEqual(v, diskConsistencyGroupPolicyProp) {
444478
obj["diskConsistencyGroupPolicy"] = diskConsistencyGroupPolicyProp
445479
}
480+
workloadPolicyProp, err := expandComputeResourcePolicyWorkloadPolicy(d.Get("workload_policy"), d, config)
481+
if err != nil {
482+
return err
483+
} else if v, ok := d.GetOkExists("workload_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(workloadPolicyProp)) && (ok || !reflect.DeepEqual(v, workloadPolicyProp)) {
484+
obj["workloadPolicy"] = workloadPolicyProp
485+
}
446486
regionProp, err := expandComputeResourcePolicyRegion(d.Get("region"), d, config)
447487
if err != nil {
448488
return err
@@ -574,6 +614,9 @@ func resourceComputeResourcePolicyRead(d *schema.ResourceData, meta interface{})
574614
if err := d.Set("disk_consistency_group_policy", flattenComputeResourcePolicyDiskConsistencyGroupPolicy(res["diskConsistencyGroupPolicy"], d, config)); err != nil {
575615
return fmt.Errorf("Error reading ResourcePolicy: %s", err)
576616
}
617+
if err := d.Set("workload_policy", flattenComputeResourcePolicyWorkloadPolicy(res["workloadPolicy"], d, config)); err != nil {
618+
return fmt.Errorf("Error reading ResourcePolicy: %s", err)
619+
}
577620
if err := d.Set("self_link", tpgresource.ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
578621
return fmt.Errorf("Error reading ResourcePolicy: %s", err)
579622
}
@@ -633,6 +676,12 @@ func resourceComputeResourcePolicyUpdate(d *schema.ResourceData, meta interface{
633676
} else if v, ok := d.GetOkExists("disk_consistency_group_policy"); ok || !reflect.DeepEqual(v, diskConsistencyGroupPolicyProp) {
634677
obj["diskConsistencyGroupPolicy"] = diskConsistencyGroupPolicyProp
635678
}
679+
workloadPolicyProp, err := expandComputeResourcePolicyWorkloadPolicy(d.Get("workload_policy"), d, config)
680+
if err != nil {
681+
return err
682+
} else if v, ok := d.GetOkExists("workload_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, workloadPolicyProp)) {
683+
obj["workloadPolicy"] = workloadPolicyProp
684+
}
636685

637686
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/resourcePolicies/{{name}}")
638687
if err != nil {
@@ -1136,6 +1185,35 @@ func flattenComputeResourcePolicyDiskConsistencyGroupPolicy(v interface{}, d *sc
11361185
return []interface{}{transformed}
11371186
}
11381187

1188+
func flattenComputeResourcePolicyWorkloadPolicy(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1189+
if v == nil {
1190+
return nil
1191+
}
1192+
original := v.(map[string]interface{})
1193+
if len(original) == 0 {
1194+
return nil
1195+
}
1196+
transformed := make(map[string]interface{})
1197+
transformed["type"] =
1198+
flattenComputeResourcePolicyWorkloadPolicyType(original["type"], d, config)
1199+
transformed["max_topology_distance"] =
1200+
flattenComputeResourcePolicyWorkloadPolicyMaxTopologyDistance(original["maxTopologyDistance"], d, config)
1201+
transformed["accelerator_topology"] =
1202+
flattenComputeResourcePolicyWorkloadPolicyAcceleratorTopology(original["acceleratorTopology"], d, config)
1203+
return []interface{}{transformed}
1204+
}
1205+
func flattenComputeResourcePolicyWorkloadPolicyType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1206+
return v
1207+
}
1208+
1209+
func flattenComputeResourcePolicyWorkloadPolicyMaxTopologyDistance(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1210+
return v
1211+
}
1212+
1213+
func flattenComputeResourcePolicyWorkloadPolicyAcceleratorTopology(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1214+
return v
1215+
}
1216+
11391217
func expandComputeResourcePolicyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
11401218
return v, nil
11411219
}
@@ -1622,6 +1700,51 @@ func expandComputeResourcePolicyDiskConsistencyGroupPolicy(v interface{}, d tpgr
16221700
return transformed, nil
16231701
}
16241702

1703+
func expandComputeResourcePolicyWorkloadPolicy(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1704+
l := v.([]interface{})
1705+
if len(l) == 0 || l[0] == nil {
1706+
return nil, nil
1707+
}
1708+
raw := l[0]
1709+
original := raw.(map[string]interface{})
1710+
transformed := make(map[string]interface{})
1711+
1712+
transformedType, err := expandComputeResourcePolicyWorkloadPolicyType(original["type"], d, config)
1713+
if err != nil {
1714+
return nil, err
1715+
} else if val := reflect.ValueOf(transformedType); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1716+
transformed["type"] = transformedType
1717+
}
1718+
1719+
transformedMaxTopologyDistance, err := expandComputeResourcePolicyWorkloadPolicyMaxTopologyDistance(original["max_topology_distance"], d, config)
1720+
if err != nil {
1721+
return nil, err
1722+
} else if val := reflect.ValueOf(transformedMaxTopologyDistance); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1723+
transformed["maxTopologyDistance"] = transformedMaxTopologyDistance
1724+
}
1725+
1726+
transformedAcceleratorTopology, err := expandComputeResourcePolicyWorkloadPolicyAcceleratorTopology(original["accelerator_topology"], d, config)
1727+
if err != nil {
1728+
return nil, err
1729+
} else if val := reflect.ValueOf(transformedAcceleratorTopology); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1730+
transformed["acceleratorTopology"] = transformedAcceleratorTopology
1731+
}
1732+
1733+
return transformed, nil
1734+
}
1735+
1736+
func expandComputeResourcePolicyWorkloadPolicyType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1737+
return v, nil
1738+
}
1739+
1740+
func expandComputeResourcePolicyWorkloadPolicyMaxTopologyDistance(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1741+
return v, nil
1742+
}
1743+
1744+
func expandComputeResourcePolicyWorkloadPolicyAcceleratorTopology(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1745+
return v, nil
1746+
}
1747+
16251748
func expandComputeResourcePolicyRegion(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
16261749
f, err := tpgresource.ParseGlobalFieldValue("regions", v.(string), "project", d, config, true)
16271750
if err != nil {

google-beta/services/compute/resource_compute_resource_policy_generated_meta.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ fields:
3131
- field: 'snapshot_schedule_policy.snapshot_properties.guest_flush'
3232
- field: 'snapshot_schedule_policy.snapshot_properties.labels'
3333
- field: 'snapshot_schedule_policy.snapshot_properties.storage_locations'
34+
- field: 'workload_policy.accelerator_topology'
35+
- field: 'workload_policy.max_topology_distance'
36+
- field: 'workload_policy.type'

google-beta/services/compute/resource_compute_resource_policy_generated_test.go

+116
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,122 @@ resource "google_compute_resource_policy" "cgroup" {
381381
`, context)
382382
}
383383

384+
func TestAccComputeResourcePolicy_resourcePolicyWorkloadPolicyExample(t *testing.T) {
385+
t.Parallel()
386+
387+
context := map[string]interface{}{
388+
"random_suffix": acctest.RandString(t, 10),
389+
}
390+
391+
acctest.VcrTest(t, resource.TestCase{
392+
PreCheck: func() { acctest.AccTestPreCheck(t) },
393+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
394+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
395+
Steps: []resource.TestStep{
396+
{
397+
Config: testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyExample(context),
398+
},
399+
{
400+
ResourceName: "google_compute_resource_policy.bar",
401+
ImportState: true,
402+
ImportStateVerify: true,
403+
ImportStateVerifyIgnore: []string{"region"},
404+
},
405+
},
406+
})
407+
}
408+
409+
func testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyExample(context map[string]interface{}) string {
410+
return acctest.Nprintf(`
411+
resource "google_compute_resource_policy" "bar" {
412+
name = "tf-test-gce-policy%{random_suffix}"
413+
region = "europe-west1"
414+
provider = google-beta
415+
workload_policy {
416+
type = "HIGH_AVAILABILITY"
417+
}
418+
}
419+
`, context)
420+
}
421+
422+
func TestAccComputeResourcePolicy_resourcePolicyWorkloadPolicyAcceleratorTopologyExample(t *testing.T) {
423+
t.Parallel()
424+
425+
context := map[string]interface{}{
426+
"random_suffix": acctest.RandString(t, 10),
427+
}
428+
429+
acctest.VcrTest(t, resource.TestCase{
430+
PreCheck: func() { acctest.AccTestPreCheck(t) },
431+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
432+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
433+
Steps: []resource.TestStep{
434+
{
435+
Config: testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyAcceleratorTopologyExample(context),
436+
},
437+
{
438+
ResourceName: "google_compute_resource_policy.bar",
439+
ImportState: true,
440+
ImportStateVerify: true,
441+
ImportStateVerifyIgnore: []string{"region"},
442+
},
443+
},
444+
})
445+
}
446+
447+
func testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyAcceleratorTopologyExample(context map[string]interface{}) string {
448+
return acctest.Nprintf(`
449+
resource "google_compute_resource_policy" "bar" {
450+
name = "tf-test-gce-policy%{random_suffix}"
451+
region = "europe-west1"
452+
provider = google-beta
453+
workload_policy {
454+
type = "HIGH_THROUGHPUT"
455+
accelerator_topology = "SOME NEW TOPOLOGY"
456+
}
457+
}
458+
`, context)
459+
}
460+
461+
func TestAccComputeResourcePolicy_resourcePolicyWorkloadPolicyMaxTopologyDistanceExample(t *testing.T) {
462+
t.Parallel()
463+
464+
context := map[string]interface{}{
465+
"random_suffix": acctest.RandString(t, 10),
466+
}
467+
468+
acctest.VcrTest(t, resource.TestCase{
469+
PreCheck: func() { acctest.AccTestPreCheck(t) },
470+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
471+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
472+
Steps: []resource.TestStep{
473+
{
474+
Config: testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyMaxTopologyDistanceExample(context),
475+
},
476+
{
477+
ResourceName: "google_compute_resource_policy.bar",
478+
ImportState: true,
479+
ImportStateVerify: true,
480+
ImportStateVerifyIgnore: []string{"region"},
481+
},
482+
},
483+
})
484+
}
485+
486+
func testAccComputeResourcePolicy_resourcePolicyWorkloadPolicyMaxTopologyDistanceExample(context map[string]interface{}) string {
487+
return acctest.Nprintf(`
488+
resource "google_compute_resource_policy" "bar" {
489+
name = "tf-test-gce-policy%{random_suffix}"
490+
region = "europe-west1"
491+
provider = google-beta
492+
workload_policy {
493+
type = "HIGH_THROUGHPUT"
494+
max_topology_distance = "BLOCK"
495+
}
496+
}
497+
`, context)
498+
}
499+
384500
func TestAccComputeResourcePolicy_resourcePolicyPlacementPolicyGpuTopologyExample(t *testing.T) {
385501
t.Parallel()
386502

website/docs/r/compute_resource_policy.html.markdown

+79
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,62 @@ resource "google_compute_resource_policy" "cgroup" {
197197
}
198198
}
199199
```
200+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
201+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=resource_policy_workload_policy&open_in_editor=main.tf" target="_blank">
202+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
203+
</a>
204+
</div>
205+
## Example Usage - Resource Policy Workload Policy
206+
207+
208+
```hcl
209+
resource "google_compute_resource_policy" "bar" {
210+
name = "gce-policy"
211+
region = "europe-west1"
212+
provider = google-beta
213+
workload_policy {
214+
type = "HIGH_AVAILABILITY"
215+
}
216+
}
217+
```
218+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
219+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=resource_policy_workload_policy_accelerator_topology&open_in_editor=main.tf" target="_blank">
220+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
221+
</a>
222+
</div>
223+
## Example Usage - Resource Policy Workload Policy Accelerator Topology
224+
225+
226+
```hcl
227+
resource "google_compute_resource_policy" "bar" {
228+
name = "gce-policy"
229+
region = "europe-west1"
230+
provider = google-beta
231+
workload_policy {
232+
type = "HIGH_THROUGHPUT"
233+
accelerator_topology = "SOME NEW TOPOLOGY"
234+
}
235+
}
236+
```
237+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
238+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=resource_policy_workload_policy_max_topology_distance&open_in_editor=main.tf" target="_blank">
239+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
240+
</a>
241+
</div>
242+
## Example Usage - Resource Policy Workload Policy Max Topology Distance
243+
244+
245+
```hcl
246+
resource "google_compute_resource_policy" "bar" {
247+
name = "gce-policy"
248+
region = "europe-west1"
249+
provider = google-beta
250+
workload_policy {
251+
type = "HIGH_THROUGHPUT"
252+
max_topology_distance = "BLOCK"
253+
}
254+
}
255+
```
200256
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
201257
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=resource_policy_placement_policy_gpu_topology&open_in_editor=main.tf" target="_blank">
202258
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
@@ -261,6 +317,11 @@ The following arguments are supported:
261317
Replication consistency group for asynchronous disk replication.
262318
Structure is [documented below](#nested_disk_consistency_group_policy).
263319

320+
* `workload_policy` -
321+
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
322+
Represents the workload policy.
323+
Structure is [documented below](#nested_workload_policy).
324+
264325
* `region` -
265326
(Optional)
266327
Region where resource policy resides.
@@ -456,6 +517,24 @@ The following arguments are supported:
456517
(Required)
457518
Enable disk consistency on the resource policy.
458519

520+
<a name="nested_workload_policy"></a>The `workload_policy` block supports:
521+
522+
* `type` -
523+
(Required)
524+
The type of workload policy.
525+
Possible values are: `HIGH_AVAILABILITY`, `HIGH_THROUGHPUT`.
526+
527+
* `max_topology_distance` -
528+
(Optional)
529+
The maximum topology distance. This field can be set only when the workload policy type is HIGH_THROUGHPUT
530+
and cannot be set if accelerator topology is set.
531+
Possible values are: `BLOCK`, `CLUSTER`, `SUBBLOCK`.
532+
533+
* `accelerator_topology` -
534+
(Optional)
535+
The accelerator topology. This field can be set only when the workload policy type is HIGH_THROUGHPUT
536+
and cannot be set if max topology distance is set.
537+
459538
## Attributes Reference
460539

461540
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)