virtual_file_system_storage.go raw

   1  package govultr
   2  
   3  import (
   4  	"context"
   5  	"fmt"
   6  	"net/http"
   7  
   8  	"github.com/google/go-querystring/query"
   9  )
  10  
  11  const virtualFileSystemStoragePath = "/v2/vfs"
  12  
  13  // VirtualFileSystemStorageService is the interface to interact with virtual
  14  // file system endpoint on the Vultr API
  15  // Link : https://www.vultr.com/api/#tag/vfs
  16  type VirtualFileSystemStorageService interface {
  17  	Create(ctx context.Context, vfsReq *VirtualFileSystemStorageReq) (*VirtualFileSystemStorage, *http.Response, error)
  18  	Get(ctx context.Context, vfsID string) (*VirtualFileSystemStorage, *http.Response, error)
  19  	Update(ctx context.Context, vfsID string, vfsUpdateReq *VirtualFileSystemStorageUpdateReq) (*VirtualFileSystemStorage, *http.Response, error) //nolint:lll
  20  	Delete(ctx context.Context, vfsID string) error
  21  	List(ctx context.Context, options *ListOptions) ([]VirtualFileSystemStorage, *Meta, *http.Response, error)
  22  
  23  	AttachmentList(ctx context.Context, vfsID string) ([]VirtualFileSystemStorageAttachment, *http.Response, error)
  24  	AttachmentGet(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error)
  25  	Attach(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error)
  26  	Detach(ctx context.Context, vfsID, targetID string) error
  27  }
  28  
  29  // VirtualFileSystemStorageServiceHandler handles interaction with the virtual
  30  // file system methods for the Vultr API.
  31  type VirtualFileSystemStorageServiceHandler struct {
  32  	client *Client
  33  }
  34  
  35  // VirtualFileSystemStorage represents a virtual file system storage.
  36  type VirtualFileSystemStorage struct {
  37  	ID          string                          `json:"id"`
  38  	Region      string                          `json:"region"`
  39  	DateCreated string                          `json:"date_created"`
  40  	Status      string                          `json:"status"`
  41  	Label       string                          `json:"label"`
  42  	Tags        []string                        `json:"tags"`
  43  	DiskType    string                          `json:"disk_type"`
  44  	StorageSize VirtualFileSystemStorageSize    `json:"storage_size"`
  45  	StorageUsed VirtualFileSystemStorageSize    `json:"storage_used"`
  46  	Billing     VirtualFileSystemStorageBilling `json:"billing"`
  47  }
  48  
  49  // VirtualFileSystemStorageSize represents the on disk size of a virtual file
  50  // system storage.
  51  type VirtualFileSystemStorageSize struct {
  52  	SizeBytes int `json:"bytes,omitempty"`
  53  	SizeGB    int `json:"gb"`
  54  }
  55  
  56  // VirtualFileSystemStorageBilling represents the billing data of a virtual
  57  // file system storage.
  58  type VirtualFileSystemStorageBilling struct {
  59  	Charges float32 `json:"charges"`
  60  	Monthly float32 `json:"monthly"`
  61  }
  62  
  63  type virtualFileSystemStoragesBase struct {
  64  	VFSs []VirtualFileSystemStorage `json:"vfs"`
  65  	Meta *Meta                      `json:"meta"`
  66  }
  67  
  68  // VirtualFileSystemStorageReq struct represents the request used when creating
  69  // a virtual file system storage.
  70  type VirtualFileSystemStorageReq struct {
  71  	Region      string                       `json:"region"`
  72  	Label       string                       `json:"label"`
  73  	StorageSize VirtualFileSystemStorageSize `json:"storage_size"`
  74  	DiskType    string                       `json:"disk_type,omitempty"`
  75  	Tags        []string                     `json:"tags,omitempty"`
  76  }
  77  
  78  // VirtualFileSystemStorageUpdateReq struct represents the request used when
  79  // updating virtual file system storage.
  80  type VirtualFileSystemStorageUpdateReq struct {
  81  	Label       string                       `json:"label,omitempty"`
  82  	StorageSize VirtualFileSystemStorageSize `json:"storage_size"`
  83  }
  84  
  85  // VirtualFileSystemStorageAttachment represents an attachment for a virtual
  86  // file system.
  87  type VirtualFileSystemStorageAttachment struct {
  88  	ID       string `json:"vfs_id"`
  89  	State    string `json:"state"`
  90  	TargetID string `json:"target_id"`
  91  	MountTag int    `json:"mount_tag"`
  92  }
  93  
  94  type virtualFileSystemStorageAttachmentsBase struct {
  95  	Attachments []VirtualFileSystemStorageAttachment `json:"attachments"`
  96  }
  97  
  98  // Create sends a create request for a virtual file system storage.
  99  func (f *VirtualFileSystemStorageServiceHandler) Create(ctx context.Context, vfsReq *VirtualFileSystemStorageReq) (*VirtualFileSystemStorage, *http.Response, error) { //nolint:lll
 100  	req, err := f.client.NewRequest(ctx, http.MethodPost, virtualFileSystemStoragePath, vfsReq)
 101  	if err != nil {
 102  		return nil, nil, err
 103  	}
 104  
 105  	vfs := new(VirtualFileSystemStorage)
 106  	resp, err := f.client.DoWithContext(ctx, req, vfs)
 107  	if err != nil {
 108  		return nil, resp, err
 109  	}
 110  
 111  	return vfs, resp, nil
 112  }
 113  
 114  // Get retrieves a single virtual file system storage.
 115  func (f *VirtualFileSystemStorageServiceHandler) Get(ctx context.Context, vfsID string) (*VirtualFileSystemStorage, *http.Response, error) {
 116  	uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
 117  
 118  	req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
 119  	if err != nil {
 120  		return nil, nil, err
 121  	}
 122  
 123  	vfs := new(VirtualFileSystemStorage)
 124  	resp, err := f.client.DoWithContext(ctx, req, vfs)
 125  	if err != nil {
 126  		return nil, resp, err
 127  	}
 128  
 129  	return vfs, resp, nil
 130  }
 131  
 132  // Update sends a update request for a virtual file system storage.
 133  func (f *VirtualFileSystemStorageServiceHandler) Update(ctx context.Context, vfsID string, vfsUpdateReq *VirtualFileSystemStorageUpdateReq) (*VirtualFileSystemStorage, *http.Response, error) { //nolint:lll
 134  	uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
 135  
 136  	req, err := f.client.NewRequest(ctx, http.MethodPut, uri, vfsUpdateReq)
 137  	if err != nil {
 138  		return nil, nil, err
 139  	}
 140  
 141  	vfs := new(VirtualFileSystemStorage)
 142  	resp, err := f.client.DoWithContext(ctx, req, vfs)
 143  	if err != nil {
 144  		return nil, resp, err
 145  	}
 146  	return vfs, resp, err
 147  }
 148  
 149  // Delete sends a delete request for a virtual file system storage.
 150  func (f *VirtualFileSystemStorageServiceHandler) Delete(ctx context.Context, vfsID string) error {
 151  	uri := fmt.Sprintf("%s/%s", virtualFileSystemStoragePath, vfsID)
 152  
 153  	req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
 154  	if err != nil {
 155  		return err
 156  	}
 157  	_, err = f.client.DoWithContext(ctx, req, nil)
 158  	return err
 159  }
 160  
 161  // List retrieves a list of all virtual file system storages.
 162  func (f *VirtualFileSystemStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]VirtualFileSystemStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
 163  	req, err := f.client.NewRequest(ctx, http.MethodGet, virtualFileSystemStoragePath, nil)
 164  	if err != nil {
 165  		return nil, nil, nil, err
 166  	}
 167  
 168  	newValues, err := query.Values(options)
 169  	if err != nil {
 170  		return nil, nil, nil, err
 171  	}
 172  
 173  	req.URL.RawQuery = newValues.Encode()
 174  
 175  	vfsStorages := new(virtualFileSystemStoragesBase)
 176  	resp, err := f.client.DoWithContext(ctx, req, vfsStorages)
 177  	if err != nil {
 178  		return nil, nil, resp, err
 179  	}
 180  
 181  	return vfsStorages.VFSs, vfsStorages.Meta, resp, nil
 182  }
 183  
 184  // Attach attaches a virtual file system storage to another instance.
 185  func (f *VirtualFileSystemStorageServiceHandler) Attach(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
 186  	uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
 187  
 188  	req, err := f.client.NewRequest(ctx, http.MethodPut, uri, nil)
 189  	if err != nil {
 190  		return nil, nil, err
 191  	}
 192  
 193  	att := new(VirtualFileSystemStorageAttachment)
 194  	resp, err := f.client.DoWithContext(ctx, req, att)
 195  	if err != nil {
 196  		return nil, resp, err
 197  	}
 198  
 199  	return att, resp, err
 200  }
 201  
 202  // AttachmentList retrieves a list the active attachments on a virtual file
 203  // system storage.
 204  func (f *VirtualFileSystemStorageServiceHandler) AttachmentList(ctx context.Context, vfsID string) ([]VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
 205  	uri := fmt.Sprintf("%s/%s/attachments", virtualFileSystemStoragePath, vfsID)
 206  
 207  	req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
 208  	if err != nil {
 209  		return nil, nil, err
 210  	}
 211  
 212  	atts := new(virtualFileSystemStorageAttachmentsBase)
 213  	resp, err := f.client.DoWithContext(ctx, req, atts)
 214  	if err != nil {
 215  		return nil, resp, err
 216  	}
 217  
 218  	return atts.Attachments, resp, err
 219  }
 220  
 221  // AttachmentGet retrieves the attachment of a virtual file system storage and
 222  // its attached instance.
 223  func (f *VirtualFileSystemStorageServiceHandler) AttachmentGet(ctx context.Context, vfsID, targetID string) (*VirtualFileSystemStorageAttachment, *http.Response, error) { //nolint:lll
 224  	uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
 225  
 226  	req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
 227  	if err != nil {
 228  		return nil, nil, err
 229  	}
 230  
 231  	att := new(VirtualFileSystemStorageAttachment)
 232  	resp, err := f.client.DoWithContext(ctx, req, att)
 233  	if err != nil {
 234  		return nil, resp, err
 235  	}
 236  
 237  	return att, resp, err
 238  }
 239  
 240  // Detach sends a delete request for an attachment of a virtual file system
 241  // storage, detaching it from its instance.
 242  func (f *VirtualFileSystemStorageServiceHandler) Detach(ctx context.Context, vfsID, targetID string) error {
 243  	uri := fmt.Sprintf("%s/%s/attachments/%s", virtualFileSystemStoragePath, vfsID, targetID)
 244  
 245  	req, err := f.client.NewRequest(ctx, http.MethodDelete, uri, nil)
 246  	if err != nil {
 247  		return err
 248  	}
 249  
 250  	if _, err := f.client.DoWithContext(ctx, req, nil); err != nil {
 251  		return err
 252  	}
 253  
 254  	return nil
 255  }
 256