Skip to content

Commit 9ca5ed5

Browse files
committed
storage
1 parent 2ca7452 commit 9ca5ed5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

api/storage_type.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package api
22

3+
import (
4+
"encoding/json"
5+
)
6+
37
type Storage struct {
48
Active int
59
Avail int
@@ -26,3 +30,20 @@ type StorageCreateOptions struct {
2630
Mkdir bool `json:"mkdir,omitempty"`
2731
Path string `json:"path,omitempty"`
2832
}
33+
34+
type StorageContent struct {
35+
Storage string `json:",omitempty"`
36+
Content string `json:",omitempty"`
37+
// to do : use custom type instead of json.Number
38+
CTime json.Number `json:",omitempty"`
39+
Encrypted string
40+
Format string
41+
Notes string
42+
Parent string
43+
Protected bool
44+
Size int
45+
Used int
46+
// to do : Verificateion
47+
VMID int
48+
VolID string `josn:"volid,omitempty"`
49+
}

proxmox/storage.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package proxmox
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57

68
"github.com/sp-yduck/proxmox-go/api"
79
"github.com/sp-yduck/proxmox-go/rest"
@@ -10,6 +12,7 @@ import (
1012
type Storage struct {
1113
restclient *rest.RESTClient
1214
Storage *api.Storage
15+
Node string
1316
}
1417

1518
func (s *Service) Storage(ctx context.Context, name string) (*Storage, error) {
@@ -29,3 +32,32 @@ func (s *Service) CreateStorage(ctx context.Context, name, storageType string, o
2932
}
3033
return &Storage{restclient: s.restclient, Storage: storage}, nil
3134
}
35+
36+
func (s *Storage) Delete(ctx context.Context) error {
37+
return s.restclient.DeleteStorage(ctx, s.Storage.Storage)
38+
}
39+
40+
func (s *Storage) GetContents(ctx context.Context) ([]*api.StorageContent, error) {
41+
var contents []*api.StorageContent
42+
if s.Node == "" {
43+
return nil, errors.New("Node must not be empty")
44+
}
45+
path := fmt.Sprintf("/nodes/%s/storage/%s/content", s.Node, s.Storage.Storage)
46+
if err := s.restclient.Get(ctx, path, &contents); err != nil {
47+
return nil, err
48+
}
49+
return contents, nil
50+
}
51+
52+
func (s *Storage) GetContent(ctx context.Context, volumeID string) (*api.StorageContent, error) {
53+
contents, err := s.GetContents(ctx)
54+
if err != nil {
55+
return nil, err
56+
}
57+
for _, content := range contents {
58+
if content.VolID == volumeID {
59+
return content, nil
60+
}
61+
}
62+
return nil, rest.NotFoundErr
63+
}

0 commit comments

Comments
 (0)