github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/open_resource_discovery/handler.go (about) 1 package ord 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/log" 9 ) 10 11 // AggregationResources holds ids of resources for ord data aggregation 12 type AggregationResources struct { 13 ApplicationIDs []string `json:"applicationIDs"` 14 ApplicationTemplateIDs []string `json:"applicationTemplateIDs"` 15 } 16 17 type handler struct { 18 ordSvc ORDService 19 cfg MetricsConfig 20 } 21 22 //go:generate mockery --name=ORDService --output=automock --outpkg=automock --case=underscore --disable-version-string 23 // ORDService missing godoc 24 type ORDService interface { 25 ProcessApplications(ctx context.Context, cfg MetricsConfig, appIDs []string) error 26 ProcessApplicationTemplates(ctx context.Context, cfg MetricsConfig, appTemplateIDs []string) error 27 } 28 29 // NewORDAggregatorHTTPHandler returns a new HTTP handler, responsible for handling HTTP requests 30 func NewORDAggregatorHTTPHandler(svc ORDService, cfg MetricsConfig) *handler { 31 return &handler{ 32 ordSvc: svc, 33 cfg: cfg, 34 } 35 } 36 37 func (h *handler) AggregateORDData(writer http.ResponseWriter, request *http.Request) { 38 ctx := request.Context() 39 40 resources := AggregationResources{} 41 if err := json.NewDecoder(request.Body).Decode(&resources); err != nil { 42 log.C(ctx).WithError(err).Errorf("Failed to parse request body") 43 http.Error(writer, "Invalid request body", http.StatusBadRequest) 44 return 45 } 46 47 if err := h.ordSvc.ProcessApplications(ctx, h.cfg, resources.ApplicationIDs); err != nil { 48 log.C(ctx).WithError(err).Errorf("ORD data aggregation failed for one or more applications") 49 http.Error(writer, "ORD data aggregation failed for one or more applications", http.StatusInternalServerError) 50 return 51 } 52 53 if err := h.ordSvc.ProcessApplicationTemplates(ctx, h.cfg, resources.ApplicationTemplateIDs); err != nil { 54 log.C(ctx).WithError(err).Errorf("ORD data aggregation failed for one or more application templates") 55 http.Error(writer, "ORD data aggregation failed for one or more application templates", http.StatusInternalServerError) 56 return 57 } 58 59 writer.WriteHeader(http.StatusOK) 60 }