forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceGroup_data_source.go
More file actions
118 lines (101 loc) · 3.85 KB
/
deviceGroup_data_source.go
File metadata and controls
118 lines (101 loc) · 3.85 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
package provider
import (
"context"
"fmt"
"strconv"
"github.com/DavidKrau/simplemdm-go-client"
"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 = &deviceGroupDataSource{}
_ datasource.DataSourceWithConfigure = &deviceGroupDataSource{}
)
// deviceGroupDataSourceModel maps the data source schema data.
type deviceGroupDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
// DeviceGroupDataSource is a helper function to simplify the provider implementation.
func DeviceGroupDataSource() datasource.DataSource {
return &deviceGroupDataSource{}
}
// deviceGroupDataSource is the data source implementation.
type deviceGroupDataSource struct {
client *simplemdm.Client
}
// Metadata returns the data source type name.
func (d *deviceGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_devicegroup"
}
// Schema defines the schema for the data source.
func (d *deviceGroupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "⚠️ DEPRECATED: Device Groups have been superseded by Assignment Groups in SimpleMDM. " +
"Please use the simplemdm_assignmentgroup data source instead. " +
"This data source is maintained for backward compatibility only. " +
"Device Group data source can be used together with Assignment Group(s) to assign group(s) to these objects.",
DeprecationMessage: "Device Groups are deprecated. Use simplemdm_assignmentgroup instead.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "Device Group name.",
},
"id": schema.StringAttribute{
Required: true,
Description: "ID of a Device Group in SimpleMDM.",
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *deviceGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state deviceGroupDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
devicegroup, err := d.client.DeviceGroupGet(state.ID.ValueString())
if err != nil {
if isNotFoundError(err) {
resp.Diagnostics.AddError(
"SimpleMDM device group not found",
fmt.Sprintf("The device group with ID %s was not found. "+
"Note: Only legacy device group IDs from migrated groups are supported. "+
"Use simplemdm_assignmentgroup for current group functionality.",
state.ID.ValueString()),
)
} else {
resp.Diagnostics.AddError(
"Unable to Read SimpleMDM device group",
"Could not read device group "+state.ID.ValueString()+": "+err.Error()+". "+
"Ensure you are using a valid legacy device group ID.",
)
}
return
}
// Map response body to model
state.Name = types.StringValue(devicegroup.Data.Attributes.Name)
state.ID = types.StringValue(strconv.Itoa(devicegroup.Data.ID))
// 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 *deviceGroupDataSource) 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
}