Skip to content

Commit 57353be

Browse files
author
David Kraushuber
committed
replacing APi call for custom profiles, handling 409/429 errors slightly better
1 parent 9f4e63e commit 57353be

File tree

3 files changed

+20
-48
lines changed

3 files changed

+20
-48
lines changed

provider/assignmentGroup_resource.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ func (r *assignment_groupResource) Update(ctx context.Context, req resource.Upda
503503
}
504504

505505
//remove any left over apps in state
506-
507506
for _, stateApp := range state.Apps {
508507
found := false
509508
for _, planApp := range plan.Apps {
@@ -656,8 +655,8 @@ func (r *assignment_groupResource) Update(ctx context.Context, req resource.Upda
656655
err := r.client.AssignmentGroupUnAssignObject(plan.ID.ValueString(), deviceId, "devices")
657656
if err != nil {
658657
resp.Diagnostics.AddError(
659-
"Error updating assignment group device group assignment",
660-
"Could not update assignment group device group, unexpected error: "+err.Error(),
658+
"Error updating assignment group device assignment",
659+
"Could not update assignment group device assignment, unexpected error: "+err.Error(),
661660
)
662661
return
663662
}
@@ -667,8 +666,8 @@ func (r *assignment_groupResource) Update(ctx context.Context, req resource.Upda
667666
err := r.client.AssignmentGroupUpdateInstalledApps(plan.ID.ValueString())
668667
if err != nil {
669668
resp.Diagnostics.AddError(
670-
"Error updating assignment group profile assignment",
671-
"Could not update assignment group profile assignment, unexpected error: "+err.Error(),
669+
"Error updating assignment App update failed",
670+
"Could not update assignment App update failed, unexpected error: "+err.Error(),
672671
)
673672
return
674673
}
@@ -678,8 +677,8 @@ func (r *assignment_groupResource) Update(ctx context.Context, req resource.Upda
678677
err := r.client.AssignmentGroupPushApps(plan.ID.ValueString())
679678
if err != nil {
680679
resp.Diagnostics.AddError(
681-
"Error updating assignment group profile assignment",
682-
"Could not update assignment group profile assignment, unexpected error: "+err.Error(),
680+
"Error updating assignment App push failed",
681+
"Could not update assignment App push failed, unexpected error: "+err.Error(),
683682
)
684683
return
685684
}
@@ -689,8 +688,8 @@ func (r *assignment_groupResource) Update(ctx context.Context, req resource.Upda
689688
err := r.client.AssignmentGroupSyncProfiles(plan.ID.ValueString())
690689
if err != nil {
691690
resp.Diagnostics.AddError(
692-
"Error updating assignment group profile assignment",
693-
"Could not update assignment group profile assignment, unexpected error: "+err.Error(),
691+
"Error updating assignment group profile sync",
692+
"Could not update assignment group profile sync, unexpected error: "+err.Error(),
694693
)
695694
return
696695
}

provider/customProfile_data_source.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,18 @@ func (d *customProfileDataSource) Read(ctx context.Context, req datasource.ReadR
6161
diags := req.Config.Get(ctx, &state)
6262
resp.Diagnostics.Append(diags...)
6363

64-
profiles, err := d.client.CustomProfileGetAll()
64+
profile, err := d.client.ProfileGet(state.ID.ValueString())
6565
if err != nil {
66-
resp.Diagnostics.AddError(
67-
"Unable to Read SimpleMDM profile",
68-
err.Error(),
69-
)
70-
return
71-
}
72-
73-
profilefound := false
74-
for _, profile := range profiles.Data {
75-
if state.ID.ValueString() == strconv.Itoa(profile.ID) {
76-
state.Name = types.StringValue(profile.Attributes.Name)
77-
profilefound = true
78-
break
79-
}
80-
}
81-
82-
if !profilefound {
8366
resp.Diagnostics.AddError(
8467
"Error Reading SimpleMDM custom profile",
85-
"Could not read custom profles ID %s from array:"+state.ID.ValueString(),
68+
"Could not read custom profile ID "+state.ID.ValueString()+": "+err.Error(),
8669
)
8770
return
8871
}
8972

9073
// Set state
74+
state.Name = types.StringValue(profile.Data.Attributes.Name)
75+
state.ID = types.StringValue(strconv.Itoa(profile.Data.ID))
9176

9277
diags = resp.State.Set(ctx, &state)
9378
resp.Diagnostics.Append(diags...)

provider/customProfile_resourse.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,22 @@ func (r *customProfileResource) Read(ctx context.Context, req resource.ReadReque
156156
return
157157
}
158158

159-
//retrive one needs to be implemented first
160159
// Get refreshed profile values from SimpleMDM
161-
profiles, err := r.client.CustomProfileGetAll()
160+
161+
profile, err := r.client.ProfileGet(state.ID.ValueString())
162162
if err != nil {
163163
resp.Diagnostics.AddError(
164164
"Error Reading SimpleMDM custom profile",
165-
"Could not read custom profles ID "+state.ID.ValueString()+": "+err.Error(),
165+
"Could not read custom profile ID "+state.ID.ValueString()+": "+err.Error(),
166166
)
167167
return
168168
}
169169

170-
profilefound := false
171-
for _, profile := range profiles.Data {
172-
if state.ID.ValueString() == strconv.Itoa(profile.ID) {
173-
state.Name = types.StringValue(profile.Attributes.Name)
174-
state.UserScope = types.BoolValue(profile.Attributes.UserScope)
175-
state.AttributeSupport = types.BoolValue(profile.Attributes.AttributeSupport)
176-
state.EscapeAttributes = types.BoolValue(profile.Attributes.EscapeAttributes)
177-
state.ReinstallAfterOSUpdate = types.BoolValue(profile.Attributes.ReinstallAfterOsUpdate)
178-
profilefound = true
179-
break
180-
}
181-
}
182-
183-
if !profilefound {
184-
resp.State.RemoveResource(ctx)
185-
return
186-
}
170+
state.Name = types.StringValue(profile.Data.Attributes.Name)
171+
state.UserScope = types.BoolValue(profile.Data.Attributes.UserScope)
172+
state.AttributeSupport = types.BoolValue(profile.Data.Attributes.AttributeSupport)
173+
state.EscapeAttributes = types.BoolValue(profile.Data.Attributes.EscapeAttributes)
174+
state.ReinstallAfterOSUpdate = types.BoolValue(profile.Data.Attributes.ReinstallAfterOsUpdate)
187175

188176
_, body, err := r.client.CustomProfileSHA(state.ID.ValueString())
189177
if err != nil {

0 commit comments

Comments
 (0)