Skip to content

Commit

Permalink
Integrated instance addresses and networks
Browse files Browse the repository at this point in the history
Moved several items from the instance package
to the network/ package, renamed some
of them, and propagated the changes across:
 - instance.Address -> network.Address
 - instance.AddressType -> network.AddressType
 - instance.HostName -> network.HostName
 - instance.Ipv4Address -> network.IPv4Address
 - instance.Ipv6Address -> network.IPv6Address
 - instance.NetworkScope -> network.Scope
 - instance.Address.NetworkScope -> n.Address.Scope
 - instance.Network* -> network.Scope*
 - instance.NewAddress(es) -> network.NewAddress(es)
 - instance.HostPort -> network.HostPort
 - instance.Port -> network.Port
 - instance.Select*() -> network.Select*()
 - instance.SortPorts -> network.SortPorts

(I might have missed some, but you get the idea).

Also, some refactoring was done to split addresses and
ports implementation in separate files and test suites.

No other logic changes introduced - the rest is just
mechanically applying the changes above.

Moved environs/network to network/ and updated imports,
as suggested on the review.
  • Loading branch information
Dimiter Naydenov committed Jun 10, 2014
1 parent 732aecd commit 57bad2b
Show file tree
Hide file tree
Showing 142 changed files with 1,624 additions and 1,543 deletions.
8 changes: 4 additions & 4 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/juju/loggo"
"github.com/juju/utils"

