Exemplo n.º 1
0
func ListRunningVM(c *echo.Context) error {
	machines, err := vms.Machines()
	if err != nil {
		return c.JSON(
			http.StatusInternalServerError, hash{
				"errors": [1]hash{
					hash{
						"detail": err.Error(),
					},
				},
			})
	}
	type attr struct {
		Name   string `json:"name"`
		Ip     string `json:"ip"`
		Status string `json:"status"`
		Id     string `json:"id"`
	}
	type virtmachine struct {
		Id  string `json:"id"`
		Att attr   `json:"attributes"`
	}
	var res = make([]virtmachine, len(machines))
	for i, val := range machines {
		res[i].Att.Name, err = val.Name()
		if err != nil {
			log.Error(err)
			return retJsonError(c, err)
		}
		res[i].Att.Id = val.Id()
		status, err := val.Status()
		if err != nil {
			log.Error(err)
			return retJsonError(c, err)
		}
		res[i].Att.Status = vm.StatusToString(status)
		ip, _ := val.IP()
		res[i].Att.Ip = string(ip)
		if err != nil {
			log.Error(err)
			return retJsonError(c, err)
		}
	}

	return c.JSON(http.StatusOK, hash{"data": res})
}
Exemplo n.º 2
0
func Machines(c *echo.Context) error {
	machines, err := vms.Machines()
	if err != nil {
		log.Error(err)
		return errors.UnableToRetrieveMachineList
	}

	res := make([]*machine, len(machines))

	for i, val := range machines {
		m := machine{}
		m.Name, err = val.Name()
		if err != nil {
			log.Error(err)
			return errors.UnableToRetrieveMachineList
		}

		m.Id = val.Id()
		status, err := val.Status()
		if err != nil {
			log.Error(err)
			return errors.UnableToRetrieveMachineList
		}

		m.Platform = val.Platform()

		m.Status = vm.StatusToString(status)

		progress, err := val.Progress()
		if err != nil {
			log.Errorf("Unable to get machine progress: %s", err)
		} else {
			m.Progress = int(progress)
		}

		ip, _ := val.IP()
		if ip != nil {
			m.Ip = ip.String()
		}

		res[i] = &m
	}

	return utils.JSON(c, http.StatusOK, res)
}