-
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathacme_account.go
More file actions
125 lines (98 loc) · 3.1 KB
/
acme_account.go
File metadata and controls
125 lines (98 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* 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).Err()
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).Err()
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).Err()
if err != nil {
return fmt.Errorf(
"error deleting ACME account: failed waiting for task: %w",
err,
)
}
return nil
}