Skip to content

Commit 22f83b8

Browse files
committed
Add helper to verify keys against the known set.
Refs: juju-model-defaults-collapse
1 parent ecc286c commit 22f83b8

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

cmd/juju/model/defaultscommand.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ type defaultsCommandAPI interface {
6767
// Close closes the api connection.
6868
Close() error
6969

70+
// ModelConfig returns all known configuration attributes and values.
71+
ModelGet() (map[string]interface{}, error)
72+
7073
// ModelDefaults returns the default config values used when creating a new model.
7174
ModelDefaults() (config.ModelDefaultAttributes, error)
7275

@@ -115,14 +118,6 @@ func (c *defaultsCommand) Init(args []string) error {
115118
}
116119
c.keys = args
117120

118-
for _, key := range c.keys {
119-
// check if the key exists in the known config
120-
// and warn the user if the key is not defined
121-
if _, exists := config.ConfigDefaults()[key]; !exists {
122-
logger.Warningf(
123-
"key %q is not defined in the known model configuration: possible misspelling", key)
124-
}
125-
}
126121
c.action = c.resetDefaults
127122
return nil
128123
}
@@ -141,15 +136,6 @@ func (c *defaultsCommand) Init(args []string) error {
141136
c.values[k] = v
142137
}
143138

144-
for key := range c.values {
145-
// check if the key exists in the known config
146-
// and warn the user if the key is not defined
147-
if _, exists := config.ConfigDefaults()[key]; !exists {
148-
logger.Warningf(
149-
"key %q is not defined in the known model configuration: possible misspelling", key)
150-
}
151-
}
152-
153139
c.action = c.setDefaults
154140
return nil
155141

@@ -215,19 +201,51 @@ func (c *defaultsCommand) getDefaults(ctx *cmd.Context) error {
215201

216202
func (c *defaultsCommand) setDefaults(ctx *cmd.Context) error {
217203
// ctx unused in this method.
218-
204+
if err := c.verifyKnownKeys(); err != nil {
205+
return errors.Trace(err)
206+
}
219207
// TODO(wallyworld) - call with cloud and region when that bit is done
220208
return block.ProcessBlockedError(c.api.SetModelDefaults("", "", c.values), block.BlockChange)
221209
}
222210

223211
func (c *defaultsCommand) resetDefaults(ctx *cmd.Context) error {
224212
// ctx unused in this method.
225-
213+
if err := c.verifyKnownKeys(); err != nil {
214+
return errors.Trace(err)
215+
}
226216
// TODO(wallyworld) - call with cloud and region when that bit is done
227217
return block.ProcessBlockedError(c.api.UnsetModelDefaults("", "", c.keys...), block.BlockChange)
228218

229219
}
230220

221+
// verifyKnownKeys is a helper to validate the keys we are operating with
222+
// against the set of known attributes from the model.
223+
func (c *defaultsCommand) verifyKnownKeys() error {
224+
known, err := c.api.ModelGet()
225+
if err != nil {
226+
return errors.Trace(err)
227+
}
228+
keys := func() []string {
229+
if c.keys != nil {
230+
return c.keys
231+
}
232+
keys := []string{}
233+
for k, _ := range c.values {
234+
keys = append(keys, k)
235+
}
236+
return keys
237+
}
238+
for _, key := range keys() {
239+
// check if the key exists in the known config
240+
// and warn the user if the key is not defined
241+
if _, exists := known[key]; !exists {
242+
logger.Warningf(
243+
"key %q is not defined in the known model configuration: possible misspelling", key)
244+
}
245+
}
246+
return nil
247+
}
248+
231249
// formatConfigTabular writes a tabular summary of default config information.
232250
func formatDefaultConfigTabular(writer io.Writer, value interface{}) error {
233251
defaultValues, ok := value.(config.ModelDefaultAttributes)

cmd/juju/model/defaultscommand_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ func (s *DefaultsCommandSuite) TestDefaultsInit(c *gc.C) {
8484
}
8585
}
8686

87-
func (s *DefaultsCommandSuite) TestInitResetUnknownValueLogs(c *gc.C) {
88-
cmd := model.NewDefaultsCommandForTest(s.fake)
89-
err := testing.InitCommand(cmd, []string{"--reset", "attr", "weird"})
87+
func (s *DefaultsCommandSuite) TestResetUnknownValueLogs(c *gc.C) {
88+
_, err := s.run(c, "--reset", "attr", "weird")
9089
c.Assert(err, jc.ErrorIsNil)
9190
expected := `key "weird" is not defined in the known model configuration: possible misspelling`
9291
c.Check(c.GetTestLog(), jc.Contains, expected)
@@ -111,9 +110,8 @@ func (s *DefaultsCommandSuite) TestResetBlockedError(c *gc.C) {
111110
c.Check(c.GetTestLog(), jc.Contains, "TestBlockedError")
112111
}
113112

114-
func (s *DefaultsCommandSuite) TestInitSetUnknownValueLogs(c *gc.C) {
115-
cmd := model.NewDefaultsCommandForTest(s.fake)
116-
err := testing.InitCommand(cmd, []string{"weird=foo"})
113+
func (s *DefaultsCommandSuite) TestSetUnknownValueLogs(c *gc.C) {
114+
_, err := s.run(c, "weird=foo")
117115
c.Assert(err, jc.ErrorIsNil)
118116
expected := `key "weird" is not defined in the known model configuration: possible misspelling`
119117
c.Check(c.GetTestLog(), jc.Contains, expected)

0 commit comments

Comments
 (0)