0

Create Resource

by
Published today

Create a resource of a given resource type, or overwrite it when update_if_exists is set. Windmill resolves `$var:`/`$res:` strings in job arguments before the script runs, so the stored value holds what they point to, not the reference.

Script windmill Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
import * as wmill from "windmill-client"
4

5
// windmill-client errors only carry the status text: throw the response body, which says why
6
wmill.OpenAPI.interceptors.response.use(async (response) => {
7
  if (!response.ok) {
8
    throw new Error(`${response.status} ${await response.text()}`)
9
  }
10
  return response
11
})
12

13
export type DynSelect_resource_type = string
14

15
export async function resource_type() {
16
  const names = await wmill.ResourceService.listResourceTypeNames({
17
    workspace: wmill.getWorkspace(),
18
  })
19
  return names.map((n) => ({ value: n, label: n }))
20
}
21

22
/**
23
 * Create Resource
24
 * Create a resource of a given resource type, or overwrite it when update_if_exists is set. Windmill resolves `$var:`/`$res:` strings in job arguments before the script runs, so the stored value holds what they point to, not the reference.
25
 */
26
export async function main(
27
  path: string,
28
  resource_type: DynSelect_resource_type,
29
  value: { [key: string]: any },
30
  description: string | undefined,
31
  update_if_exists: boolean | undefined
32
) {
33
  return await wmill.ResourceService.createResource({
34
    workspace: wmill.getWorkspace(),
35
    updateIfExists: update_if_exists || undefined,
36
    requestBody: {
37
      path,
38
      resource_type,
39
      value: value ?? {},
40
      description: description || undefined,
41
    },
42
  })
43
}
44