Skip to content

Commit

Permalink
Merge branch 'upstream/3.3' into 3.3-into-3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
manadart committed Mar 12, 2024
2 parents 6c47542 + 56b3cc7 commit bbb0d5c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 12 additions & 1 deletion core/network/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (pas ProviderAddresses) Values() []string {
}

// ToSpaceAddresses transforms the ProviderAddresses to SpaceAddresses by using
// the input lookup for conversion of space name to space ID.
// the input lookup to get a space ID from the name or the CIDR.
func (pas ProviderAddresses) ToSpaceAddresses(lookup SpaceLookup) (SpaceAddresses, error) {
if pas == nil {
return nil, nil
Expand All @@ -532,13 +532,24 @@ func (pas ProviderAddresses) ToSpaceAddresses(lookup SpaceLookup) (SpaceAddresse
sas := make(SpaceAddresses, len(pas))
for i, pa := range pas {
sas[i] = SpaceAddress{MachineAddress: pa.MachineAddress}

// If the provider explicitly sets the space, i.e. MAAS, prefer the name.
if pa.SpaceName != "" {
info := spaceInfos.GetByName(string(pa.SpaceName))
if info == nil {
return nil, errors.NotFoundf("space with name %q", pa.SpaceName)
}
sas[i].SpaceID = info.ID
continue
}

// Otherwise attempt to look up the CIDR.
sInfo, err := spaceInfos.InferSpaceFromCIDRAndSubnetID(pa.CIDR, string(pa.ProviderSubnetID))
if err != nil {
logger.Debugf("no matching subnet for CIDR %q and provider ID %q", pa.CIDR, pa.ProviderSubnetID)
continue
}
sas[i].SpaceID = sInfo.ID
}
return sas, nil
}
Expand Down
28 changes: 27 additions & 1 deletion core/network/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,21 @@ func (s stubLookup) AllSpaceInfos() (network.SpaceInfos, error) {
return network.SpaceInfos{
{ID: "1", Name: "space-one", ProviderId: "p1"},
{ID: "2", Name: "space-two"},
{
ID: "6",
Name: "space-six",
Subnets: network.SubnetInfos{
{
ID: "666",
CIDR: "10.0.0.0/24",
ProviderId: "61",
},
},
},
}, nil
}

func (s *AddressSuite) TestProviderAddressesToSpaceAddresses(c *gc.C) {
func (s *AddressSuite) TestProviderAddressesToSpaceAddressesByName(c *gc.C) {
// Check success.
addrs := network.ProviderAddresses{
network.NewMachineAddress("1.2.3.4").AsProviderAddress(network.WithSpaceName("space-one")),
Expand All @@ -905,6 +916,21 @@ func (s *AddressSuite) TestProviderAddressesToSpaceAddresses(c *gc.C) {
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}

func (s *AddressSuite) TestProviderAddressesToSpaceAddressesBySubnet(c *gc.C) {
// Check success.
addrs := network.ProviderAddresses{
network.NewMachineAddress(
"10.0.0.6",
network.WithCIDR("10.0.0.0/24"),
).AsProviderAddress(network.WithProviderSubnetID("61")),
}

res, err := addrs.ToSpaceAddresses(stubLookup{})
c.Assert(err, jc.ErrorIsNil)
c.Assert(res, gc.HasLen, 1)
c.Check(res[0].SpaceID, gc.Equals, "6")
}

func (s *AddressSuite) TestSpaceAddressesToProviderAddresses(c *gc.C) {
// Check success.
addrs := network.NewSpaceAddresses("1.2.3.4", "2.3.4.5", "3.4.5.6")
Expand Down

0 comments on commit bbb0d5c

Please sign in to comment.