@orsetra/shared-ui 1.10.28 → 1.10.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ui/config-selection-context.tsx +176 -0
- package/components/ui/index.ts +1 -0
- package/components/ui/resources-input.tsx +1 -1
- package/components/ui/secret-input.tsx +116 -23
- package/components/ui/secrets-input.tsx +1 -1
- package/index.ts +2 -0
- package/lib/config-selection.ts +175 -0
- package/lib/config-source.ts +114 -0
- package/package.json +1 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"
|
|
4
|
+
import type { ReactNode } from "react"
|
|
5
|
+
import {
|
|
6
|
+
configChoices, distributionRequest,
|
|
7
|
+
type ConfigChoices, type ConfigDistribution, type ConfigEnvironment, type StoredConfig,
|
|
8
|
+
} from "../../lib/config-selection"
|
|
9
|
+
import type { ConfigSource } from "../../lib/config-source"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What the fields that pick a config share on a page: the configs already read, where they
|
|
13
|
+
* are distributed, and the environment the page is for.
|
|
14
|
+
*
|
|
15
|
+
* A field asks for the templates it accepts and is given its choices. No page lists the
|
|
16
|
+
* templates its forms may need: a field that appears is served, whatever form it is in.
|
|
17
|
+
*/
|
|
18
|
+
interface ConfigSelectionValue {
|
|
19
|
+
/** Says a field needs the configs of these templates; they are read if they were not. */
|
|
20
|
+
need: (templates: string[]) => void
|
|
21
|
+
/** Whether the configs of every template named, and the distributions, have been read. */
|
|
22
|
+
ready: (templates: string[]) => boolean
|
|
23
|
+
choices: (templates: string[]) => ConfigChoices
|
|
24
|
+
/** The environment the page is for; null when it is for none. */
|
|
25
|
+
environment: ConfigEnvironment | null
|
|
26
|
+
/** Makes a config usable in the environment of the page. */
|
|
27
|
+
makeAvailable: (config: StoredConfig) => Promise<void>
|
|
28
|
+
/** Reads everything again, after a config was created elsewhere on the page. */
|
|
29
|
+
reload: () => void
|
|
30
|
+
/** Why the configs could not be read, when they could not. */
|
|
31
|
+
error: string | null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ConfigSelectionContext = createContext<ConfigSelectionValue | null>(null)
|
|
35
|
+
|
|
36
|
+
interface ConfigSelectionProviderProps {
|
|
37
|
+
children: ReactNode
|
|
38
|
+
source: ConfigSource
|
|
39
|
+
/** The business unit the configs belong to. */
|
|
40
|
+
project: string
|
|
41
|
+
/** The environment the page is for, by its name; nothing when it is for none. */
|
|
42
|
+
environmentName?: string | null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function ConfigSelectionProvider({ children, source, project, environmentName }: ConfigSelectionProviderProps) {
|
|
46
|
+
const [byTemplate, setByTemplate] = useState<Map<string, StoredConfig[]>>(new Map())
|
|
47
|
+
const [wanted, setWanted] = useState<string[]>([])
|
|
48
|
+
const [distributions, setDistributions] = useState<ConfigDistribution[] | null>(null)
|
|
49
|
+
const [environment, setEnvironment] = useState<ConfigEnvironment | null>(null)
|
|
50
|
+
const [environmentRead, setEnvironmentRead] = useState(false)
|
|
51
|
+
const [error, setError] = useState<string | null>(null)
|
|
52
|
+
const [round, setRound] = useState(0)
|
|
53
|
+
const reading = useRef<Set<string>>(new Set())
|
|
54
|
+
|
|
55
|
+
// Another business unit or another environment: nothing read so far applies.
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
setByTemplate(new Map())
|
|
58
|
+
setDistributions(null)
|
|
59
|
+
setEnvironment(null)
|
|
60
|
+
setEnvironmentRead(false)
|
|
61
|
+
setError(null)
|
|
62
|
+
reading.current = new Set()
|
|
63
|
+
}, [project, environmentName, round])
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!project) return
|
|
67
|
+
let cancelled = false
|
|
68
|
+
if (!environmentName) {
|
|
69
|
+
setEnvironmentRead(true)
|
|
70
|
+
setDistributions([])
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
Promise.all([source.environmentOf(project, environmentName), source.distributionsOf(project)])
|
|
74
|
+
.then(([found, listed]) => {
|
|
75
|
+
if (cancelled) return
|
|
76
|
+
setEnvironment(found)
|
|
77
|
+
setDistributions(listed)
|
|
78
|
+
setEnvironmentRead(true)
|
|
79
|
+
})
|
|
80
|
+
.catch((failure: any) => {
|
|
81
|
+
if (cancelled) return
|
|
82
|
+
setError(failure?.message || "Could not read where configurations are available")
|
|
83
|
+
setDistributions([])
|
|
84
|
+
setEnvironmentRead(true)
|
|
85
|
+
})
|
|
86
|
+
return () => { cancelled = true }
|
|
87
|
+
}, [source, project, environmentName, round])
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (!project) return
|
|
91
|
+
const missing = wanted.filter(template => !byTemplate.has(template) && !reading.current.has(template))
|
|
92
|
+
if (missing.length === 0) return
|
|
93
|
+
missing.forEach(template => reading.current.add(template))
|
|
94
|
+
let cancelled = false
|
|
95
|
+
source.configsOf(project, missing)
|
|
96
|
+
.then(configs => {
|
|
97
|
+
if (cancelled) return
|
|
98
|
+
setByTemplate(previous => {
|
|
99
|
+
const next = new Map(previous)
|
|
100
|
+
for (const template of missing) next.set(template, configs.filter(config => config.template === template))
|
|
101
|
+
return next
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
.catch((failure: any) => {
|
|
105
|
+
if (cancelled) return
|
|
106
|
+
setError(failure?.message || "Could not read the configurations")
|
|
107
|
+
setByTemplate(previous => {
|
|
108
|
+
const next = new Map(previous)
|
|
109
|
+
for (const template of missing) next.set(template, [])
|
|
110
|
+
return next
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
.finally(() => missing.forEach(template => reading.current.delete(template)))
|
|
114
|
+
return () => { cancelled = true }
|
|
115
|
+
}, [source, project, wanted, byTemplate])
|
|
116
|
+
|
|
117
|
+
const need = useCallback((templates: string[]) => {
|
|
118
|
+
setWanted(previous => {
|
|
119
|
+
const added = templates.filter(template => !previous.includes(template))
|
|
120
|
+
return added.length === 0 ? previous : [...previous, ...added]
|
|
121
|
+
})
|
|
122
|
+
}, [])
|
|
123
|
+
|
|
124
|
+
const reload = useCallback(() => setRound(previous => previous + 1), [])
|
|
125
|
+
|
|
126
|
+
const value = useMemo<ConfigSelectionValue>(() => {
|
|
127
|
+
const configsOf = (templates: string[]) => templates.flatMap(template => byTemplate.get(template) ?? [])
|
|
128
|
+
return {
|
|
129
|
+
need,
|
|
130
|
+
ready: templates => environmentRead && distributions !== null && templates.every(template => byTemplate.has(template)),
|
|
131
|
+
choices: templates => configChoices(configsOf(templates), templates, environmentName ? (environment ?? { name: environmentName, targets: [] }) : null, distributions ?? []),
|
|
132
|
+
environment,
|
|
133
|
+
makeAvailable: async config => {
|
|
134
|
+
if (!environment) throw new Error("This page is for no environment")
|
|
135
|
+
await source.distribute(project, distributionRequest(config, environment))
|
|
136
|
+
setDistributions(await source.distributionsOf(project))
|
|
137
|
+
},
|
|
138
|
+
reload,
|
|
139
|
+
error,
|
|
140
|
+
}
|
|
141
|
+
}, [need, reload, byTemplate, distributions, environment, environmentRead, environmentName, error, source, project])
|
|
142
|
+
|
|
143
|
+
return <ConfigSelectionContext.Provider value={value}>{children}</ConfigSelectionContext.Provider>
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function useConfigSelection(): ConfigSelectionValue | null {
|
|
147
|
+
return useContext(ConfigSelectionContext)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface FieldConfigChoices extends ConfigChoices {
|
|
151
|
+
loading: boolean
|
|
152
|
+
error: string | null
|
|
153
|
+
environment: ConfigEnvironment | null
|
|
154
|
+
makeAvailable: (config: StoredConfig) => Promise<void>
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The choices of one field, read as needed. Null when the page provides no config source. */
|
|
158
|
+
export function useConfigChoices(templates: string[]): FieldConfigChoices | null {
|
|
159
|
+
const selection = useConfigSelection()
|
|
160
|
+
const key = templates.join("\u0000")
|
|
161
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
162
|
+
const stable = useMemo(() => templates, [key])
|
|
163
|
+
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (selection && stable.length > 0) selection.need(stable)
|
|
166
|
+
}, [selection, stable])
|
|
167
|
+
|
|
168
|
+
if (!selection) return null
|
|
169
|
+
return {
|
|
170
|
+
...selection.choices(stable),
|
|
171
|
+
loading: stable.length > 0 && !selection.ready(stable),
|
|
172
|
+
error: selection.error,
|
|
173
|
+
environment: selection.environment,
|
|
174
|
+
makeAvailable: selection.makeAvailable,
|
|
175
|
+
}
|
|
176
|
+
}
|
package/components/ui/index.ts
CHANGED
|
@@ -76,6 +76,7 @@ export { KVDynamicInput } from './kv-dynamic-input'
|
|
|
76
76
|
export { SelectInput } from './select-input'
|
|
77
77
|
export { SecretContextProvider, useSecretContext, type SecretEntry } from './secret-context'
|
|
78
78
|
export { SecretInput } from './secret-input'
|
|
79
|
+
export { ConfigSelectionProvider, useConfigSelection, useConfigChoices, type FieldConfigChoices } from './config-selection-context'
|
|
79
80
|
export { SecretsInput } from './secrets-input'
|
|
80
81
|
export { StructsInput } from './structs-input'
|
|
81
82
|
export { CodeEditor } from './code-editor'
|
|
@@ -157,7 +157,7 @@ export function ResourcesInput({ id, value, onChange, disabled = false, ...rest
|
|
|
157
157
|
|
|
158
158
|
<div className="flex gap-2">
|
|
159
159
|
<div className="flex-1">
|
|
160
|
-
<SecretInput value={draft} onChange={setDraft} disabled={disabled} type={
|
|
160
|
+
<SecretInput value={draft} onChange={setDraft} disabled={disabled} type={rest.type} templates={rest.templates} exclude={items.map(item => parseItem(item).name)} />
|
|
161
161
|
</div>
|
|
162
162
|
<Button
|
|
163
163
|
type="button"
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
|
+
import { useState } from "react"
|
|
4
|
+
import { AlertTriangle, Loader2 } from "lucide-react"
|
|
3
5
|
import { SelectInput } from "./select-input"
|
|
4
6
|
import { useSecretContext } from "./secret-context"
|
|
5
|
-
import {
|
|
7
|
+
import { useConfigChoices } from "./config-selection-context"
|
|
8
|
+
import { acceptedTemplates, compatibleConfigs, configLabel, type StoredConfig } from "../../lib/config-selection"
|
|
6
9
|
|
|
7
10
|
interface SecretInputProps {
|
|
8
11
|
id?: string
|
|
9
|
-
|
|
12
|
+
/** The template the field accepts, or several. */
|
|
13
|
+
type?: string | string[]
|
|
14
|
+
/** The templates the field accepts; wins over `type`. */
|
|
15
|
+
templates?: string[]
|
|
10
16
|
value?: string
|
|
11
17
|
onChange?: (value: string) => void
|
|
12
18
|
disabled?: boolean
|
|
@@ -15,41 +21,128 @@ interface SecretInputProps {
|
|
|
15
21
|
exclude?: string[]
|
|
16
22
|
}
|
|
17
23
|
|
|
24
|
+
const NOTICE_TONES = {
|
|
25
|
+
neutral: { box: "border-ibm-gray-30 bg-ibm-gray-10 text-ibm-gray-70", icon: "text-ibm-gray-60" },
|
|
26
|
+
warning: { box: "border-ibm-orange-60 bg-ibm-orange-10 text-ibm-orange-70", icon: "text-ibm-orange-60" },
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Notice({ children, tone = "neutral" }: { children: React.ReactNode; tone?: keyof typeof NOTICE_TONES }) {
|
|
30
|
+
return (
|
|
31
|
+
<div className={`flex items-start gap-2 border px-3 py-2 text-xs ${NOTICE_TONES[tone].box}`}>
|
|
32
|
+
<AlertTriangle className={`h-3.5 w-3.5 mt-0.5 flex-shrink-0 ${NOTICE_TONES[tone].icon}`} />
|
|
33
|
+
<div className="min-w-0 space-y-1.5">{children}</div>
|
|
34
|
+
</div>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Picks a stored configuration among those the field accepts, and that can be used in the
|
|
40
|
+
* environment of the page. One that is accepted but not available there yet is shown apart,
|
|
41
|
+
* with what makes it available.
|
|
42
|
+
*/
|
|
18
43
|
export function SecretInput({
|
|
19
44
|
id,
|
|
20
45
|
type,
|
|
46
|
+
templates,
|
|
21
47
|
value,
|
|
22
48
|
onChange,
|
|
23
49
|
disabled,
|
|
24
50
|
className,
|
|
25
|
-
placeholder = "Select a
|
|
51
|
+
placeholder = "Select a configuration",
|
|
26
52
|
exclude = [],
|
|
27
53
|
}: SecretInputProps) {
|
|
28
|
-
const
|
|
54
|
+
const accepted = templates && templates.length > 0 ? acceptedTemplates(templates) : acceptedTemplates(type)
|
|
55
|
+
const shared = useConfigChoices(accepted)
|
|
56
|
+
const legacy = useSecretContext()
|
|
57
|
+
const [busy, setBusy] = useState<string | null>(null)
|
|
58
|
+
const [failure, setFailure] = useState<string | null>(null)
|
|
29
59
|
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
)
|
|
60
|
+
if (accepted.length === 0) {
|
|
61
|
+
return <Notice><p>This field does not say which kind of configuration it takes, so none can be offered.</p></Notice>
|
|
62
|
+
}
|
|
63
|
+
if (!shared && !legacy) {
|
|
64
|
+
return <Notice><p>Configurations cannot be read on this page.</p></Notice>
|
|
36
65
|
}
|
|
37
66
|
|
|
38
|
-
|
|
39
|
-
|
|
67
|
+
// A page that hands its own list is served from it: the configs of the templates accepted,
|
|
68
|
+
// and never any other.
|
|
69
|
+
const fromLegacy: StoredConfig[] = shared ? [] : compatibleConfigs(
|
|
70
|
+
accepted.flatMap(template => (legacy!.secretsMap.get(template) ?? []).map(entry => ({
|
|
71
|
+
name: entry.name, description: entry.description, template: entry.template ?? template,
|
|
72
|
+
}))),
|
|
73
|
+
accepted,
|
|
74
|
+
)
|
|
75
|
+
const loading = shared ? shared.loading : legacy!.loading
|
|
76
|
+
const available = (shared ? shared.available : fromLegacy).filter(config => !exclude.includes(config.name))
|
|
77
|
+
const undistributed = (shared ? shared.undistributed : []).filter(config => !exclude.includes(config.name))
|
|
78
|
+
const environmentLabel = shared?.environment?.alias || shared?.environment?.name || "this environment"
|
|
79
|
+
const selectedElsewhere = !!value && !loading && !available.some(config => config.name === value)
|
|
80
|
+
const selectedUndistributed = undistributed.find(config => config.name === value)
|
|
81
|
+
|
|
82
|
+
const makeAvailable = async (config: StoredConfig) => {
|
|
83
|
+
if (!shared) return
|
|
84
|
+
setBusy(config.name)
|
|
85
|
+
setFailure(null)
|
|
86
|
+
try {
|
|
87
|
+
await shared.makeAvailable(config)
|
|
88
|
+
if (!value) onChange?.(config.name)
|
|
89
|
+
} catch (error: any) {
|
|
90
|
+
setFailure(error?.message || `Could not make ${configLabel(config)} available`)
|
|
91
|
+
} finally {
|
|
92
|
+
setBusy(null)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
40
95
|
|
|
41
96
|
return (
|
|
42
|
-
<div className="
|
|
43
|
-
<
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
97
|
+
<div className="space-y-2">
|
|
98
|
+
<div className="flex items-center gap-2">
|
|
99
|
+
<SelectInput
|
|
100
|
+
id={id}
|
|
101
|
+
value={value}
|
|
102
|
+
onChange={onChange}
|
|
103
|
+
disabled={disabled || loading}
|
|
104
|
+
className={className}
|
|
105
|
+
placeholder={loading ? "Loading…" : available.length === 0 ? "Nothing to select" : placeholder}
|
|
106
|
+
options={[
|
|
107
|
+
...available.map(config => ({ label: configLabel(config), value: config.name })),
|
|
108
|
+
...(selectedElsewhere ? [{ label: `${selectedUndistributed ? configLabel(selectedUndistributed) : value} (not available here)`, value: value! }] : []),
|
|
109
|
+
]}
|
|
110
|
+
/>
|
|
111
|
+
{loading && <Loader2 className="h-4 w-4 animate-spin text-ibm-blue-60 flex-shrink-0" />}
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
{selectedElsewhere && !selectedUndistributed && (
|
|
115
|
+
<Notice tone="warning"><p>The configuration selected no longer exists, or is of another kind. Select another one.</p></Notice>
|
|
116
|
+
)}
|
|
117
|
+
|
|
118
|
+
{!loading && undistributed.length > 0 && (
|
|
119
|
+
<Notice tone="warning">
|
|
120
|
+
<p className="font-medium">
|
|
121
|
+
{undistributed.length === 1 ? "One configuration is" : `${undistributed.length} configurations are`} not available in {environmentLabel} yet.
|
|
122
|
+
</p>
|
|
123
|
+
<ul className="space-y-1">
|
|
124
|
+
{undistributed.map(config => (
|
|
125
|
+
<li key={config.name} className="flex items-center justify-between gap-3">
|
|
126
|
+
<span className="truncate text-ibm-gray-100">{configLabel(config)}</span>
|
|
127
|
+
<button
|
|
128
|
+
type="button"
|
|
129
|
+
onClick={() => makeAvailable(config)}
|
|
130
|
+
disabled={disabled || busy !== null}
|
|
131
|
+
className="flex-shrink-0 text-ibm-blue-60 hover:text-ibm-blue-70 disabled:opacity-40"
|
|
132
|
+
>
|
|
133
|
+
{busy === config.name ? "Making available…" : "Make available"}
|
|
134
|
+
</button>
|
|
135
|
+
</li>
|
|
136
|
+
))}
|
|
137
|
+
</ul>
|
|
138
|
+
</Notice>
|
|
139
|
+
)}
|
|
140
|
+
|
|
141
|
+
{!loading && available.length === 0 && undistributed.length === 0 && !selectedElsewhere && (
|
|
142
|
+
<p className="text-xs text-ibm-gray-50">No configuration of this kind exists yet.</p>
|
|
143
|
+
)}
|
|
144
|
+
|
|
145
|
+
{(failure || shared?.error) && <p role="alert" className="text-xs text-red-600">{failure || shared?.error}</p>}
|
|
53
146
|
</div>
|
|
54
147
|
)
|
|
55
148
|
}
|
|
@@ -91,7 +91,7 @@ export function SecretsInput({ id, value, onChange, disabled = false, ...rest }:
|
|
|
91
91
|
|
|
92
92
|
<div className="flex gap-2">
|
|
93
93
|
<div className="flex-1">
|
|
94
|
-
<SecretInput value={draft} onChange={setDraft} disabled={disabled} type={
|
|
94
|
+
<SecretInput value={draft} onChange={setDraft} disabled={disabled} type={rest.type} templates={rest.templates} exclude={items.map(parseName)} />
|
|
95
95
|
</div>
|
|
96
96
|
<Button
|
|
97
97
|
type="button"
|
package/index.ts
CHANGED
|
@@ -3,6 +3,8 @@ export * from './lib/utils'
|
|
|
3
3
|
export * from './lib/menu-utils'
|
|
4
4
|
export * from './lib/error-utils'
|
|
5
5
|
export { BaseService } from './lib/base-service'
|
|
6
|
+
export * from './lib/config-selection'
|
|
7
|
+
export { createPlatformConfigSource, templateFilter, configOfHit, environmentWithTargets, type ConfigSource } from './lib/config-source'
|
|
6
8
|
export { default as HttpClient, useHttpClient, ApiError } from './lib/http-client'
|
|
7
9
|
|
|
8
10
|
// UI Components
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which stored configs a field may offer.
|
|
3
|
+
*
|
|
4
|
+
* Two things decide it, and nothing else:
|
|
5
|
+
*
|
|
6
|
+
* 1. the template the config was created from — a field states which templates it accepts,
|
|
7
|
+
* and a config of any other is never offered;
|
|
8
|
+
* 2. the environment the form is for — a config is only usable where it has been
|
|
9
|
+
* distributed, so one that is not distributed to every target of the environment is not
|
|
10
|
+
* offered either. It is told apart, so that it can be made available there.
|
|
11
|
+
*
|
|
12
|
+
* A field that states no template accepts nothing. It is never taken to accept everything:
|
|
13
|
+
* that is how a certificate came to be offered where a script was asked for.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface ConfigTemplateRef {
|
|
17
|
+
id?: string
|
|
18
|
+
ids?: string[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What a form field says of the configs it picks, as its schema states it. */
|
|
22
|
+
export interface ConfigFieldSchema {
|
|
23
|
+
uiType?: string
|
|
24
|
+
template?: ConfigTemplateRef | null
|
|
25
|
+
subParameters?: ConfigFieldSchema[] | null
|
|
26
|
+
additionalParameter?: ConfigFieldSchema | null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface StoredConfig {
|
|
30
|
+
name: string
|
|
31
|
+
alias?: string
|
|
32
|
+
description?: string
|
|
33
|
+
/** The template the config was created from. */
|
|
34
|
+
template: string
|
|
35
|
+
namespace?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Where an environment deploys to. */
|
|
39
|
+
export interface ConfigTarget {
|
|
40
|
+
clusterName: string
|
|
41
|
+
namespace: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ConfigDistribution {
|
|
45
|
+
name: string
|
|
46
|
+
configs: Array<{ name: string; namespace?: string }>
|
|
47
|
+
targets: ConfigTarget[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ConfigEnvironment {
|
|
51
|
+
name: string
|
|
52
|
+
/** How the environment is called on screen. */
|
|
53
|
+
alias?: string
|
|
54
|
+
targets: ConfigTarget[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ConfigChoices {
|
|
58
|
+
/** Compatible, and usable in the environment: what the field offers. */
|
|
59
|
+
available: StoredConfig[]
|
|
60
|
+
/** Compatible, but not distributed to the environment yet. */
|
|
61
|
+
undistributed: StoredConfig[]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function cleaned(values: Array<string | null | undefined>): string[] {
|
|
65
|
+
const seen = new Set<string>()
|
|
66
|
+
const kept: string[] = []
|
|
67
|
+
for (const value of values) {
|
|
68
|
+
const text = typeof value === "string" ? value.trim() : ""
|
|
69
|
+
if (text === "" || seen.has(text)) continue
|
|
70
|
+
seen.add(text)
|
|
71
|
+
kept.push(text)
|
|
72
|
+
}
|
|
73
|
+
return kept
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The templates a field accepts, each once, in the order stated. None when it states none. */
|
|
77
|
+
export function acceptedTemplates(template: ConfigTemplateRef | string | string[] | null | undefined): string[] {
|
|
78
|
+
if (!template) return []
|
|
79
|
+
if (typeof template === "string") return cleaned([template])
|
|
80
|
+
if (Array.isArray(template)) return cleaned(template)
|
|
81
|
+
return cleaned([template.id, ...(Array.isArray(template.ids) ? template.ids : [])])
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Every template the fields of a form accept, those inside other fields included. */
|
|
85
|
+
export function templatesOfSchema(schema: Array<ConfigFieldSchema | null | undefined> | null | undefined): string[] {
|
|
86
|
+
const found: string[] = []
|
|
87
|
+
const visit = (field: ConfigFieldSchema | null | undefined) => {
|
|
88
|
+
if (!field) return
|
|
89
|
+
found.push(...acceptedTemplates(field.template))
|
|
90
|
+
for (const inner of field.subParameters ?? []) visit(inner)
|
|
91
|
+
visit(field.additionalParameter)
|
|
92
|
+
}
|
|
93
|
+
for (const field of schema ?? []) visit(field)
|
|
94
|
+
return cleaned(found)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The configs of the templates accepted, each once, in the order given. */
|
|
98
|
+
export function compatibleConfigs(configs: StoredConfig[], accepted: string[]): StoredConfig[] {
|
|
99
|
+
if (accepted.length === 0) return []
|
|
100
|
+
const wanted = new Set(accepted)
|
|
101
|
+
const seen = new Set<string>()
|
|
102
|
+
return configs.filter(config => {
|
|
103
|
+
if (!wanted.has(config.template) || seen.has(config.name)) return false
|
|
104
|
+
seen.add(config.name)
|
|
105
|
+
return true
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const sameTarget = (left: ConfigTarget, right: ConfigTarget) =>
|
|
110
|
+
left.clusterName === right.clusterName && left.namespace === right.namespace
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether a config can be used in an environment: it is distributed to every target the
|
|
114
|
+
* environment deploys to. An environment with no target deploys nowhere, and nothing is
|
|
115
|
+
* usable in it.
|
|
116
|
+
*/
|
|
117
|
+
export function isDistributedTo(configName: string, environment: ConfigEnvironment, distributions: ConfigDistribution[]): boolean {
|
|
118
|
+
if (environment.targets.length === 0) return false
|
|
119
|
+
const reached = distributions
|
|
120
|
+
.filter(distribution => distribution.configs.some(config => config.name === configName))
|
|
121
|
+
.flatMap(distribution => distribution.targets)
|
|
122
|
+
return environment.targets.every(target => reached.some(covered => sameTarget(covered, target)))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* What a field offers, and what it could offer once distributed. With no environment —
|
|
127
|
+
* a form that is not for one — every compatible config is offered.
|
|
128
|
+
*/
|
|
129
|
+
export function configChoices(
|
|
130
|
+
configs: StoredConfig[],
|
|
131
|
+
accepted: string[],
|
|
132
|
+
environment: ConfigEnvironment | null | undefined,
|
|
133
|
+
distributions: ConfigDistribution[],
|
|
134
|
+
): ConfigChoices {
|
|
135
|
+
const compatible = compatibleConfigs(configs, accepted)
|
|
136
|
+
if (!environment) return { available: compatible, undistributed: [] }
|
|
137
|
+
const available: StoredConfig[] = []
|
|
138
|
+
const undistributed: StoredConfig[] = []
|
|
139
|
+
for (const config of compatible) {
|
|
140
|
+
(isDistributedTo(config.name, environment, distributions) ? available : undistributed).push(config)
|
|
141
|
+
}
|
|
142
|
+
return { available, undistributed }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A name a distribution can take: lower case letters, digits and dashes, 63 at most. */
|
|
146
|
+
export function distributionName(configName: string, environmentName: string): string {
|
|
147
|
+
const safe = (text: string) => text.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "")
|
|
148
|
+
const environment = safe(environmentName).slice(0, 24).replace(/-$/, "")
|
|
149
|
+
const config = safe(configName).slice(0, 63 - environment.length - 1).replace(/-$/, "")
|
|
150
|
+
return `${config}-${environment}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface DistributionRequest {
|
|
154
|
+
name: string
|
|
155
|
+
configs: Array<{ name: string; namespace: string }>
|
|
156
|
+
targets: ConfigTarget[]
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* What makes a config usable in an environment: a distribution of its own, to every target
|
|
161
|
+
* of the environment. It is named after the config and the environment, so that it never
|
|
162
|
+
* replaces the distribution of another config.
|
|
163
|
+
*/
|
|
164
|
+
export function distributionRequest(config: StoredConfig, environment: ConfigEnvironment): DistributionRequest {
|
|
165
|
+
return {
|
|
166
|
+
name: distributionName(config.name, environment.name),
|
|
167
|
+
configs: [{ name: config.name, namespace: config.namespace ?? "" }],
|
|
168
|
+
targets: environment.targets.map(target => ({ clusterName: target.clusterName, namespace: target.namespace })),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** How a config is shown in a list: its alias, else its description, else its name. */
|
|
173
|
+
export function configLabel(config: StoredConfig): string {
|
|
174
|
+
return config.alias?.trim() || config.description?.trim() || config.name
|
|
175
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import HttpClient, { type AuthHeadersProvider } from "./http-client"
|
|
2
|
+
import type { ConfigDistribution, ConfigEnvironment, DistributionRequest, StoredConfig } from "./config-selection"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where the fields that pick a config read from, and how one is made available in an
|
|
6
|
+
* environment. The fields depend on this, never on the service of one app: each app hands
|
|
7
|
+
* the same thing, built from the address of the platform and its own credentials.
|
|
8
|
+
*/
|
|
9
|
+
export interface ConfigSource {
|
|
10
|
+
/** The configs of a business unit created from any of the templates named. */
|
|
11
|
+
configsOf(project: string, templates: string[]): Promise<StoredConfig[]>
|
|
12
|
+
/** Where each config of a business unit has been distributed. */
|
|
13
|
+
distributionsOf(project: string): Promise<ConfigDistribution[]>
|
|
14
|
+
/** An environment with the targets it deploys to; null when it is not known. */
|
|
15
|
+
environmentOf(project: string, environmentName: string): Promise<ConfigEnvironment | null>
|
|
16
|
+
/** Distributes a config. */
|
|
17
|
+
distribute(project: string, request: DistributionRequest): Promise<void>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Search answers a page at a time: more configs than this of one kind is not expected. */
|
|
21
|
+
const SEARCH_LIMIT = 500
|
|
22
|
+
|
|
23
|
+
const quoted = (value: string) => `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`
|
|
24
|
+
|
|
25
|
+
/** The search filter that keeps the configs of the templates named, and only those. */
|
|
26
|
+
export function templateFilter(templates: string[]): string {
|
|
27
|
+
return templates.map(template => `template = ${quoted(template)}`).join(" OR ")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface SearchHit {
|
|
31
|
+
id?: string
|
|
32
|
+
alias?: string
|
|
33
|
+
description?: string
|
|
34
|
+
template?: string
|
|
35
|
+
namespace?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A search hit as a config; null for one that has no name or no template. */
|
|
39
|
+
export function configOfHit(hit: SearchHit | null | undefined): StoredConfig | null {
|
|
40
|
+
if (!hit || typeof hit.id !== "string" || hit.id === "" || typeof hit.template !== "string" || hit.template === "") return null
|
|
41
|
+
return {
|
|
42
|
+
name: hit.id,
|
|
43
|
+
template: hit.template,
|
|
44
|
+
...(hit.alias ? { alias: hit.alias } : {}),
|
|
45
|
+
...(hit.description ? { description: hit.description } : {}),
|
|
46
|
+
...(hit.namespace ? { namespace: hit.namespace } : {}),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ListedEnvironment {
|
|
51
|
+
name: string
|
|
52
|
+
alias?: string
|
|
53
|
+
targets?: Array<{ name: string }>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface ListedTarget {
|
|
57
|
+
name: string
|
|
58
|
+
cluster?: { clusterName?: string; namespace?: string }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** An environment with where its targets deploy; a target with no place is left out. */
|
|
62
|
+
export function environmentWithTargets(environment: ListedEnvironment, targets: ListedTarget[]): ConfigEnvironment {
|
|
63
|
+
const named = new Set((environment.targets ?? []).map(target => target.name))
|
|
64
|
+
return {
|
|
65
|
+
name: environment.name,
|
|
66
|
+
...(environment.alias ? { alias: environment.alias } : {}),
|
|
67
|
+
targets: targets
|
|
68
|
+
.filter(target => named.has(target.name) && target.cluster?.clusterName && target.cluster?.namespace)
|
|
69
|
+
.map(target => ({ clusterName: target.cluster!.clusterName!, namespace: target.cluster!.namespace! })),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The platform's own API as a config source. */
|
|
74
|
+
export function createPlatformConfigSource(baseUrl: string, authHeaders?: AuthHeadersProvider): ConfigSource {
|
|
75
|
+
const http = HttpClient.getInstance(baseUrl, authHeaders)
|
|
76
|
+
const as = (project: string) => ({ headers: { "x-business-unit-id": project } })
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
async configsOf(project, templates) {
|
|
80
|
+
if (templates.length === 0) return []
|
|
81
|
+
const answer = await http.post<{ hits?: SearchHit[] }>(
|
|
82
|
+
"/secret/v1/project/configs/search",
|
|
83
|
+
{ q: "", limit: SEARCH_LIMIT, offset: 0, filter: templateFilter(templates) },
|
|
84
|
+
as(project),
|
|
85
|
+
)
|
|
86
|
+
const accepted = new Set(templates)
|
|
87
|
+
return (answer?.hits ?? [])
|
|
88
|
+
.map(configOfHit)
|
|
89
|
+
.filter((config): config is StoredConfig => config !== null && accepted.has(config.template))
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async distributionsOf(project) {
|
|
93
|
+
const answer = await http.get<{ distributions?: ConfigDistribution[] }>("/secret/v1/project/distributions", as(project))
|
|
94
|
+
return (answer?.distributions ?? []).map(distribution => ({
|
|
95
|
+
name: distribution.name,
|
|
96
|
+
configs: distribution.configs ?? [],
|
|
97
|
+
targets: distribution.targets ?? [],
|
|
98
|
+
}))
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
async environmentOf(project, environmentName) {
|
|
102
|
+
const [environments, targets] = await Promise.all([
|
|
103
|
+
http.get<{ envs?: ListedEnvironment[] }>("/cloud/v1/environments", as(project)),
|
|
104
|
+
http.get<{ targets?: ListedTarget[] }>("/cloud/v1/targets", as(project)),
|
|
105
|
+
])
|
|
106
|
+
const environment = (environments?.envs ?? []).find(listed => listed.name === environmentName)
|
|
107
|
+
return environment ? environmentWithTargets(environment, targets?.targets ?? []) : null
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async distribute(project, request) {
|
|
111
|
+
await http.post("/secret/v1/project/distributions", request, as(project))
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
}
|