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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package poolmanager
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/storage"
    12  )
    13  
    14  // A PoolManager provides access to storage pools.
    15  type PoolManager interface {
    16  	// Create makes a new pool with the specified configuration and persists it to state.
    17  	Create(name string, providerType storage.ProviderType, attrs map[string]interface{}) (*storage.Config, error)
    18  
    19  	// Delete removes the pool with name from state.
    20  	Delete(name string) error
    21  
    22  	// Replace replaces pool configuration with the newly provided values.
    23  	Replace(name, provider string, attrs map[string]interface{}) error
    24  
    25  	// Get returns the pool with name from state.
    26  	Get(name string) (*storage.Config, error)
    27  
    28  	// List returns all the pools from state.
    29  	List() ([]*storage.Config, error)
    30  }
    31  
    32  type SettingsManager interface {
    33  	CreateSettings(key string, settings map[string]interface{}) error
    34  	ReplaceSettings(key string, settings map[string]interface{}) error
    35  	ReadSettings(key string) (map[string]interface{}, error)
    36  	RemoveSettings(key string) error
    37  	ListSettings(keyPrefix string) (map[string]map[string]interface{}, error)
    38  }
    39  
    40  // MemSettings is an in-memory implementation of SettingsManager.
    41  // This type does not provide any goroutine-safety.
    42  type MemSettings struct {
    43  	Settings map[string]map[string]interface{}
    44  }
    45  
    46  // CreateSettings is part of the SettingsManager interface.
    47  func (m MemSettings) CreateSettings(key string, settings map[string]interface{}) error {
    48  	if _, ok := m.Settings[key]; ok {
    49  		return errors.AlreadyExistsf("settings with key %q", key)
    50  	}
    51  	m.Settings[key] = settings
    52  	return nil
    53  }
    54  
    55  // ReplaceSettings is part of the SettingsManager interface.
    56  func (m MemSettings) ReplaceSettings(key string, settings map[string]interface{}) error {
    57  	if _, ok := m.Settings[key]; !ok {
    58  		return errors.NotFoundf("settings with key %q", key)
    59  	}
    60  	m.Settings[key] = settings
    61  	return nil
    62  }
    63  
    64  // ReadSettings is part of the SettingsManager interface.
    65  func (m MemSettings) ReadSettings(key string) (map[string]interface{}, error) {
    66  	settings, ok := m.Settings[key]
    67  	if !ok {
    68  		return nil, errors.NotFoundf("settings with key %q", key)
    69  	}
    70  	return settings, nil
    71  }
    72  
    73  // RemoveSettings is part of the SettingsManager interface.
    74  func (m MemSettings) RemoveSettings(key string) error {
    75  	if _, ok := m.Settings[key]; !ok {
    76  		return errors.NotFoundf("settings with key %q", key)
    77  	}
    78  	delete(m.Settings, key)
    79  	return nil
    80  }
    81  
    82  // ListSettings is part of the SettingsManager interface.
    83  func (m MemSettings) ListSettings(keyPrefix string) (map[string]map[string]interface{}, error) {
    84  	result := make(map[string]map[string]interface{})
    85  	for key, settings := range m.Settings {
    86  		if !strings.HasPrefix(key, keyPrefix) {
    87  			continue
    88  		}
    89  		result[key] = settings
    90  	}
    91  	return result, nil
    92  }