|
17 | 17 | package compute |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "context" |
20 | 21 | "fmt" |
21 | 22 | "log" |
22 | 23 | "strings" |
@@ -50,6 +51,7 @@ func ResourceComputeInstanceGroupManager() *schema.Resource { |
50 | 51 | CustomizeDiff: customdiff.All( |
51 | 52 | tpgresource.DefaultProviderProject, |
52 | 53 | tpgresource.DefaultProviderZone, |
| 54 | + customdiff.ForceNewIfChange("resource_policies.0.workload_policy", ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty), |
53 | 55 | ), |
54 | 56 | Schema: map[string]*schema.Schema{ |
55 | 57 | "base_instance_name": { |
@@ -554,11 +556,31 @@ func ResourceComputeInstanceGroupManager() *schema.Resource { |
554 | 556 | }, |
555 | 557 | }, |
556 | 558 | }, |
| 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 | + }, |
557 | 575 | }, |
558 | 576 | UseJSONNumber: true, |
559 | 577 | } |
560 | 578 | } |
561 | 579 |
|
| 580 | +func ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty(_ context.Context, old, new, _ interface{}) bool { |
| 581 | + return (old.(string) != "") && (new.(string) == "") |
| 582 | +} |
| 583 | + |
562 | 584 | func parseUniqueId(s string) (string, string) { |
563 | 585 | splits := strings.SplitN(s, "?uniqueId=", 2) |
564 | 586 | if len(splits) == 2 { |
@@ -647,6 +669,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte |
647 | 669 | InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})), |
648 | 670 | AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})), |
649 | 671 | StatefulPolicy: expandStatefulPolicy(d), |
| 672 | + ResourcePolicies: expandResourcePolicies(d.Get("resource_policies").([]interface{})), |
650 | 673 |
|
651 | 674 | // Force send TargetSize to allow a value of 0. |
652 | 675 | ForceSendFields: []string{"TargetSize"}, |
@@ -891,6 +914,9 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf |
891 | 914 | if err = d.Set("status", flattenStatus(manager.Status)); err != nil { |
892 | 915 | return fmt.Errorf("Error setting status in state: %s", err.Error()) |
893 | 916 | } |
| 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 | + } |
894 | 920 |
|
895 | 921 | // If unset in state set to default value |
896 | 922 | if d.Get("wait_for_instances_status").(string) == "" { |
@@ -995,6 +1021,11 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte |
995 | 1021 | change = true |
996 | 1022 | } |
997 | 1023 |
|
| 1024 | + if d.HasChange("resource_policies") { |
| 1025 | + updatedManager.ResourcePolicies = expandResourcePolicies(d.Get("resource_policies").([]interface{})) |
| 1026 | + change = true |
| 1027 | + } |
| 1028 | + |
998 | 1029 | if change { |
999 | 1030 | op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Patch(project, zone, d.Get("name").(string), updatedManager).Do() |
1000 | 1031 | if err != nil { |
@@ -1261,6 +1292,20 @@ func expandVersions(configured []interface{}) []*compute.InstanceGroupManagerVer |
1261 | 1292 | return versions |
1262 | 1293 | } |
1263 | 1294 |
|
| 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 | + |
1264 | 1309 | func expandFixedOrPercent(configured []interface{}) *compute.FixedOrPercent { |
1265 | 1310 | fixedOrPercent := &compute.FixedOrPercent{} |
1266 | 1311 |
|
@@ -1579,6 +1624,17 @@ func flattenStatusAllInstancesConfig(allInstancesConfig *compute.InstanceGroupMa |
1579 | 1624 | return results |
1580 | 1625 | } |
1581 | 1626 |
|
| 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 | + |
1582 | 1638 | func resourceInstanceGroupManagerStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
1583 | 1639 | if err := d.Set("wait_for_instances", false); err != nil { |
1584 | 1640 | return nil, fmt.Errorf("Error setting wait_for_instances: %s", err) |
|
0 commit comments