Skip to content

Commit

Permalink
Merge branch 'beta/1.8.0' into cmd-master
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlHembrough committed Jun 5, 2018
2 parents aa7d999 + 55e93e4 commit 82b2707
Show file tree
Hide file tree
Showing 9 changed files with 1,728 additions and 403 deletions.
76 changes: 67 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
Expand All @@ -18,12 +17,14 @@ import (
"github.com/ONSdigital/dp-dataset-api/url"
"github.com/ONSdigital/go-ns/audit"
"github.com/ONSdigital/go-ns/common"
"github.com/ONSdigital/go-ns/handlers/requestID"
"github.com/ONSdigital/go-ns/healthcheck"
"github.com/ONSdigital/go-ns/identity"
"github.com/ONSdigital/go-ns/log"
"github.com/ONSdigital/go-ns/server"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/pkg/errors"
)

var httpServer *server.Server
Expand All @@ -38,19 +39,25 @@ const (
dimensionOptionDocType = "dimension-option"

// audit actions
getDatasetsAction = "getDatasets"
getDatasetAction = "getDataset"
getEditionsAction = "getEditions"
getEditionAction = "getEdition"
getVersionsAction = "getVersions"
getVersionAction = "getVersion"
getDatasetsAction = "getDatasets"
getDatasetAction = "getDataset"
deleteDatasetAction = "deleteDataset"
addDatasetAction = "addDataset"
putDatasetAction = "putDataset"
getEditionsAction = "getEditions"
getEditionAction = "getEdition"
getVersionsAction = "getVersions"
getVersionAction = "getVersion"
getDimensionsAction = "getDimensions"
getMetadataAction = "getMetadata"

// audit results
actionAttempted = "attempted"
actionSuccessful = "successful"
actionUnsuccessful = "unsuccessful"

auditError = "error while attempting to record audit event, failing request"
auditError = "error while attempting to record audit event, failing request"
auditActionErr = "failed to audit action"
)

// PublishCheck Checks if an version has been published
Expand Down Expand Up @@ -183,11 +190,23 @@ func handleErrorType(docType string, err error, w http.ResponseWriter) {

switch docType {
default:
if err == errs.ErrDatasetNotFound || err == errs.ErrEditionNotFound || err == errs.ErrVersionNotFound || err == errs.ErrDimensionNodeNotFound || err == errs.ErrInstanceNotFound {
if err == errs.ErrEditionNotFound || err == errs.ErrVersionNotFound || err == errs.ErrDimensionNodeNotFound || err == errs.ErrInstanceNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case "dataset":
if err == errs.ErrDatasetNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
} else if err == errs.ErrDeleteDatasetNotFound {
http.Error(w, err.Error(), http.StatusNoContent)
} else if err == errs.ErrDeletePublishedDatasetForbidden || err == errs.ErrAddDatasetAlreadyExists {
http.Error(w, err.Error(), http.StatusForbidden)
} else if err == errs.ErrAddUpdateDatasetBadRequest {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
case "edition":
if err == errs.ErrDatasetNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
Expand Down Expand Up @@ -345,6 +364,45 @@ func handleAuditingFailure(w http.ResponseWriter, err error, logData log.Data) {
http.Error(w, "internal server error", http.StatusInternalServerError)
}

func auditActionFailure(ctx context.Context, auditedAction string, auditedResult string, err error, logData log.Data) {
if logData == nil {
logData = log.Data{}
}

logData["auditAction"] = auditedAction
logData["auditResult"] = auditedResult

logError(ctx, errors.WithMessage(err, auditActionErr), logData)
}

func logError(ctx context.Context, err error, data log.Data) {
if data == nil {
data = log.Data{}
}
reqID := requestID.Get(ctx)
if user := common.User(ctx); user != "" {
data["user"] = user
}
if caller := common.Caller(ctx); caller != "" {
data["caller"] = caller
}
log.ErrorC(reqID, err, data)
}

func logInfo(ctx context.Context, message string, data log.Data) {
if data == nil {
data = log.Data{}
}
reqID := requestID.Get(ctx)
if user := common.User(ctx); user != "" {
data["user"] = user
}
if caller := common.Caller(ctx); caller != "" {
data["caller"] = caller
}
log.InfoC(reqID, message, data)
}

// Close represents the graceful shutting down of the http server
func Close(ctx context.Context) error {
if err := httpServer.Shutdown(ctx); err != nil {
Expand Down
Loading

0 comments on commit 82b2707

Please sign in to comment.