Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1081)

Side by Side Diff: state/statecmd/get.go

Issue 10083047: state: Service ConfigSettings methods
Patch Set: state: Service ConfigSettings methods Created 10 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « state/statecmd/deploy_test.go ('k') | state/unit_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 Canonical Ltd. 1 // Copyright 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 // The statecmd package is a temporary package 4 // The statecmd package is a temporary package
5 // to put code that's used by both cmd/juju and state/api. 5 // to put code that's used by both cmd/juju and state/api.
6 // It is intended to wither away to nothing as functionality 6 // It is intended to wither away to nothing as functionality
7 // gets absorbed into state and state/api as appropriate 7 // gets absorbed into state and state/api as appropriate
8 // when the command-line commands can invoke the 8 // when the command-line commands can invoke the
9 // API directly. 9 // API directly.
10 package statecmd 10 package statecmd
11 11
12 import ( 12 import (
13 "launchpad.net/juju-core/charm" 13 "launchpad.net/juju-core/charm"
14 "launchpad.net/juju-core/constraints" 14 "launchpad.net/juju-core/constraints"
15 "launchpad.net/juju-core/state" 15 "launchpad.net/juju-core/state"
16 "launchpad.net/juju-core/state/api/params" 16 "launchpad.net/juju-core/state/api/params"
17 ) 17 )
18 18
19 // ServiceGet returns the configuration for the named service. 19 // ServiceGet returns the configuration for the named service.
20 func ServiceGet(st *state.State, p params.ServiceGet) (params.ServiceGetResults, error) { 20 func ServiceGet(st *state.State, p params.ServiceGet) (params.ServiceGetResults, error) {
21 » svc, err := st.Service(p.ServiceName) 21 » service, err := st.Service(p.ServiceName)
22 if err != nil { 22 if err != nil {
23 return params.ServiceGetResults{}, err 23 return params.ServiceGetResults{}, err
24 } 24 }
25 » svcCfg, err := svc.Config() 25 » settings, err := service.ConfigSettings()
26 if err != nil { 26 if err != nil {
27 return params.ServiceGetResults{}, err 27 return params.ServiceGetResults{}, err
28 } 28 }
29 » charm, _, err := svc.Charm() 29 » charm, _, err := service.Charm()
30 if err != nil { 30 if err != nil {
31 return params.ServiceGetResults{}, err 31 return params.ServiceGetResults{}, err
32 } 32 }
33 » charmCfg := charm.Config().Options 33 » configInfo := describe(settings, charm.Config())
34
35 var constraints constraints.Value 34 var constraints constraints.Value
36 » if svc.IsPrincipal() { 35 » if service.IsPrincipal() {
37 » » constraints, err = svc.Constraints() 36 » » constraints, err = service.Constraints()
38 if err != nil { 37 if err != nil {
39 return params.ServiceGetResults{}, err 38 return params.ServiceGetResults{}, err
40 } 39 }
41 } 40 }
42
43 return params.ServiceGetResults{ 41 return params.ServiceGetResults{
44 Service: p.ServiceName, 42 Service: p.ServiceName,
45 Charm: charm.Meta().Name, 43 Charm: charm.Meta().Name,
46 » » Config: merge(svcCfg.Map(), charmCfg), 44 » » Config: configInfo,
47 Constraints: constraints, 45 Constraints: constraints,
48 }, nil 46 }, nil
49 } 47 }
50 48
51 // merge returns the service settings merged with the charm 49 func describe(settings charm.Settings, config *charm.Config) map[string]interfac e{} {
52 // schema, taking default values from the configuration
53 // in the charm metadata.
54 func merge(serviceCfg map[string]interface{}, charmCfg map[string]charm.Option) map[string]interface{} {
55 results := make(map[string]interface{}) 50 results := make(map[string]interface{})
56 » for k, v := range charmCfg { 51 » for name, option := range config.Options {
57 » » m := map[string]interface{}{ 52 » » info := map[string]interface{}{
58 » » » "description": v.Description, 53 » » » "description": option.Description,
59 » » » "type": v.Type, 54 » » » "type": option.Type,
60 } 55 }
61 » » if s, ok := serviceCfg[k]; ok { 56 » » if value := settings[name]; value != nil {
62 » » » m["value"] = s 57 » » » info["value"] = value
63 } else { 58 } else {
64 » » » m["value"] = v.Default 59 » » » info["value"] = option.Default
65 » » » m["default"] = true 60 » » » info["default"] = true
66 } 61 }
67 » » results[k] = m 62 » » results[name] = info
68 } 63 }
69 return results 64 return results
70 } 65 }
OLDNEW
« no previous file with comments | « state/statecmd/deploy_test.go ('k') | state/unit_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b