mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-03 12:02:57 +00:00
* feat(cluster): Implement initial support for "hardware mappings" Right now it is alredy possible to use a mapped resource [1], but there is no dedicated `proxmox_virtual_environment_cluster_hardware_mapping` resource but this step must still be done manually (or automated through other ways that interact with the Proxmox API). This commit implements support for "hardware mapping" resources and data sources for the, currently, available bus types PCI and USB, based on the Proxmox VE API documentations [2]. There are some "specialities" in these resources and data sources: 1. The Proxmox VE API attribute, but this implementations names it "comment" since this naming is generally across the Proxmox VE web UI and API documentations. This still follows the Terraform "best practices" [3] as it improves the user experience by matching the field name to the naming used in the human-facing interfaces. 2. Like in point 1, the name of the attribute of "node checks diagnostics" for USB hardware mappings is "errors" in the Proxmox VE API while it is "checks" for hardware mappings of type PCI. The second naming pattern is also generally used across the Proxmox VE web UI and API documentations, including the "check_node" attribute that is also implemented in the "proxmox_virtual_environment_hardware_mappings" data source. Therefore, this implementation named both attributes "checks" which still follows the Terraform "best practices" [3] as it improves the user experience by matching the field name to the naming used in the human-facing interfaces. 3. This implmenetation comes with the "unique" feature of allowing comments (named "descriptions" by the Proxmox VE API) for an entry in a device map which is not possible through the web UI at all but only adding a comment for the whole mapping entry instead. Note that this implementation also adds another point in the "Known Issues" documentation since it is only possible to map a PCI/USB device using the `root` PAM account, but this is still better than having to manually configure it through the web UI or by interacting with the Proxmox VE API on other ways. [1]: https://github.com/bpg/terraform-provider-proxmox/pull/500 [2]: https://pve.proxmox.com/pve-docs/api-viewer/#/cluster/mapping/pci [3]: https://developer.hashicorp.com/terraform/plugin/best-practices/hashicorp-provider-design-principles#resource-and-attribute-schema-should-closely-match-the-underlying-api Signed-off-by: Sven Greb <development@svengreb.de> * fix linter Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --------- Signed-off-by: Sven Greb <development@svengreb.de> Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
170 lines
5.5 KiB
Go
170 lines
5.5 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 hardwaremapping
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
|
|
|
"github.com/bpg/terraform-provider-proxmox/fwprovider/structure"
|
|
customtypes "github.com/bpg/terraform-provider-proxmox/fwprovider/types/hardwaremapping"
|
|
"github.com/bpg/terraform-provider-proxmox/fwprovider/validators"
|
|
"github.com/bpg/terraform-provider-proxmox/proxmox"
|
|
mappings "github.com/bpg/terraform-provider-proxmox/proxmox/cluster/mapping"
|
|
proxmoxtypes "github.com/bpg/terraform-provider-proxmox/proxmox/types/hardwaremapping"
|
|
)
|
|
|
|
// Ensure the implementation satisfies the required interfaces.
|
|
var (
|
|
_ datasource.DataSource = &dataSourcePCI{}
|
|
_ datasource.DataSourceWithConfigure = &dataSourcePCI{}
|
|
)
|
|
|
|
// dataSourcePCI is the data source implementation for a PCI hardware mapping.
|
|
type dataSourcePCI struct {
|
|
// client is the hardware mapping API client.
|
|
client *mappings.Client
|
|
}
|
|
|
|
// Configure adds the provider-configured client to the data source.
|
|
func (d *dataSourcePCI) 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.Cluster().HardwareMapping()
|
|
}
|
|
|
|
// Metadata returns the data source type name.
|
|
func (d *dataSourcePCI) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_hardware_mapping_pci"
|
|
}
|
|
|
|
// Read fetches the specified PCI hardware mapping from the Proxmox VE API.
|
|
func (d *dataSourcePCI) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var hm modelPCI
|
|
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &hm)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
hmID := hm.Name.ValueString()
|
|
// Ensure to keep both in sync since the name represents the ID.
|
|
hm.ID = hm.Name
|
|
|
|
data, err := d.client.Get(ctx, proxmoxtypes.TypePCI, hmID)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
fmt.Sprintf("Unable to read PCI hardware mapping %q", hmID),
|
|
err.Error(),
|
|
)
|
|
|
|
return
|
|
}
|
|
|
|
hm.importFromAPI(ctx, data)
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &hm)...)
|
|
}
|
|
|
|
// Schema defines the schema for the PCI hardware mapping.
|
|
func (d *dataSourcePCI) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
comment := dataSourceSchemaBaseAttrComment
|
|
comment.Optional = false
|
|
comment.Computed = true
|
|
comment.Description = "The comment of this PCI hardware mapping."
|
|
commentMap := comment
|
|
commentMap.Description = "The comment of the mapped PCI device."
|
|
|
|
resp.Schema = schema.Schema{
|
|
Description: "Retrieves a PCI hardware mapping from a Proxmox VE cluster.",
|
|
Attributes: map[string]schema.Attribute{
|
|
schemaAttrNameComment: comment,
|
|
schemaAttrNameMap: schema.SetNestedAttribute{
|
|
Computed: true,
|
|
Description: "The actual map of devices for the hardware mapping.",
|
|
NestedObject: schema.NestedAttributeObject{
|
|
Attributes: map[string]schema.Attribute{
|
|
schemaAttrNameComment: commentMap,
|
|
schemaAttrNameMapIOMMUGroup: schema.Int64Attribute{
|
|
Computed: true,
|
|
Description: "The IOMMU group attribute of the map.",
|
|
},
|
|
schemaAttrNameMapDeviceID: schema.StringAttribute{
|
|
Computed: true,
|
|
Description: "The ID attribute of the map.",
|
|
Validators: []validator.String{
|
|
validators.HardwareMappingDeviceIDValidator(),
|
|
},
|
|
},
|
|
schemaAttrNameMapNode: schema.StringAttribute{
|
|
Computed: true,
|
|
Description: "The node name attribute of the map.",
|
|
},
|
|
schemaAttrNameMapPath: schema.StringAttribute{
|
|
// For hardware mappings of type PCI, the path is required while it is optional for USB.
|
|
Computed: true,
|
|
CustomType: customtypes.PathType{},
|
|
Description: "The path attribute of the map.",
|
|
},
|
|
schemaAttrNameMapSubsystemID: schema.StringAttribute{
|
|
Computed: true,
|
|
Description: "The subsystem ID attribute of the map." +
|
|
"Not mandatory for the Proxmox VE API call, but causes a PCI hardware mapping to be incomplete when " +
|
|
"not set.",
|
|
Validators: []validator.String{
|
|
validators.HardwareMappingDeviceIDValidator(),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Validators: []validator.Set{
|
|
setvalidator.SizeAtLeast(1),
|
|
},
|
|
},
|
|
schemaAttrNameMediatedDevices: schema.BoolAttribute{
|
|
Computed: true,
|
|
Description: "Indicates whether to use with mediated devices.",
|
|
},
|
|
schemaAttrNameName: schema.StringAttribute{
|
|
Description: "The name of this PCI hardware mapping.",
|
|
Required: true,
|
|
},
|
|
schemaAttrNameTerraformID: structure.IDAttribute(
|
|
"The unique identifier of this PCI hardware mapping data source.",
|
|
),
|
|
},
|
|
}
|
|
}
|
|
|
|
// NewDataSourcePCI returns a new data source for a PCI hardware mapping.
|
|
// This is a helper function to simplify the provider implementation.
|
|
func NewDataSourcePCI() datasource.DataSource {
|
|
return &dataSourcePCI{}
|
|
}
|