github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 "github.com/juju/juju/storage" 11 ) 12 13 // A PoolManager provides access to storage pools. 14 type PoolManager interface { 15 // Create makes a new pool with the specified configuration and persists it to state. 16 Create(name string, providerType storage.ProviderType, attrs map[string]interface{}) (*storage.Config, error) 17 18 // Delete removes the pool with name from state. 19 Delete(name string) error 20 21 // Get returns the pool with name from state. 22 Get(name string) (*storage.Config, error) 23 24 // List returns all the pools from state. 25 List() ([]*storage.Config, error) 26 } 27 28 type SettingsManager interface { 29 CreateSettings(key string, settings map[string]interface{}) error 30 ReadSettings(key string) (map[string]interface{}, error) 31 RemoveSettings(key string) error 32 ListSettings(keyPrefix string) (map[string]map[string]interface{}, error) 33 } 34 35 // MemSettings is an in-memory implementation of SettingsManager. 36 // This type does not provide any goroutine-safety. 37 type MemSettings struct { 38 Settings map[string]map[string]interface{} 39 } 40 41 // CreateSettings is part of the SettingsManager interface. 42 func (m MemSettings) CreateSettings(key string, settings map[string]interface{}) error { 43 if _, ok := m.Settings[key]; ok { 44 return errors.AlreadyExistsf("settings with key %q", key) 45 } 46 m.Settings[key] = settings 47 return nil 48 } 49 50 // ReadSettings is part of the SettingsManager interface. 51 func (m MemSettings) ReadSettings(key string) (map[string]interface{}, error) { 52 settings, ok := m.Settings[key] 53 if !ok { 54 return nil, errors.NotFoundf("settings with key %q", key) 55 } 56 return settings, nil 57 } 58 59 // RemoveSettings is part of the SettingsManager interface. 60 func (m MemSettings) RemoveSettings(key string) error { 61 if _, ok := m.Settings[key]; !ok { 62 return errors.NotFoundf("settings with key %q", key) 63 } 64 delete(m.Settings, key) 65 return nil 66 } 67 68 // ListSettings is part of the SettingsManager interface. 69 func (m MemSettings) ListSettings(keyPrefix string) (map[string]map[string]interface{}, error) { 70 result := make(map[string]map[string]interface{}) 71 for key, settings := range m.Settings { 72 if !strings.HasPrefix(key, keyPrefix) { 73 continue 74 } 75 result[key] = settings 76 } 77 return result, nil 78 }