Skip to content

Commit be92028

Browse files
Add max_distance field to group placement policy (#7354) (#5257)
Signed-off-by: Modular Magician <[email protected]>
1 parent a839621 commit be92028

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

.changelog/7354.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `max_distance` field to `resource-policy` resource
3+
```

google-beta/resource_compute_resource_policy.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ Specify 'COLLOCATED' to enable collocation. Can only be specified with 'vm_count
8282
with a COLLOCATED policy, then exactly 'vm_count' instances must be created at the same time with the resource policy
8383
attached. Possible values: ["COLLOCATED"]`,
8484
},
85+
"max_distance": {
86+
Type: schema.TypeInt,
87+
Optional: true,
88+
ForceNew: true,
89+
Description: `Specifies the number of max logical switches.`,
90+
},
8591
"vm_count": {
8692
Type: schema.TypeInt,
8793
Optional: true,
@@ -837,6 +843,8 @@ func flattenComputeResourcePolicyGroupPlacementPolicy(v interface{}, d *schema.R
837843
flattenComputeResourcePolicyGroupPlacementPolicyAvailabilityDomainCount(original["availabilityDomainCount"], d, config)
838844
transformed["collocation"] =
839845
flattenComputeResourcePolicyGroupPlacementPolicyCollocation(original["collocation"], d, config)
846+
transformed["max_distance"] =
847+
flattenComputeResourcePolicyGroupPlacementPolicyMaxDistance(original["maxDistance"], d, config)
840848
return []interface{}{transformed}
841849
}
842850
func flattenComputeResourcePolicyGroupPlacementPolicyVmCount(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -877,6 +885,23 @@ func flattenComputeResourcePolicyGroupPlacementPolicyCollocation(v interface{},
877885
return v
878886
}
879887

888+
func flattenComputeResourcePolicyGroupPlacementPolicyMaxDistance(v interface{}, d *schema.ResourceData, config *Config) interface{} {
889+
// Handles the string fixed64 format
890+
if strVal, ok := v.(string); ok {
891+
if intVal, err := stringToFixed64(strVal); err == nil {
892+
return intVal
893+
}
894+
}
895+
896+
// number values are represented as float64
897+
if floatVal, ok := v.(float64); ok {
898+
intVal := int(floatVal)
899+
return intVal
900+
}
901+
902+
return v // let terraform core handle it otherwise
903+
}
904+
880905
func flattenComputeResourcePolicyInstanceSchedulePolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
881906
if v == nil {
882907
return nil
@@ -1278,6 +1303,13 @@ func expandComputeResourcePolicyGroupPlacementPolicy(v interface{}, d TerraformR
12781303
transformed["collocation"] = transformedCollocation
12791304
}
12801305

1306+
transformedMaxDistance, err := expandComputeResourcePolicyGroupPlacementPolicyMaxDistance(original["max_distance"], d, config)
1307+
if err != nil {
1308+
return nil, err
1309+
} else if val := reflect.ValueOf(transformedMaxDistance); val.IsValid() && !isEmptyValue(val) {
1310+
transformed["maxDistance"] = transformedMaxDistance
1311+
}
1312+
12811313
return transformed, nil
12821314
}
12831315

@@ -1293,6 +1325,10 @@ func expandComputeResourcePolicyGroupPlacementPolicyCollocation(v interface{}, d
12931325
return v, nil
12941326
}
12951327

1328+
func expandComputeResourcePolicyGroupPlacementPolicyMaxDistance(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1329+
return v, nil
1330+
}
1331+
12961332
func expandComputeResourcePolicyInstanceSchedulePolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
12971333
l := v.([]interface{})
12981334
if len(l) == 0 || l[0] == nil {

google-beta/resource_compute_resource_policy_generated_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,46 @@ resource "google_compute_resource_policy" "baz" {
156156
`, context)
157157
}
158158

159+
func TestAccComputeResourcePolicy_resourcePolicyPlacementPolicyMaxDistanceExample(t *testing.T) {
160+
t.Parallel()
161+
162+
context := map[string]interface{}{
163+
"random_suffix": randString(t, 10),
164+
}
165+
166+
vcrTest(t, resource.TestCase{
167+
PreCheck: func() { testAccPreCheck(t) },
168+
Providers: testAccProvidersOiCS,
169+
CheckDestroy: testAccCheckComputeResourcePolicyDestroyProducer(t),
170+
Steps: []resource.TestStep{
171+
{
172+
Config: testAccComputeResourcePolicy_resourcePolicyPlacementPolicyMaxDistanceExample(context),
173+
},
174+
{
175+
ResourceName: "google_compute_resource_policy.baz",
176+
ImportState: true,
177+
ImportStateVerify: true,
178+
ImportStateVerifyIgnore: []string{"region"},
179+
},
180+
},
181+
})
182+
}
183+
184+
func testAccComputeResourcePolicy_resourcePolicyPlacementPolicyMaxDistanceExample(context map[string]interface{}) string {
185+
return Nprintf(`
186+
resource "google_compute_resource_policy" "baz" {
187+
name = "policy%{random_suffix}"
188+
region = "us-central1"
189+
provider = google-beta
190+
group_placement_policy {
191+
vm_count = 2
192+
collocation = "COLLOCATED"
193+
max_distance = 2
194+
}
195+
}
196+
`, context)
197+
}
198+
159199
func TestAccComputeResourcePolicy_resourcePolicyInstanceSchedulePolicyExample(t *testing.T) {
160200
t.Parallel()
161201

website/docs/r/compute_resource_policy.html.markdown

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ resource "google_compute_resource_policy" "baz" {
9696
}
9797
}
9898
```
99+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
100+
<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_placement_policy_max_distance&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
101+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
102+
</a>
103+
</div>
104+
## Example Usage - Resource Policy Placement Policy Max Distance
105+
106+
107+
```hcl
108+
resource "google_compute_resource_policy" "baz" {
109+
name = "policy"
110+
region = "us-central1"
111+
provider = google-beta
112+
group_placement_policy {
113+
vm_count = 2
114+
collocation = "COLLOCATED"
115+
max_distance = 2
116+
}
117+
}
118+
```
99119
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
100120
<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_instance_schedule_policy&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
101121
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
@@ -338,6 +358,10 @@ The following arguments are supported:
338358
attached.
339359
Possible values are `COLLOCATED`.
340360

361+
* `max_distance` -
362+
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
363+
Specifies the number of max logical switches.
364+
341365
<a name="nested_instance_schedule_policy"></a>The `instance_schedule_policy` block supports:
342366

343367
* `vm_start_schedule` -

0 commit comments

Comments
 (0)