/
chargepoints_data_source.go
215 lines (194 loc) · 7.08 KB
/
chargepoints_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &ChargepointsDataSource{}
_ datasource.DataSourceWithConfigure = &ChargepointsDataSource{}
)
// ChargepointsDataSource is the data source implementation.
type ChargepointsDataSource struct {
client *Client
}
type ChargepointsDataSourceModel struct {
Chargepoints []ChargepointDataSourceModel `tfsdk:"chargepoints"`
}
type ChargepointDataSourceModel struct {
ID types.String `tfsdk:"id"`
ChargepointID types.String `tfsdk:"chargepoint_id"`
DateDeleted types.String `tfsdk:"date_deleted"`
DisplayName types.String `tfsdk:"display_name"`
RoamingName types.String `tfsdk:"roaming_name"`
ChargeBoxSerialNumber types.String `tfsdk:"charge_box_serial_number"`
ChargepointVendor types.String `tfsdk:"chargepoint_vendor"`
Evses []EvseDataSourceModel `tfsdk:"evses"`
}
type EvseDataSourceModel struct {
EvseID types.String `tfsdk:"evse_id"`
Connectors []ConnectorDataSourceModel `tfsdk:"connectors"`
}
type ConnectorDataSourceModel struct {
ID types.String `tfsdk:"id"`
OperationalStatus types.String `tfsdk:"operational_status"`
Standard types.String `tfsdk:"standard"`
Format types.String `tfsdk:"format"`
PowerType types.String `tfsdk:"power_type"`
MaxVoltage types.Int64 `tfsdk:"max_voltage"`
MaxAmperage types.Int64 `tfsdk:"max_amperage"`
MaxElectricalPower types.Int64 `tfsdk:"max_electrical_power"`
}
// NewChargepointsDataSource is a helper function to simplify the provider implementation.
func NewChargepointsDataSource() datasource.DataSource {
return &ChargepointsDataSource{}
}
// Configure adds the provider configured client to the data source.
func (d *ChargepointsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
// Metadata returns the data source type name.
func (d *ChargepointsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_chargepoints"
}
func (d *ChargepointsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"chargepoints": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"chargepoint_id": schema.StringAttribute{
Computed: true,
},
"date_deleted": schema.StringAttribute{
Computed: true,
},
"display_name": schema.StringAttribute{
Computed: true,
},
"roaming_name": schema.StringAttribute{
Computed: true,
},
"charge_box_serial_number": schema.StringAttribute{
Computed: true,
},
"chargepoint_vendor": schema.StringAttribute{
Computed: true,
},
"evses": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"evse_id": schema.StringAttribute{
Computed: true,
},
"connectors": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"operational_status": schema.StringAttribute{
Computed: true,
},
"standard": schema.StringAttribute{
Computed: true,
},
"format": schema.StringAttribute{
Computed: true,
},
"power_type": schema.StringAttribute{
Computed: true,
},
"max_voltage": schema.Int64Attribute{
Computed: true,
},
"max_amperage": schema.Int64Attribute{
Computed: true,
},
"max_electrical_power": schema.Int64Attribute{
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *ChargepointsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ChargepointsDataSourceModel
chargepoints, err := d.client.GetChargepoints()
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Longship Webhooks",
err.Error(),
)
return
}
for _, chargepoint := range chargepoints {
chargepointState := ChargepointDataSourceModel{
ID: types.StringValue(chargepoint.ID),
ChargepointID: types.StringValue(chargepoint.ChargepointID),
DateDeleted: types.StringValue(chargepoint.DateDeleted),
DisplayName: types.StringValue(chargepoint.DisplayName),
RoamingName: types.StringValue(chargepoint.RoamingName),
ChargeBoxSerialNumber: types.StringValue(chargepoint.ChargeBoxSerialNumber),
ChargepointVendor: types.StringValue(chargepoint.ChargepointVendor),
}
evsesState := []EvseDataSourceModel{}
for _, evse := range chargepoint.Evses {
connectorsState := []ConnectorDataSourceModel{}
for _, connector := range evse.Connectors {
connectorState := ConnectorDataSourceModel{
ID: types.StringValue(connector.ID),
OperationalStatus: types.StringValue(connector.OperationalStatus),
Standard: types.StringValue(connector.Standard),
Format: types.StringValue(connector.Format),
PowerType: types.StringValue(connector.PowerType),
MaxVoltage: types.Int64Value(connector.MaxVoltage),
MaxAmperage: types.Int64Value(connector.MaxAmperage),
MaxElectricalPower: types.Int64Value(connector.MaxElectricalPower),
}
connectorsState = append(connectorsState, connectorState)
}
evsesState = append(evsesState, EvseDataSourceModel{
EvseID: types.StringValue(evse.EvseID),
Connectors: connectorsState,
})
}
chargepointState.Evses = evsesState
state.Chargepoints = append(state.Chargepoints, chargepointState)
}
// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}