Skip to content

Commit

Permalink
[r=gz] state: Add Addresses and SetAddresses on Machine
Browse files Browse the repository at this point in the history
New Addresses() and SetAddresses() methods on Machine
manage the list of addresses recorded.

https://codereview.appspot.com/12211043/

R=dimitern, wallyworld
  • Loading branch information
bz2 authored and Tarmac committed Aug 7, 2013
2 parents a4406d0 + 2998f79 commit d332dab
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
24 changes: 12 additions & 12 deletions state/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ import (
// address represents the location of a machine, including metadata about what
// kind of location the address describes.
type address struct {
value string
addresstype instance.AddressType
networkname string `bson:",omitempty"`
networkscope instance.NetworkScope `bson:",omitempty"`
Value string
AddressType instance.AddressType
NetworkName string `bson:",omitempty"`
NetworkScope instance.NetworkScope `bson:",omitempty"`
}

func NewAddress(addr instance.Address) address {
stateaddr := address{
value: addr.Value,
addresstype: addr.Type,
networkname: addr.NetworkName,
networkscope: addr.NetworkScope,
Value: addr.Value,
AddressType: addr.Type,
NetworkName: addr.NetworkName,
NetworkScope: addr.NetworkScope,
}
return stateaddr
}

func (addr *address) InstanceAddress() instance.Address {
instanceaddr := instance.Address{
Value: addr.value,
Type: addr.addresstype,
NetworkName: addr.networkname,
NetworkScope: addr.networkscope,
Value: addr.Value,
Type: addr.AddressType,
NetworkName: addr.NetworkName,
NetworkScope: addr.NetworkScope,
}
return instanceaddr
}
32 changes: 32 additions & 0 deletions state/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type machineDoc struct {
Jobs []MachineJob
PasswordHash string
Clean bool
Addresses []address
// Deprecated. InstanceId, now lives on instanceData.
// This attribute is retained so that data from existing machines can be read.
// SCHEMACHANGE
Expand Down Expand Up @@ -585,6 +586,37 @@ func IsNotProvisionedError(err error) bool {
return false
}

// Addresses returns any hostnames and ips associated with a machine
func (m *Machine) Addresses() (addresses []instance.Address) {
for _, address := range m.doc.Addresses {
addresses = append(addresses, address.InstanceAddress())
}
return
}

// SetAddresses records any addresses related to the machine
func (m *Machine) SetAddresses(addresses []instance.Address) (err error) {
var stateAddresses []address
for _, address := range addresses {
stateAddresses = append(stateAddresses, NewAddress(address))
}

ops := []txn.Op{
{
C: m.st.machines.Name,
Id: m.doc.Id,
Assert: notDeadDoc,
Update: D{{"$set", D{{"addresses", stateAddresses}}}},
},
}

if err = m.st.runTransaction(ops); err != nil {
return fmt.Errorf("cannot set addresses of machine %v: %v", m, onAbort(err, errDead))
}
m.doc.Addresses = stateAddresses
return nil
}

func (e *NotProvisionedError) Error() string {
return fmt.Sprintf("machine %v is not provisioned", e.machineId)
}
Expand Down
15 changes: 15 additions & 0 deletions state/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,3 +934,18 @@ func (s *MachineSuite) TestGetSetStatusWhileNotAlive(c *C) {
_, _, err = s.machine.Status()
c.Assert(err, ErrorMatches, "status not found")
}

func (s *MachineSuite) TestSetAddresses(c *C) {
machine, err := s.State.AddMachine("series", state.JobHostUnits)
c.Assert(err, IsNil)
c.Assert(machine.Addresses(), HasLen, 0)

addresses := []instance.Address{
instance.NewAddress("127.0.0.1"),
instance.NewAddress("8.8.8.8"),
}
err = machine.SetAddresses(addresses)
c.Assert(err, IsNil)
machine.Refresh()
c.Assert(machine.Addresses(), DeepEquals, addresses)
}

0 comments on commit d332dab

Please sign in to comment.