0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 11:02:59 +00:00
terraform-provider-proxmox/fwprovider/acme/datasource_acme_plugin.go
Björn Brauer a6eb81af08
feat(acme): implement resources and data sources for ACME plugins (#1479)
* feat(acme): implement CRUD API for proxmox cluster ACME plugins

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement acme_plugins data source

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement acme_plugin data source

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement plugin resource creation

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement plugin resource read

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement plugin resource update

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement plugin resource deletion

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat(acme): implement plugin resource import

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* docs(acme): generate documentation

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: apply suggestions from code review

Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* refactor: extract common fields into BasePluginData

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: restrict plugin resource to type=dns only

because type=standalone is not configurable and always enabled by
default.

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: remove unused 'nodes' property

https://github.com/bpg/terraform-provider-proxmox/pull/1479/files#r1710916265

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: remove "delete" property

https://github.com/bpg/terraform-provider-proxmox/pull/1479/files#r1710908809

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* feat: implement attribute deletion

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: ignore empty lines in dns plugin data

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: partial revert of code review suggestions

Joining the values with a string literal would produce \\n instead of \n
and splitting at \\n doesn't match a newline.

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* refactor: extract acme plugin models into separate file

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

* fix: format disable parameter as int

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>

---------

Signed-off-by: Björn Brauer <zaubernerd@zaubernerd.de>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-09-08 10:54:16 -04:00

154 lines
4.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 acme
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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/hashicorp/terraform-plugin-framework/types"
"github.com/bpg/terraform-provider-proxmox/proxmox"
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/acme/plugins"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &acmePluginDatasource{}
_ datasource.DataSourceWithConfigure = &acmePluginDatasource{}
)
// NewACMEPluginDataSource is a helper function to simplify the provider implementation.
func NewACMEPluginDataSource() datasource.DataSource {
return &acmePluginDatasource{}
}
// acmePluginDatasource is the data source implementation for ACME plugin.
type acmePluginDatasource struct {
client *plugins.Client
}
// Metadata returns the data source type name.
func (d *acmePluginDatasource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_acme_plugin"
}
// Schema returns the schema for the data source.
func (d *acmePluginDatasource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Retrieves a single ACME plugin by plugin ID name.",
Attributes: map[string]schema.Attribute{
"api": schema.StringAttribute{
Description: "API plugin name.",
Computed: true,
},
"data": schema.MapAttribute{
Description: "DNS plugin data.",
Computed: true,
ElementType: types.StringType,
},
"digest": schema.StringAttribute{
Description: "Prevent changes if current configuration file has a different digest. " +
"This can be used to prevent concurrent modifications.",
Computed: true,
},
"plugin": schema.StringAttribute{
Description: "ACME Plugin ID name.",
Required: true,
},
"type": schema.StringAttribute{
Description: "ACME challenge type (dns, standalone).",
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf("dns", "standalone"),
},
},
"validation_delay": schema.Int64Attribute{
Description: "Extra delay in seconds to wait before requesting validation. " +
"Allows to cope with a long TTL of DNS records (0 - 172800).",
Computed: true,
Validators: []validator.Int64{
int64validator.Between(0, 172800),
},
},
},
}
}
// Configure adds the provider-configured client to the data source.
func (d *acmePluginDatasource) 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().ACME().Plugins()
}
// Read fetches the ACME plugin from the Proxmox cluster.
func (d *acmePluginDatasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state acmePluginModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
id := state.Plugin.ValueString()
plugin, err := d.client.Get(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read ACME plugin",
err.Error(),
)
return
}
state.API = types.StringValue(plugin.API)
mapValue, diags := types.MapValueFrom(ctx, types.StringType, plugin.Data)
resp.Diagnostics.Append(diags...)
state.Data = mapValue
state.Digest = types.StringValue(plugin.Digest)
state.Plugin = types.StringValue(plugin.Plugin)
state.Type = types.StringValue(plugin.Type)
state.ValidationDelay = types.Int64Value(plugin.ValidationDelay)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}