forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_profiles_data_source.go
More file actions
180 lines (157 loc) · 5.58 KB
/
device_profiles_data_source.go
File metadata and controls
180 lines (157 loc) · 5.58 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
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 = &deviceProfilesDataSource{}
_ datasource.DataSourceWithConfigure = &deviceProfilesDataSource{}
)
type deviceProfilesDataSource struct {
client *simplemdm.Client
}
type deviceProfilesDataSourceModel struct {
DeviceID types.String `tfsdk:"device_id"`
Profiles []deviceProfileDataSourceModel `tfsdk:"profiles"`
}
type deviceProfileDataSourceModel struct {
ID types.String `tfsdk:"id"`
Type types.String `tfsdk:"type"`
Name types.String `tfsdk:"name"`
ProfileIdentifier types.String `tfsdk:"profile_identifier"`
UserScope types.Bool `tfsdk:"user_scope"`
AttributeSupport types.Bool `tfsdk:"attribute_support"`
}
func DeviceProfilesDataSource() datasource.DataSource {
return &deviceProfilesDataSource{}
}
func (d *deviceProfilesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_device_profiles"
}
func (d *deviceProfilesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieves the list of profiles directly assigned to a device. Note: Profiles assigned through groups are not included.",
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{
"profiles": schema.ListNestedBlock{
Description: "Collection of profiles applied directly to the device (excludes profiles assigned through groups).",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Profile identifier.",
},
"type": schema.StringAttribute{
Computed: true,
Description: "Profile resource type.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Profile name.",
},
"profile_identifier": schema.StringAttribute{
Computed: true,
Description: "Profile identifier string from the configuration profile.",
},
"user_scope": schema.BoolAttribute{
Computed: true,
Description: "Whether the profile is user-scoped.",
},
"attribute_support": schema.BoolAttribute{
Computed: true,
Description: "Whether the profile supports custom attributes.",
},
},
},
},
},
}
}
func (d *deviceProfilesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state deviceProfilesDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
profiles, err := simplemdmext.ListDeviceProfiles(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 device profiles",
fmt.Sprintf("Failed to retrieve profiles for device %s: %s", state.DeviceID.ValueString(), err.Error()),
)
}
return
}
items := make([]deviceProfileDataSourceModel, 0, len(profiles.Data))
for _, item := range profiles.Data {
profile := deviceProfileDataSourceModel{
ID: types.StringValue(item.ID.String()),
Type: types.StringValue(item.Type),
}
// Parse attributes from the map
if name, ok := item.Attributes["name"].(string); ok {
profile.Name = types.StringValue(name)
} else {
profile.Name = types.StringNull()
}
if profileIdentifier, ok := item.Attributes["profile_identifier"].(string); ok {
profile.ProfileIdentifier = types.StringValue(profileIdentifier)
} else {
profile.ProfileIdentifier = types.StringNull()
}
if userScope, ok := item.Attributes["user_scope"].(bool); ok {
profile.UserScope = types.BoolValue(userScope)
} else {
profile.UserScope = types.BoolNull()
}
if attributeSupport, ok := item.Attributes["attribute_support"].(bool); ok {
profile.AttributeSupport = types.BoolValue(attributeSupport)
} else {
profile.AttributeSupport = types.BoolNull()
}
items = append(items, profile)
}
state.Profiles = items
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
func (d *deviceProfilesDataSource) 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
}