|
| 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 | +} |
0 commit comments