github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/fleetmanager/hypervisors/listLocations.go (about)

     1  package hypervisors
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/fleetmanager/topology"
     9  )
    10  
    11  func (m *Manager) listLocations(dirname string) ([]string, error) {
    12  	topo, err := m.getTopology()
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  	directory, err := topo.FindDirectory(dirname)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	var locations []string
    21  	directory.Walk(func(directory *topology.Directory) error {
    22  		for _, machine := range directory.Machines {
    23  			hypervisor, err := m.getLockedHypervisor(machine.Hostname, false)
    24  			if err != nil {
    25  				continue
    26  			}
    27  			if hypervisor.probeStatus == probeStatusConnected &&
    28  				(hypervisor.healthStatus == "" ||
    29  					hypervisor.healthStatus == "healthy") {
    30  				locations = append(locations, directory.GetPath())
    31  				hypervisor.mutex.RUnlock()
    32  				return nil
    33  			}
    34  			hypervisor.mutex.RUnlock()
    35  		}
    36  		return nil
    37  	})
    38  	return locations, nil
    39  }
    40  
    41  func (m *Manager) listLocationsHandler(w http.ResponseWriter,
    42  	req *http.Request) {
    43  	writer := bufio.NewWriter(w)
    44  	defer writer.Flush()
    45  	if locations, err := m.listLocations(""); err != nil {
    46  		fmt.Fprintln(writer, err)
    47  	} else {
    48  		for _, location := range locations {
    49  			fmt.Fprintln(writer, location)
    50  		}
    51  	}
    52  }