Skip to content

Commit

Permalink
update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 1, 2024
1 parent 9438dde commit d9834d6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
12 changes: 7 additions & 5 deletions apps/webservice/src/app/api/v1/targets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ const patchBodySchema = z.object({
),
});

export const PATCH = request()
export const POST = request()
.use(authn)
.use(parseBody(patchBodySchema))
.use(
authz(({ can, ctx }) =>
can
authz(({ can, ctx }) => {
return can
.perform(Permission.TargetUpdate)
.on({ type: "workspace", id: ctx.body.workspaceId }),
),
.on({ type: "workspace", id: ctx.body.workspaceId });
}),
)
.handle<{ user: schema.User; body: z.infer<typeof patchBodySchema> }>(
async (ctx) => {
Expand All @@ -55,6 +55,8 @@ export const PATCH = request()
{ status: 400 },
);

console.log("ctx.body.targets", ctx.body.targets);

const targets = await upsertTargets(
db,
ctx.body.targets.map((t) => ({
Expand Down
82 changes: 82 additions & 0 deletions openapi.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,88 @@ info:
title: Ctrlplane API
version: 1.0.0
paths:
/v1/targets:
post:
summary: Create or update multiple targets
operationId: upsertTargets
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- workspaceId
- targets
properties:
workspaceId:
type: string
format: uuid
targets:
type: array
items:
type: object
required:
- name
- kind
- identifier
- version
- config
properties:
name:
type: string
kind:
type: string
identifier:
type: string
version:
type: string
config:
type: object
metadata:
type: object
additionalProperties:
type: string
variables:
type: array
items:
type: object
required:
- key
- value
properties:
key:
type: string
value:
type: [string, number, boolean]
sensitive:
type: boolean
responses:
"200":
description: Targets created successfully
content:
application/json:
schema:
type: object
required:
- count
properties:
count:
type: integer
description: Number of targets created/updated
"400":
description: Bad request - No targets provided or invalid input
content:
application/json:
schema:
type: object
properties:
error:
type: string
"401":
description: Unauthorized
"403":
description: Permission denied
/v1/targets/{targetId}:
get:
summary: Get a target
Expand Down
23 changes: 22 additions & 1 deletion packages/job-dispatch/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
eq,
inArray,
isNotNull,
or,
} from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import {
Expand Down Expand Up @@ -212,13 +213,33 @@ export const upsertTargets = async (
>,
) => {
try {
// Get existing targets from the database, grouped by providerId.
// - For targets without a providerId, look them up by workspaceId and
// identifier.
// - For targets with a providerId, get all targets for that provider.
log.info("Upserting targets", {
targetsToInsertCount: targetsToInsert.length,
});
const targetsBeforeInsertPromises = _.chain(targetsToInsert)
.groupBy((t) => t.providerId)
.filter((t) => t[0]?.providerId != null)
.map(async (targets) => {
const providerId = targets[0]?.providerId;

return providerId == null
? []
? db
.select()
.from(target)
.where(
or(
...targets.map((t) =>
and(
eq(target.workspaceId, t.workspaceId),
eq(target.identifier, t.identifier),
),
),
),
)
: getExistingTargetsForProvider(tx, providerId);
})
.value();
Expand Down

0 comments on commit d9834d6

Please sign in to comment.