Skip to content

Commit 456c59c

Browse files
committed
Improvements for certificates table, adds expansion object to certificates
1 parent 6c76c04 commit 456c59c

File tree

9 files changed

+95
-22
lines changed

9 files changed

+95
-22
lines changed

backend/internal/api/handler/certificates.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func GetCertificates() func(http.ResponseWriter, *http.Request) {
2626
return
2727
}
2828

29-
certificates, err := certificate.List(pageInfo, middleware.GetFiltersFromContext(r))
29+
certificates, err := certificate.List(pageInfo, middleware.GetFiltersFromContext(r), getExpandFromContext(r))
3030
if err != nil {
3131
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
3232
} else {
@@ -46,11 +46,16 @@ func GetCertificate() func(http.ResponseWriter, *http.Request) {
4646
return
4747
}
4848

49-
cert, err := certificate.GetByID(certificateID)
50-
if err != nil {
49+
item, err := certificate.GetByID(certificateID)
50+
switch err {
51+
case sql.ErrNoRows:
52+
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not found", nil)
53+
case nil:
54+
// nolint: errcheck,gosec
55+
item.Expand(getExpandFromContext(r))
56+
h.ResultResponseJSON(w, r, http.StatusOK, item)
57+
default:
5158
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
52-
} else {
53-
h.ResultResponseJSON(w, r, http.StatusOK, cert)
5459
}
5560
}
5661
}

backend/internal/entity/certificate/methods.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func Update(certificate *Model) error {
102102
}
103103

104104
// List will return a list of certificates
105-
func List(pageInfo model.PageInfo, filters []model.Filter) (ListResponse, error) {
105+
func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ListResponse, error) {
106106
var result ListResponse
107107
var exampleModel Model
108108

@@ -135,6 +135,15 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (ListResponse, error)
135135
return result, err
136136
}
137137

138+
if expand != nil {
139+
for idx := range items {
140+
expandErr := items[idx].Expand(expand)
141+
if expandErr != nil {
142+
logger.Error("CertificatesExpansionError", expandErr)
143+
}
144+
}
145+
}
146+
138147
result = ListResponse{
139148
Items: items,
140149
Total: totalRows,

backend/internal/entity/certificate/model.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"npm/internal/database"
1414
"npm/internal/entity/certificateauthority"
1515
"npm/internal/entity/dnsprovider"
16+
"npm/internal/entity/user"
1617
"npm/internal/logger"
1718
"npm/internal/types"
19+
"npm/internal/util"
1820
)
1921

