60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package rainyun
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.yoresee.cc/nightstar/ry/internal/provider"
|
|
)
|
|
|
|
type StorageService struct {
|
|
client *client
|
|
}
|
|
|
|
func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance, error) {
|
|
var raw []struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Status string `json:"status"`
|
|
}
|
|
if err := s.client.getWithQuery(ctx, "/product/ros/instance", map[string]string{"options": "1"}, &raw); err != nil {
|
|
return nil, err
|
|
}
|
|
instances := make([]provider.StorageInstance, len(raw))
|
|
for i, r := range raw {
|
|
instances[i] = provider.StorageInstance{
|
|
ID: fmt.Sprintf("%d", r.ID),
|
|
Name: r.Name,
|
|
Raw: r,
|
|
}
|
|
}
|
|
return instances, nil
|
|
}
|
|
|
|
func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]provider.Bucket, error) {
|
|
type rainyunBucket struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Region string `json:"region"`
|
|
}
|
|
var raw []rainyunBucket
|
|
path := fmt.Sprintf("/product/ros/bucket/%s", instanceID)
|
|
if err := s.client.get(ctx, path, &raw); err != nil {
|
|
return nil, err
|
|
}
|
|
buckets := make([]provider.Bucket, len(raw))
|
|
for i, r := range raw {
|
|
buckets[i] = provider.Bucket{
|
|
ID: fmt.Sprintf("%d", r.ID),
|
|
Name: r.Name,
|
|
}
|
|
}
|
|
return buckets, nil
|
|
}
|
|
|
|
func (s *StorageService) BucketCreate(ctx context.Context, instanceID, name string) error {
|
|
body := map[string]string{"name": name}
|
|
path := fmt.Sprintf("/product/ros/bucket/%s", instanceID)
|
|
return s.client.post(ctx, path, body, nil)
|
|
}
|