// GetAllNodesForServer returns a list of all nodes on a given server
func GetAllNodesForServer(w rest.ResponseWriter, r *rest.Request) {
	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return

	}
	defer dbConn.Close()

	err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-read")
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	serverID := r.PathParam("ServerID")
	if serverID == "" {
		logit.Error.Println("GetAllNodesForServer: error serverID required")
		rest.Error(w, "serverID required", http.StatusBadRequest)
		return
	}

	serverIPAddress := strings.Replace(serverID, "_", ".", -1)

	results, err := swarmapi.DockerPs(serverIPAddress)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	nodes := make([]types.ClusterNode, len(results.Output))
	i := 0
	var container types.Container

	for _, each := range results.Output {
		logit.Info.Println("got back Name:" + each.Name + " Status:" + each.Status + " Image:" + each.Image)
		nodes[i].Name = each.Name
		container, err = admindb.GetContainerByName(dbConn, each.Name)
		if err != nil {
			logit.Error.Println(err.Error())
			nodes[i].ID = "unknown"
			nodes[i].ProjectID = "unknown"
		} else {
			nodes[i].ID = container.ID
			nodes[i].ProjectID = container.ProjectID
		}

		nodes[i].Status = each.Status
		nodes[i].Image = each.Image
		i++
	}

	w.WriteJson(&nodes)

}
// AdminStopServerContainers stops all containers on a given server
func AdminStopServerContainers(w rest.ResponseWriter, r *rest.Request) {
	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return

	}
	defer dbConn.Close()

	err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-read")
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	//serverID
	serverid := r.PathParam("ID")
	if serverid == "" {
		logit.Error.Println("AdminStopoServerContainers: error ID required")
		rest.Error(w, "ID required", http.StatusBadRequest)
		return
	}

	cleanIP := strings.Replace(serverid, "_", ".", -1)

	containers, err := swarmapi.DockerPs(cleanIP)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	//for each, get server, stop container
	for _, each := range containers.Output {

		if strings.HasPrefix(each.Status, "Up") {
			//stop container
			request := &swarmapi.DockerStopRequest{}
			request.ContainerName = each.Name
			logit.Info.Println("stopping " + request.ContainerName)
			_, err = swarmapi.DockerStop(request)
			if err != nil {
				logit.Error.Println("AdminStopServerContainers: error when trying to start container " + err.Error())
			}
		}
	}

	w.WriteHeader(http.StatusOK)
	status := types.SimpleStatus{}
	status.Status = "OK"
	w.WriteJson(&status)

}
// AdminStartServerContainers starts all containers on a given server
func AdminStartServerContainers(w rest.ResponseWriter, r *rest.Request) {
	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return

	}
	defer dbConn.Close()

	err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-read")
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	//serverID
	serverid := r.PathParam("ID")
	if serverid == "" {
		logit.Error.Println(" error ID required")
		rest.Error(w, "ID required", http.StatusBadRequest)
		return
	}

	cleanIP := strings.Replace(serverid, "_", ".", -1)

	containers, err := swarmapi.DockerPs(cleanIP)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	//for each, get server, start container
	//use a 'best effort' approach here since containers
	//can be removed outside of CPM's control

	for _, each := range containers.Output {

		//start the container
		var response swarmapi.DockerStartResponse
		var err error
		request := &swarmapi.DockerStartRequest{}
		logit.Info.Println("trying to start " + each.Name)
		request.ContainerName = each.Name
		response, err = swarmapi.DockerStart(request)
		if err != nil {
			logit.Error.Println("AdminStartServerContainers: error when trying to start container " + err.Error())
			logit.Error.Println(response.Output)
		}
		//logit.Info.Println(response.Output)

	}

	w.WriteHeader(http.StatusOK)
	status := types.SimpleStatus{}
	status.Status = "OK"
	w.WriteJson(&status)

}