forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_data_source.go
More file actions
114 lines (98 loc) · 3.55 KB
/
attribute_data_source.go
File metadata and controls
114 lines (98 loc) · 3.55 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
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"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &attributeDataSource{}
_ datasource.DataSourceWithConfigure = &attributeDataSource{}
)
// attributeDataSourceModel maps the data source schema data.
type attributeDataSourceModel struct {
ID types.String `tfsdk:"id"`
DefaultValue types.String `tfsdk:"default_value"`
Name types.String `tfsdk:"name"`
}
// AttributeDataSource is a helper function to simplify the provider implementation.
func AttributeDataSource() datasource.DataSource {
return &attributeDataSource{}
}
// AttributeDataSource is the data source implementation.
type attributeDataSource struct {
client *simplemdm.Client
}
// Metadata returns the data source type name.
func (d *attributeDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_attribute"
}
// Schema defines the schema for the data source.
func (d *attributeDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Attribute data source can be used together with Device(s) or Device Group(s) to set values or in lifecycle management.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "The unique identifier for the custom attribute (same as name).",
},
"name": schema.StringAttribute{
Required: true,
Description: "The name (and ID) of the Attribute.",
},
"default_value": schema.StringAttribute{
Computed: true,
Description: "Default (global) value of the Attribute.",
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *attributeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state attributeDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
attribute, err := d.client.AttributeGet(state.Name.ValueString())
if err != nil {
if isNotFoundError(err) {
resp.Diagnostics.AddError(
"SimpleMDM attribute not found",
fmt.Sprintf("The attribute with name %s was not found. It may have been deleted.", state.Name.ValueString()),
)
} else {
resp.Diagnostics.AddError(
"Unable to Read SimpleMDM attribute",
err.Error(),
)
}
return
}
// Map response body to model
state.ID = types.StringValue(attribute.Data.Attributes.Name)
state.Name = types.StringValue(attribute.Data.Attributes.Name)
state.DefaultValue = types.StringValue(attribute.Data.Attributes.DefaultValue)
// 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 *attributeDataSource) 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
}