Skip to content

Commit 0888085

Browse files
committed
Model playlist items as nested blocks
1 parent b6460b5 commit 0888085

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

docs/resources/playlist.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,33 @@ resource "grafana_playlist" "test" {
4040
### Required
4141

4242
- `interval` (String)
43-
- `item` (Set of Object) (see [below for nested schema](#nestedatt--item))
4443
- `name` (String) The name of the playlist.
4544

4645
### Optional
4746

47+
- `item` (Block Set) (see [below for nested schema](#nestedblock--item))
4848
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
4949

5050
### Read-Only
5151

5252
- `id` (String) The ID of this resource.
5353

54-
<a id="nestedatt--item"></a>
54+
<a id="nestedblock--item"></a>
5555
### Nested Schema for `item`
5656

5757
Required:
5858

59-
- `id` (String)
6059
- `order` (Number)
60+
61+
Optional:
62+
6163
- `type` (String)
6264
- `value` (String)
6365

66+
Read-Only:
67+
68+
- `id` (String)
69+
6470
## Import
6571

6672
Import is supported using the following syntax:

internal/resources/grafana/resource_playlist.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"github.com/grafana/grafana-openapi-client-go/client/playlists"
1111
"github.com/grafana/grafana-openapi-client-go/models"
1212
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
13+
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1314
"github.com/hashicorp/terraform-plugin-framework/attr"
1415
"github.com/hashicorp/terraform-plugin-framework/diag"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
20+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1921
"github.com/hashicorp/terraform-plugin-framework/types"
2022
)
2123

@@ -117,9 +119,28 @@ func (r *playlistResource) Schema(ctx context.Context, req resource.SchemaReques
117119
"interval": schema.StringAttribute{
118120
Required: true,
119121
},
120-
"item": schema.SetAttribute{
121-
Required: true,
122-
ElementType: types.ObjectType{AttrTypes: playlistItemAttrTypes},
122+
},
123+
Blocks: map[string]schema.Block{
124+
"item": schema.SetNestedBlock{
125+
Validators: []validator.Set{
126+
setvalidator.SizeAtLeast(1),
127+
},
128+
NestedObject: schema.NestedBlockObject{
129+
Attributes: map[string]schema.Attribute{
130+
"id": schema.StringAttribute{
131+
Computed: true,
132+
},
133+
"order": schema.Int64Attribute{
134+
Required: true,
135+
},
136+
"type": schema.StringAttribute{
137+
Optional: true,
138+
},
139+
"value": schema.StringAttribute{
140+
Optional: true,
141+
},
142+
},
143+
},
123144
},
124145
},
125146
}
@@ -376,8 +397,12 @@ func flattenPlaylistItemsToSet(ctx context.Context, items []*models.PlaylistItem
376397
if order == 0 {
377398
order = int64(i + 1)
378399
}
400+
idAttr := types.StringNull()
401+
if item.ID != 0 {
402+
idAttr = types.StringValue(strconv.FormatInt(item.ID, 10))
403+
}
379404
obj, objDiags := types.ObjectValue(playlistItemAttrTypes, map[string]attr.Value{
380-
"id": types.StringNull(),
405+
"id": idAttr,
381406
"order": types.Int64Value(order),
382407
"type": types.StringValue(item.Type),
383408
"value": types.StringValue(item.Value),

0 commit comments

Comments
 (0)