generated from hashicorp/terraform-provider-scaffolding-framework
/
root_folders_data_source.go
127 lines (113 loc) · 4.12 KB
/
root_folders_data_source.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package provider
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"golift.io/starr/sonarr"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ provider.DataSourceType = dataRootFoldersType{}
var _ datasource.DataSource = dataRootFolders{}
type dataRootFoldersType struct{}
type dataRootFolders struct {
provider sonarrProvider
}
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
// QualityProfiles is a list of QualityProfile.
type RootFolders struct {
ID types.String `tfsdk:"id"`
RootFolders types.Set `tfsdk:"root_folders"`
}
func (t dataRootFoldersType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the delay server.
MarkdownDescription: "List all available rootfolders",
Attributes: map[string]tfsdk.Attribute{
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
"id": {
Computed: true,
Type: types.StringType,
},
"root_folders": {
MarkdownDescription: "List of rootfolders",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"path": {
MarkdownDescription: "Absolute path of rootFolder",
Computed: true,
Type: types.StringType,
},
"accessible": {
MarkdownDescription: "Access flag",
Computed: true,
Type: types.BoolType,
},
"id": {
MarkdownDescription: "RootFolder ID",
Computed: true,
Type: types.Int64Type,
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.UseStateForUnknown(),
},
},
"unmapped_folders": {
MarkdownDescription: "List of folders with no associated series",
Computed: true,
Attributes: tfsdk.SetNestedAttributes(map[string]tfsdk.Attribute{
"path": {
MarkdownDescription: "Path of unmapped folder",
Computed: true,
Type: types.StringType,
},
"name": {
MarkdownDescription: "Name of unmapped folder",
Computed: true,
Type: types.StringType,
},
}),
},
}),
},
},
}, nil
}
func (t dataRootFoldersType) NewDataSource(ctx context.Context, in provider.Provider) (datasource.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return dataRootFolders{
provider: provider,
}, diags
}
func (d dataRootFolders) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data RootFolders
diags := resp.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get rootfolders current value
response, err := d.provider.client.GetRootFoldersContext(ctx)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read rootfolders, got error: %s", err))
return
}
// Map response body to resource schema attribute
rootFolders := *writeRootFolders(ctx, response)
tfsdk.ValueFrom(ctx, rootFolders, data.RootFolders.Type(context.Background()), &data.RootFolders)
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
data.ID = types.String{Value: strconv.Itoa(len(response))}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func writeRootFolders(ctx context.Context, folders []*sonarr.RootFolder) *[]RootFolder {
output := make([]RootFolder, len(folders))
for i, f := range folders {
output[i] = *writeRootFolder(ctx, f)
}
return &output
}