forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdep_servers_data_source.go
More file actions
125 lines (109 loc) · 3.91 KB
/
dep_servers_data_source.go
File metadata and controls
125 lines (109 loc) · 3.91 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
package provider
import (
"context"
"fmt"
"github.com/DavidKrau/simplemdm-go-client"
"github.com/DavidKrau/terraform-provider-simplemdm/internal/simplemdmext"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &depServersDataSource{}
_ datasource.DataSourceWithConfigure = &depServersDataSource{}
)
type depServersDataSource struct {
client *simplemdm.Client
}
type depServersDataSourceModel struct {
DepServers []depServerModel `tfsdk:"dep_servers"`
}
type depServerModel struct {
ID types.String `tfsdk:"id"`
ServerName types.String `tfsdk:"server_name"`
ServerUUID types.String `tfsdk:"server_uuid"`
OrganizationName types.String `tfsdk:"organization_name"`
DevicesFetchedAt types.String `tfsdk:"devices_fetched_at"`
}
func DepServersDataSource() datasource.DataSource {
return &depServersDataSource{}
}
func (d *depServersDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dep_servers"
}
func (d *depServersDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Fetches all Apple DEP (Device Enrollment Program) servers associated with your SimpleMDM account.",
Blocks: map[string]schema.Block{
"dep_servers": schema.ListNestedBlock{
Description: "Collection of DEP server records.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "The unique identifier of the DEP server.",
},
"server_name": schema.StringAttribute{
Computed: true,
Description: "The name of the DEP server.",
},
"server_uuid": schema.StringAttribute{
Computed: true,
Description: "The UUID of the DEP server from Apple Business Manager.",
},
"organization_name": schema.StringAttribute{
Computed: true,
Description: "The organization name from Apple Business Manager.",
},
"devices_fetched_at": schema.StringAttribute{
Computed: true,
Description: "The timestamp when devices were last fetched from Apple.",
},
},
},
},
},
}
}
func (d *depServersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state depServersDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
servers, err := simplemdmext.ListDepServers(ctx, d.client)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read SimpleMDM DEP Servers",
err.Error(),
)
return
}
state.DepServers = make([]depServerModel, 0, len(servers))
for _, s := range servers {
state.DepServers = append(state.DepServers, depServerModel{
ID: types.StringValue(fmt.Sprintf("%d", s.ID)),
ServerName: types.StringValue(s.Attributes.ServerName),
ServerUUID: types.StringValue(s.Attributes.ServerUUID),
OrganizationName: types.StringValue(s.Attributes.OrganizationName),
DevicesFetchedAt: types.StringValue(s.Attributes.DevicesFetchedAt),
})
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
func (d *depServersDataSource) 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 *simplemdm.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}