Skip to content

Commit 5825c4f

Browse files
committed
juju model-defaults supports region for get
1 parent 12e978b commit 5825c4f

File tree

4 files changed

+87
-39
lines changed

4 files changed

+87
-39
lines changed

cmd/juju/model/defaultscommand.go

+37-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package model
44

55
import (
66
"bytes"
7+
"fmt"
78
"io"
89
"sort"
910
"strings"
@@ -38,6 +39,8 @@ By default, the model is the current model.
3839
Examples:
3940
juju model-defaults
4041
juju model-defaults http-proxy
42+
juju model-defaults aws/us-east-1 http-proxy
43+
juju model-defaults us-east-1 http-proxy
4144
juju model-defaults -m mymodel type
4245
juju model-defaults ftp-proxy=10.0.0.1:8000
4346
juju model-defaults aws/us-east-1 ftp-proxy=10.0.0.1:8000
@@ -214,8 +217,8 @@ func (c *defaultsCommand) Init(args []string) error {
214217
// set args.
215218
return c.handleSetArgs(args)
216219
case len(args) == 0:
217-
// This should be reset only (but possibly for a region).
218-
return c.handleZeroArgs()
220+
c.action = c.getDefaults
221+
return nil
219222
case len(args) == 1:
220223
// We want to get settings for the provided key.
221224
return c.handleOneArg(args[0])
@@ -386,39 +389,19 @@ func (c *defaultsCommand) parseSetKeys(args []string) error {
386389
return nil
387390
}
388391

389-
// handleZeroArgs determines whether we are doing a reset only and if any
390-
// additional arguments are valid -- or why they are not.
391-
func (c *defaultsCommand) handleZeroArgs() error {
392-
if c.regionName != "" {
393-
if c.resetKeys == nil {
394-
// It doesn't make sense to specify a region unless we are resetting
395-
// values here.
396-
return errors.New("specifying a region when retrieving defaults is invalid")
397-
}
398-
}
399-
// We can reset for a region. We can also reset for a controller if there
400-
// is no region specified. In either case c.action remains nil. We short
401-
// circuited at the begining of c.Init so we should never reach a case
402-
// where there is no region or reset keys specified.
403-
return nil
404-
}
405-
406392
// handleOneArg handles the case where we have one positional arg after
407393
// processing for a region and the reset flag.
408394
func (c *defaultsCommand) handleOneArg(arg string) error {
409395
resetSpecified := c.resetKeys != nil
410396
regionSpecified := c.regionName != ""
411397

412398
if regionSpecified {
413-
if !resetSpecified {
414-
// It doesn't make sense to specify a region unless we are resetting
415-
// values here.
416-
return errors.New("specifying a region when retrieving defaults for a setting is invalid")
399+
if resetSpecified {
400+
// If a region was specified and reset was specified, we shouldn't have
401+
// an extra arg. If it had an "=" in it, we should have handled it
402+
// already.
403+
return errors.New("cannot retrieve defaults for a region and reset attributes at the same time")
417404
}
418-
// If a region was specified and reset was specified, we shouldn't have
419-
// an extra arg. If it had an "=" in it, we should have handled it
420-
// already.
421-
return errors.New("cannot retrieve defaults for a key and reset args at the same time")
422405
}
423406
if resetSpecified {
424407
// It makes no sense to supply a positional arg that isn't a region if
@@ -501,13 +484,39 @@ func (c *defaultsCommand) getDefaults(client defaultsCommandAPI, ctx *cmd.Contex
501484
return err
502485
}
503486

487+
valueForRegion := func(region string, regions []config.RegionDefaultValue) (config.RegionDefaultValue, bool) {
488+
for _, r := range regions {
489+
if r.Name == region {
490+
return r, true
491+
}
492+
}
493+
return config.RegionDefaultValue{}, false
494+
}
495+
496+
// Filter by region if necessary.
497+
if c.regionName != "" {
498+
for attrName, attr := range attrs {
499+
if regionDefault, ok := valueForRegion(c.regionName, attr.Regions); !ok {
500+
delete(attrs, attrName)
501+
} else {
502+
attrForRegion := attr
503+
attrForRegion.Regions = []config.RegionDefaultValue{regionDefault}
504+
attrs[attrName] = attrForRegion
505+
}
506+
}
507+
}
508+
504509
if c.key != "" {
505510
if value, ok := attrs[c.key]; ok {
506511
attrs = config.ModelDefaultAttributes{
507512
c.key: value,
508513
}
509514
} else {
510-
return errors.Errorf("key %q not found in %q model defaults.", c.key, attrs["name"])
515+
msg := fmt.Sprintf("there are no default model values for %q", c.key)
516+
if c.regionName != "" {
517+
msg += fmt.Sprintf(" in region %q", c.regionName)
518+
}
519+
return errors.New(msg)
511520
}
512521
}
513522
// If c.keys is empty, write out the whole lot.

cmd/juju/model/defaultscommand_test.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (s *DefaultsCommandSuite) TestDefaultsInit(c *gc.C) {
7474
{
7575
description: "test reset with valid region and extra positional arg",
7676
args: []string{"--reset", "something", "dummy-region", "weird"},
77-
errorMatch: "cannot retrieve defaults for a key and reset args at the same time",
77+
errorMatch: "cannot retrieve defaults for a region and reset attributes at the same time",
7878
}, {
7979
description: "test reset with valid region only",
8080
args: []string{"--reset", "foo", "dummy-region"},
@@ -151,11 +151,11 @@ func (s *DefaultsCommandSuite) TestDefaultsInit(c *gc.C) {
151151
}, {
152152
description: "test valid region and one arg",
153153
args: []string{"dummy-region", "one"},
154-
errorMatch: "specifying a region when retrieving defaults for a setting is invalid",
154+
nilErr: true,
155155
}, {
156156
description: "test valid region and no args",
157157
args: []string{"dummy-region"},
158-
errorMatch: "specifying a region when retrieving defaults is invalid",
158+
nilErr: true,
159159
}, {
160160
// test cloud/region
161161
description: "test invalid cloud fails",
@@ -242,6 +242,9 @@ func (s *DefaultsCommandSuite) TestResetAttr(c *gc.C) {
242242
"attr2": {Controller: "bar", Default: nil, Regions: []config.RegionDefaultValue{{
243243
Name: "dummy-region",
244244
Value: "dummy-value",
245+
}, {
246+
Name: "another-region",
247+
Value: "another-value",
245248
}}},
246249
})
247250
}
@@ -267,6 +270,9 @@ func (s *DefaultsCommandSuite) TestSet(c *gc.C) {
267270
"attr2": {Controller: "bar", Default: nil, Regions: []config.RegionDefaultValue{{
268271
Name: "dummy-region",
269272
Value: "dummy-value",
273+
}, {
274+
Name: "another-region",
275+
Value: "another-value",
270276
}}},
271277
"special": {Controller: "extra", Default: nil, Regions: nil},
272278
})
@@ -307,9 +313,10 @@ func (s *DefaultsCommandSuite) TestGetSingleValue(c *gc.C) {
307313

308314
output := strings.TrimSpace(testing.Stdout(context))
309315
expected := "" +
310-
"Attribute Default Controller\n" +
311-
"attr2 - bar\n" +
312-
" dummy-region dummy-value -"
316+
"Attribute Default Controller\n" +
317+
"attr2 - bar\n" +
318+
" dummy-region dummy-value -\n" +
319+
" another-region another-value -"
313320
c.Assert(output, gc.Equals, expected)
314321
}
315322

@@ -319,7 +326,7 @@ func (s *DefaultsCommandSuite) TestGetSingleValueJSON(c *gc.C) {
319326

320327
output := strings.TrimSpace(testing.Stdout(context))
321328
c.Assert(output, gc.Equals,
322-
`{"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"}]}}`)
329+
`{"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"},{"name":"another-region","value":"another-value"}]}}`)
323330
}
324331

325332
func (s *DefaultsCommandSuite) TestGetAllValuesYAML(c *gc.C) {
@@ -334,7 +341,9 @@ func (s *DefaultsCommandSuite) TestGetAllValuesYAML(c *gc.C) {
334341
" controller: bar\n" +
335342
" regions:\n" +
336343
" - name: dummy-region\n" +
337-
" value: dummy-value"
344+
" value: dummy-value\n" +
345+
" - name: another-region\n" +
346+
" value: another-value"
338347
c.Assert(output, gc.Equals, expected)
339348
}
340349

@@ -343,18 +352,31 @@ func (s *DefaultsCommandSuite) TestGetAllValuesJSON(c *gc.C) {
343352
c.Assert(err, jc.ErrorIsNil)
344353

345354
output := strings.TrimSpace(testing.Stdout(context))
346-
expected := `{"attr":{"default":"foo"},"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"}]}}`
355+
expected := `{"attr":{"default":"foo"},"attr2":{"controller":"bar","regions":[{"name":"dummy-region","value":"dummy-value"},{"name":"another-region","value":"another-value"}]}}`
347356
c.Assert(output, gc.Equals, expected)
348357
}
349358

350359
func (s *DefaultsCommandSuite) TestGetAllValuesTabular(c *gc.C) {
351360
context, err := s.run(c)
352361
c.Assert(err, jc.ErrorIsNil)
353362

363+
output := strings.TrimSpace(testing.Stdout(context))
364+
expected := "" +
365+
"Attribute Default Controller\n" +
366+
"attr foo -\n" +
367+
"attr2 - bar\n" +
368+
" dummy-region dummy-value -\n" +
369+
" another-region another-value -"
370+
c.Assert(output, gc.Equals, expected)
371+
}
372+
373+
func (s *DefaultsCommandSuite) TestGetRegionValuesTabular(c *gc.C) {
374+
context, err := s.run(c, "dummy-region")
375+
c.Assert(err, jc.ErrorIsNil)
376+
354377
output := strings.TrimSpace(testing.Stdout(context))
355378
expected := "" +
356379
"Attribute Default Controller\n" +
357-
"attr foo -\n" +
358380
"attr2 - bar\n" +
359381
" dummy-region dummy-value -"
360382
c.Assert(output, gc.Equals, expected)

cmd/juju/model/fakeenv_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ func (s *fakeModelDefaultEnvSuite) SetUpTest(c *gc.C) {
9797
Controller: "bar",
9898
Regions: []config.RegionDefaultValue{{
9999
"dummy-region",
100-
"dummy-value"}}},
100+
"dummy-value",
101+
}, {
102+
"another-region",
103+
"another-value",
104+
}}},
101105
},
102106
}
103107
s.fakeCloudAPI = &fakeCloudAPI{

featuretests/cmd_juju_model_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ special - known
141141
`[1:])
142142
}
143143

144+
func (s *cmdModelSuite) TestModelDefaultsGetRegion(c *gc.C) {
145+
err := s.State.UpdateModelConfigDefaultValues(map[string]interface{}{"special": "known"}, nil, &environs.RegionSpec{"dummy", "dummy-region"})
146+
c.Assert(err, jc.ErrorIsNil)
147+
148+
context := s.run(c, "model-defaults", "dummy-region", "special")
149+
c.Assert(testing.Stdout(context), gc.Equals, `
150+
Attribute Default Controller
151+
special - -
152+
dummy-region known -
153+
154+
`[1:])
155+
}
156+
144157
func (s *cmdModelSuite) TestModelDefaultsSet(c *gc.C) {
145158
s.run(c, "model-defaults", "special=known")
146159
defaults, err := s.State.ModelConfigDefaultValues()

0 commit comments

Comments
 (0)