Skip to content

Commit ca61756

Browse files
author
David Kraushuber
committed
customdeclaration data source added
1 parent b75f2dc commit ca61756

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/DavidKrau/simplemdm-go-client"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
)
13+
14+
// Ensure the implementation satisfies the expected interfaces.
15+
var (
16+
_ datasource.DataSource = &customDeclarationDataSource{}
17+
_ datasource.DataSourceWithConfigure = &customDeclarationDataSource{}
18+
)
19+
20+
// customDeclarationModel maps the data source schema data.
21+
type customDeclarationDataSourceModel struct {
22+
Name types.String `tfsdk:"name"`
23+
ID types.String `tfsdk:"id"`
24+
}
25+
26+
// AttributeDataSource is a helper function to simplify the provider implementation.
27+
func CustomDeclarationDataSource() datasource.DataSource {
28+
return &customDeclarationDataSource{}
29+
}
30+
31+
// AttributeDataSource is the data source implementation.
32+
type customDeclarationDataSource struct {
33+
client *simplemdm.Client
34+
}
35+
36+
// Metadata returns the data source type name.
37+
func (d *customDeclarationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
38+
resp.TypeName = req.ProviderTypeName + "_customdeclaration"
39+
}
40+
41+
// Schema defines the schema for the data source.
42+
func (d *customDeclarationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
43+
resp.Schema = schema.Schema{
44+
Description: "Custom Declaration data source can be used together with Device(s) or Device Group(s) to set values or in lifecycle management.",
45+
Attributes: map[string]schema.Attribute{
46+
"id": schema.StringAttribute{
47+
Required: true,
48+
Description: "ID of the custom Declaration.",
49+
},
50+
"name": schema.StringAttribute{
51+
Computed: true,
52+
Description: "Default (global) value of the Attribute.",
53+
},
54+
},
55+
}
56+
}
57+
58+
// Read refreshes the Terraform state with the latest data.
59+
func (d *customDeclarationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
60+
var state customDeclarationDataSourceModel
61+
diags := req.Config.Get(ctx, &state)
62+
resp.Diagnostics.Append(diags...)
63+
64+
declaration, err := d.client.ProfileGet(state.Name.ValueString())
65+
if err != nil {
66+
resp.Diagnostics.AddError(
67+
"Unable to Read SimpleMDM custom declaration",
68+
err.Error(),
69+
)
70+
return
71+
}
72+
73+
// Map response body to model
74+
state.Name = types.StringValue(declaration.Data.Attributes.Name)
75+
state.ID = types.StringValue(strconv.Itoa(declaration.Data.ID))
76+
77+
// Set state
78+
79+
diags = resp.State.Set(ctx, &state)
80+
resp.Diagnostics.Append(diags...)
81+
if resp.Diagnostics.HasError() {
82+
return
83+
}
84+
}
85+
86+
// Configure adds the provider configured client to the data source.
87+
func (d *customDeclarationDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
88+
if req.ProviderData == nil {
89+
return
90+
}
91+
92+
client, ok := req.ProviderData.(*simplemdm.Client)
93+
if !ok {
94+
resp.Diagnostics.AddError(
95+
"Unexpected Data Source Configure Type",
96+
fmt.Sprintf("Expected *Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
97+
)
98+
99+
return
100+
}
101+
102+
d.client = client
103+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccCustomeDeclarationDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
12+
Steps: []resource.TestStep{
13+
// Read testing
14+
{
15+
Config: providerConfig + `data "simplemdm_customdeclaration" "test" {id ="214149"}`,
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
// Verify returned values
18+
resource.TestCheckResourceAttr("data.simplemdm_customdeclaration.test", "name", "testdeclaration"),
19+
resource.TestCheckResourceAttr("data.simplemdm_customdeclaration.test", "id", "214149"),
20+
),
21+
},
22+
},
23+
})
24+
}

provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (p *simplemdmProvider) Configure(ctx context.Context, req provider.Configur
151151
// DataSources defines the data sources implemented in the provider.
152152
func (p *simplemdmProvider) DataSources(_ context.Context) []func() datasource.DataSource {
153153
return []func() datasource.DataSource{
154-
AppDataSource, AttributeDataSource, DeviceGroupDataSource, CustomProfileDataSource, ProfileDataSource, DeviceDataSource, ScriptDataSource,
154+
AppDataSource, AttributeDataSource, DeviceGroupDataSource, CustomProfileDataSource, ProfileDataSource, DeviceDataSource, ScriptDataSource, CustomDeclarationDataSource,
155155
}
156156
}
157157

0 commit comments

Comments
 (0)