~juju-qa/juju-core/1.16-packaging

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/worker/localstorage/config.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-10-10 18:07:45 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20131010180745-wuo0vv7hq7faavdk
Tags: 1.16.0-0ubuntu1
New upstream stable release (LP: #1219879).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package localstorage
 
5
 
 
6
import (
 
7
        "launchpad.net/goyaml"
 
8
 
 
9
        "launchpad.net/juju-core/agent"
 
10
)
 
11
 
 
12
const (
 
13
        // TODO(axw) 2013-09-25 bug #1230131
 
14
        // Move these variables out of agent when we can do upgrades in
 
15
        // the right place. In this case, the local provider should do
 
16
        // the envvar-to-agent.conf migration.
 
17
        StorageDir        = agent.StorageDir
 
18
        StorageAddr       = agent.StorageAddr
 
19
        SharedStorageDir  = agent.SharedStorageDir
 
20
        SharedStorageAddr = agent.SharedStorageAddr
 
21
        StorageCACert     = "StorageCACert"
 
22
        StorageCAKey      = "StorageCAKey"
 
23
        StorageHostnames  = "StorageHostnames"
 
24
        StorageAuthKey    = "StorageAuthKey"
 
25
)
 
26
 
 
27
// LocalStorageConfig is an interface that, if implemented, may be used
 
28
// to configure a machine agent for use with the localstorage worker in
 
29
// this package.
 
30
type LocalStorageConfig interface {
 
31
        StorageDir() string
 
32
        StorageAddr() string
 
33
        SharedStorageDir() string
 
34
        SharedStorageAddr() string
 
35
}
 
36
 
 
37
// LocalTLSStorageConfig is an interface that extends LocalStorageConfig
 
38
// to support serving storage over TLS.
 
39
type LocalTLSStorageConfig interface {
 
40
        LocalStorageConfig
 
41
 
 
42
        // StorageCACert is the CA certificate in PEM format.
 
43
        StorageCACert() []byte
 
44
 
 
45
        // StorageCAKey is the CA private key in PEM format.
 
46
        StorageCAKey() []byte
 
47
 
 
48
        // StorageHostnames is the set of hostnames that will
 
49
        // be assigned to the storage server's certificate.
 
50
        StorageHostnames() []string
 
51
 
 
52
        // StorageAuthKey is the key that clients must present
 
53
        // to perform modifying operations.
 
54
        StorageAuthKey() string
 
55
}
 
56
 
 
57
type config struct {
 
58
        storageDir        string
 
59
        storageAddr       string
 
60
        sharedStorageDir  string
 
61
        sharedStorageAddr string
 
62
        caCertPEM         []byte
 
63
        caKeyPEM          []byte
 
64
        hostnames         []string
 
65
        authkey           string
 
66
}
 
67
 
 
68
// StoreConfig takes a LocalStorageConfig (or derivative interface),
 
69
// and stores it in a map[string]string suitable for updating an
 
70
// agent.Config's key/value map.
 
71
func StoreConfig(storageConfig LocalStorageConfig) (map[string]string, error) {
 
72
        kv := make(map[string]string)
 
73
        kv[StorageDir] = storageConfig.StorageDir()
 
74
        kv[StorageAddr] = storageConfig.StorageAddr()
 
75
        kv[SharedStorageDir] = storageConfig.SharedStorageDir()
 
76
        kv[SharedStorageAddr] = storageConfig.SharedStorageAddr()
 
77
        if tlsConfig, ok := storageConfig.(LocalTLSStorageConfig); ok {
 
78
                if authkey := tlsConfig.StorageAuthKey(); authkey != "" {
 
79
                        kv[StorageAuthKey] = authkey
 
80
                }
 
81
                if cert := tlsConfig.StorageCACert(); cert != nil {
 
82
                        kv[StorageCACert] = string(cert)
 
83
                }
 
84
                if key := tlsConfig.StorageCAKey(); key != nil {
 
85
                        kv[StorageCAKey] = string(key)
 
86
                }
 
87
                if hostnames := tlsConfig.StorageHostnames(); len(hostnames) > 0 {
 
88
                        data, err := goyaml.Marshal(hostnames)
 
89
                        if err != nil {
 
90
                                return nil, err
 
91
                        }
 
92
                        kv[StorageHostnames] = string(data)
 
93
                }
 
94
        }
 
95
        return kv, nil
 
96
}
 
97
 
 
98
func loadConfig(agentConfig agent.Config) (*config, error) {
 
99
        config := &config{
 
100
                storageDir:        agentConfig.Value(StorageDir),
 
101
                storageAddr:       agentConfig.Value(StorageAddr),
 
102
                sharedStorageDir:  agentConfig.Value(SharedStorageDir),
 
103
                sharedStorageAddr: agentConfig.Value(SharedStorageAddr),
 
104
                authkey:           agentConfig.Value(StorageAuthKey),
 
105
        }
 
106
 
 
107
        caCertPEM := agentConfig.Value(StorageCACert)
 
108
        if len(caCertPEM) > 0 {
 
109
                config.caCertPEM = []byte(caCertPEM)
 
110
        }
 
111
 
 
112
        caKeyPEM := agentConfig.Value(StorageCAKey)
 
113
        if len(caKeyPEM) > 0 {
 
114
                config.caKeyPEM = []byte(caKeyPEM)
 
115
        }
 
116
 
 
117
        hostnames := agentConfig.Value(StorageHostnames)
 
118
        if len(hostnames) > 0 {
 
119
                err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames)
 
120
                if err != nil {
 
121
                        return nil, err
 
122
                }
 
123
        }
 
124
 
 
125
        return config, nil
 
126
}