github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/oci/storage.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package oci
     5  
     6  import (
     7  	"github.com/juju/clock"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/provider/oci/common"
    11  	"github.com/juju/juju/storage"
    12  )
    13  
    14  type poolType string
    15  
    16  const (
    17  	ociStorageProviderType = storage.ProviderType("oci")
    18  
    19  	// maxVolumeSizeInGB represents the maximum size in GiB for
    20  	// a single volume. For more information please see:
    21  	// https://docs.oracle.com/cloud/latest/stcomputecs/STCSA/op-storage-volume--post.html#request
    22  	maxVolumeSizeInGB = 16000
    23  	// minVolumeSizeInGB represents the minimum size in GiB for
    24  	// a single volume. For more information please see:
    25  	// https://docs.oracle.com/cloud/latest/stcomputecs/STCSA/op-storage-volume--post.html#request
    26  	minVolumeSizeInGB = 50
    27  
    28  	iscsiPool     poolType = "iscsi"
    29  	ociVolumeType string   = "volume-type"
    30  )
    31  
    32  var poolTypeMap map[string]poolType = map[string]poolType{
    33  	"iscsi": iscsiPool,
    34  }
    35  
    36  type StorageAPI interface{}
    37  
    38  type storageProvider struct {
    39  	env *Environ
    40  	api common.OCIStorageClient
    41  }
    42  
    43  var _ storage.Provider = (*storageProvider)(nil)
    44  
    45  func (s *storageProvider) VolumeSource(cfg *storage.Config) (storage.VolumeSource, error) {
    46  	envConfig := s.env.Config()
    47  	name := envConfig.Name()
    48  	uuid := envConfig.UUID()
    49  	return &volumeSource{
    50  		env:        s.env,
    51  		envName:    name,
    52  		modelUUID:  uuid,
    53  		storageAPI: s.env.Storage,
    54  		computeAPI: s.env.Compute,
    55  		clock:      clock.WallClock,
    56  	}, nil
    57  }
    58  
    59  func (s *storageProvider) FilesystemSource(cfg *storage.Config) (storage.FilesystemSource, error) {
    60  	return nil, errors.NotSupportedf("filesystemsource")
    61  }
    62  
    63  func (s *storageProvider) Supports(kind storage.StorageKind) bool {
    64  	return kind == storage.StorageKindBlock
    65  }
    66  
    67  func (s *storageProvider) Scope() storage.Scope {
    68  	return storage.ScopeEnviron
    69  }
    70  
    71  func (s *storageProvider) Dynamic() bool {
    72  	return true
    73  }
    74  
    75  func (s *storageProvider) Releasable() bool {
    76  	// TODO (gsamfira): add support
    77  	return false
    78  }
    79  
    80  func (s *storageProvider) DefaultPools() []*storage.Config {
    81  	pool, _ := storage.NewConfig("iscsi", ociStorageProviderType, map[string]interface{}{
    82  		ociVolumeType: iscsiPool,
    83  	})
    84  	return []*storage.Config{pool}
    85  }
    86  
    87  func (s *storageProvider) ValidateConfig(cfg *storage.Config) error {
    88  	attrs := cfg.Attrs()
    89  	var pool string
    90  	if volType, ok := attrs[ociVolumeType]; ok {
    91  		switch kind := volType.(type) {
    92  		case string:
    93  			pool = volType.(string)
    94  
    95  		case poolType:
    96  			pool = string(volType.(poolType))
    97  		default:
    98  			return errors.Errorf("invalid volume-type %T", kind)
    99  		}
   100  		if _, ok := poolTypeMap[pool]; !ok {
   101  			return errors.Errorf("invalid volume-type %q", volType)
   102  		}
   103  		return nil
   104  	}
   105  	return nil
   106  }
   107  
   108  // StorageProviderTypes implements storage.ProviderRegistry.
   109  func (e *Environ) StorageProviderTypes() ([]storage.ProviderType, error) {
   110  	return []storage.ProviderType{ociStorageProviderType}, nil
   111  }
   112  
   113  // StorageProvider implements storage.ProviderRegistry.
   114  func (e *Environ) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
   115  	if t == ociStorageProviderType {
   116  		return &storageProvider{
   117  			env: e,
   118  			api: e.Storage,
   119  		}, nil
   120  	}
   121  
   122  	return nil, errors.NotFoundf("storage provider %q", t)
   123  }