Skip to content

Commit 35906d4

Browse files
Feature Gap: Implement resourcePolicies.workloadPolicy for MIG (#14255) (#10265)
[upstream:587caf7f4a7983a91bad804d20f828948b092f72] Signed-off-by: Modular Magician <[email protected]>
1 parent 1d9c1d3 commit 35906d4

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-beta/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": {
@@ -585,11 +587,31 @@ func ResourceComputeInstanceGroupManager() *schema.Resource {
585587
},
586588
},
587589
},
590+
"resource_policies": {
591+
Type: schema.TypeList,
592+
Optional: true,
593+
Description: `Resource policies for this managed instance group.`,
594+
MaxItems: 1,
595+
Elem: &schema.Resource{
596+
Schema: map[string]*schema.Schema{
597+
"workload_policy": {
598+
Type: schema.TypeString,
599+
Optional: true,
600+
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
601+
Description: `The URL of the workload policy that is specified for this managed instance group. It can be a full or partial URL.`,
602+
},
603+
},
604+
},
605+
},
588606
},
589607
UseJSONNumber: true,
590608
}
591609
}
592610

611+
func ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty(_ context.Context, old, new, _ interface{}) bool {
612+
return (old.(string) != "") && (new.(string) == "")
613+
}
614+
593615
func parseUniqueId(s string) (string, string) {
594616
splits := strings.SplitN(s, "?uniqueId=", 2)
595617
if len(splits) == 2 {
@@ -678,6 +700,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
678700
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
679701
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
680702
StatefulPolicy: expandStatefulPolicy(d),
703+
ResourcePolicies: expandResourcePolicies(d.Get("resource_policies").([]interface{})),
681704
Params: expandInstanceGroupManagerParams(d),
682705

683706
// Force send TargetSize to allow a value of 0.
@@ -923,6 +946,9 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
923946
if err = d.Set("status", flattenStatus(manager.Status)); err != nil {
924947
return fmt.Errorf("Error setting status in state: %s", err.Error())
925948
}
949+
if err = d.Set("resource_policies", flattenResourcePolicies(manager.ResourcePolicies)); err != nil {
950+
return fmt.Errorf("Error setting resource_policies in state: %s", err.Error())
951+
}
926952

927953
// If unset in state set to default value
928954
if d.Get("wait_for_instances_status").(string) == "" {
@@ -1027,6 +1053,11 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
10271053
change = true
10281054
}
10291055

1056+
if d.HasChange("resource_policies") {
1057+
updatedManager.ResourcePolicies = expandResourcePolicies(d.Get("resource_policies").([]interface{}))
1058+
change = true
1059+
}
1060+
10301061
if change {
10311062
op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Patch(project, zone, d.Get("name").(string), updatedManager).Do()
10321063
if err != nil {
@@ -1293,6 +1324,20 @@ func expandVersions(configured []interface{}) []*compute.InstanceGroupManagerVer
12931324
return versions
12941325
}
12951326

1327+
func expandResourcePolicies(configured []interface{}) *compute.InstanceGroupManagerResourcePolicies {
1328+
resourcePolicies := &compute.InstanceGroupManagerResourcePolicies{}
1329+
1330+
if len(configured) > 0 {
1331+
data := configured[0].(map[string]interface{})
1332+
resourcePolicies.WorkloadPolicy = data["workload_policy"].(string)
1333+
resourcePolicies.ForceSendFields = []string{"WorkloadPolicy"}
1334+
} else {
1335+
resourcePolicies.NullFields = []string{"WorkloadPolicy"}
1336+
}
1337+
1338+
return resourcePolicies
1339+
}
1340+
12961341
func expandFixedOrPercent(configured []interface{}) *compute.FixedOrPercent {
12971342
fixedOrPercent := &compute.FixedOrPercent{}
12981343

@@ -1624,6 +1669,17 @@ func flattenStatusAllInstancesConfig(allInstancesConfig *compute.InstanceGroupMa
16241669
return results
16251670
}
16261671

1672+
func flattenResourcePolicies(resourcePolicies *compute.InstanceGroupManagerResourcePolicies) []map[string]interface{} {
1673+
results := []map[string]interface{}{}
1674+
if resourcePolicies != nil {
1675+
data := map[string]interface{}{
1676+
"workload_policy": resourcePolicies.WorkloadPolicy,
1677+
}
1678+
results = append(results, data)
1679+
}
1680+
return results
1681+
}
1682+
16271683
func resourceInstanceGroupManagerStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
16281684
if err := d.Set("wait_for_instances", false); err != nil {
16291685
return nil, fmt.Errorf("Error setting wait_for_instances: %s", err)

0 commit comments

Comments
 (0)