0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 10:33:46 +00:00
terraform-provider-proxmox/fwprovider/vm/datasource.go
Pavel Boldyrev 72f7cb81a8
feat(provider): reliable sequential and random vm_id generation (#1557)
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-10-03 20:18:37 -04:00

101 lines
2.3 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package vm
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/bpg/terraform-provider-proxmox/fwprovider/config"
"github.com/bpg/terraform-provider-proxmox/proxmox"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &Datasource{}
_ datasource.DataSourceWithConfigure = &Datasource{}
)
// Datasource is the implementation of VM datasource.
type Datasource struct {
client proxmox.Client
}
// NewDataSource creates a new VM datasource.
func NewDataSource() datasource.DataSource {
return &Datasource{}
}
// Metadata defines the name of the resource.
func (d *Datasource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_vm2"
}
// Configure sets the client for the resource.
func (d *Datasource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
cfg, ok := req.ProviderData.(config.DataSource)
if !ok {
resp.Diagnostics.AddError(
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected config.DataSource, got: %T", req.ProviderData),
)
return
}
d.client = cfg.Client
}
//nolint:dupl
func (d *Datasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model Model
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
timeout, diags := model.Timeouts.Read(ctx, defaultReadTimeout)
resp.Diagnostics.Append(diags...)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
exists := read(ctx, d.client, &model, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !exists {
tflog.Info(ctx, "VM does not exist, removing from the state", map[string]interface{}{
"id": model.ID.ValueInt64(),
})
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
}