Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 // The statecmd package is a temporary package | |
2 // to put code that's used by both cmd/juju and state/api. | |
3 // It is intended to wither away to nothing as functionality | |
4 // gets absorbed into state and state/api as appropriate | |
5 // when the command-line commands can invoking the | |
fwereade
2013/02/21 21:07:17
s/invoking/invoke/
bac
2013/02/21 21:15:43
Done.
| |
6 // API directly. | |
7 package statecmd | |
8 | |
9 import ( | |
10 "reflect" | |
11 | |
12 "launchpad.net/juju-core/charm" | |
13 "launchpad.net/juju-core/state" | |
14 ) | |
15 | |
16 // Parameters for making the GetConfig call. | |
fwereade
2013/02/21 21:07:17
ServiceGet?
bac
2013/02/21 21:15:43
Done.
| |
17 type ServiceGetParams struct { | |
18 ServiceName string | |
19 } | |
20 | |
21 // Return struct for ServiceGet call. | |
22 type ServiceGetResults struct { | |
23 Service string | |
24 Charm string | |
25 Settings map[string]interface{} | |
26 } | |
27 | |
28 // ServiceGet returns the configuration for the named service. | |
29 func ServiceGet(st *state.State, p ServiceGetParams) (*ServiceGetResults, error) { | |
30 svc, err := st.Service(p.ServiceName) | |
31 if err != nil { | |
32 return nil, err | |
33 } | |
34 svcfg, err := svc.Config() | |
35 if err != nil { | |
36 return nil, err | |
37 } | |
38 charm, _, err := svc.Charm() | |
39 if err != nil { | |
40 return nil, err | |
41 } | |
42 chcfg := charm.Config().Options | |
43 | |
44 return &ServiceGetResults{ | |
45 Service: p.ServiceName, | |
46 Charm: charm.Meta().Name, | |
47 Settings: merge(svcfg.Map(), chcfg), | |
48 }, nil | |
49 } | |
50 | |
51 // Merge service settings and charm schema. | |
52 func merge(serviceCfg map[string]interface{}, charmCfg map[string]charm.Option) map[string]interface{} { | |
53 results := make(map[string]interface{}) | |
54 for k, v := range charmCfg { | |
55 m := map[string]interface{}{ | |
56 "description": v.Description, | |
57 "type": v.Type, | |
58 } | |
59 s, ok := serviceCfg[k] | |
60 if ok { | |
61 m["value"] = s | |
62 } else { | |
63 // Breaks compatibility with py/juju. | |
64 m["value"] = nil | |
65 } | |
66 if v.Default != nil { | |
67 if reflect.DeepEqual(v.Default, s) { | |
68 m["default"] = true | |
69 } | |
70 } | |
71 results[k] = m | |
72 } | |
73 return results | |
74 } | |
OLD | NEW |