Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Fix JSON response to create App.
Browse files Browse the repository at this point in the history
- missing lifecycle type
- missing data field in relationships
- the AppExists call could return false due to eventual consistency,
  which results in the create request failing due to the webhook check
- we are currently relying on the exact error returned by the webhook to
  catch the error and generate the appropriate API response
- converted all app handler tests to use strings instead of []bytes
- remove presenter use from api handler tests
- remove presenter use from route api handler tests
- apply singular naming convention
- apply g.Expect convention

Co-authored-by: Dave Walter <walterda@vmware.com>
  • Loading branch information
2 people authored and matt-royal committed Sep 16, 2021
1 parent a919a9f commit 5fa8ea9
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 287 deletions.
19 changes: 15 additions & 4 deletions apis/app_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"fmt"
"net/http"

"github.com/google/uuid"

"code.cloudfoundry.org/cf-k8s-api/message"
"code.cloudfoundry.org/cf-k8s-api/presenter"
"code.cloudfoundry.org/cf-k8s-api/repositories"

"github.com/go-logr/logr"
"github.com/google/uuid"
"github.com/gorilla/mux"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -156,8 +157,18 @@ func (h *AppHandler) AppCreateHandler(w http.ResponseWriter, r *http.Request) {

responseAppRecord, err := h.AppRepo.CreateApp(client, ctx, createAppRecord)
if err != nil {
h.Logger.Error(err, "Failed to create app", "App Name", appName)
writeUnknownErrorResponse(w)
switch errtype := err.(type) {
case *k8serrors.StatusError:
reason := errtype.Status().Reason
if reason == "CFApp with the same spec.name exists" {
errorDetail := fmt.Sprintf("App with the name '%s' already exists.", appName)
h.Logger.Error(err, errorDetail, "App Name", appName)
writeUniquenessError(w, errorDetail)
}
default:
h.Logger.Error(err, "Failed to create app", "App Name", appName)
writeUnknownErrorResponse(w)
}
return
}
responseBody, err := json.Marshal(presenter.ForApp(responseAppRecord, h.ServerURL))
Expand Down
Loading

0 comments on commit 5fa8ea9

Please sign in to comment.