github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/api/registry.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" 10 ) 11 12 func (a *Api) registries(w http.ResponseWriter, r *http.Request) { 13 registries, err := a.manager.Registries() 14 if err != nil { 15 http.Error(w, err.Error(), http.StatusInternalServerError) 16 return 17 } 18 19 if err := json.NewEncoder(w).Encode(registries); err != nil { 20 http.Error(w, err.Error(), http.StatusInternalServerError) 21 return 22 } 23 } 24 25 func (a *Api) addRegistry(w http.ResponseWriter, r *http.Request) { 26 var registry *shipyard.Registry 27 if err := json.NewDecoder(r.Body).Decode(®istry); err != nil { 28 http.Error(w, err.Error(), http.StatusInternalServerError) 29 return 30 } 31 32 if err := a.manager.AddRegistry(registry); err != nil { 33 log.Errorf("error saving registry: %s", err) 34 http.Error(w, err.Error(), http.StatusInternalServerError) 35 return 36 } 37 38 log.Infof("added registry: name=%s", registry.Name) 39 w.WriteHeader(http.StatusNoContent) 40 } 41 42 func (a *Api) registry(w http.ResponseWriter, r *http.Request) { 43 w.Header().Set("content-type", "application/json") 44 45 vars := mux.Vars(r) 46 name := vars["name"] 47 48 registry, err := a.manager.Registry(name) 49 if err != nil { 50 http.Error(w, err.Error(), http.StatusInternalServerError) 51 return 52 } 53 54 if err := json.NewEncoder(w).Encode(registry); err != nil { 55 http.Error(w, err.Error(), http.StatusInternalServerError) 56 return 57 } 58 } 59 60 func (a *Api) removeRegistry(w http.ResponseWriter, r *http.Request) { 61 vars := mux.Vars(r) 62 name := vars["name"] 63 64 registry, err := a.manager.Registry(name) 65 if err != nil { 66 http.Error(w, err.Error(), http.StatusInternalServerError) 67 return 68 } 69 70 if err := a.manager.RemoveRegistry(registry); err != nil { 71 log.Errorf("error deleting registry: %s", err) 72 http.Error(w, err.Error(), http.StatusInternalServerError) 73 return 74 } 75 } 76 77 func (a *Api) repositories(w http.ResponseWriter, r *http.Request) { 78 w.Header().Set("content-type", "application/json") 79 80 vars := mux.Vars(r) 81 name := vars["name"] 82 83 if name != "" { 84 registry, err := a.manager.Registry(name) 85 if err != nil { 86 http.Error(w, err.Error(), http.StatusInternalServerError) 87 return 88 } 89 90 repos, err := registry.Repositories() 91 if err != nil { 92 http.Error(w, err.Error(), http.StatusInternalServerError) 93 return 94 } 95 if err := json.NewEncoder(w).Encode(repos); err != nil { 96 http.Error(w, err.Error(), http.StatusInternalServerError) 97 return 98 } 99 } 100 } 101 102 func (a *Api) repository(w http.ResponseWriter, r *http.Request) { 103 w.Header().Set("content-type", "application/json") 104 105 vars := mux.Vars(r) 106 name := vars["name"] 107 repoName := vars["repo"] 108 109 registry, err := a.manager.Registry(name) 110 if err != nil { 111 http.Error(w, err.Error(), http.StatusInternalServerError) 112 return 113 } 114 115 repo, err := registry.Repository(repoName) 116 if err != nil { 117 http.Error(w, err.Error(), http.StatusInternalServerError) 118 return 119 } 120 if err := json.NewEncoder(w).Encode(repo); err != nil { 121 http.Error(w, err.Error(), http.StatusInternalServerError) 122 return 123 } 124 } 125 126 func (a *Api) deleteRepository(w http.ResponseWriter, r *http.Request) { 127 vars := mux.Vars(r) 128 name := vars["name"] 129 repoName := vars["repo"] 130 131 registry, err := a.manager.Registry(name) 132 if err != nil { 133 http.Error(w, err.Error(), http.StatusInternalServerError) 134 return 135 } 136 137 if err := registry.DeleteRepository(repoName); err != nil { 138 http.Error(w, err.Error(), http.StatusInternalServerError) 139 return 140 } 141 142 w.WriteHeader(http.StatusNoContent) 143 } 144 145 func (a *Api) inspectRepository(w http.ResponseWriter, r *http.Request) { 146 vars := mux.Vars(r) 147 name := vars["name"] 148 repoName := vars["repo"] 149 150 registry, err := a.manager.Registry(name) 151 if err != nil { 152 http.Error(w, err.Error(), http.StatusInternalServerError) 153 return 154 } 155 156 repo, err := registry.Repository(repoName) 157 if err != nil { 158 http.Error(w, err.Error(), http.StatusInternalServerError) 159 return 160 } 161 if err := json.NewEncoder(w).Encode(repo); err != nil { 162 http.Error(w, err.Error(), http.StatusInternalServerError) 163 return 164 } 165 }