generated from hashicorp/terraform-provider-scaffolding-framework
/
delay_profiles_data_source.go
131 lines (115 loc) · 4.6 KB
/
delay_profiles_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
128
129
130
131
package provider
import (
"context"
"strconv"
"github.com/devopsarr/terraform-provider-whisparr/internal/helpers"
"github.com/devopsarr/whisparr-go/whisparr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
const delayProfilesDataSourceName = "delay_profiles"
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &DelayProfilesDataSource{}
func NewDelayProfilesDataSource() datasource.DataSource {
return &DelayProfilesDataSource{}
}
// DelayProfilesDataSource defines the delay profiles implementation.
type DelayProfilesDataSource struct {
client *whisparr.APIClient
}
// DelayProfiles describes the delay profiles data model.
type DelayProfiles struct {
DelayProfiles types.Set `tfsdk:"delay_profiles"`
ID types.String `tfsdk:"id"`
}
func (d *DelayProfilesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + delayProfilesDataSourceName
}
func (d *DelayProfilesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the delay server.
MarkdownDescription: "<!-- subcategory:Profiles -->List all available [Delay Profiles](../resources/delay_profile).",
Attributes: map[string]schema.Attribute{
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
"id": schema.StringAttribute{
Computed: true,
},
"delay_profiles": schema.SetNestedAttribute{
MarkdownDescription: "Delay Profile list.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "Delay Profile ID.",
Computed: true,
},
"enable_usenet": schema.BoolAttribute{
MarkdownDescription: "Usenet allowed Flag.",
Computed: true,
},
"enable_torrent": schema.BoolAttribute{
MarkdownDescription: "Torrent allowed Flag.",
Computed: true,
},
"bypass_if_highest_quality": schema.BoolAttribute{
MarkdownDescription: "Bypass for highest quality Flag.",
Computed: true,
},
"usenet_delay": schema.Int64Attribute{
MarkdownDescription: "Usenet delay.",
Computed: true,
},
"torrent_delay": schema.Int64Attribute{
MarkdownDescription: "Torrent Delay.",
Computed: true,
},
"order": schema.Int64Attribute{
MarkdownDescription: "Order.",
Computed: true,
},
"tags": schema.SetAttribute{
MarkdownDescription: "List of associated tags.",
Computed: true,
ElementType: types.Int64Type,
},
"preferred_protocol": schema.StringAttribute{
MarkdownDescription: "Preferred protocol.",
Computed: true,
},
},
},
},
},
}
}
func (d *DelayProfilesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if client := helpers.DataSourceConfigure(ctx, req, resp); client != nil {
d.client = client
}
}
func (d *DelayProfilesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data *DelayProfiles
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Get delayprofiles current value
response, _, err := d.client.DelayProfileApi.ListDelayProfile(ctx).Execute()
if err != nil {
resp.Diagnostics.AddError(helpers.ClientError, helpers.ParseClientError(helpers.List, delayProfileResourceName, err))
return
}
tflog.Trace(ctx, "read "+delayProfileResourceName)
// Map response body to resource schema attribute
profiles := make([]DelayProfile, len(response))
for i, p := range response {
profiles[i].write(ctx, p)
}
tfsdk.ValueFrom(ctx, profiles, data.DelayProfiles.Type(ctx), &data.DelayProfiles)
// TODO: remove ID once framework support tests without ID https://www.terraform.io/plugin/framework/acctests#implement-id-attribute
data.ID = types.StringValue(strconv.Itoa(len(response)))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}