2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
import (
|
2014-12-09 21:38:07 +00:00
|
|
|
"encoding/json"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
|
|
|
|
2015-02-13 21:59:50 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-05-27 00:18:32 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-02-11 01:32:22 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2014-11-11 02:57:38 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tagsDispatcher constructs the tags handler api endpoint.
|
|
|
|
func tagsDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
tagsHandler := &tagsHandler{
|
|
|
|
Context: ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlers.MethodHandler{
|
|
|
|
"GET": http.HandlerFunc(tagsHandler.GetTags),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsHandler handles requests for lists of tags under a repository name.
|
|
|
|
type tagsHandler struct {
|
|
|
|
*Context
|
|
|
|
}
|
|
|
|
|
2014-12-09 21:38:07 +00:00
|
|
|
type tagsAPIResponse struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
2014-11-11 02:57:38 +00:00
|
|
|
// GetTags returns a json list of tags for a specific image name.
|
|
|
|
func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) {
|
2014-12-09 21:38:07 +00:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
tagService := th.Repository.Tags(th)
|
|
|
|
tags, err := tagService.All(th)
|
2014-12-09 21:38:07 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
2015-02-13 21:59:50 +00:00
|
|
|
case distribution.ErrRepositoryUnknown:
|
2016-02-01 21:47:34 +00:00
|
|
|
th.Errors = append(th.Errors, v2.ErrorCodeNameUnknown.WithDetail(map[string]string{"name": th.Repository.Named().Name()}))
|
2016-04-29 21:34:24 +00:00
|
|
|
case errcode.Error:
|
|
|
|
th.Errors = append(th.Errors, err)
|
2014-12-09 21:38:07 +00:00
|
|
|
default:
|
2015-06-03 13:52:39 +00:00
|
|
|
th.Errors = append(th.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
2014-12-09 21:38:07 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-31 04:09:45 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
2014-12-09 21:38:07 +00:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
if err := enc.Encode(tagsAPIResponse{
|
2016-02-01 21:47:34 +00:00
|
|
|
Name: th.Repository.Named().Name(),
|
2014-12-09 21:38:07 +00:00
|
|
|
Tags: tags,
|
|
|
|
}); err != nil {
|
2015-06-03 13:52:39 +00:00
|
|
|
th.Errors = append(th.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
2014-12-09 21:38:07 +00:00
|
|
|
return
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|