github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/azure/internal/azurestorage/interface.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azurestorage
     5  
     6  import (
     7  	"github.com/Azure/azure-sdk-for-go/storage"
     8  	"github.com/juju/errors"
     9  )
    10  
    11  // Client is an interface providing access to Azure storage services.
    12  type Client interface {
    13  	// GetBlobService returns a BlobStorageClient which can operate
    14  	// on the blob service of the storage account.
    15  	GetBlobService() BlobStorageClient
    16  }
    17  
    18  // BlobStorageClient is an interface providing access to Azure blob storage.
    19  //
    20  // This interface the subet of functionality provided by
    21  // https://godoc.org/github.com/Azure/azure-sdk-for-go/storage#BlobStorageClient
    22  // that is required by Juju.
    23  type BlobStorageClient interface {
    24  	// GetContainerReference returns a Container object for the specified container name.
    25  	GetContainerReference(name string) Container
    26  }
    27  
    28  // Container provides access to an Azure storage container.
    29  type Container interface {
    30  	// Blobs returns the blobs in the container.
    31  	//
    32  	// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/List-Blobs
    33  	Blobs() ([]Blob, error)
    34  
    35  	// Blob returns a Blob object for the specified blob name.
    36  	Blob(name string) Blob
    37  }
    38  
    39  // Blob provides access to an Azure storage blob.
    40  type Blob interface {
    41  	// Name returns the name of the blob.
    42  	Name() string
    43  
    44  	// Properties returns the properties of the blob.
    45  	Properties() storage.BlobProperties
    46  
    47  	// DeleteIfExists deletes the given blob from the specified container If the
    48  	// blob is deleted with this call, returns true. Otherwise returns false.
    49  	//
    50  	// See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Blob
    51  	DeleteIfExists(*storage.DeleteBlobOptions) (bool, error)
    52  }
    53  
    54  // NewClientFunc is the type of the NewClient function.
    55  type NewClientFunc func(
    56  	accountName, accountKey, blobServiceBaseURL, apiVersion string,
    57  	useHTTPS bool,
    58  ) (Client, error)
    59  
    60  // NewClient returns a Client that is backed by a storage.Client created with
    61  // storage.NewClient
    62  func NewClient(accountName, accountKey, blobServiceBaseURL, apiVersion string, useHTTPS bool) (Client, error) {
    63  	client, err := storage.NewClient(accountName, accountKey, blobServiceBaseURL, apiVersion, useHTTPS)
    64  	if err != nil {
    65  		return nil, errors.Trace(err)
    66  	}
    67  	return clientWrapper{client}, nil
    68  }
    69  
    70  type clientWrapper struct {
    71  	storage.Client
    72  }
    73  
    74  // GetBlobService is part of the Client interface.
    75  func (w clientWrapper) GetBlobService() BlobStorageClient {
    76  	return &blobStorageClient{w.Client.GetBlobService()}
    77  }
    78  
    79  type blobStorageClient struct {
    80  	storage.BlobStorageClient
    81  }
    82  
    83  // GetContainerReference is part of the BlobStorageClient interface.
    84  func (c *blobStorageClient) GetContainerReference(name string) Container {
    85  	return container{c.BlobStorageClient.GetContainerReference(name)}
    86  }
    87  
    88  type container struct {
    89  	*storage.Container
    90  }
    91  
    92  // Blobs is part of the Container interface.
    93  func (c container) Blobs() ([]Blob, error) {
    94  	//TODO(axw) handle pagination.
    95  	resp, err := c.Container.ListBlobs(storage.ListBlobsParameters{})
    96  	if err != nil {
    97  		return nil, errors.Trace(err)
    98  	}
    99  	blobs := make([]Blob, len(resp.Blobs))
   100  	for i := range blobs {
   101  		blobs[i] = blob{&resp.Blobs[i]}
   102  	}
   103  	return blobs, nil
   104  }
   105  
   106  // Blob is part of the Container interface.
   107  func (c container) Blob(name string) Blob {
   108  	return blob{c.Container.GetBlobReference(name)}
   109  }
   110  
   111  type blob struct {
   112  	*storage.Blob
   113  }
   114  
   115  // Name is part of the Blob interface.
   116  func (b blob) Name() string {
   117  	return b.Blob.Name
   118  }
   119  
   120  // Properties is part of the Blob interface.
   121  func (b blob) Properties() storage.BlobProperties {
   122  	return b.Blob.Properties
   123  }