github.com/Cloud-Foundations/Dominator@v0.3.4/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  	"github.com/Cloud-Foundations/Dominator/lib/url"
    10  )
    11  
    12  func (m *Manager) listLocations(dirname string) ([]string, error) {
    13  	topo, err := m.getTopology()
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	directory, err := topo.FindDirectory(dirname)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	var locations []string
    22  	directory.Walk(func(directory *topology.Directory) error {
    23  		for _, machine := range directory.Machines {
    24  			hypervisor, err := m.getLockedHypervisor(machine.Hostname, false)
    25  			if err != nil {
    26  				continue
    27  			}
    28  			if hypervisor.probeStatus == probeStatusConnected &&
    29  				(hypervisor.healthStatus == "" ||
    30  					hypervisor.healthStatus == "healthy") {
    31  				locations = append(locations, directory.GetPath())
    32  				hypervisor.mutex.RUnlock()
    33  				return nil
    34  			}
    35  			hypervisor.mutex.RUnlock()
    36  		}
    37  		return nil
    38  	})
    39  	return locations, nil
    40  }
    41  
    42  func (m *Manager) listLocationsHandler(w http.ResponseWriter,
    43  	req *http.Request) {
    44  	writer := bufio.NewWriter(w)
    45  	defer writer.Flush()
    46  	parsedQuery := url.ParseQuery(req.URL)
    47  	locations, err := m.listLocations("")
    48  	if err != nil {
    49  		fmt.Fprintln(writer, err)
    50  		return
    51  	}
    52  	switch parsedQuery.OutputType() {
    53  	case url.OutputTypeHtml:
    54  		fmt.Fprintf(writer, "<title>List of Hypervisor Locations</title>\n")
    55  		fmt.Fprintln(writer, "<body>")
    56  		for _, location := range locations {
    57  			fmt.Fprintf(writer,
    58  				"<a href=\"listHypervisors?location=%s\">%s</a><br>\n",
    59  				location, location)
    60  		}
    61  		fmt.Fprintln(writer, "</body>")
    62  	case url.OutputTypeText:
    63  		for _, location := range locations {
    64  			fmt.Fprintln(writer, location)
    65  		}
    66  	}
    67  }