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_accounts.go
Björn Brauer 9de4037a82
feat(acme): implement resources and data sources for ACME accounts (#1455)
* feat(acme): implement CRUD API for proxmox cluster ACME
* feat(acme): implement acme_accounts data source
* feat(acme): implement acme_account data source
* fix(acme): wait for task status on account creation
* feat(acme): implement account resource creation
* feat(acme): implement account read
* fix(acme): wait for task status on account update
* feat(acme): implement account update
* fix(acme): wait for task status on account deletion
* feat(acme): implement account deletion
* feat(acme): implement account import
* feat(acme): provide correctly typed API response for `account` field
* feat(acme): implement account schema for acme_account data source
* fix(acme): read `location` into state in acme_account resource
* fix(acme): ensure `name` of acme_account resource can't be changed
* docs(acme): generate documentation
* feat(acme): read back ACME account details from API
* Revert "fix(acme): ensure `name` of acme_account resource can't be changed"
* fix(acme): provide default for acme account name
* fix(acme): acme account name can't be changed
* chore(acme): update resource doc to clarify PVE auth requirements
* chore(acme): add `created_at` attr to the resource, sort model fields & schema attributes alphabetically

---------

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

120 lines
3.2 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/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/bpg/terraform-provider-proxmox/proxmox"
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/acme/account"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &acmeAccountsDatasource{}
_ datasource.DataSourceWithConfigure = &acmeAccountsDatasource{}
)
// NewACMEAccountsDataSource is a helper function to simplify the provider implementation.
func NewACMEAccountsDataSource() datasource.DataSource {
return &acmeAccountsDatasource{}
}
// acmeAccountsDatasource is the data source implementation for ACME accounts.
type acmeAccountsDatasource struct {
client *account.Client
}
// acmeAccountsModel maps the schema data for the ACME accounts data source.
type acmeAccountsModel struct {
Accounts types.Set `tfsdk:"accounts"`
}
// Metadata returns the data source type name.
func (d *acmeAccountsDatasource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_acme_accounts"
}
// Schema returns the schema for the data source.
func (d *acmeAccountsDatasource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Retrieves the list of ACME accounts.",
Attributes: map[string]schema.Attribute{
"accounts": schema.SetAttribute{
Description: "The identifiers of the ACME accounts.",
ElementType: types.StringType,
Computed: true,
},
},
}
}
// Configure adds the provider-configured client to the data source.
func (d *acmeAccountsDatasource) 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().Account()
}
// Read fetches the list of ACME Accounts from the Proxmox cluster then converts it to a list of strings.
func (d *acmeAccountsDatasource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
var state acmeAccountsModel
list, err := d.client.List(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read ACME accounts",
err.Error(),
)
return
}
accounts := make([]attr.Value, len(list))
for i, v := range list {
accounts[i] = types.StringValue(v.Name)
}
accountsValue, diags := types.SetValue(types.StringType, accounts)
resp.Diagnostics.Append(diags...)
state.Accounts = accountsValue
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}