/
resource_library_albums.go
102 lines (82 loc) · 2.87 KB
/
resource_library_albums.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package spotify
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zmb3/spotify/v2"
)
func resourceLibraryAlbums() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLibraryAlbumsCreate,
ReadContext: resourceLibraryAlbumsRead,
UpdateContext: resourceLibraryAlbumsUpdate,
DeleteContext: resourceLibraryAlbumsDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"albums": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "The list of track IDs to save to your 'liked albums'. *Note, if used incorrectly you may unlike all of your albums - use with caution*",
},
},
}
}
func resourceLibraryAlbumsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*spotify.Client)
trackIDs := spotifyIdsInterface(d.Get("albums").(*schema.Set).List())
for _, rng := range batches(len(trackIDs), 100) {
if err := client.AddAlbumsToLibrary(ctx, trackIDs[rng.Start:rng.End]...); err != nil {
return diag.Errorf("AddAlbumsToLibrary: %s", err.Error())
}
}
d.SetId("library")
return nil
}
func resourceLibraryAlbumsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*spotify.Client)
trackIDs := schema.NewSet(schema.HashString, nil)
Albums, err := client.CurrentUsersAlbums(ctx)
if err != nil {
return diag.Errorf("CurrentUsersAlbums: %s", err.Error())
}
for err == nil {
for _, track := range Albums.Albums {
trackIDs.Add(string(track.ID))
}
err = client.NextPage(ctx, Albums)
}
if err := d.Set("albums", trackIDs); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceLibraryAlbumsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*spotify.Client)
if d.HasChange("albums") {
old, new := d.GetChange("albums")
oldSet := old.(*schema.Set)
newSet := new.(*schema.Set)
add := newSet.Difference(oldSet).List()
sub := oldSet.Difference(newSet).List()
addTrackIDs := spotifyIdsInterface(add)
subTrackIDs := spotifyIdsInterface(sub)
for _, rng := range batches(len(add), 100) {
if err := client.AddAlbumsToLibrary(ctx, addTrackIDs[rng.Start:rng.End]...); err != nil {
return diag.Errorf("AddAlbumsToLibrary: %s", err.Error())
}
}
for _, rng := range batches(len(sub), 100) {
if err := client.RemoveAlbumsFromLibrary(ctx, subTrackIDs[rng.Start:rng.End]...); err != nil {
return diag.Errorf("AddAlbumsToLibrary: %s", err.Error())
}
}
}
return nil
}
func resourceLibraryAlbumsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}