7241: Add AzureBlobVolume
[arvados.git] / services / keepstore / azure_blob_volume.go
1 package main
2
3 import (
4         "fmt"
5         "io"
6         "time"
7
8         "github.com/Azure/azure-sdk-for-go/storage"
9 )
10
11 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
12 // container.
13 type AzureBlobVolume struct {
14         azClient      storage.Client
15         bsClient      storage.BlobStorageClient
16         containerName string
17         readonly      bool
18 }
19
20 func NewAzureBlobVolume(client storage.Client, containerName string, readonly bool) *AzureBlobVolume {
21         return &AzureBlobVolume{
22                 azClient: client,
23                 bsClient: client.GetBlobService(),
24                 containerName: containerName,
25                 readonly: readonly,
26         }
27 }
28
29 func (v *AzureBlobVolume) Get(loc string) ([]byte, error) {
30         rdr, err := v.bsClient.GetBlob(v.containerName, loc)
31         if err != nil {
32                 return nil, err
33         }
34         buf := bufs.Get(BlockSize)
35         n, err := io.ReadFull(rdr, buf)
36         switch err {
37         case io.EOF, io.ErrUnexpectedEOF:
38                 return buf[:n], nil
39         default:
40                 bufs.Put(buf)
41                 return nil, err
42         }
43 }
44
45 func (v *AzureBlobVolume) Compare(loc string, data []byte) error {
46         return NotFoundError
47 }
48
49 func (v *AzureBlobVolume) Put(loc string, block []byte) error {
50         return NotFoundError
51 }
52
53 func (v *AzureBlobVolume) Touch(loc string) error {
54         return NotFoundError
55 }
56
57 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
58         return time.Time{}, NotFoundError
59 }
60
61 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
62         return nil
63 }
64
65 func (v *AzureBlobVolume) Delete(loc string) error {
66         return NotFoundError
67 }
68
69 func (v *AzureBlobVolume) Status() *VolumeStatus {
70         return &VolumeStatus{
71                 DeviceNum: 1,
72                 BytesFree: BlockSize * 1000,
73                 BytesUsed: 1,
74         }
75 }
76
77 func (v *AzureBlobVolume) String() string {
78         return fmt.Sprintf("%+v", v.azClient)
79 }
80
81 func (v *AzureBlobVolume) Writable() bool {
82         return !v.readonly
83 }