generated from hashicorp/terraform-provider-scaffolding-framework
/
home_data_source.go
173 lines (152 loc) · 5.74 KB
/
home_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
package provider
import (
"context"
"fmt"
"github.com/gonzolino/gotado/v2"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &HomeDataSource{}
func NewHomeDataSource() datasource.DataSource {
return &HomeDataSource{}
}
type HomeDataSource struct {
client *gotado.Tado
username string
password string
}
type HomeDataSourceModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
TemperatureUnit types.String `tfsdk:"temperature_unit"`
ContactName types.String `tfsdk:"contact_name"`
ContactEmail types.String `tfsdk:"contact_email"`
ContactPhone types.String `tfsdk:"contact_phone"`
AddressLine1 types.String `tfsdk:"address_line1"`
AddressLine2 types.String `tfsdk:"address_line2"`
AddressZipcode types.String `tfsdk:"address_zipcode"`
AddressCity types.String `tfsdk:"address_city"`
AddressState types.String `tfsdk:"address_state"`
AddressCountry types.String `tfsdk:"address_country"`
GeolocationLat types.Float64 `tfsdk:"geolocation_lat"`
GeolocationLong types.Float64 `tfsdk:"geolocation_long"`
}
func (*HomeDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_home"
}
func (HomeDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "A tado home holds all tado devices and heating zones. The home data source provides information such as contact details, address, etc.",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "Home ID.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the home.",
Required: true,
},
"temperature_unit": schema.StringAttribute{
MarkdownDescription: "Temperature unit used in the home. Either 'Celsius' or 'Fahrenheit'.",
Computed: true,
},
"contact_name": schema.StringAttribute{
MarkdownDescription: "Name of the contact person.",
Computed: true,
},
"contact_email": schema.StringAttribute{
MarkdownDescription: "Email address of the contact person.",
Computed: true,
},
"contact_phone": schema.StringAttribute{
MarkdownDescription: "Phone number of the contact person.",
Computed: true,
},
"address_line1": schema.StringAttribute{
MarkdownDescription: "Address line 1.",
Computed: true,
},
"address_line2": schema.StringAttribute{
MarkdownDescription: "Address line 2.",
Computed: true,
},
"address_zipcode": schema.StringAttribute{
MarkdownDescription: "Zip code.",
Computed: true,
},
"address_city": schema.StringAttribute{
MarkdownDescription: "City.",
Computed: true,
},
"address_state": schema.StringAttribute{
MarkdownDescription: "State.",
Computed: true,
},
"address_country": schema.StringAttribute{
MarkdownDescription: "Country.",
Computed: true,
},
"geolocation_lat": schema.Float64Attribute{
MarkdownDescription: "Latitude used for Geofencing.",
Computed: true,
},
"geolocation_long": schema.Float64Attribute{
MarkdownDescription: "Longitude used for Geofencing.",
Computed: true,
},
},
}
}
func (d *HomeDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*tadoProviderData)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *tadoProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = data.client
d.username = data.username
d.password = data.password
}
func (d HomeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data HomeDataSourceModel
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
me, err := d.client.Me(ctx, d.username, d.password)
if err != nil {
resp.Diagnostics.AddError("Tado Authentication Error", fmt.Sprintf("Unable to authenticate with Tado: %v", err))
return
}
home, err := me.GetHome(ctx, data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("Tado Authentication Error", fmt.Sprintf("Unable to authenticate with Tado: %v", err))
return
}
data.ID = types.Int64Value(int64(home.ID))
data.Name = types.StringValue(home.Name)
data.TemperatureUnit = types.StringValue(string(home.TemperatureUnit))
data.ContactName = toTypesString(home.ContactDetails.Name)
data.ContactEmail = toTypesString(home.ContactDetails.Email)
data.ContactPhone = toTypesString(home.ContactDetails.Phone)
data.AddressLine1 = toTypesString(home.Address.AddressLine1)
data.AddressLine2 = toTypesString(home.Address.AddressLine2)
data.AddressZipcode = toTypesString(home.Address.ZipCode)
data.AddressCity = toTypesString(home.Address.City)
data.AddressState = toTypesString(home.Address.State)
data.AddressCountry = toTypesString(home.Address.Country)
data.GeolocationLat = types.Float64Value(home.Geolocation.Latitude)
data.GeolocationLong = types.Float64Value(home.Geolocation.Longitude)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}