1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
"github.com/juju/errors"
13
type memStore struct {
15
envs map[string]*memInfo
24
// clone returns a copy of the given environment info, isolated
25
// from the store itself.
26
func (info *memInfo) clone() *memInfo {
27
// Note that none of the Set* methods ever set fields inside
28
// references, which makes this OK to do.
30
newAttrs := make(map[string]interface{})
31
for name, attr := range info.bootstrapConfig {
34
info1.bootstrapConfig = newAttrs
35
info1.source = sourceMem
39
// NewMem returns a ConfigStorage implementation that
40
// stores configuration in memory.
41
func NewMem() Storage {
43
envs: make(map[string]*memInfo),
47
// CreateInfo implements Storage.CreateInfo.
48
func (m *memStore) CreateInfo(envName string) EnvironInfo {
55
info.source = sourceCreated
59
// List implements Storage.List
60
func (m *memStore) List() ([]string, error) {
64
for name, env := range m.envs {
65
api := env.APIEndpoint()
66
if api.ServerUUID == "" || api.ModelUUID != "" {
67
envs = append(envs, name)
73
// ListSystems implements Storage.ListSystems
74
func (m *memStore) ListSystems() ([]string, error) {
78
for name, env := range m.envs {
79
api := env.APIEndpoint()
80
if api.ServerUUID == "" ||
81
api.ServerUUID == api.ModelUUID ||
83
servers = append(servers, name)
89
// ReadInfo implements Storage.ReadInfo.
90
func (m *memStore) ReadInfo(envName string) (EnvironInfo, error) {
93
info := m.envs[envName]
95
return info.clone(), nil
97
return nil, errors.NotFoundf("model %q", envName)
100
// Location implements EnvironInfo.Location.
101
func (info *memInfo) Location() string {
105
// Write implements EnvironInfo.Write.
106
func (info *memInfo) Write() error {
111
if !info.initialized() && m.envs[info.name] != nil {
112
return ErrEnvironInfoAlreadyExists
115
info.source = sourceMem
116
m.envs[info.name] = info.clone()
120
// Destroy implements EnvironInfo.Destroy.
121
func (info *memInfo) Destroy() error {
125
if info.initialized() {
126
if m.envs[info.name] == nil {
127
return fmt.Errorf("model info has already been removed")
129
delete(m.envs, info.name)