OLD | NEW |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package state | 4 package state |
5 | 5 |
6 import ( | 6 import ( |
| 7 "fmt" |
| 8 |
7 "launchpad.net/juju-core/instance" | 9 "launchpad.net/juju-core/instance" |
8 ) | 10 ) |
9 | 11 |
10 // address represents the location of a machine, including metadata about what | 12 // address represents the location of a machine, including metadata about what |
11 // kind of location the address describes. | 13 // kind of location the address describes. |
12 type address struct { | 14 type address struct { |
13 Value string | 15 Value string |
14 AddressType instance.AddressType | 16 AddressType instance.AddressType |
15 NetworkName string `bson:",omitempty"` | 17 NetworkName string `bson:",omitempty"` |
16 NetworkScope instance.NetworkScope `bson:",omitempty"` | 18 NetworkScope instance.NetworkScope `bson:",omitempty"` |
(...skipping 11 matching lines...) Expand all Loading... |
28 | 30 |
29 func (addr *address) InstanceAddress() instance.Address { | 31 func (addr *address) InstanceAddress() instance.Address { |
30 instanceaddr := instance.Address{ | 32 instanceaddr := instance.Address{ |
31 Value: addr.Value, | 33 Value: addr.Value, |
32 Type: addr.AddressType, | 34 Type: addr.AddressType, |
33 NetworkName: addr.NetworkName, | 35 NetworkName: addr.NetworkName, |
34 NetworkScope: addr.NetworkScope, | 36 NetworkScope: addr.NetworkScope, |
35 } | 37 } |
36 return instanceaddr | 38 return instanceaddr |
37 } | 39 } |
| 40 |
| 41 func addressesToInstanceAddresses(addrs []address) []instance.Address { |
| 42 instanceAddrs := make([]instance.Address, len(addrs)) |
| 43 for i, addr := range addrs { |
| 44 instanceAddrs[i] = addr.InstanceAddress() |
| 45 } |
| 46 return instanceAddrs |
| 47 } |
| 48 |
| 49 // stateServerAddresses returns the list of internal addresses of the state |
| 50 // server machines. |
| 51 func (st *State) stateServerAddresses() ([]string, error) { |
| 52 type addressMachine struct { |
| 53 Addresses []address |
| 54 } |
| 55 var allAddresses []addressMachine |
| 56 // TODO(rog) 2013/10/14 index machines on jobs. |
| 57 err := st.machines.Find(D{{"jobs", JobManageState}}).All(&allAddresses) |
| 58 if err != nil { |
| 59 return nil, err |
| 60 } |
| 61 if len(allAddresses) == 0 { |
| 62 return nil, fmt.Errorf("no state server machines found") |
| 63 } |
| 64 apiAddrs := make([]string, 0, len(allAddresses)) |
| 65 for _, addrs := range allAddresses { |
| 66 instAddrs := addressesToInstanceAddresses(addrs.Addresses) |
| 67 addr := instance.SelectInternalAddress(instAddrs, false) |
| 68 if addr != "" { |
| 69 apiAddrs = append(apiAddrs, addr) |
| 70 } |
| 71 } |
| 72 if len(apiAddrs) == 0 { |
| 73 return nil, fmt.Errorf("no state server machines with addresses
found") |
| 74 } |
| 75 return apiAddrs, nil |
| 76 } |
| 77 |
| 78 func appendPort(addrs []string, port int) []string { |
| 79 newAddrs := make([]string, len(addrs)) |
| 80 for i, addr := range addrs { |
| 81 newAddrs[i] = fmt.Sprintf("%s:%d", addr, port) |
| 82 } |
| 83 return newAddrs |
| 84 } |
| 85 |
| 86 // Addresses returns the list of cloud-internal addresses that |
| 87 // can be used to connect to the state. |
| 88 func (st *State) Addresses() ([]string, error) { |
| 89 addrs, err := st.stateServerAddresses() |
| 90 if err != nil { |
| 91 return nil, err |
| 92 } |
| 93 config, err := st.EnvironConfig() |
| 94 if err != nil { |
| 95 return nil, err |
| 96 } |
| 97 return appendPort(addrs, config.StatePort()), nil |
| 98 } |
| 99 |
| 100 // APIAddresses returns the list of cloud-internal addresses that |
| 101 // can be used to connect to the state API server. |
| 102 func (st *State) APIAddresses() ([]string, error) { |
| 103 addrs, err := st.stateServerAddresses() |
| 104 if err != nil { |
| 105 return nil, err |
| 106 } |
| 107 config, err := st.EnvironConfig() |
| 108 if err != nil { |
| 109 return nil, err |
| 110 } |
| 111 return appendPort(addrs, config.APIPort()), nil |
| 112 } |
OLD | NEW |