0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00
terraform-provider-proxmox/proxmox/cluster/acme/account/acme_account.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

126 lines
3.1 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 account
import (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)
// List returns a list of ACME accounts.
func (c *Client) List(ctx context.Context) ([]*ACMEAccountListResponseData, error) {
resBody := &ACMEAccountListResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath(""), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error listing ACME accounts: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].Name < resBody.Data[j].Name
})
return resBody.Data, nil
}
// Get retrieves a single ACME account based on its identifier.
func (c *Client) Get(ctx context.Context, name string) (*ACMEAccountGetResponseData, error) {
resBody := &ACMEAccountGetResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath(url.PathEscape(name)), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error reading ACME account: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}
// Create creates a new ACME account.
func (c *Client) Create(ctx context.Context, data *ACMEAccountCreateRequestBody) error {
resBody := &ACMEAccountCreateResponseBody{}
err := c.DoRequest(ctx, http.MethodPost, c.ExpandPath(""), data, resBody)
if err != nil {
return fmt.Errorf("error creating ACME account: %w", err)
}
if resBody.Data == nil {
return api.ErrNoDataObjectInResponse
}
err = c.Tasks().WaitForTask(ctx, *resBody.Data)
if err != nil {
return fmt.Errorf(
"error updating ACME account: failed waiting for task: %w",
err,
)
}
return nil
}
// Update updates an existing ACME account.
func (c *Client) Update(ctx context.Context, accountName string, data *ACMEAccountUpdateRequestBody) error {
resBody := &ACMEAccountUpdateResponseBody{}
err := c.DoRequest(ctx, http.MethodPut, c.ExpandPath(url.PathEscape(accountName)), data, resBody)
if err != nil {
return fmt.Errorf("error updating ACME account: %w", err)
}
if resBody.Data == nil {
return api.ErrNoDataObjectInResponse
}
err = c.Tasks().WaitForTask(ctx, *resBody.Data)
if err != nil {
return fmt.Errorf(
"error updating ACME account: failed waiting for task: %w",
err,
)
}
return nil
}
// Delete removes an ACME account.
func (c *Client) Delete(ctx context.Context, accountName string) error {
resBody := &ACMEAccountDeleteResponseBody{}
err := c.DoRequest(ctx, http.MethodDelete, c.ExpandPath(url.PathEscape(accountName)), nil, resBody)
if err != nil {
return fmt.Errorf("error deleting ACME account: %w", err)
}
if resBody.Data == nil {
return api.ErrNoDataObjectInResponse
}
err = c.Tasks().WaitForTask(ctx, *resBody.Data)
if err != nil {
return fmt.Errorf(
"error deleting ACME account: failed waiting for task: %w",
err,
)
}
return nil
}