"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state/api/params"
Expand Down Expand Up @@ -146,7 +146,7 @@ type ConfigSetterOnly interface {
SetUpgradedToVersion(newVersion version.Number)

// SetAPIHostPorts sets the API host/port addresses to connect to.
SetAPIHostPorts(servers [][]instance.HostPort)
SetAPIHostPorts(servers [][]network.HostPort)

// Migrate takes an existing agent config and applies the given
// parameters to change it.
Expand Down Expand Up @@ -434,13 +434,13 @@ func (c *configInternal) SetUpgradedToVersion(newVersion version.Number) {
c.upgradedToVersion = newVersion
}

func (c *configInternal) SetAPIHostPorts(servers [][]instance.HostPort) {
func (c *configInternal) SetAPIHostPorts(servers [][]network.HostPort) {
if c.apiDetails == nil {
return
}
var addrs []string
for _, serverHostPorts := range servers {
addr := instance.SelectInternalHostPort(serverHostPorts, false)
addr := network.SelectInternalHostPort(serverHostPorts, false)
if addr != "" {
addrs = append(addrs, addr)
}
Expand Down
28 changes: 14 additions & 14 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
gc "launchpad.net/gocheck"

"github.com/juju/juju/agent"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state/api/params"
Expand Down Expand Up @@ -546,19 +546,19 @@ func (*suite) TestSetAPIHostPorts(c *gc.C) {
//
// If a server has only machine-local addresses, or none
// at all, then it will be excluded.
server1 := instance.NewAddresses("0.1.2.3", "0.1.2.4", "zeroonetwothree")
server1[0].NetworkScope = instance.NetworkCloudLocal
server1[1].NetworkScope = instance.NetworkCloudLocal
server1[2].NetworkScope = instance.NetworkPublic
server2 := instance.NewAddresses("127.0.0.1")
server2[0].NetworkScope = instance.NetworkMachineLocal
server3 := instance.NewAddresses("0.1.2.5", "zeroonetwofive")
server3[0].NetworkScope = instance.NetworkUnknown
server3[1].NetworkScope = instance.NetworkUnknown
conf.SetAPIHostPorts([][]instance.HostPort{
instance.AddressesWithPort(server1, 123),
instance.AddressesWithPort(server2, 124),
instance.AddressesWithPort(server3, 125),
server1 := network.NewAddresses("0.1.2.3", "0.1.2.4", "zeroonetwothree")
server1[0].Scope = network.ScopeCloudLocal
server1[1].Scope = network.ScopeCloudLocal
server1[2].Scope = network.ScopePublic
server2 := network.NewAddresses("127.0.0.1")
server2[0].Scope = network.ScopeMachineLocal
server3 := network.NewAddresses("0.1.2.5", "zeroonetwofive")
server3[0].Scope = network.ScopeUnknown
server3[1].Scope = network.ScopeUnknown
conf.SetAPIHostPorts([][]network.HostPort{
network.AddressesWithPort(server1, 123),
network.AddressesWithPort(server2, 124),
network.AddressesWithPort(server3, 125),
})
addrs, err = conf.APIAddresses()
c.Assert(err, gc.IsNil)
Expand Down
9 changes: 5 additions & 4 deletions agent/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/version"
Expand All @@ -38,7 +39,7 @@ type StateInitializer interface {
// to attach to the bootstrap machine.
type BootstrapMachineConfig struct {
// Addresses holds the bootstrap machine's addresses.
Addresses []instance.Address
Addresses []network.Address

// Constraints holds the bootstrap machine's constraints.
// This value is also used for the environment-level constraints.
Expand Down Expand Up @@ -194,7 +195,7 @@ func initBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineC
}

// initAPIHostPorts sets the initial API host/port addresses in state.
func initAPIHostPorts(c ConfigSetter, st *state.State, addrs []instance.Address, apiPort int) error {
hostPorts := instance.AddressesWithPort(addrs, apiPort)
return st.SetAPIHostPorts([][]instance.HostPort{hostPorts})
func initAPIHostPorts(c ConfigSetter, st *state.State, addrs []network.Address, apiPort int) error {
hostPorts := network.AddressesWithPort(addrs, apiPort)
return st.SetAPIHostPorts([][]network.HostPort{hostPorts})
}
7 changes: 4 additions & 3 deletions agent/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/provider/dummy"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
expectConstraints := constraints.MustParse("mem=1024M")
expectHW := instance.MustParseHardware("mem=2048M")
mcfg := agent.BootstrapMachineConfig{
Addresses: instance.NewAddresses("0.1.2.3", "zeroonetwothree"),
Addresses: network.NewAddresses("0.1.2.3", "zeroonetwothree"),
Constraints: expectConstraints,
Jobs: []params.MachineJob{params.JobHostUnits},
InstanceId: "i-bootstrap",
Expand Down Expand Up @@ -126,8 +127,8 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {
// Check that the API host ports are initialised correctly.
apiHostPorts, err := st.APIHostPorts()
c.Assert(err, gc.IsNil)
c.Assert(apiHostPorts, gc.DeepEquals, [][]instance.HostPort{
instance.AddressesWithPort(mcfg.Addresses, 1234),
c.Assert(apiHostPorts, gc.DeepEquals, [][]network.HostPort{
network.AddressesWithPort(mcfg.Addresses, 1234),
})

// Check that the state serving info is initialised correctly.
Expand Down
12 changes: 6 additions & 6 deletions agent/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/juju/utils/apt"
"labix.org/v2/mgo"

"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/replicaset"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/upstart"
Expand Down Expand Up @@ -57,7 +57,7 @@ var (
// WithAddresses represents an entity that has a set of
// addresses. e.g. a state Machine object
type WithAddresses interface {
Addresses() []instance.Address
Addresses() []network.Address
}

// IsMaster returns a boolean that represents whether the given
Expand Down Expand Up @@ -89,14 +89,14 @@ func IsMaster(session *mgo.Session, obj WithAddresses) (bool, error) {

// SelectPeerAddress returns the address to use as the
// mongo replica set peer address by selecting it from the given addresses.
func SelectPeerAddress(addrs []instance.Address) string {
return instance.SelectInternalAddress(addrs, false)
func SelectPeerAddress(addrs []network.Address) string {
return network.SelectInternalAddress(addrs, false)
}

// SelectPeerHostPort returns the HostPort to use as the
// mongo replica set peer by selecting it from the given hostPorts.
func SelectPeerHostPort(hostPorts []instance.HostPort) string {
return instance.SelectInternalHostPort(hostPorts, false)
func SelectPeerHostPort(hostPorts []network.HostPort) string {
return network.SelectInternalHostPort(hostPorts, false)
}

// GenerateSharedSecret generates a pseudo-random shared secret (keyfile)
Expand Down
42 changes: 21 additions & 21 deletions agent/mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
gc "launchpad.net/gocheck"

"github.com/juju/juju/agent/mongo"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/state/api/params"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/upstart"
Expand Down Expand Up @@ -293,35 +293,35 @@ func (s *MongoSuite) TestServiceName(c *gc.C) {
}

func (s *MongoSuite) TestSelectPeerAddress(c *gc.C) {
addresses := []instance.Address{{
Value: "10.0.0.1",
Type: instance.Ipv4Address,
NetworkName: "cloud",
NetworkScope: instance.NetworkCloudLocal}, {
Value: "8.8.8.8",
Type: instance.Ipv4Address,
NetworkName: "public",
NetworkScope: instance.NetworkPublic}}
addresses := []network.Address{{
Value: "10.0.0.1",
Type: network.IPv4Address,
NetworkName: "cloud",
Scope: network.ScopeCloudLocal}, {
Value: "8.8.8.8",
Type: network.IPv4Address,
NetworkName: "public",
Scope: network.ScopePublic}}

address := mongo.SelectPeerAddress(addresses)
c.Assert(address, gc.Equals, "10.0.0.1")
}

func (s *MongoSuite) TestSelectPeerHostPort(c *gc.C) {

hostPorts := []instance.HostPort{{
Address: instance.Address{
Value: "10.0.0.1",
Type: instance.Ipv4Address,
NetworkName: "cloud",
NetworkScope: instance.NetworkCloudLocal,
hostPorts := []network.HostPort{{
Address: network.Address{
Value: "10.0.0.1",
Type: network.IPv4Address,
NetworkName: "cloud",
Scope: network.ScopeCloudLocal,
},
Port: 37017}, {
Address: instance.Address{
Value: "8.8.8.8",
Type: instance.Ipv4Address,
NetworkName: "public",
NetworkScope: instance.NetworkPublic,
Address: network.Address{
Value: "8.8.8.8",
Type: network.IPv4Address,
NetworkName: "public",
Scope: network.ScopePublic,
},
Port: 37017}}

Expand Down
12 changes: 7 additions & 5 deletions cmd/juju/scp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/juju/juju/charm"
charmtesting "github.com/juju/juju/charm/testing"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
coretesting "github.com/juju/juju/testing"
)

Expand Down Expand Up @@ -119,11 +119,13 @@ func (s *SCPSuite) TestSCPCommand(c *gc.C) {
s.addUnit(srv, m[2], c)
srv = s.AddTestingService(c, "ipv6-svc", dummyCharm)
s.addUnit(srv, m[3], c)
// TODO(dimitern) This is a horrible hack and needs to
// be fixed as we implement proper #IPv6 support.
// Simulate machine 3 has a public IPv6 address.
ipv6Addr := instance.Address{
Value: "2001:db8::",
Type: instance.Ipv4Address, // ..because SelectPublicAddress ignores IPv6 addresses
NetworkScope: instance.NetworkPublic,
ipv6Addr := network.Address{
Value: "2001:db8::",
Type: network.IPv4Address, // ..because SelectPublicAddress ignores IPv6 addresses
Scope: network.ScopePublic,
}
err = m[3].SetAddresses(ipv6Addr)
c.Assert(err, gc.IsNil)
Expand Down
6 changes: 3 additions & 3 deletions cmd/juju/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
charmtesting "github.com/juju/juju/charm/testing"
"github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/envcmd"
"github.com/juju/juju/instance"
"github.com/juju/juju/juju/testing"
"github.com/juju/juju/network"
"github.com/juju/juju/state"
coretesting "github.com/juju/juju/testing"
)
Expand Down Expand Up @@ -194,8 +194,8 @@ func (s *SSHSuite) testSSHCommandHostAddressRetry(c *gc.C, proxy bool) {
}

func (s *SSHCommonSuite) setAddresses(m *state.Machine, c *gc.C) {
addrPub := instance.NewAddress(fmt.Sprintf("dummyenv-%s.dns", m.Id()), instance.NetworkPublic)
addrPriv := instance.NewAddress(fmt.Sprintf("dummyenv-%s.internal", m.Id()), instance.NetworkCloudLocal)
addrPub := network.NewAddress(fmt.Sprintf("dummyenv-%s.dns", m.Id()), network.ScopePublic)
addrPriv := network.NewAddress(fmt.Sprintf("dummyenv-%s.internal", m.Id()), network.ScopeCloudLocal)
err := m.SetAddresses(addrPub, addrPriv)
c.Assert(err, gc.IsNil)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/juju/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/envcmd"
"github.com/juju/juju/environs/network"
"github.com/juju/juju/instance"
"github.com/juju/juju/juju"
"github.com/juju/juju/network"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/apiserver/client"
Expand Down
Loading

0 comments on commit 57bad2b

Please sign in to comment.