Skip to content

Commit

Permalink
Handle blank space tag in ListSubnets
Browse files Browse the repository at this point in the history
Subnets obtained from providers with no space discovery will have blank
space tags - the API and client need to handle that.
  • Loading branch information
babbageclunk committed Mar 3, 2017
1 parent 23862ca commit b7344ac
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
6 changes: 5 additions & 1 deletion apiserver/common/networkingcommon/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,16 @@ func ListSubnets(api NetworkBacking, args params.SubnetsFilters) (results params
)
continue
}
var spaceTag string
if subnet.SpaceName() != "" {
spaceTag = names.NewSpaceTag(subnet.SpaceName()).String()
}
result := params.Subnet{
CIDR: subnet.CIDR(),
ProviderId: string(subnet.ProviderId()),
VLANTag: subnet.VLANTag(),
Life: subnet.Life(),
SpaceTag: names.NewSpaceTag(subnet.SpaceName()).String(),
SpaceTag: spaceTag,
Zones: subnet.AvailabilityZones(),
Status: subnet.Status(),
}
Expand Down
20 changes: 20 additions & 0 deletions apiserver/common/networkingcommon/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,26 @@ func (s *SubnetsSuite) TestListSubnetsInvalidSpaceTag(c *gc.C) {
c.Assert(err, gc.ErrorMatches, `"invalid" is not a valid tag`)
}

func (s *SubnetsSuite) TestListSubnetsBlankSpaceTag(c *gc.C) {
args := params.SubnetsFilters{}
apiservertesting.BackingInstance.Subnets = []networkingcommon.BackingSubnet{
&apiservertesting.FakeSubnet{Info: networkingcommon.BackingSubnetInfo{
CIDR: "10.0.10.0/24",
ProviderId: "1",
AvailabilityZones: []string{"zone1"},
SpaceName: "",
}},
}
results, err := networkingcommon.ListSubnets(apiservertesting.BackingInstance, args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, params.ListSubnetsResults{Results: []params.Subnet{{
CIDR: "10.0.10.0/24",
ProviderId: "1",
SpaceTag: "",
Zones: []string{"zone1"},
}}})
}

func (s *SubnetsSuite) TestListSubnetsAllSubnetError(c *gc.C) {
boom := errors.New("no subnets for you")
apiservertesting.BackingInstance.SetErrors(boom)
Expand Down
22 changes: 11 additions & 11 deletions apiserver/testing/stub_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (f *FakeSpace) Subnets() (bs []networkingcommon.BackingSubnet, err error) {
AvailabilityZones: zones,
Status: status,
}
outputSubnets = append(outputSubnets, &FakeSubnet{info: backing})
outputSubnets = append(outputSubnets, &FakeSubnet{Info: backing})
}

return outputSubnets, nil
Expand Down Expand Up @@ -288,42 +288,42 @@ func (f *FakeZone) GoString() string {

// FakeSubnet implements networkingcommon.BackingSubnet for testing.
type FakeSubnet struct {
info networkingcommon.BackingSubnetInfo
Info networkingcommon.BackingSubnetInfo
}

var _ networkingcommon.BackingSubnet = (*FakeSubnet)(nil)

// GoString implements fmt.GoStringer.
func (f *FakeSubnet) GoString() string {
return fmt.Sprintf("&FakeSubnet{%#v}", f.info)
return fmt.Sprintf("&FakeSubnet{%#v}", f.Info)
}

func (f *FakeSubnet) Status() string {
return f.info.Status
return f.Info.Status
}

func (f *FakeSubnet) CIDR() string {
return f.info.CIDR
return f.Info.CIDR
}

func (f *FakeSubnet) AvailabilityZones() []string {
return f.info.AvailabilityZones
return f.Info.AvailabilityZones
}

func (f *FakeSubnet) ProviderId() network.Id {
return f.info.ProviderId
return f.Info.ProviderId
}

func (f *FakeSubnet) VLANTag() int {
return f.info.VLANTag
return f.Info.VLANTag
}

func (f *FakeSubnet) SpaceName() string {
return f.info.SpaceName
return f.Info.SpaceName
}

func (f *FakeSubnet) Life() params.Life {
return f.info.Life
return f.Info.Life
}

// ResetStub resets all recorded calls and errors of the given stub.
Expand Down Expand Up @@ -503,7 +503,7 @@ func (sb *StubBacking) AddSubnet(subnetInfo networkingcommon.BackingSubnetInfo)
if err := sb.NextErr(); err != nil {
return nil, err
}
fs := &FakeSubnet{info: subnetInfo}
fs := &FakeSubnet{Info: subnetInfo}
sb.Subnets = append(sb.Subnets, fs)
return fs, nil
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/juju/subnet/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ func (c *listCommand) Run(ctx *cmd.Context) error {
} else if ip.To16() != nil {
subResult.Type = typeIPv6
}
// Space must be valid, but verify anyway.
spaceTag, err := names.ParseSpaceTag(sub.SpaceTag)
if err != nil {
return errors.Annotatef(err, "subnet %q has invalid space", sub.CIDR)
if sub.SpaceTag != "" {
// Space must be valid, but verify anyway.
spaceTag, err := names.ParseSpaceTag(sub.SpaceTag)
if err != nil {
return errors.Annotatef(err, "subnet %q has invalid space", sub.CIDR)
}
subResult.Space = spaceTag.Id()
}
subResult.Space = spaceTag.Id()

// Display correct status according to the life cycle value.
switch sub.Life {
Expand Down
20 changes: 20 additions & 0 deletions cmd/juju/subnet/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,23 @@ func (s *ListSuite) TestRunWhenASubnetHasInvalidSpaceFails(c *gc.C) {
s.api.CheckCallNames(c, "ListSubnets", "Close")
s.api.CheckCall(c, 0, "ListSubnets", nil, "")
}

func (s *ListSuite) TestRunWhenSubnetHasBlankSpace(c *gc.C) {
s.api.Subnets = s.api.Subnets[0:1]
s.api.Subnets[0].SpaceTag = ""

expectedYAML := `
subnets:
10.20.0.0/24:
type: ipv4
provider-id: subnet-foo
status: in-use
space: ""
zones:
- zone1
- zone2
`[1:]
s.AssertRunSucceeds(c, "", expectedYAML)
s.api.CheckCallNames(c, "ListSubnets", "Close")
s.api.CheckCall(c, 0, "ListSubnets", nil, "")
}

0 comments on commit b7344ac

Please sign in to comment.