api: implement getLayer

This commit is contained in:
Jimmy Zelinskie 2016-01-29 14:14:12 -05:00
parent 431c0ccb03
commit d130d2fab4
2 changed files with 88 additions and 24 deletions

View File

@ -19,8 +19,7 @@ type ErrorResponse struct {
} }
type Error struct { type Error struct {
Error string Message string
Type string
} }
type LayerRequest struct { type LayerRequest struct {
@ -29,6 +28,7 @@ type LayerRequest struct {
type Layer struct { type Layer struct {
Name string Name string
NamespaceName string
Path string Path string
ParentName string ParentName string
Format string Format string
@ -38,7 +38,7 @@ type Layer struct {
type Vulnerability struct { type Vulnerability struct {
Name string Name string
Namespace string NamespaceName string
Description string Description string
Severity string Severity string
FixedBy string FixedBy string

View File

@ -18,12 +18,14 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"net/url"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/coreos/clair/api/context" "github.com/coreos/clair/api/context"
cerrors "github.com/coreos/clair/utils/errors" cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/utils/types"
"github.com/coreos/clair/worker" "github.com/coreos/clair/worker"
) )
@ -35,42 +37,104 @@ func decodeJSON(r *http.Request, v interface{}) error {
return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v) return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v)
} }
func writeError(w io.Writer, err error, errType string) { func writeError(w http.ResponseWriter, err error) {
err = json.NewEncoder(w).Encode(ErrorResponse{Error{err.Error(), errType}}) writeResponse(w, ErrorResponse{Error{err.Error()}})
}
func writeResponse(w io.Writer, resp interface{}) {
err := json.NewEncoder(w).Encode(resp)
if err != nil { if err != nil {
panic("v1: failed to marshal error response: " + err.Error()) panic("v1: failed to marshal response: " + err.Error())
} }
} }
func writeHeader(w http.ResponseWriter, status int) int {
w.WriteHeader(status)
return status
}
func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
request := LayerRequest{} request := LayerRequest{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) writeError(w, err)
writeError(w, err, "BadRequest") return writeHeader(w, http.StatusBadRequest)
return http.StatusBadRequest
} }
err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format) err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil { if err != nil {
if _, ok := err.(*cerrors.ErrBadRequest); ok { if _, ok := err.(*cerrors.ErrBadRequest); ok {
w.WriteHeader(http.StatusBadRequest) writeError(w, err)
writeError(w, err, "BadRequest") return writeHeader(w, http.StatusBadRequest)
return http.StatusBadRequest
} }
w.WriteHeader(http.StatusInternalServerError) writeError(w, err)
writeError(w, err, "InternalServerError") return writeHeader(w, http.StatusInternalServerError)
return http.StatusInternalServerError
} }
w.WriteHeader(http.StatusCreated) return writeHeader(w, http.StatusCreated)
return http.StatusCreated
} }
func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
// ez parsedQuery, err := url.ParseQuery(r.URL.RawQuery)
return 0 if err != nil {
writeError(w, err)
return writeHeader(w, http.StatusBadRequest)
}
withFeatures := parsedQuery.Get("features") != ""
withVulnerabilities := parsedQuery.Get("vulnerabilities") != ""
dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities)
if err == cerrors.ErrNotFound {
writeError(w, err)
return writeHeader(w, http.StatusNotFound)
} else if err != nil {
writeError(w, err)
return writeHeader(w, http.StatusInternalServerError)
}
layer := Layer{
Name: dbLayer.Name,
IndexedByVersion: dbLayer.EngineVersion,
}
if dbLayer.Parent != nil {
layer.ParentName = dbLayer.Parent.Name
}
if dbLayer.Namespace != nil {
layer.NamespaceName = dbLayer.Namespace.Name
}
if withFeatures || withVulnerabilities && dbLayer.Features != nil {
for _, dbFeatureVersion := range dbLayer.Features {
feature := Feature{
Name: dbFeatureVersion.Feature.Name,
Namespace: dbFeatureVersion.Feature.Namespace.Name,
Version: dbFeatureVersion.Version.String(),
}
for _, dbVuln := range dbFeatureVersion.AffectedBy {
vuln := Vulnerability{
Name: dbVuln.Name,
NamespaceName: dbVuln.Namespace.Name,
Description: dbVuln.Description,
Severity: string(dbVuln.Severity),
}
if dbVuln.FixedBy != types.MaxVersion {
vuln.FixedBy = dbVuln.FixedBy.String()
}
feature.Vulnerabilities = append(feature.Vulnerabilities, vuln)
}
layer.Features = append(layer.Features, feature)
}
}
writeResponse(w, layer)
return writeHeader(w, http.StatusOK)
} }
func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
// ez // ez
return 0 return 0