-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_rack_types.go
64 lines (53 loc) · 1.92 KB
/
data_source_rack_types.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
package tfapstra
import (
"context"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSourceWithConfigure = &dataSourceRackTypes{}
var _ datasourceWithSetClient = &dataSourceRackTypes{}
type dataSourceRackTypes struct {
client *apstra.Client
}
func (o *dataSourceRackTypes) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_rack_types"
}
func (o *dataSourceRackTypes) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
configureDataSource(ctx, o, req, resp)
}
func (o *dataSourceRackTypes) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryDesign + "This data source returns the ID numbers of all Rack Types.",
Attributes: map[string]schema.Attribute{
"ids": schema.SetAttribute{
MarkdownDescription: "A set of Apstra object ID numbers.",
Computed: true,
ElementType: types.StringType,
},
},
}
}
func (o *dataSourceRackTypes) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
ids, err := o.client.ListRackTypeIds(ctx)
if err != nil {
resp.Diagnostics.AddError("Error retrieving Rack Type IDs", err.Error())
return
}
idSet, diags := types.SetValueFrom(ctx, types.StringType, ids)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// create new state object
var state struct {
Ids types.Set `tfsdk:"ids"`
}
state.Ids = idSet
// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (o *dataSourceRackTypes) setClient(client *apstra.Client) {
o.client = client
}