-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathresources-api.ts
More file actions
253 lines (244 loc) · 9.11 KB
/
resources-api.ts
File metadata and controls
253 lines (244 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import {
ResourceType, CreateResourceData, FindMethodOptions, FindAllMethodOptions, FindFilteredPageOptions, FindFilteredLabelSelectorOptions,
FindFilteredPageResponse, FindFilteredLabelSelectorResponse
} from './resource-base';
import { ResourceInstance } from './resource-instance';
import { SteveListResponse } from '@shell/types/rancher/steve.api';
/**
* Base interface for all resource API operations.
*
* Provides type-safe methods for interacting with Kubernetes and Rancher resources.
* Used by both cluster-scoped and management-scoped APIs, and can be extended
* by custom store implementations (e.g. Harvester, Epinio).
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const deployment = await resources.cluster.find(K8S.DEPLOYMENT, 'default/my-app');
* ```
*/
export interface ResourcesApi {
/**
* Finds a specific resource by its type and ID.
*
* @template T - The type of the resource (defaults to ResourceInstance)
* @param resourceType - The type of the resource to find (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param resourceId - The unique identifier of the resource to find. If the resource is namespaced, this should be in the format `namespace/name`.
* @param options - Optional find arguments
* @returns The found resource item or null if not found.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* // Namespaced resource - ID must be in "namespace/name" format
* const pod = await resources.cluster.find(K8S.POD, 'default/my-pod-123');
*
* // Cluster-scoped resource - ID is just the name
* const node = await resources.cluster.find(K8S.NODE, 'worker-1');
* ```
*/
find<T = ResourceInstance>(
resourceType: ResourceType,
resourceId: string,
options?: FindMethodOptions
): Promise<T | null>;
/**
* Finds resources using pagination mode with server-side filtering, sorting, and pagination.
*
* Requires `ui-sql-cache` to be enabled.
*
* @template T - The type of the resources (defaults to ResourceInstance)
* @param resourceType - The type of the resources to find (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param options - Pagination options with server-side filtering and sorting via the Steve API's pagination cache. See {@link FindFilteredPageOptions}.
* @returns Response containing resource items (may be transient if requested, otherwise cached array).
* @throws Error if pagination mode is requested but `ui-sql-cache` is not enabled.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const pods = await resources.cluster.findFiltered(K8S.POD, {
* pagination: {
* page: 1,
* pageSize: 10,
* filters: [],
* sort: []
* }
* });
* ```
*/
findFiltered<T = SteveListResponse>(
resourceType: ResourceType,
options: FindFilteredPageOptions
): Promise<FindFilteredPageResponse<T>>;
/**
* Finds resources using label selector matching.
*
* Filters resources by Kubernetes labels. The store automatically handles pagination:
* - If `ui-sql-cache` is enabled: uses server-side pagination
* - Otherwise: uses native Kubernetes API pagination
*
* @template T - The type of the resources (defaults to SteveListResponse)
* @param resourceType - The type of the resources to find (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param options - Label selector options for filtering. See {@link FindFilteredLabelSelectorOptions}.
* @returns Response containing resource items (may be transient if requested, otherwise cached array).
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const pods = await resources.cluster.findFiltered(K8S.POD, {
* labelSelector: { matchLabels: { type: 'my-type' } }
* });
* ```
*/
findFiltered<T = SteveListResponse>(
resourceType: ResourceType,
options: FindFilteredLabelSelectorOptions
): Promise<FindFilteredLabelSelectorResponse<T>>;
/**
* @internal Implementation - use one of the overloads above
*/
findFiltered<T = SteveListResponse>(
resourceType: ResourceType,
options: FindFilteredPageOptions | FindFilteredLabelSelectorOptions
): Promise<FindFilteredPageResponse<T> | FindFilteredLabelSelectorResponse<T>>;
/**
* Fetches all resources of a specific type with advanced options.
* This method provides additional capabilities like incremental loading and namespace filtering.
*
* @template T - The type of the resources (defaults to SteveListResponse)
* @param resourceType - The type of the resources to find (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param options - Optional advanced fetch options (incremental loading, namespace filtering, etc.)
* @returns An array of resource items or an empty array if none are found.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
* const allPods = await resources.cluster.findAll(K8S.POD, {
* namespaced: ['default', 'kube-system']
* });
* ```
*/
findAll<T = SteveListResponse>(
resourceType: ResourceType,
options?: FindAllMethodOptions
): Promise<T[]>;
/**
* Creates a new resource.
*
* The `data` object must include a `type` property identifying the resource type.
* Checks `canCreate` permissions before saving.
*
* @template T - The type of the resource (defaults to ResourceInstance)
* @param data - The resource data to create. Must include a `type` property (use **{@link K8S}** constant). See also {@link CreateResourceData}.
* @returns The created resource instance.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const configMap = await resources.cluster.create({
* type: K8S.CONFIG_MAP,
* metadata: { name: 'my-config', namespace: 'default' },
* data: { key: 'value' }
* });
* ```
*/
create<T = ResourceInstance>(
data: CreateResourceData
): Promise<T>;
/**
* Applies a partial update to a resource using HTTP PATCH (merge-patch).
*
* Only the fields provided in `data` are sent to the server.
* This is a raw HTTP operation — it does not check permissions or update the store cache.
*
* @template T - The type of the response (defaults to ResourceInstance)
* @param resourceType - The type of the resource (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param resourceId - The unique identifier. If namespaced, use `namespace/name` format.
* @param data - An object containing only the fields to update.
* @returns The server response.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const result = await resources.cluster.patch(K8S.CONFIG_MAP, 'default/my-config', {
* data: { newKey: 'newValue' }
* });
* ```
*/
patch<T = ResourceInstance>(
resourceType: ResourceType,
resourceId: string,
data: Record<string, any>
): Promise<T>;
/**
* Performs a full replacement update of a resource using HTTP PUT.
*
* Runs `cleanForSave` on the data before sending.
* This is a raw HTTP operation — it does not check permissions or update the store cache.
*
* @template T - The type of the response (defaults to ResourceInstance)
* @param resourceType - The type of the resource (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param resourceId - The unique identifier. If namespaced, use `namespace/name` format.
* @param data - The complete resource data to send as the replacement.
* @returns The server response.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* const result = await resources.cluster.update(K8S.CONFIG_MAP, 'default/my-config', {
* type: 'configmap',
* metadata: { name: 'my-config', namespace: 'default', resourceVersion: '12345' },
* data: { key: 'replacedValue' }
* });
* ```
*/
update<T = ResourceInstance>(
resourceType: ResourceType,
resourceId: string,
data: Record<string, any>
): Promise<T>;
/**
* Deletes a resource by type and ID using HTTP DELETE.
*
* This is a raw HTTP operation — it does not check permissions or update the store cache.
*
* @param resourceType - The type of the resource (use **{@link K8S}** constant). See also {@link ResourceType}.
* @param resourceId - The unique identifier. If namespaced, use `namespace/name` format.
*
* @example
* ```ts
* import { useResources, K8S } from '@shell/apis';
*
* const resources = useResources();
*
* await resources.cluster.delete(K8S.CONFIG_MAP, 'default/my-config');
* ```
*/
delete(
resourceType: ResourceType,
resourceId: string
): Promise<void>;
}