Skip to content

Commit 729859d

Browse files
Feature Gap: Implement resourcePolicies.workloadPolicy for MIG (#14255) (#23420)
[upstream:587caf7f4a7983a91bad804d20f828948b092f72] Signed-off-by: Modular Magician <[email protected]>
1 parent 8a84da5 commit 729859d

File tree

4 files changed

+454
-0
lines changed

4 files changed

+454
-0
lines changed

.changelog/14255.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 `resource_policies.0.workload_policy` to `google_compute_instance_group_manager` resource (ga).
3+
```

google/services/compute/resource_compute_instance_group_manager.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package compute
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"log"
2223
"strings"
@@ -50,6 +51,7 @@ func ResourceComputeInstanceGroupManager() *schema.Resource {
5051
CustomizeDiff: customdiff.All(
5152
tpgresource.DefaultProviderProject,
5253
tpgresource.DefaultProviderZone,
54+
customdiff.ForceNewIfChange("resource_policies.0.workload_policy", ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty),
5355
),
5456
Schema: map[string]*schema.Schema{
5557
"base_instance_name": {
@@ -554,11 +556,31 @@ func ResourceComputeInstanceGroupManager() *schema.Resource {
554556
},
555557
},
556558
},
559+
"resource_policies": {
560+
Type: schema.TypeList,
561+
Optional: true,
562+
Description: `Resource policies for this managed instance group.`,
563+
MaxItems: 1,
564+
Elem: &schema.Resource{
565+
Schema: map[string]*schema.Schema{
566+
"workload_policy": {
567+
Type: schema.TypeString,
568+
Optional: true,
569+
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
570+
Description: `The URL of the workload policy that is specified for this managed instance group. It can be a full or partial URL.`,
571+
},
572+
},
573+
},
574+
},
557575
},
558576
UseJSONNumber: true,
559577
}
560578
}
561579

580+
func ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty(_ context.Context, old, new, _ interface{}) bool {
581+
return (old.(string) != "") && (new.(string) == "")
582+
}
583+
562584
func parseUniqueId(s string) (string, string) {
563585
splits := strings.SplitN(s, "?uniqueId=", 2)
564586
if len(splits) == 2 {
@@ -647,6 +669,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
647669
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
648670
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
649671
StatefulPolicy: expandStatefulPolicy(d),
672+
ResourcePolicies: expandResourcePolicies(d.Get("resource_policies").([]interface{})),
650673

651674
// Force send TargetSize to allow a value of 0.
652675
ForceSendFields: []string{"TargetSize"},
@@ -891,6 +914,9 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
891914
if err = d.Set("status", flattenStatus(manager.Status)); err != nil {
892915
return fmt.Errorf("Error setting status in state: %s", err.Error())
893916
}
917+
if err = d.Set("resource_policies", flattenResourcePolicies(manager.ResourcePolicies)); err != nil {
918+
return fmt.Errorf("Error setting resource_policies in state: %s", err.Error())
919+
}
894920

895921
// If unset in state set to default value
896922
if d.Get("wait_for_instances_status").(string) == "" {
@@ -995,6 +1021,11 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
9951021
change = true
9961022
}
9971023

1024+
if d.HasChange("resource_policies") {
1025+
updatedManager.ResourcePolicies = expandResourcePolicies(d.Get("resource_policies").([]interface{}))
1026+
change = true
1027+
}
1028+
9981029
if change {
9991030
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Patch(project, zone, d.Get("name").(string), updatedManager).Do()
10001031
if err != nil {
@@ -1261,6 +1292,20 @@ func expandVersions(configured []interface{}) []*compute.InstanceGroupManagerVer
12611292
return versions
12621293
}
12631294

1295+
func expandResourcePolicies(configured []interface{}) *compute.InstanceGroupManagerResourcePolicies {
1296+
resourcePolicies := &compute.InstanceGroupManagerResourcePolicies{}
1297+
1298+
if len(configured) > 0 {
1299+
data := configured[0].(map[string]interface{})
1300+
resourcePolicies.WorkloadPolicy = data["workload_policy"].(string)
1301+
resourcePolicies.ForceSendFields = []string{"WorkloadPolicy"}
1302+
} else {
1303+
resourcePolicies.NullFields = []string{"WorkloadPolicy"}
1304+
}
1305+
1306+
return resourcePolicies
1307+
}
1308+
12641309
func expandFixedOrPercent(configured []interface{}) *compute.FixedOrPercent {
12651310
fixedOrPercent := &compute.FixedOrPercent{}
12661311

@@ -1579,6 +1624,17 @@ func flattenStatusAllInstancesConfig(allInstancesConfig *compute.InstanceGroupMa
15791624
return results
15801625
}
15811626

1627+
func flattenResourcePolicies(resourcePolicies *compute.InstanceGroupManagerResourcePolicies) []map[string]interface{} {
1628+
results := []map[string]interface{}{}
1629+
if resourcePolicies != nil {
1630+
data := map[string]interface{}{
1631+
"workload_policy": resourcePolicies.WorkloadPolicy,
1632+
}
1633+
results = append(results, data)
1634+
}
1635+
return results
1636+
}
1637+
15821638
func resourceInstanceGroupManagerStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
15831639
if err := d.Set("wait_for_instances", false); err != nil {
15841640
return nil, fmt.Errorf("Error setting wait_for_instances: %s", err)

0 commit comments

Comments
 (0)