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

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	log "github.com/Sirupsen/logrus"
     8  	"github.com/gorilla/mux"
     9  	"github.com/shipyard/shipyard/dockerhub"
    10  )
    11  
    12  func (a *Api) webhookKeys(w http.ResponseWriter, r *http.Request) {
    13  	w.Header().Set("content-type", "application/json")
    14  
    15  	keys, err := a.manager.WebhookKeys()
    16  	if err != nil {
    17  		http.Error(w, err.Error(), http.StatusInternalServerError)
    18  		return
    19  	}
    20  	if err := json.NewEncoder(w).Encode(keys); err != nil {
    21  		http.Error(w, err.Error(), http.StatusInternalServerError)
    22  		return
    23  	}
    24  }
    25  
    26  func (a *Api) webhookKey(w http.ResponseWriter, r *http.Request) {
    27  	w.Header().Set("content-type", "application/json")
    28  
    29  	vars := mux.Vars(r)
    30  	id := vars["id"]
    31  	key, err := a.manager.WebhookKey(id)
    32  	if err != nil {
    33  		http.Error(w, err.Error(), http.StatusInternalServerError)
    34  		return
    35  	}
    36  	if err := json.NewEncoder(w).Encode(key); err != nil {
    37  		http.Error(w, err.Error(), http.StatusInternalServerError)
    38  		return
    39  	}
    40  }
    41  
    42  func (a *Api) addWebhookKey(w http.ResponseWriter, r *http.Request) {
    43  	var k *dockerhub.WebhookKey
    44  	if err := json.NewDecoder(r.Body).Decode(&k); err != nil {
    45  		http.Error(w, err.Error(), http.StatusInternalServerError)
    46  		return
    47  	}
    48  	key, err := a.manager.NewWebhookKey(k.Image)
    49  	if err != nil {
    50  		log.Errorf("error generating webhook key: %s", err)
    51  		http.Error(w, err.Error(), http.StatusInternalServerError)
    52  		return
    53  	}
    54  	log.Infof("saved webhook key image=%s", key.Image)
    55  	if err := json.NewEncoder(w).Encode(key); err != nil {
    56  		http.Error(w, err.Error(), http.StatusInternalServerError)
    57  		return
    58  	}
    59  }
    60  
    61  func (a *Api) deleteWebhookKey(w http.ResponseWriter, r *http.Request) {
    62  	vars := mux.Vars(r)
    63  	id := vars["id"]
    64  	if err := a.manager.DeleteWebhookKey(id); err != nil {
    65  		log.Errorf("error deleting webhook key: %s", err)
    66  		http.Error(w, err.Error(), http.StatusInternalServerError)
    67  		return
    68  	}
    69  	log.Infof("removed webhook key id=%s", id)
    70  	w.WriteHeader(http.StatusNoContent)
    71  }