Skip to content

Commit 084ac9c

Browse files
DavidKrauDavid Kraushuber
andauthored
Custom profiles and Profile for device groups (#17)
* added reading of profiles * small bug fix * go mod update * Update in readme * read me update --------- Co-authored-by: David Kraushuber <david.kraushuber@mytaxi.com>
1 parent 2862ae0 commit 084ac9c

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ provider to build your own SimpleMDM infrastructure.Provider's official document
2121

2222
- API current doesnt support Create and Delete for Device Groups
2323
- API currently doesnt support update of "name" attribute for Device Groups
24-
- Custom Profiles for Assignment group, Device group and Device can no be updated because of API limitation (they are compared only between plan and state from previous plan), aka adding profile via web will not be considered in next apply.
24+
- Custom Profiles/Profiles for Assignment group and Devices can no be updated because of API limitation (they are compared only between plan and state from previous plan), aka adding profile via web will not be considered in next apply.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/DavidKrau/terraform-provider-simplemdm
33
go 1.22.2
44

55
require (
6-
github.com/DavidKrau/simplemdm-go-client v0.1.6
6+
github.com/DavidKrau/simplemdm-go-client v0.1.7
77
github.com/hashicorp/terraform-plugin-framework v1.12.0
88
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
99
github.com/hashicorp/terraform-plugin-go v0.24.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/DavidKrau/simplemdm-go-client v0.1.5 h1:xFrPxO6LoCWX1wkHqD7KguvkQhUUr
1212
github.com/DavidKrau/simplemdm-go-client v0.1.5/go.mod h1:tIrL/8ckUf4o2nmW9KCK91uDO3477WV+MEP77QYD0R0=
1313
github.com/DavidKrau/simplemdm-go-client v0.1.6 h1:jT5gEyeLMn9oreC4Rw7yVDKoEtzvp7WzZsuUPViAZTc=
1414
github.com/DavidKrau/simplemdm-go-client v0.1.6/go.mod h1:tIrL/8ckUf4o2nmW9KCK91uDO3477WV+MEP77QYD0R0=
15+
github.com/DavidKrau/simplemdm-go-client v0.1.7 h1:fdkTMqkJ9eeoZMj4SiQS3vxEOPCLBQ9vi9b5PK51qh0=
16+
github.com/DavidKrau/simplemdm-go-client v0.1.7/go.mod h1:tIrL/8ckUf4o2nmW9KCK91uDO3477WV+MEP77QYD0R0=
1517
github.com/Kunde21/markdownfmt/v3 v3.1.0 h1:KiZu9LKs+wFFBQKhrZJrFZwtLnCCWJahL+S+E/3VnM0=
1618
github.com/Kunde21/markdownfmt/v3 v3.1.0/go.mod h1:tPXN1RTyOzJwhfHoon9wUr4HGYmWgVxSQN6VBJDkrVc=
1719
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=

provider/deviceGroup_resource.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"strconv"
56
"strings"
67

78
"github.com/DavidKrau/simplemdm-go-client"
@@ -108,7 +109,7 @@ func (r *deviceGroupResource) Create(ctx context.Context, req resource.CreateReq
108109
resp.Diagnostics.AddError(
109110
"Resource can not be created!",
110111
"Device groups currently do not support creation via API request, if you wish to create new group "+
111-
"go to website and create group there and use import. Name of the group also can not be managed via provider, "+
112+
"go to website and create group and use import. Name of the group also can not be managed via provider, "+
112113
"same as deletion of the group can not be done via terraform. This will be implemented properly once API will have correct endpoints.",
113114
)
114115
if resp.Diagnostics.HasError() {
@@ -208,6 +209,53 @@ func (r *deviceGroupResource) Read(ctx context.Context, req resource.ReadRequest
208209
// Overwrite items with refreshed state
209210
state.Name = types.StringValue(devicegroup.Data.Attributes.Name)
210211

212+
// Load all profiles in SimpleMDM
213+
profiles, err := r.client.ProfileGetAll()
214+
if err != nil {
215+
resp.Diagnostics.AddError(
216+
"Error Reading SimpleMDM profiles",
217+
"Could not read SimpleMDM profiles: "+err.Error(),
218+
)
219+
return
220+
}
221+
// //read all profiles and put them to slice
222+
profilesPresent := false
223+
profilesElements := []attr.Value{}
224+
customProfilesPresent := false
225+
customProfilesElements := []attr.Value{}
226+
227+
for _, profile := range profiles.Data { //<<edit here
228+
for _, group := range profile.Relationships.DeviceGroups.Data {
229+
if strconv.Itoa(group.ID) == state.ID.ValueString() {
230+
if profile.Type == "custom_configuration_profile" {
231+
customProfilesElements = append(customProfilesElements, types.StringValue(strconv.Itoa(profile.ID)))
232+
customProfilesPresent = true
233+
} else {
234+
profilesElements = append(profilesElements, types.StringValue(strconv.Itoa(profile.ID)))
235+
profilesPresent = true
236+
}
237+
}
238+
}
239+
240+
}
241+
242+
//if there are profile or custom profiles return them to state
243+
if profilesPresent {
244+
profilesSetValue, _ := types.SetValue(types.StringType, profilesElements)
245+
state.Profiles = profilesSetValue
246+
} else {
247+
profilesSetValue := types.SetNull(types.StringType)
248+
state.Profiles = profilesSetValue
249+
}
250+
251+
if customProfilesPresent {
252+
customProfilesSetValue, _ := types.SetValue(types.StringType, customProfilesElements)
253+
state.CustomProfiles = customProfilesSetValue
254+
} else {
255+
customProfilesSetValue := types.SetNull(types.StringType)
256+
state.CustomProfiles = customProfilesSetValue
257+
}
258+
211259
// Set refreshed state
212260
diags = resp.State.Set(ctx, &state)
213261
resp.Diagnostics.Append(diags...)
@@ -240,8 +288,7 @@ func (r *deviceGroupResource) Update(ctx context.Context, req resource.UpdateReq
240288
resp.Diagnostics.AddWarning(
241289
"Name can not be changed via terraform",
242290
"Device groups currently do not support change of the name via API request, in case you wish to change "+
243-
"name of the device group please do it via website. Profiles assigned to the group are currently also limited"+
244-
" as we are missing data from API which profiles are assigned to the group. This will be implemented later when API will provide data about profiles.",
291+
"name of the device group please do it via website.",
245292
)
246293

247294
//comparing planed attributes and their values to attributes in SimpleMDM
@@ -365,7 +412,7 @@ func (r *deviceGroupResource) Update(ctx context.Context, req resource.UpdateReq
365412

366413
//removing profiles
367414
for _, profileId := range customProfilesToRemove {
368-
err := r.client.CustomProfileAssignToDeviceGroup(profileId, plan.ID.ValueString())
415+
err := r.client.CustomProfileUnassignFromDeviceGroup(profileId, plan.ID.ValueString())
369416
if err != nil {
370417
resp.Diagnostics.AddError(
371418
"Error updating device group profile assignment",

0 commit comments

Comments
 (0)