2022
const (
@@ -59,6 +61,7 @@ type Model struct {
5961
// Expansions:
6062
CertificateAuthority *certificateauthority.Model `json:"certificate_authority,omitempty"`
6163
DNSProvider *dnsprovider.Model `json:"dns_provider,omitempty"`
64+
User *user.Model `json:"user,omitempty"`
6265
}
6366

6467
func (m *Model) getByQuery(query string, params []interface{}) error {
@@ -161,7 +164,8 @@ func (m *Model) ValidateWildcardSupport() bool {
161164
}
162165

163166
if hasWildcard {
164-
m.Expand()
167+
// nolint: errcheck, gosec
168+
m.Expand([]string{"certificate-authority", "dns-provider"})
165169
if !m.CertificateAuthority.IsWildcardSupported {
166170
return false
167171
}
@@ -182,15 +186,28 @@ func (m *Model) setDefaultStatus() {
182186
}
183187

184188
// Expand will populate attached objects for the model
185-
func (m *Model) Expand() {
186-
if m.CertificateAuthorityID > 0 {
187-
certificateAuthority, _ := certificateauthority.GetByID(m.CertificateAuthorityID)
189+
func (m *Model) Expand(items []string) error {
190+
var err error
191+
192+
if util.SliceContainsItem(items, "certificate-authority") && m.CertificateAuthorityID > 0 {
193+
var certificateAuthority certificateauthority.Model
194+
certificateAuthority, err = certificateauthority.GetByID(m.CertificateAuthorityID)
188195
m.CertificateAuthority = &certificateAuthority
189196
}
190-
if m.DNSProviderID > 0 {
191-
dnsProvider, _ := dnsprovider.GetByID(m.DNSProviderID)
197+
198+
if util.SliceContainsItem(items, "dns-provider") && m.DNSProviderID > 0 {
199+
var dnsProvider dnsprovider.Model
200+
dnsProvider, err = dnsprovider.GetByID(m.DNSProviderID)
192201
m.DNSProvider = &dnsProvider
193202
}
203+
204+
if util.SliceContainsItem(items, "user") && m.ID > 0 {
205+
var usr user.Model
206+
usr, err = user.GetByID(m.UserID)
207+
m.User = &usr
208+
}
209+
210+
return err
194211
}
195212

196213
// GetCertificateLocations will return the paths on disk where the SSL
@@ -222,7 +239,8 @@ func (m *Model) GetCertificateLocations() (string, string, string) {
222239
func (m *Model) Request() error {
223240
logger.Info("Requesting certificate for: #%d %v", m.ID, m.Name)
224241

225-
m.Expand()
242+
// nolint: errcheck, gosec
243+
m.Expand([]string{"certificate-authority", "dns-provider"})
226244
m.Status = StatusRequesting
227245
if err := m.Save(); err != nil {
228246
logger.Error("CertificateSaveError", err)

frontend/src/api/npm/getCertificates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function getCertificates(
1111
const { result } = await api.get(
1212
{
1313
url: "certificates",
14-
params: { limit, offset, sort, ...filters },
14+
params: { limit, offset, sort, expand: "user", ...filters },
1515
},
1616
abortController,
1717
);

frontend/src/components/Monospace.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Text, TextProps } from "@chakra-ui/react";
2+
3+
function Monospace(props: TextProps) {
4+
return (
5+
<Text as="span" className="monospace" {...props}>
6+
{props.children}
7+
</Text>
8+
);
9+
}
10+
11+
export { Monospace };

frontend/src/components/Table/Formatters.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Avatar, Badge, Text, Tooltip } from "@chakra-ui/react";
2-
import { RowAction, RowActionsMenu } from "components";
2+
import { Monospace, RowAction, RowActionsMenu } from "components";
33
import { intl } from "locale";
44
import getNiceDNSProvider from "modules/Acmesh";
55

@@ -73,13 +73,19 @@ function CapabilitiesFormatter() {
7373

7474
function CertificateStatusFormatter() {
7575
const formatCell = ({ value }: any) => {
76-
return (
77-
<Badge color={value ? "cyan.500" : "red.400"}>
78-
{value
79-
? intl.formatMessage({ id: "ready" })
80-
: intl.formatMessage({ id: "setup-required" })}
81-
</Badge>
82-
);
76+
let color = "cyan.500";
77+
switch (value) {
78+
case "failed":
79+
color = "red.400";
80+
break;
81+
case "provided":
82+
color = "green.400";
83+
break;
84+
case "requesting":
85+
color = "yellow.400";
86+
break;
87+
}
88+
return <Badge color={color}>{intl.formatMessage({ id: value })}</Badge>;
8389
};
8490

8591
return formatCell;
@@ -185,6 +191,14 @@ function HostStatusFormatter() {
185191
return formatCell;
186192
}
187193

194+
function MonospaceFormatter() {
195+
const formatCell = ({ value }: any) => {
196+
return <Monospace>{value}</Monospace>;
197+
};
198+
199+
return formatCell;
200+
}
201+
188202
function UpstreamStatusFormatter() {
189203
const formatCell = ({ value, row }: any) => {
190204
if (value === "ready") {
@@ -245,6 +259,7 @@ export {
245259
HostStatusFormatter,
246260
HostTypeFormatter,
247261
IDFormatter,
262+
MonospaceFormatter,
248263
SecondsFormatter,
249264
UpstreamStatusFormatter,
250265
};

frontend/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from "./HelpDrawer";
55
export * from "./Loader";
66
export * from "./Loading";
77
export * from "./LocalePicker";
8+
export * from "./Monospace";
89
export * from "./Navigation";
910
export * from "./Permissions";
1011
export * from "./PrettyButton";

frontend/src/index.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ table td.w-80 {
2525
width: 80px;
2626
}
2727

28+
span.monospace {
29+
font-family: monospace;
30+
}
31+
2832
/* helpdoc */
2933
.helpdoc-body {
3034
p {

frontend/src/pages/Certificates/CertificatesTable.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { useEffect, useMemo } from "react";
33
import {
44
tableEvents,
55
ActionsFormatter,
6+
CertificateStatusFormatter,
7+
GravatarFormatter,
68
IDFormatter,
9+
MonospaceFormatter,
710
TableFilter,
811
TableLayout,
912
TablePagination,
@@ -41,6 +44,11 @@ function CertificatesTable({
4144
}: CertificatesTableProps) {
4245
const [columns, tableData] = useMemo(() => {
4346
const columns = [
47+
{
48+
accessor: "user.gravatarUrl",
49+
Cell: GravatarFormatter(),
50+
className: "w-80",
51+
},
4452
{
4553
Header: intl.formatMessage({ id: "column.id" }),
4654
accessor: "id",
@@ -53,6 +61,7 @@ function CertificatesTable({
5361
accessor: "name",
5462
sortable: true,
5563
Filter: TextFilter,
64+
Cell: MonospaceFormatter(),
5665
},
5766
{
5867
Header: intl.formatMessage({ id: "column.validation-type" }),
@@ -65,6 +74,7 @@ function CertificatesTable({
6574
accessor: "status",
6675
sortable: true,
6776
Filter: TextFilter,
77+
Cell: CertificateStatusFormatter(),
6878
},
6979
{
7080
id: "actions",

0 commit comments

Comments
 (0)