github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/api/scale.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strconv"
     7  
     8  	"github.com/gorilla/mux"
     9  )
    10  
    11  func (a *Api) scaleContainer(w http.ResponseWriter, r *http.Request) {
    12  	w.Header().Set("content-type", "application/json")
    13  
    14  	vars := mux.Vars(r)
    15  	containerId := vars["id"]
    16  	n := r.URL.Query()["n"]
    17  
    18  	if len(n) == 0 {
    19  		http.Error(w, "you must enter a number of instances (param: n)", http.StatusBadRequest)
    20  		return
    21  	}
    22  
    23  	numInstances, err := strconv.Atoi(n[0])
    24  	if err != nil {
    25  		http.Error(w, err.Error(), http.StatusBadRequest)
    26  		return
    27  	}
    28  
    29  	if numInstances <= 0 {
    30  		http.Error(w, "you must enter a positive value", http.StatusBadRequest)
    31  		return
    32  	}
    33  
    34  	result := a.manager.ScaleContainer(containerId, numInstances)
    35  	// If we received any errors, continue to write result to the writer, but return a 500
    36  	if len(result.Errors) > 0 {
    37  		w.WriteHeader(http.StatusInternalServerError)
    38  	}
    39  	if err := json.NewEncoder(w).Encode(result); err != nil {
    40  		http.Error(w, err.Error(), http.StatusInternalServerError)
    41  		return
    42  	}
    43  
    44  }