Skip to content

Commit

Permalink
feature: startusing a single put call for teams list update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rex Scaria committed Dec 9, 2024
1 parent 1c39e97 commit ffb1990
Showing 1 changed file with 17 additions and 80 deletions.
97 changes: 17 additions & 80 deletions internal/sdkv2provider/resource_cloudflare_teams_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ffb1990

Please sign in to comment.