forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagedConfig_data_source.go
More file actions
121 lines (105 loc) · 3.8 KB
/
managedConfig_data_source.go
File metadata and controls
121 lines (105 loc) · 3.8 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
package provider
import (
"context"
"fmt"
"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"
)
var (
_ datasource.DataSource = &managedConfigDataSource{}
_ datasource.DataSourceWithConfigure = &managedConfigDataSource{}
)
type managedConfigDataSourceModel struct {
ID types.String `tfsdk:"id"`
AppID types.String `tfsdk:"app_id"`
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
ValueType types.String `tfsdk:"value_type"`
}
type managedConfigDataSource struct {
client *simplemdm.Client
}
func ManagedConfigDataSource() datasource.DataSource {
return &managedConfigDataSource{}
}
func (d *managedConfigDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_managed_config"
}
func (d *managedConfigDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Managed Config data source retrieves the value of a managed app configuration for a given app.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
Description: "Managed config identifier in the format <app_id>:<managed_config_id>.",
},
"app_id": schema.StringAttribute{
Computed: true,
Description: "ID of the app that owns the managed configuration.",
},
"key": schema.StringAttribute{
Computed: true,
Description: "Configuration key returned by the SimpleMDM API.",
},
"value": schema.StringAttribute{
Computed: true,
Description: "Raw value returned by the SimpleMDM API.",
},
"value_type": schema.StringAttribute{
Computed: true,
Description: "Data type that SimpleMDM reports for the value.",
},
},
}
}
func (d *managedConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state managedConfigDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
appID, configID, err := parseManagedConfigID(state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Invalid managed config identifier", err.Error())
return
}
config, err := fetchManagedConfig(ctx, d.client, appID, configID)
if err != nil {
if isNotFoundError(err) || err.Error() == errManagedConfigNotFound.Error() {
resp.Diagnostics.AddError(
"SimpleMDM managed config not found",
fmt.Sprintf("The managed config %s for app %s was not found. It may have been deleted.", configID, appID),
)
} else {
resp.Diagnostics.AddError(
"Unable to read managed app configuration",
fmt.Sprintf("failed to fetch managed config %s for app %s: %v", configID, appID, err),
)
}
return
}
state.AppID = types.StringValue(appID)
state.Key = types.StringValue(config.Attributes.Key)
state.Value = types.StringValue(config.Attributes.Value)
state.ValueType = types.StringValue(config.Attributes.ValueType)
state.ID = types.StringValue(fmt.Sprintf("%s:%d", appID, config.ID))
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
func (d *managedConfigDataSource) 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
}