github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/maas/maas2storage.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"io/ioutil"
    10  	"sort"
    11  	"strings"
    12  
    13  	"github.com/juju/errors"
    14  	"github.com/juju/gomaasapi"
    15  	"github.com/juju/utils"
    16  
    17  	"github.com/juju/juju/environs/storage"
    18  )
    19  
    20  type maas2Storage struct {
    21  	// The Environ that this Storage is for.
    22  	environ *maasEnviron
    23  
    24  	// The Controller for the MAAS 2 cluster.
    25  	maasController gomaasapi.Controller
    26  }
    27  
    28  var _ storage.Storage = (*maas2Storage)(nil)
    29  
    30  func (stor *maas2Storage) prefixWithPrivateNamespace(name string) string {
    31  	return prefixWithPrivateNamespace(stor.environ, name)
    32  }
    33  
    34  // Get implements storage.StorageReader
    35  func (stor *maas2Storage) Get(name string) (io.ReadCloser, error) {
    36  	name = stor.prefixWithPrivateNamespace(name)
    37  	file, err := stor.maasController.GetFile(name)
    38  	if err != nil {
    39  		if gomaasapi.IsNoMatchError(err) {
    40  			return nil, errors.Wrap(err, errors.NotFoundf(name))
    41  		}
    42  		return nil, errors.Trace(err)
    43  	}
    44  	contents, err := file.ReadAll()
    45  	if err != nil {
    46  		return nil, errors.Trace(err)
    47  	}
    48  	return ioutil.NopCloser(bytes.NewReader(contents)), nil
    49  }
    50  
    51  // List implements storage.StorageReader
    52  func (stor *maas2Storage) List(prefix string) ([]string, error) {
    53  	prefix = stor.prefixWithPrivateNamespace(prefix)
    54  	files, err := stor.maasController.Files(prefix)
    55  	if err != nil {
    56  		return nil, errors.Trace(err)
    57  	}
    58  	privatePrefix := stor.prefixWithPrivateNamespace("")
    59  	names := make([]string, len(files))
    60  	for i, file := range files {
    61  		// Remove the private namespacing.
    62  		names[i] = strings.TrimPrefix(file.Filename(), privatePrefix)
    63  	}
    64  	sort.Strings(names)
    65  	return names, nil
    66  }
    67  
    68  // URL implements storage.StorageReader
    69  func (stor *maas2Storage) URL(name string) (string, error) {
    70  	name = stor.prefixWithPrivateNamespace(name)
    71  	file, err := stor.maasController.GetFile(name)
    72  	if err != nil {
    73  		return "", errors.Trace(err)
    74  	}
    75  	return file.AnonymousURL(), nil
    76  }
    77  
    78  // DefaultConsistencyStrategy implements storage.StorageReader
    79  func (stor *maas2Storage) DefaultConsistencyStrategy() utils.AttemptStrategy {
    80  	return utils.AttemptStrategy{}
    81  }
    82  
    83  // ShouldRetry implements storage.StorageReader
    84  func (stor *maas2Storage) ShouldRetry(err error) bool {
    85  	return false
    86  }
    87  
    88  // Put implements storage.StorageWriter
    89  func (stor *maas2Storage) Put(name string, r io.Reader, length int64) error {
    90  	name = stor.prefixWithPrivateNamespace(name)
    91  	args := gomaasapi.AddFileArgs{Filename: name, Reader: r, Length: length}
    92  	return stor.maasController.AddFile(args)
    93  }
    94  
    95  // Remove implements storage.StorageWriter
    96  func (stor *maas2Storage) Remove(name string) error {
    97  	name = stor.prefixWithPrivateNamespace(name)
    98  	file, err := stor.maasController.GetFile(name)
    99  	if err != nil {
   100  		return errors.Trace(err)
   101  	}
   102  	return file.Delete()
   103  }
   104  
   105  // RemoveAll implements storage.StorageWriter
   106  func (stor *maas2Storage) RemoveAll() error {
   107  	return removeAll(stor)
   108  }