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-01-17 02:32:27 +00:00
|
|
|
manifests := th.Repository.Manifests()
|
2014-12-09 21:38:07 +00:00
|
|
|
|
2015-01-17 02:32:27 +00:00
|
|
|
tags, err := manifests.Tags()
|
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:
|
2015-05-27 00:18:32 +00:00
|
|
|
th.Errors = append(th.Errors, errcode.NewError(v2.ErrorCodeNameUnknown, map[string]string{"name": th.Repository.Name()}))
|
2014-12-09 21:38:07 +00:00
|
|
|
default:
|
2015-05-27 00:18:32 +00:00
|
|
|
th.Errors = append(th.Errors, errcode.NewError(errcode.ErrorCodeUnknown, 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{
|
2015-01-17 02:32:27 +00:00
|
|
|
Name: th.Repository.Name(),
|
2014-12-09 21:38:07 +00:00
|
|
|
Tags: tags,
|
|
|
|
}); err != nil {
|
2015-05-27 00:18:32 +00:00
|
|
|
th.Errors = append(th.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
|
2014-12-09 21:38:07 +00:00
|
|
|
return
|
|
|
|
}
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|