-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbulk_handlers.go
58 lines (47 loc) · 1.39 KB
/
bulk_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"net/http"
h "github.com/RedHatInsights/sources-api-go/middleware/headers"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/service"
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/identity"
)
func BulkCreate(c echo.Context) error {
req := m.BulkCreateRequest{}
err := c.Bind(&req)
if err != nil {
return err
}
tenantID, ok := c.Get(h.TenantID).(int64)
if !ok {
return fmt.Errorf("failed to pull tenant from request")
}
xrhid, ok := c.Get(h.XRHID).(string)
if !ok {
c.Logger().Warnf("bad xrhid %v", c.Get(h.XRHID))
}
id, ok := c.Get(h.ParsedIdentity).(*identity.XRHID)
if !ok {
c.Logger().Warnf("failed to pull identity from request")
return fmt.Errorf("failed to pull identity from request")
}
user := &m.User{TenantID: tenantID}
userID, ok := c.Get(h.UserID).(int64)
if ok {
user.UserID = id.Identity.User.UserID
user.Id = userID
}
// TODO: Pull the identity from the context after the org_id changes are merged.
output, err := service.BulkAssembly(req, &m.Tenant{Id: tenantID, ExternalTenant: id.Identity.AccountNumber}, user)
if err != nil {
return err
}
forwardableHeaders, err := service.ForwadableHeaders(c)
if err != nil {
return err
}
service.SendBulkMessages(output, forwardableHeaders, xrhid)
return c.JSON(http.StatusCreated, output.ToResponse())
}