|
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": { |
@@ -585,11 +587,31 @@ func ResourceComputeInstanceGroupManager() *schema.Resource { |
585 | 587 | }, |
586 | 588 | }, |
587 | 589 | }, |
| 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 | + }, |
588 | 606 | }, |
589 | 607 | UseJSONNumber: true, |
590 | 608 | } |
591 | 609 | } |
592 | 610 |
|
| 611 | +func ForceNewResourcePoliciesWorkloadPolicyIfNewIsEmpty(_ context.Context, old, new, _ interface{}) bool { |
| 612 | + return (old.(string) != "") && (new.(string) == "") |
| 613 | +} |
| 614 | + |
593 | 615 | func parseUniqueId(s string) (string, string) { |
594 | 616 | splits := strings.SplitN(s, "?uniqueId=", 2) |
595 | 617 | if len(splits) == 2 { |
@@ -678,6 +700,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte |
678 | 700 | InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})), |
679 | 701 | AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})), |
680 | 702 | StatefulPolicy: expandStatefulPolicy(d), |
| 703 | + ResourcePolicies: expandResourcePolicies(d.Get("resource_policies").([]interface{})), |
681 | 704 | Params: expandInstanceGroupManagerParams(d), |
682 | 705 |
|
683 | 706 | // Force send TargetSize to allow a value of 0. |
@@ -923,6 +946,9 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf |
923 | 946 | if err = d.Set("status", flattenStatus(manager.Status)); err != nil { |
924 | 947 | return fmt.Errorf("Error setting status in state: %s", err.Error()) |
925 | 948 | } |
| 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 | + } |
926 | 952 |
|
927 | 953 | // If unset in state set to default value |
928 | 954 | if d.Get("wait_for_instances_status").(string) == "" { |
@@ -1027,6 +1053,11 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte |
1027 | 1053 | change = true |
1028 | 1054 | } |
1029 | 1055 |
|
| 1056 | + if d.HasChange("resource_policies") { |
| 1057 | + updatedManager.ResourcePolicies = expandResourcePolicies(d.Get("resource_policies").([]interface{})) |
| 1058 | + change = true |
| 1059 | + } |
| 1060 | + |
1030 | 1061 | if change { |
1031 | 1062 | op, err := config.NewComputeClient(userAgent).InstanceGroupManagers.Patch(project, zone, d.Get("name").(string), updatedManager).Do() |
1032 | 1063 | if err != nil { |
@@ -1293,6 +1324,20 @@ func expandVersions(configured []interface{}) []*compute.InstanceGroupManagerVer |
1293 | 1324 | return versions |
1294 | 1325 | } |
1295 | 1326 |
|
| 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 | + |
1296 | 1341 | func expandFixedOrPercent(configured []interface{}) *compute.FixedOrPercent { |
1297 | 1342 | fixedOrPercent := &compute.FixedOrPercent{} |
1298 | 1343 |
|
@@ -1624,6 +1669,17 @@ func flattenStatusAllInstancesConfig(allInstancesConfig *compute.InstanceGroupMa |
1624 | 1669 | return results |
1625 | 1670 | } |
1626 | 1671 |
|
| 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 | + |
1627 | 1683 | func resourceInstanceGroupManagerStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
1628 | 1684 | if err := d.Set("wait_for_instances", false); err != nil { |
1629 | 1685 | return nil, fmt.Errorf("Error setting wait_for_instances: %s", err) |
|
0 commit comments