github.com/Cloud-Foundations/Dominator@v0.3.4/fleetmanager/topology/api.go (about) 1 package topology 2 3 import ( 4 "net" 5 "time" 6 7 "github.com/Cloud-Foundations/Dominator/lib/log" 8 "github.com/Cloud-Foundations/Dominator/lib/tags" 9 fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager" 10 hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor" 11 ) 12 13 type Directory struct { 14 Name string 15 Directories []*Directory `json:",omitempty"` 16 Machines []*fm_proto.Machine `json:",omitempty"` 17 Subnets []*Subnet `json:",omitempty"` 18 Tags tags.Tags `json:",omitempty"` 19 logger log.DebugLogger 20 nameToDirectory map[string]*Directory // Key: directory name. 21 owners *ownersType 22 parent *Directory 23 path string 24 subnetIdToSubnet map[string]*Subnet // Key: subnet ID. 25 } 26 27 func (directory *Directory) GetPath() string { 28 return directory.path 29 } 30 31 func (directory *Directory) Walk(fn func(*Directory) error) error { 32 return directory.walk(fn) 33 } 34 35 type ownersType struct { 36 OwnerGroups []string `json:",omitempty"` 37 OwnerUsers []string `json:",omitempty"` 38 } 39 40 type Params struct { 41 Logger log.DebugLogger 42 TopologyDir string // Directory containing topology data. 43 VariablesDir string // Directory containing variables. 44 } 45 46 type Subnet struct { 47 hyper_proto.Subnet 48 FirstAutoIP net.IP `json:",omitempty"` 49 LastAutoIP net.IP `json:",omitempty"` 50 ReservedIPs []net.IP `json:",omitempty"` 51 reservedIpAddrs map[string]struct{} // Key: IP address. 52 } 53 54 type WatchParams struct { 55 Params 56 CheckInterval time.Duration 57 LocalRepositoryDir string // Local directory. 58 MetricsDirectory string // Metrics namespace directory. 59 TopologyRepository string // Remote Git repository. 60 } 61 62 func (s *Subnet) CheckIfIpIsReserved(ipAddr string) bool { 63 _, ok := s.reservedIpAddrs[ipAddr] 64 return ok 65 } 66 67 func (subnet *Subnet) Shrink() { 68 subnet.shrink() 69 } 70 71 type Topology struct { 72 Root *Directory 73 Variables map[string]string 74 hostIpAddresses map[string]struct{} 75 logger log.DebugLogger 76 machineParents map[string]*Directory // Key: machine name. 77 reservedIpAddrs map[string]struct{} // Key: IP address. 78 } 79 80 func Load(topologyDir string) (*Topology, error) { 81 return load(Params{TopologyDir: topologyDir}) 82 } 83 84 func LoadWithParams(params Params) (*Topology, error) { 85 return load(params) 86 } 87 88 func Watch(topologyRepository, localRepositoryDir, topologyDir string, 89 checkInterval time.Duration, 90 logger log.DebugLogger) (<-chan *Topology, error) { 91 return watch(WatchParams{ 92 Params: Params{ 93 Logger: logger, 94 TopologyDir: topologyDir, 95 }, 96 CheckInterval: checkInterval, 97 LocalRepositoryDir: localRepositoryDir, 98 TopologyRepository: topologyRepository, 99 }) 100 } 101 102 func WatchWithParams(params WatchParams) (<-chan *Topology, error) { 103 return watch(params) 104 } 105 106 func (t *Topology) CheckIfIpIsHost(ipAddr string) bool { 107 _, ok := t.hostIpAddresses[ipAddr] 108 return ok 109 } 110 111 func (t *Topology) CheckIfIpIsReserved(ipAddr string) bool { 112 _, ok := t.reservedIpAddrs[ipAddr] 113 return ok 114 } 115 116 func (t *Topology) CheckIfMachineHasSubnet(name, subnetId string) ( 117 bool, error) { 118 return t.checkIfMachineHasSubnet(name, subnetId) 119 } 120 121 func (t *Topology) FindDirectory(dirname string) (*Directory, error) { 122 return t.findDirectory(dirname) 123 } 124 125 func (t *Topology) GetLocationOfMachine(name string) (string, error) { 126 return t.getLocationOfMachine(name) 127 } 128 129 func (t *Topology) GetNumMachines() uint { 130 return uint(len(t.machineParents)) 131 } 132 133 func (t *Topology) GetSubnetsForMachine(name string) ([]*Subnet, error) { 134 return t.getSubnetsForMachine(name) 135 } 136 137 func (t *Topology) ListMachines(dirname string) ([]*fm_proto.Machine, error) { 138 return t.listMachines(dirname) 139 } 140 141 func (t *Topology) Walk(fn func(*Directory) error) error { 142 return t.Root.Walk(fn) 143 }