forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_installed_apps_data_source.go
More file actions
235 lines (207 loc) · 7.21 KB
/
device_installed_apps_data_source.go
File metadata and controls
235 lines (207 loc) · 7.21 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package provider
import (
"context"
"fmt"
"regexp"
"github.com/DavidKrau/simplemdm-go-client"
"github.com/DavidKrau/terraform-provider-simplemdm/internal/simplemdmext"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &deviceInstalledAppsDataSource{}
_ datasource.DataSourceWithConfigure = &deviceInstalledAppsDataSource{}
)
type deviceInstalledAppsDataSource struct {
client *simplemdm.Client
}
type deviceInstalledAppsDataSourceModel struct {
DeviceID types.String `tfsdk:"device_id"`
InstalledApps []deviceInstalledAppDataSourceModel `tfsdk:"installed_apps"`
}
type deviceInstalledAppDataSourceModel struct {
ID types.String `tfsdk:"id"`
Type types.String `tfsdk:"type"`
Name types.String `tfsdk:"name"`
Identifier types.String `tfsdk:"identifier"`
Version types.String `tfsdk:"version"`
ShortVersion types.String `tfsdk:"short_version"`
BundleSize types.Int64 `tfsdk:"bundle_size"`
DynamicSize types.Int64 `tfsdk:"dynamic_size"`
Managed types.Bool `tfsdk:"managed"`
DiscoveredAt types.String `tfsdk:"discovered_at"`
LastSeenAt types.String `tfsdk:"last_seen_at"`
}
func DeviceInstalledAppsDataSource() datasource.DataSource {
return &deviceInstalledAppsDataSource{}
}
func (d *deviceInstalledAppsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_device_installed_apps"
}
func (d *deviceInstalledAppsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieves installed applications for a device.",
Attributes: map[string]schema.Attribute{
"device_id": schema.StringAttribute{
Required: true,
Description: "Identifier of the device.",
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^\d+$`),
"device_id must be a numeric string",
),
},
},
},
Blocks: map[string]schema.Block{
"installed_apps": schema.ListNestedBlock{
Description: "Collection of installed applications on the device.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Installed app identifier.",
},
"type": schema.StringAttribute{
Computed: true,
Description: "Installed app resource type.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Application name.",
},
"identifier": schema.StringAttribute{
Computed: true,
Description: "Application bundle identifier.",
},
"version": schema.StringAttribute{
Computed: true,
Description: "Application version.",
},
"short_version": schema.StringAttribute{
Computed: true,
Description: "Application short version string.",
},
"bundle_size": schema.Int64Attribute{
Computed: true,
Description: "Size of the application bundle in bytes.",
},
"dynamic_size": schema.Int64Attribute{
Computed: true,
Description: "Dynamic size of the application in bytes.",
},
"managed": schema.BoolAttribute{
Computed: true,
Description: "Whether the application is managed by SimpleMDM.",
},
"discovered_at": schema.StringAttribute{
Computed: true,
Description: "Timestamp when the application was first discovered.",
},
"last_seen_at": schema.StringAttribute{
Computed: true,
Description: "Timestamp when the application was last seen.",
},
},
},
},
},
}
}
func (d *deviceInstalledAppsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state deviceInstalledAppsDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
apps, err := simplemdmext.ListDeviceInstalledApps(ctx, d.client, state.DeviceID.ValueString())
if err != nil {
if isNotFoundError(err) {
resp.Diagnostics.AddError(
"Device not found",
fmt.Sprintf("The device with ID %s was not found. It may have been deleted.", state.DeviceID.ValueString()),
)
} else {
resp.Diagnostics.AddError(
"Unable to list installed apps",
fmt.Sprintf("Failed to retrieve installed apps for device %s: %s", state.DeviceID.ValueString(), err.Error()),
)
}
return
}
items := make([]deviceInstalledAppDataSourceModel, 0, len(apps.Data))
for _, item := range apps.Data {
app := deviceInstalledAppDataSourceModel{
ID: types.StringValue(item.ID.String()),
Type: types.StringValue(item.Type),
}
// Parse attributes from the map
if name, ok := item.Attributes["name"].(string); ok {
app.Name = types.StringValue(name)
} else {
app.Name = types.StringNull()
}
if identifier, ok := item.Attributes["identifier"].(string); ok {
app.Identifier = types.StringValue(identifier)
} else {
app.Identifier = types.StringNull()
}
if version, ok := item.Attributes["version"].(string); ok {
app.Version = types.StringValue(version)
} else {
app.Version = types.StringNull()
}
if shortVersion, ok := item.Attributes["short_version"].(string); ok {
app.ShortVersion = types.StringValue(shortVersion)
} else {
app.ShortVersion = types.StringNull()
}
if bundleSize, ok := item.Attributes["bundle_size"].(float64); ok {
app.BundleSize = types.Int64Value(int64(bundleSize))
} else {
app.BundleSize = types.Int64Null()
}
if dynamicSize, ok := item.Attributes["dynamic_size"].(float64); ok {
app.DynamicSize = types.Int64Value(int64(dynamicSize))
} else {
app.DynamicSize = types.Int64Null()
}
if managed, ok := item.Attributes["managed"].(bool); ok {
app.Managed = types.BoolValue(managed)
} else {
app.Managed = types.BoolNull()
}
if discoveredAt, ok := item.Attributes["discovered_at"].(string); ok {
app.DiscoveredAt = types.StringValue(discoveredAt)
} else {
app.DiscoveredAt = types.StringNull()
}
if lastSeenAt, ok := item.Attributes["last_seen_at"].(string); ok {
app.LastSeenAt = types.StringValue(lastSeenAt)
} else {
app.LastSeenAt = types.StringNull()
}
items = append(items, app)
}
state.InstalledApps = items
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
func (d *deviceInstalledAppsDataSource) 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
}