github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/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  	nameToDirectory  map[string]*Directory // Key: directory name.
    20  	owners           *ownersType
    21  	parent           *Directory
    22  	path             string
    23  	subnetIdToSubnet map[string]*Subnet // Key: subnet ID.
    24  }
    25  
    26  func (directory *Directory) GetPath() string {
    27  	return directory.path
    28  }
    29  
    30  func (directory *Directory) Walk(fn func(*Directory) error) error {
    31  	return directory.walk(fn)
    32  }
    33  
    34  type ownersType struct {
    35  	OwnerGroups []string `json:",omitempty"`
    36  	OwnerUsers  []string `json:",omitempty"`
    37  }
    38  
    39  type Subnet struct {
    40  	hyper_proto.Subnet
    41  	FirstAutoIP     net.IP              `json:",omitempty"`
    42  	LastAutoIP      net.IP              `json:",omitempty"`
    43  	ReservedIPs     []net.IP            `json:",omitempty"`
    44  	reservedIpAddrs map[string]struct{} // Key: IP address.
    45  }
    46  
    47  func (s *Subnet) CheckIfIpIsReserved(ipAddr string) bool {
    48  	_, ok := s.reservedIpAddrs[ipAddr]
    49  	return ok
    50  }
    51  
    52  func (subnet *Subnet) Shrink() {
    53  	subnet.shrink()
    54  }
    55  
    56  type Topology struct {
    57  	Root            *Directory
    58  	machineParents  map[string]*Directory // Key: machine name.
    59  	reservedIpAddrs map[string]struct{}   // Key: IP address.
    60  }
    61  
    62  func Load(topologyDir string) (*Topology, error) {
    63  	return load(topologyDir)
    64  }
    65  
    66  func Watch(topologyRepository, localRepositoryDir, topologyDir string,
    67  	checkInterval time.Duration,
    68  	logger log.DebugLogger) (<-chan *Topology, error) {
    69  	return watch(topologyRepository, localRepositoryDir, topologyDir,
    70  		checkInterval, logger)
    71  }
    72  
    73  func (t *Topology) CheckIfIpIsReserved(ipAddr string) bool {
    74  	_, ok := t.reservedIpAddrs[ipAddr]
    75  	return ok
    76  }
    77  
    78  func (t *Topology) CheckIfMachineHasSubnet(name, subnetId string) (
    79  	bool, error) {
    80  	return t.checkIfMachineHasSubnet(name, subnetId)
    81  }
    82  
    83  func (t *Topology) FindDirectory(dirname string) (*Directory, error) {
    84  	return t.findDirectory(dirname)
    85  }
    86  
    87  func (t *Topology) GetLocationOfMachine(name string) (string, error) {
    88  	return t.getLocationOfMachine(name)
    89  }
    90  
    91  func (t *Topology) GetNumMachines() uint {
    92  	return uint(len(t.machineParents))
    93  }
    94  
    95  func (t *Topology) GetSubnetsForMachine(name string) ([]*Subnet, error) {
    96  	return t.getSubnetsForMachine(name)
    97  }
    98  
    99  func (t *Topology) ListMachines(dirname string) ([]*fm_proto.Machine, error) {
   100  	return t.listMachines(dirname)
   101  }
   102  
   103  func (t *Topology) Walk(fn func(*Directory) error) error {
   104  	return t.Root.Walk(fn)
   105  }