-
Notifications
You must be signed in to change notification settings - Fork 29
/
internal_handlers.go
114 lines (90 loc) · 3.07 KB
/
internal_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
package main
import (
"fmt"
"net/http"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
)
// InternalAuthenticationGet fetches one authentication and returns it with the password exposed. Internal use only.
func InternalAuthenticationGet(c echo.Context) error {
authDao, err := getAuthenticationDao(c)
if err != nil {
return err
}
auth, err := authDao.GetById(c.Param("uuid"))
if err != nil {
return err
}
exposeEncryptedAttribute := c.QueryParam("expose_encrypted_attribute[]")
if exposeEncryptedAttribute == "password" {
return c.JSON(http.StatusOK, auth.ToInternalResponse())
}
return c.JSON(http.StatusOK, auth.ToResponse())
}
// InternalSourceList lists all the sources in a compact format —since the client that will use it,
// "sources-monitor-go" only requires a small set of fields—.
func InternalSourceList(c echo.Context) error {
filters, err := getFilters(c)
if err != nil {
return err
}
limit, offset, err := getLimitAndOffset(c)
if err != nil {
return err
}
// The DAO doesn't need a tenant set, since the queries won't be filtered by that tenant
sourcesDB := dao.GetSourceDao(nil)
sources, count, err := sourcesDB.ListInternal(limit, offset, filters)
if err != nil {
return err
}
out := make([]interface{}, len(sources))
for i := 0; i < len(sources); i++ {
out[i] = sources[i].ToInternalResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
// GetUntranslatedTenants returns all the tenants from the database that have just an "external_tenant" set up.
func GetUntranslatedTenants(c echo.Context) error {
tenantsDao := dao.GetTenantDao()
tenants, err := tenantsDao.GetUntranslatedTenants()
if err != nil {
return fmt.Errorf("unable to fetch the untranslated tenants: %w", err)
}
out := make([]interface{}, len(tenants))
for i := 0; i < len(tenants); i++ {
out[i] = tenants[i]
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), len(tenants), 0, 0))
}
// TranslateTenants attempts to translate the
func TranslateTenants(c echo.Context) error {
tenantsDao := dao.GetTenantDao()
translatableTenants, translatedTenants, untranslatedTenants, translationResults, err := tenantsDao.TranslateTenants()
if err != nil {
return fmt.Errorf("unable to translate the EBS account numbers to orgIds: %w", err)
}
response := map[string]interface{}{
"total_translatable_tenants_count": translatableTenants,
"translation_results": translationResults,
"translated_tenants": translatedTenants,
"untranslated_tenants": untranslatedTenants,
}
return c.JSON(http.StatusOK, response)
}
func InternalSecretGet(c echo.Context) error {
secretDao, err := getSecretDao(c)
if err != nil {
return err
}
paramID, err := util.InterfaceToInt64(c.Param("id"))
if err != nil {
return util.NewErrBadRequest(err)
}
secret, err := secretDao.GetById(¶mID)
if err != nil {
return err
}
return c.JSON(http.StatusOK, secret.ToInternalSecretResponse())
}