Skip to content

Commit

Permalink
update code per review comments
Browse files Browse the repository at this point in the history
Signed-off-by: wang yan <wangyan@vmware.com>
  • Loading branch information
wy65701436 committed Jun 4, 2020
1 parent c9fa405 commit 09b833f
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/lib/errors/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const (
BadRequestCode = "BAD_REQUEST"
// ForbiddenCode ...
ForbiddenCode = "FORBIDDEN"
// MethodNotAllowedCode ...
MethodNotAllowedCode = "METHOD_NOT_ALLOWED"
// PreconditionCode ...
PreconditionCode = "PRECONDITION"
// GeneralCode ...
Expand Down Expand Up @@ -59,6 +61,11 @@ func ForbiddenError(err error) *Error {
return New("forbidden").WithCode(ForbiddenCode).WithCause(err)
}

// MethodNotAllowedError is error for the case of forbidden
func MethodNotAllowedError(err error) *Error {
return New("method not allowed").WithCode(MethodNotAllowedCode).WithCause(err)
}

// PreconditionFailedError is error for the case of precondition failed
func PreconditionFailedError(err error) *Error {
return New("precondition failed").WithCode(PreconditionCode).WithCause(err)
Expand Down
6 changes: 3 additions & 3 deletions src/registryctl/api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func HandleInternalServerError(w http.ResponseWriter, err error) {
HandleError(w, errors.UnknownError(err))
}

// HandleForbidden ...
func HandleForbidden(w http.ResponseWriter) {
HandleError(w, errors.ForbiddenError(nil))
// HandleNotMethodAllowed ...
func HandleNotMethodAllowed(w http.ResponseWriter) {
HandleError(w, errors.MethodNotAllowedError(nil))
}

// HandleBadRequest ...
Expand Down
6 changes: 3 additions & 3 deletions src/registryctl/api/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func TestHandleError(t *testing.T) {
}

w = httptest.NewRecorder()
HandleForbidden(w)
if w.Code != http.StatusForbidden {
t.Errorf("unexpected status code: %d != %d", w.Code, http.StatusForbidden)
HandleNotMethodAllowed(w)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("unexpected status code: %d != %d", w.Code, http.StatusMethodNotAllowed)
}

w = httptest.NewRecorder()
Expand Down
4 changes: 3 additions & 1 deletion src/registryctl/api/registry/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blob
import (
"errors"
"github.com/docker/distribution/registry/storage"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/registryctl/api"
regConf "github.com/goharbor/harbor/src/registryctl/config/registry"
"github.com/gorilla/mux"
Expand All @@ -22,7 +23,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
case http.MethodDelete:
h.delete(w, req)
default:
api.HandleForbidden(w)
api.HandleNotMethodAllowed(w)
}
}

Expand All @@ -36,6 +37,7 @@ func (h *handler) delete(w http.ResponseWriter, r *http.Request) {
// don't parse the reference here as RemoveBlob does.
cleaner := storage.NewVacuum(r.Context(), regConf.StorageDriver)
if err := cleaner.RemoveBlob(ref); err != nil {
log.Info("failed to remove blob: %s, with error:%v", ref, err)
api.HandleError(w, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/registryctl/api/registry/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
case http.MethodPost:
h.start(w, req)
default:
api.HandleForbidden(w)
api.HandleNotMethodAllowed(w)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/registryctl/api/registry/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manifest
import (
"github.com/docker/distribution/registry/storage"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/registryctl/api"
regConf "github.com/goharbor/harbor/src/registryctl/config/registry"
"github.com/gorilla/mux"
Expand All @@ -24,7 +25,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
case http.MethodDelete:
h.delete(w, req)
default:
api.HandleForbidden(w)
api.HandleNotMethodAllowed(w)
}
}

Expand Down Expand Up @@ -52,6 +53,7 @@ func (h *handler) delete(w http.ResponseWriter, r *http.Request) {

cleaner := storage.NewVacuum(r.Context(), regConf.StorageDriver)
if err := cleaner.RemoveManifest(repoName, dgst, tags); err != nil {
log.Info("failed to remove manifest: %s, with error:%v", ref, err)
api.HandleInternalServerError(w, err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/registryctl/config/registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"os"
)

// StorageDriver ...
// StorageDriver the storage driver bases on the registry configurations, like filesystem, oss, gcs, S3, and etc.
var StorageDriver storagedriver.StorageDriver

// ResolveConfiguration ...
// ResolveConfiguration loads the mounted registry configuration file, which is shared with the registry controller.
func ResolveConfiguration(configPath string) (*configuration.Configuration, error) {
fp, err := os.Open(configPath)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/server/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
errors.UNSUPPORTED: http.StatusBadRequest,
errors.UnAuthorizedCode: http.StatusUnauthorized,
errors.ForbiddenCode: http.StatusForbidden,
errors.MethodNotAllowedCode: http.StatusMethodNotAllowed,
errors.DENIED: http.StatusForbidden,
errors.NotFoundCode: http.StatusNotFound,
errors.ConflictCode: http.StatusConflict,
Expand Down

0 comments on commit 09b833f

Please sign in to comment.