Skip to content

Commit b399ec4

Browse files
committed
feat: add list of builds per template
1 parent 00895b1 commit b399ec4

File tree

14 files changed

+717
-127
lines changed

14 files changed

+717
-127
lines changed

packages/api/internal/api/api.gen.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/spec.gen.go

Lines changed: 107 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/types.gen.go

Lines changed: 65 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/handlers/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
// This is a security measure to prevent the use of weak secrets (like empty).
4040
const minSupabaseJWTSecretLength = 16
4141

42+
var _ api.ServerInterface = (*APIStore)(nil)
43+
4244
type APIStore struct {
4345
Healthy bool
4446
config cfg.Config

packages/api/internal/handlers/template_build_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem
6565

6666
// early return if still waiting for build start
6767
if buildInfo.BuildStatus == envbuild.StatusWaiting {
68-
result := api.TemplateBuild{
68+
result := api.TemplateBuildInfo{
6969
LogEntries: make([]api.BuildLogEntry, 0),
7070
Logs: make([]string, 0),
7171
TemplateID: templateID,
@@ -79,7 +79,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem
7979
}
8080

8181
// Needs to be before logs request so the status is not set to done too early
82-
result := api.TemplateBuild{
82+
result := api.TemplateBuildInfo{
8383
LogEntries: nil,
8484
Logs: nil,
8585
TemplateID: templateID,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
9+
"github.com/e2b-dev/infra/packages/api/internal/api"
10+
"github.com/e2b-dev/infra/packages/db/dberrors"
11+
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
12+
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
13+
)
14+
15+
func (a *APIStore) GetTemplatesTemplateID(c *gin.Context, templateID api.TemplateID) {
16+
ctx := c.Request.Context()
17+
18+
team, apiErr := a.GetTeamAndLimits(c, nil)
19+
if apiErr != nil {
20+
a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg)
21+
telemetry.ReportCriticalError(ctx, "error when getting team and tier", apiErr.Err)
22+
23+
return
24+
}
25+
26+
telemetry.SetAttributes(ctx,
27+
telemetry.WithTeamID(team.ID.String()),
28+
)
29+
30+
template, err := a.sqlcDB.GetTemplateByIDWithAliases(ctx, templateID)
31+
switch {
32+
case dberrors.IsNotFoundError(err):
33+
a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Template %s not found", templateID))
34+
telemetry.ReportError(ctx, "template not found", err, telemetry.WithTemplateID(templateID))
35+
36+
return
37+
case err != nil:
38+
a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting template")
39+
telemetry.ReportCriticalError(ctx, "error when getting template", err)
40+
41+
return
42+
case template.TeamID != team.ID:
43+
telemetry.ReportError(ctx, "user doesn't have access to the template", nil, telemetry.WithTemplateID(templateID))
44+
a.sendAPIStoreError(c, http.StatusForbidden, fmt.Sprintf("You don't have access to this sandbox template (%s)", templateID))
45+
46+
return
47+
}
48+
49+
builds, err := a.sqlcDB.GetTemplateBuilds(ctx, templateID)
50+
if err != nil {
51+
a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting builds")
52+
telemetry.ReportCriticalError(ctx, "error when getting builds", err)
53+
54+
return
55+
}
56+
57+
res := api.TemplateWithBuilds{
58+
TemplateID: template.ID,
59+
Public: template.Public,
60+
Aliases: template.Aliases,
61+
CreatedAt: template.CreatedAt,
62+
UpdatedAt: template.UpdatedAt,
63+
LastSpawnedAt: template.LastSpawnedAt,
64+
SpawnCount: template.SpawnCount,
65+
Builds: make([]api.TemplateBuild, 0, len(builds)),
66+
}
67+
68+
for _, item := range builds {
69+
res.Builds = append(res.Builds, api.TemplateBuild{
70+
BuildID: item.ID,
71+
Status: api.TemplateBuildStatus(item.Status),
72+
CreatedAt: item.CreatedAt,
73+
UpdatedAt: item.UpdatedAt,
74+
FinishedAt: item.FinishedAt,
75+
CpuCount: api.CPUCount(item.Vcpu),
76+
MemoryMB: api.MemoryMB(item.RamMb),
77+
DiskSizeMB: utils.CastPtr(item.TotalDiskSizeMb, func(v int64) api.DiskSizeMB { return api.DiskSizeMB(v) }),
78+
EnvdVersion: item.EnvdVersion,
79+
})
80+
}
81+
82+
c.JSON(http.StatusOK, res)
83+
}

packages/db/queries/get_template_by_id.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@
22
SELECT t.*
33
FROM "public"."envs" t
44
WHERE t.id = $1;
5+
6+
-- name: GetTemplateByIDWithAliases :one
7+
SELECT e.*, al.aliases
8+
FROM "public"."envs" e
9+
CROSS JOIN LATERAL (
10+
SELECT array_agg(alias)::text[] AS aliases
11+
FROM public.env_aliases
12+
WHERE env_id = e.id
13+
) AS al
14+
WHERE e.id = $1;

packages/db/queries/get_template_by_id.sql.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: GetTemplateBuilds :many
2+
SELECT eb.*
3+
FROM public.env_builds eb
4+
WHERE eb.env_id = sqlc.arg(template_id)
5+
ORDER BY eb.created_at DESC;

0 commit comments

Comments
 (0)