mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-06-30 02:31:10 +00:00
* chore(vm2): add datasource implementation --------- Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
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/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
|
|
}
|
|
|
|
client, ok := req.ProviderData.(proxmox.Client)
|
|
|
|
if !ok {
|
|
resp.Diagnostics.AddError(
|
|
"Unexpected Resource Configure Type",
|
|
fmt.Sprintf("Expected *proxmox.Client, got: %T", req.ProviderData),
|
|
)
|
|
|
|
return
|
|
}
|
|
|
|
d.client = client
|
|
}
|
|
|
|
//nolint:dupl
|
|
func (d *Datasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var config Model
|
|
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
timeout, diags := config.Timeouts.Read(ctx, defaultReadTimeout)
|
|
resp.Diagnostics.Append(diags...)
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
exists := read(ctx, d.client, &config, &resp.Diagnostics)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
if !exists {
|
|
tflog.Info(ctx, "VM does not exist, removing from the state", map[string]interface{}{
|
|
"id": config.ID.ValueInt64(),
|
|
})
|
|
resp.State.RemoveResource(ctx)
|
|
|
|
return
|
|
}
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, config)...)
|
|
}
|