Skip to content

Commit

Permalink
feat: retrieve ports in the client
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Mar 5, 2024
1 parent 91e4c41 commit 229f391
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import useUser from "./apps/Auth/hooks/useUser";
import AccountEmails from "./apps/Auth/pages/Account/AccountEmails";
import { getAuthToken } from "./backend/server";
import AccountTheme from "./apps/Auth/pages/Account/AccountTheme";
import ContainerPorts from "./apps/Containers/pages/ContainerPorts/ContainerPorts";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -179,6 +180,10 @@ function AllRoutes() {
path="/containers/:uuid/environment"
element={<ContainerEnv />}
/>
<Route
path="/containers/:uuid/ports"
element={<ContainerPorts />}
/>
<Route
path="/containers/:uuid/database"
element={<ContainerDetailsDatabase />}
Expand Down
6 changes: 6 additions & 0 deletions client/src/apps/Containers/backend/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const saveEnv = (id: string, env: EnvVariables) => {
return server.patch(`/containers/${id}/environment`, { env });
};

const getContainerPorts = async (id: string) => {
const { data } = await server.get(`/containers/${id}/ports`);
return data;
};

const getDocker = async (id: string) => {
const { data } = await server.get<DockerContainerInfo>(
`/containers/${id}/docker`
Expand Down Expand Up @@ -111,6 +116,7 @@ export const API = {
getLogs,
getContainerEnvironment,
saveEnv,
getContainerPorts,
getDockerInfo: getDocker,
recreateDocker,
updateService,
Expand Down
12 changes: 12 additions & 0 deletions client/src/apps/Containers/hooks/useContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ export function useContainerEnv(id?: string) {
errorEnv: queryEnv.error,
};
}

export function useContainerPorts(id?: string) {
const queryPorts = useQuery({
queryKey: ["container_ports", id],
queryFn: () => API.getContainerPorts(id),
});
return {
ports: queryPorts.data,
isLoadingPorts: queryPorts.isLoading,
errorPorts: queryPorts.error,
};
}
6 changes: 6 additions & 0 deletions client/src/apps/Containers/pages/Container/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Cube,
Gear,
House,
ShareNetwork,
TerminalWindow,
Textbox,
Trash,
Expand Down Expand Up @@ -107,6 +108,11 @@ export default function ContainerDetails() {
icon={<Textbox />}
link={l(`/containers/${uuid}/environment`)}
/>
<Sidebar.Item
label="Ports"
icon={<ShareNetwork />}
link={l(`/containers/${uuid}/ports`)}
/>
{container?.databases && (
<Sidebar.Item
label="Database"
Expand Down
56 changes: 56 additions & 0 deletions client/src/apps/Containers/pages/ContainerPorts/ContainerPorts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
Button,
Input,
Table,
TableBody,
TableCell,
TableHead,
TableHeadCell,
TableRow,
Title,
Vertical,
} from "@vertex-center/components";
import { useParams } from "react-router-dom";
import { useQueryClient } from "@tanstack/react-query";
import { useContainerPorts } from "../../hooks/useContainer";
import { ProgressOverlay } from "../../../../components/Progress/Progress";
import { APIError } from "../../../../components/Error/APIError";
import { Horizontal } from "../../../../components/Layouts/Layouts";

export default function ContainerPorts() {
const { uuid } = useParams();
// const queryClient = useQueryClient();

const { ports, isLoadingPorts, errorPorts } = useContainerPorts(uuid);

const error = errorPorts;
const isLoading = isLoadingPorts;

return (
<Vertical gap={24}>
<Title variant="h2">Ports</Title>
<APIError error={error} />
<ProgressOverlay show={isLoading} />
<Table>
<TableHead>
<TableRow>
<TableHeadCell>Port inside container</TableHeadCell>
<TableHeadCell>Port outside container</TableHeadCell>
</TableRow>
</TableHead>
<TableBody>
{ports?.map((port, i) => (
<TableRow>
<TableCell>
<Input value={port?.in} disabled />
</TableCell>
<TableCell>
<Input value={port?.out} disabled />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Vertical>
);
}
6 changes: 6 additions & 0 deletions server/apps/containers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func (a *App) InitializeRouter(r *fizz.RouterGroup) error {
fizz.Response("500", "", nil, nil, map[string]interface{}{"error": "failed to patch container environment"}),
}, containersHandler.PatchEnvironment())

containers.GET("/:container_id/ports", []fizz.OperationOption{
fizz.ID("getContainerPorts"),
fizz.Summary("Get container ports"),
fizz.Response("404", "Container not found", nil, nil, map[string]interface{}{"error": "container not found"}),
}, containersHandler.GetContainerPorts())

containers.GET("/:container_id/events", []fizz.OperationOption{
fizz.ID("eventsContainer"),
fizz.Summary("Get container events"),
Expand Down
1 change: 1 addition & 0 deletions server/apps/containers/core/port/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
AddContainerTag() gin.HandlerFunc
GetContainerEnv() gin.HandlerFunc
PatchEnvironment() gin.HandlerFunc
GetContainerPorts() gin.HandlerFunc
GetDocker() gin.HandlerFunc
RecreateDocker() gin.HandlerFunc
GetLogs() gin.HandlerFunc
Expand Down
1 change: 1 addition & 0 deletions server/apps/containers/core/port/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
SetDatabases(ctx context.Context, c *types.Container, databases map[string]uuid.UUID, options map[string]*types.SetDatabasesOptions) error
GetContainerEnv(ctx context.Context, id uuid.UUID) (types.EnvVariables, error)
SaveEnv(ctx context.Context, id uuid.UUID, env types.EnvVariables) error
GetContainerPorts(ctx context.Context, id uuid.UUID) (types.Ports, error)
GetAllVersions(ctx context.Context, id uuid.UUID, useCache bool) ([]string, error)
GetContainerInfo(ctx context.Context, id uuid.UUID) (map[string]any, error)
WaitStatus(ctx context.Context, id uuid.UUID, status string) error
Expand Down
5 changes: 5 additions & 0 deletions server/apps/containers/core/port/services_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (m *MockContainerService) GetContainerEnv(ctx context.Context, id uuid.UUID
return args.Get(0).(types.EnvVariables), args.Error(1)
}

func (m *MockContainerService) GetContainerPorts(ctx context.Context, id uuid.UUID) (types.Ports, error) {
args := m.Called(ctx, id)
return args.Get(0).(types.Ports), args.Error(1)
}

func (m *MockContainerService) RecreateContainer(ctx context.Context, uuid uuid.UUID) error {
args := m.Called(ctx, uuid)
return args.Error(0)
Expand Down
4 changes: 4 additions & 0 deletions server/apps/containers/core/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ func (s *containerService) GetContainerEnv(ctx context.Context, id uuid.UUID) (t
return s.vars.GetContainerVariables(ctx, id)
}

func (s *containerService) GetContainerPorts(ctx context.Context, id uuid.UUID) (types.Ports, error) {
return s.ports.GetContainerPorts(ctx, id)
}

// remapDatabaseEnv remaps the environment variables of a container.
func (s *containerService) remapDatabaseEnv(ctx context.Context, c *types.Container, options map[string]*types.SetDatabasesOptions) error {
for databaseID, databaseContainerID := range c.Databases {
Expand Down
11 changes: 11 additions & 0 deletions server/apps/containers/handler/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ func (h *containerHandler) PatchEnvironment() gin.HandlerFunc {
}, http.StatusOK)
}

type GetContainerPortsParams struct {
ContainerID uuid.NullUUID `path:"container_id"`
}

func (h *containerHandler) GetContainerPorts() gin.HandlerFunc {
return tonic.Handler(func(ctx *gin.Context, params *GetContainerPortsParams) (types.Ports, error) {
return h.containerService.GetContainerPorts(ctx, params.ContainerID.UUID)
}, http.StatusOK)

}

func (h *containerHandler) GetDocker() gin.HandlerFunc {
return tonic.Handler(func(ctx *gin.Context, params *GetContainerParams) (map[string]any, error) {
return h.containerService.GetContainerInfo(ctx, params.ContainerID.UUID)
Expand Down

0 comments on commit 229f391

Please sign in to comment.