From ffb199086ed78a5bb89d3a40ab024e5ac8def787 Mon Sep 17 00:00:00 2001 From: Rex Scaria Date: Mon, 9 Dec 2024 14:42:27 -0500 Subject: [PATCH] feature: startusing a single put call for teams list update --- .../resource_cloudflare_teams_list.go | 97 ++++--------------- 1 file changed, 17 insertions(+), 80 deletions(-) diff --git a/internal/sdkv2provider/resource_cloudflare_teams_list.go b/internal/sdkv2provider/resource_cloudflare_teams_list.go index 14a1447b3f7..3138368dbc6 100644 --- a/internal/sdkv2provider/resource_cloudflare_teams_list.go +++ b/internal/sdkv2provider/resource_cloudflare_teams_list.go @@ -64,8 +64,8 @@ func resourceCloudflareTeamsListCreate(ctx context.Context, d *schema.ResourceDa itemsWithoutDescription := d.Get("items").(*schema.Set).List() itemsWithDescriptionValues := d.Get("items_with_description").(*schema.Set).List() - allItems := append([]interface{}{}, itemsWithoutDescription...) - allItems = append(allItems, itemsWithDescriptionValues...) + allItems := append([]interface{}{}, itemsWithDescriptionValues...) + allItems = append(allItems, itemsWithoutDescription...) for _, v := range allItems { item, err := convertItemCFTeamsListItems(v) if err != nil { @@ -135,12 +135,25 @@ func resourceCloudflareTeamsListUpdate(ctx context.Context, d *schema.ResourceDa Name: d.Get("name").(string), Type: d.Get("type").(string), Description: d.Get("description").(string), + Items: []cloudflare.TeamsListItem{}, } - tflog.Debug(ctx, fmt.Sprintf("Updating Cloudflare Teams List from struct: %+v", updatedTeamsList)) - accountID := d.Get(consts.AccountIDSchemaKey).(string) + itemsWithDescriptionValues := d.Get("items_with_description").(*schema.Set).List() + itemsWithoutDescription := d.Get("items").(*schema.Set).List() + allItems := append([]interface{}{}, itemsWithDescriptionValues...) + allItems = append(allItems, itemsWithoutDescription...) + for _, v := range allItems { + item, err := convertItemCFTeamsListItems(v) + if err != nil { + return diag.FromErr(fmt.Errorf("error creating Teams List for account %q: %w", accountID, err)) + } + updatedTeamsList.Items = append(updatedTeamsList.Items, *item) + } + + tflog.Debug(ctx, fmt.Sprintf("Updating Cloudflare Teams List from struct: %+v", updatedTeamsList)) + identifier := cloudflare.AccountIdentifier(accountID) teamsList, err := client.UpdateTeamsList(ctx, identifier, updatedTeamsList) if err != nil { @@ -150,62 +163,6 @@ func resourceCloudflareTeamsListUpdate(ctx context.Context, d *schema.ResourceDa return diag.FromErr(fmt.Errorf("failed to find Teams List ID in update response; resource was empty")) } - if d.HasChanges("items", "items_with_description") { - oldItemsIface, newItemsIface := d.GetChange("items") - oldItemsWithDescriptionIface, newItemsWithDescriptionIface := d.GetChange("items_with_description") - - oldItems := oldItemsIface.(*schema.Set).List() - newItems := newItemsIface.(*schema.Set).List() - oldItemsWithDescription := oldItemsWithDescriptionIface.(*schema.Set).List() - newItemsWithDescription := newItemsWithDescriptionIface.(*schema.Set).List() - - convertedOldItems := []cloudflare.TeamsListItem{} - convertedNewItems := []cloudflare.TeamsListItem{} - - for _, v := range oldItems { - item, err := convertItemCFTeamsListItems(v) - if err != nil { - return diag.FromErr(fmt.Errorf("error creating Teams List for account %q: %w", accountID, err)) - } - convertedOldItems = append(convertedOldItems, *item) - } - - for _, v := range oldItemsWithDescription { - item, err := convertItemCFTeamsListItems(v) - if err != nil { - return diag.FromErr(fmt.Errorf("error creating Teams List for account %q: %w", accountID, err)) - } - convertedOldItems = append(convertedOldItems, *item) - } - - for _, v := range newItems { - item, err := convertItemCFTeamsListItems(v) - if err != nil { - return diag.FromErr(fmt.Errorf("error creating Teams List for account %q: %w", accountID, err)) - } - convertedNewItems = append(convertedNewItems, *item) - } - - for _, v := range newItemsWithDescription { - item, err := convertItemCFTeamsListItems(v) - if err != nil { - return diag.FromErr(fmt.Errorf("error creating Teams List for account %q: %w", accountID, err)) - } - convertedNewItems = append(convertedNewItems, *item) - } - - patchTeamsList := cloudflare.PatchTeamsListParams{ID: d.Id()} - setListItemDiff(&patchTeamsList, convertedOldItems, convertedNewItems) - - l, err := client.PatchTeamsList(ctx, identifier, patchTeamsList) - - if err != nil { - return diag.FromErr(fmt.Errorf("error updating Teams List for account %q: %w", accountID, err)) - } - - teamsList.Items = l.Items - } - return resourceCloudflareTeamsListRead(ctx, d, meta) } @@ -246,26 +203,6 @@ func resourceCloudflareTeamsListImport(ctx context.Context, d *schema.ResourceDa return []*schema.ResourceData{d}, nil } -func setListItemDiff(patchList *cloudflare.PatchTeamsListParams, oldItems, newItems []cloudflare.TeamsListItem) { - counts := make(map[cloudflare.TeamsListItem]int) - - for _, item := range newItems { - counts[item] += 1 - } - for _, item := range oldItems { - counts[item] -= 1 - } - - for item, val := range counts { - if val > 0 { - patchList.Append = append(patchList.Append, item) - } - if val < 0 { - patchList.Remove = append(patchList.Remove, item.Value) - } - } -} - func convertItemCFTeamsListItems(item any) (*cloudflare.TeamsListItem, error) { switch item.(type) { case string: