forked from RedHatInsights/sources-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_authentication_handlers.go
165 lines (130 loc) · 3.91 KB
/
application_authentication_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"net/http"
"strconv"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/marketplace"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/service"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
)
// function that defines how we get the dao - default implementation below.
var getApplicationAuthenticationDao func(c echo.Context) (dao.ApplicationAuthenticationDao, error)
func getApplicationAuthenticationDaoWithTenant(c echo.Context) (dao.ApplicationAuthenticationDao, error) {
requestParams, err := dao.NewRequestParamsFromContext(c)
if err != nil {
return nil, err
}
return dao.GetApplicationAuthenticationDao(requestParams), nil
}
func ApplicationAuthenticationList(c echo.Context) error {
appAuthDB, err := getApplicationAuthenticationDao(c)
if err != nil {
return err
}
filters, err := getFilters(c)
if err != nil {
return err
}
limit, offset, err := getLimitAndOffset(c)
if err != nil {
return err
}
appAuths, count, err := appAuthDB.List(limit, offset, filters)
if err != nil {
return err
}
c.Logger().Infof("tenant: %v", *appAuthDB.Tenant())
out := make([]interface{}, len(appAuths))
for i, a := range appAuths {
out[i] = *a.ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func ApplicationAuthenticationGet(c echo.Context) error {
appAuthDB, err := getApplicationAuthenticationDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
c.Logger().Infof("Getting ApplicationAuthentication ID %v", id)
appAuth, err := appAuthDB.GetById(&id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, appAuth.ToResponse())
}
func ApplicationAuthenticationCreate(c echo.Context) error {
appAuthDB, err := getApplicationAuthenticationDao(c)
if err != nil {
return err
}
input := m.ApplicationAuthenticationCreateRequest{}
if err := c.Bind(&input); err != nil {
return err
}
err = service.ValidateApplicationAuthenticationCreateRequest(&input)
if err != nil {
return util.NewErrBadRequest(err)
}
appAuth := &m.ApplicationAuthentication{
ApplicationID: input.ApplicationID,
AuthenticationID: input.AuthenticationID,
}
err = appAuthDB.Create(appAuth)
if err != nil {
return err
}
accountNumber, err := getAccountNumberFromEchoContext(c)
if err != nil {
c.Logger().Warn(err)
}
appAuth.Tenant = m.Tenant{Id: appAuth.TenantID, ExternalTenant: accountNumber}
setEventStreamResource(c, appAuth)
return c.JSON(http.StatusCreated, appAuth.ToResponse())
}
func ApplicationAuthenticationDelete(c echo.Context) error {
appAuthDB, err := getApplicationAuthenticationDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
appAuth, err := appAuthDB.Delete(&id)
if err != nil {
return err
}
setEventStreamResource(c, appAuth)
return c.NoContent(http.StatusNoContent)
}
func ApplicationAuthenticationListAuthentications(c echo.Context) error {
authDao, err := getAuthenticationDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("application_authentication_id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
auths, count, err := authDao.ListForApplicationAuthentication(id, 100, 0, nil)
if err != nil {
return err
}
tenantId := authDao.Tenant()
out := make([]interface{}, count)
for i := 0; i < int(count); i++ {
// Set the marketplace token —if the auth is of the marketplace type— for the authentication.
err := marketplace.SetMarketplaceTokenAuthExtraField(*tenantId, &auths[i])
if err != nil {
return err
}
out[i] = auths[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), 100, 0))
}