@@ -67,6 +67,9 @@ type defaultsCommandAPI interface {
67
67
// Close closes the api connection.
68
68
Close () error
69
69
70
+ // ModelConfig returns all known configuration attributes and values.
71
+ ModelGet () (map [string ]interface {}, error )
72
+
70
73
// ModelDefaults returns the default config values used when creating a new model.
71
74
ModelDefaults () (config.ModelDefaultAttributes , error )
72
75
@@ -115,14 +118,6 @@ func (c *defaultsCommand) Init(args []string) error {
115
118
}
116
119
c .keys = args
117
120
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
- }
126
121
c .action = c .resetDefaults
127
122
return nil
128
123
}
@@ -141,15 +136,6 @@ func (c *defaultsCommand) Init(args []string) error {
141
136
c .values [k ] = v
142
137
}
143
138
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
-
153
139
c .action = c .setDefaults
154
140
return nil
155
141
@@ -215,19 +201,51 @@ func (c *defaultsCommand) getDefaults(ctx *cmd.Context) error {
215
201
216
202
func (c * defaultsCommand ) setDefaults (ctx * cmd.Context ) error {
217
203
// ctx unused in this method.
218
-
204
+ if err := c .verifyKnownKeys (); err != nil {
205
+ return errors .Trace (err )
206
+ }
219
207
// TODO(wallyworld) - call with cloud and region when that bit is done
220
208
return block .ProcessBlockedError (c .api .SetModelDefaults ("" , "" , c .values ), block .BlockChange )
221
209
}
222
210
223
211
func (c * defaultsCommand ) resetDefaults (ctx * cmd.Context ) error {
224
212
// ctx unused in this method.
225
-
213
+ if err := c .verifyKnownKeys (); err != nil {
214
+ return errors .Trace (err )
215
+ }
226
216
// TODO(wallyworld) - call with cloud and region when that bit is done
227
217
return block .ProcessBlockedError (c .api .UnsetModelDefaults ("" , "" , c .keys ... ), block .BlockChange )
228
218
229
219
}
230
220
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
+
231
249
// formatConfigTabular writes a tabular summary of default config information.
232
250
func formatDefaultConfigTabular (writer io.Writer , value interface {}) error {
233
251
defaultValues , ok := value .(config.ModelDefaultAttributes )
0 commit comments