-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
content.go
102 lines (86 loc) · 2.19 KB
/
content.go
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
/*
* 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 storage
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"github.com/avast/retry-go/v4"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)
// DeleteDatastoreFile deletes a file in a datastore.
func (c *Client) DeleteDatastoreFile(
ctx context.Context,
volumeID string,
) error {
path := c.ExpandPath(fmt.Sprintf("content/%s", url.PathEscape(volumeID)))
err := retry.Do(
func() error {
return c.DoRequest(ctx, http.MethodDelete, path, nil, nil)
},
retry.Context(ctx),
retry.RetryIf(func(err error) bool {
return !errors.Is(err, api.ErrResourceDoesNotExist)
}),
retry.LastErrorOnly(true),
)
if err != nil {
return fmt.Errorf("error deleting file %s from datastore %s: %w", volumeID, c.StorageName, err)
}
return nil
}
// ListDatastoreFiles retrieves a list of the files in a datastore.
func (c *Client) ListDatastoreFiles(
ctx context.Context,
) ([]*DatastoreFileListResponseData, error) {
resBody := &DatastoreFileListResponseBody{}
err := c.DoRequest(
ctx,
http.MethodGet,
c.ExpandPath("content"),
nil,
resBody,
)
if err != nil {
return nil, fmt.Errorf("error retrieving files from datastore %s: %w", c.StorageName, err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].VolumeID < resBody.Data[j].VolumeID
})
return resBody.Data, nil
}
// GetDatastoreFile get a file details in a datastore.
func (c *Client) GetDatastoreFile(
ctx context.Context,
volumeID string,
) (*DatastoreFileGetResponseData, error) {
resBody := &DatastoreFileGetResponseBody{}
err := c.DoRequest(
ctx,
http.MethodGet,
c.ExpandPath(
fmt.Sprintf(
"content/%s",
url.PathEscape(volumeID),
),
),
nil,
resBody,
)
if err != nil {
return nil, fmt.Errorf("error get file %s from datastore %s: %w", volumeID, c.StorageName, err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}