forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_data_source.go
More file actions
177 lines (159 loc) · 5.64 KB
/
device_data_source.go
File metadata and controls
177 lines (159 loc) · 5.64 KB
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
package provider
import (
"context"
"fmt"
"strconv"
"github.com/DavidKrau/simplemdm-go-client"
"github.com/DavidKrau/terraform-provider-simplemdm/internal/simplemdmext"
"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 = &deviceDataSource{}
_ datasource.DataSourceWithConfigure = &deviceDataSource{}
)
// deviceDataSourceModel maps the data source schema data.
type deviceDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
DeviceName types.String `tfsdk:"devicename"`
Status types.String `tfsdk:"status"`
DeviceGroup types.String `tfsdk:"devicegroup"`
EnrollmentURL types.String `tfsdk:"enrollmenturl"`
EnrolledAt types.String `tfsdk:"enrolled_at"`
Details types.Map `tfsdk:"details"`
}
// deviceDataSource is a helper function to simplify the provider implementation.
func DeviceDataSource() datasource.DataSource {
return &deviceDataSource{}
}
// deviceDataSource is the data source implementation.
type deviceDataSource struct {
client *simplemdm.Client
}
// Metadata returns the data source type name.
func (d *deviceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_device"
}
// Schema defines the schema for the data source.
func (d *deviceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Device data source can be used together Assignment Group(s) to assign device to these objects.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "The SimpleMDM name of the device.",
},
"id": schema.StringAttribute{
Required: true,
Description: "The ID of the device.",
},
"devicename": schema.StringAttribute{
Computed: true,
Description: "The hostname reported by the device.",
},
"status": schema.StringAttribute{
Computed: true,
Description: "Current enrollment status of the device (e.g., enrolled, awaiting_enrollment).",
},
"devicegroup": schema.StringAttribute{
Computed: true,
Description: "Device group identifier for the device.",
},
"enrollmenturl": schema.StringAttribute{
Computed: true,
Description: "Enrollment URL generated for the device, when available.",
},
"enrolled_at": schema.StringAttribute{
Computed: true,
Description: "Timestamp when the device was enrolled.",
},
"details": schema.MapAttribute{
Computed: true,
ElementType: types.StringType,
Description: "Full set of attributes returned by the SimpleMDM API for the device.",
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *deviceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state deviceDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
device, err := simplemdmext.GetDevice(ctx, d.client, state.ID.ValueString(), true)
if err != nil {
if isNotFoundError(err) {
resp.Diagnostics.AddError(
"SimpleMDM device not found",
fmt.Sprintf("The device with ID %s was not found. It may have been deleted.", state.ID.ValueString()),
)
} else {
resp.Diagnostics.AddError(
"Unable to Read SimpleMDM device",
err.Error(),
)
}
return
}
// Map response body to model
state.ID = types.StringValue(strconv.Itoa(device.Data.ID))
flatAttributes := simplemdmext.FlattenAttributes(device.Data.Attributes)
if name, ok := flatAttributes["name"]; ok && name != "" {
state.Name = types.StringValue(name)
}
if deviceName, ok := flatAttributes["device_name"]; ok && deviceName != "" {
state.DeviceName = types.StringValue(deviceName)
} else {
state.DeviceName = types.StringNull()
}
if status, ok := flatAttributes["status"]; ok && status != "" {
state.Status = types.StringValue(status)
} else {
state.Status = types.StringNull()
}
if enrollmentURL, ok := flatAttributes["enrollment_url"]; ok && enrollmentURL != "" {
state.EnrollmentURL = types.StringValue(enrollmentURL)
} else {
state.EnrollmentURL = types.StringNull()
}
if enrolledAt, ok := flatAttributes["enrolled_at"]; ok && enrolledAt != "" {
state.EnrolledAt = types.StringValue(enrolledAt)
} else {
state.EnrolledAt = types.StringNull()
}
if groupID := device.Data.Relationships.DeviceGroup.Data.ID; groupID != 0 {
state.DeviceGroup = types.StringValue(strconv.Itoa(groupID))
} else {
state.DeviceGroup = types.StringNull()
}
detailsValue, detailsDiags := types.MapValueFrom(ctx, types.StringType, flatAttributes)
resp.Diagnostics.Append(detailsDiags...)
if resp.Diagnostics.HasError() {
return
}
state.Details = detailsValue
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the data source.
func (d *deviceDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*simplemdm.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
}