Skip to content

Commit 24f78bc

Browse files
authored
feat(instance): add snapshot export (#75)
1 parent bbbd435 commit 24f78bc

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

packages/clients/src/api/instance/v1/api.gen.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
marshalCreateServerRequest,
1818
marshalCreateSnapshotRequest,
1919
marshalCreateVolumeRequest,
20+
marshalExportSnapshotRequest,
2021
marshalServerActionRequest,
2122
marshalSetImageRequest,
2223
marshalSetPlacementGroupRequest,
@@ -40,6 +41,7 @@ import {
4041
unmarshalCreateServerResponse,
4142
unmarshalCreateSnapshotResponse,
4243
unmarshalCreateVolumeResponse,
44+
unmarshalExportSnapshotResponse,
4345
unmarshalGetBootscriptResponse,
4446
unmarshalGetDashboardResponse,
4547
unmarshalGetImageResponse,
@@ -110,6 +112,8 @@ import type {
110112
DeleteServerUserDataRequest,
111113
DeleteSnapshotRequest,
112114
DeleteVolumeRequest,
115+
ExportSnapshotRequest,
116+
ExportSnapshotResponse,
113117
GetBootscriptRequest,
114118
GetBootscriptResponse,
115119
GetDashboardRequest,
@@ -627,7 +631,7 @@ export class InstanceV1GenAPI extends API {
627631
enrichForPagination('snapshots', this.pageOfListSnapshots, request)
628632

629633
/**
630-
* Create a snapshot from a given volume
634+
* Create a snapshot from a given volume or from a QCOW2 file
631635
*
632636
* @param request - The request {@link CreateSnapshotRequest}
633637
* @returns A Promise of CreateSnapshotResponse
@@ -696,6 +700,31 @@ export class InstanceV1GenAPI extends API {
696700
)}/snapshots/${validatePathParam('snapshotId', request.snapshotId)}`,
697701
})
698702

703+
/**
704+
* Export a snapshot to a given S3 bucket in the same region.
705+
*
706+
* @param request - The request {@link ExportSnapshotRequest}
707+
* @returns A Promise of ExportSnapshotResponse
708+
*/
709+
exportSnapshot = (request: Readonly<ExportSnapshotRequest>) =>
710+
this.client.fetch<ExportSnapshotResponse>(
711+
{
712+
body: JSON.stringify(
713+
marshalExportSnapshotRequest(request, this.client.settings),
714+
),
715+
headers: jsonContentHeaders,
716+
method: 'POST',
717+
path: `/instance/v1/zones/${validatePathParam(
718+
'zone',
719+
request.zone ?? this.client.settings.defaultZone,
720+
)}/snapshots/${validatePathParam(
721+
'snapshotId',
722+
request.snapshotId,
723+
)}/export`,
724+
},
725+
unmarshalExportSnapshotResponse,
726+
)
727+
699728
protected pageOfListVolumes = (request: Readonly<ListVolumesRequest> = {}) =>
700729
this.client.fetch<ListVolumesResponse>(
701730
{

packages/clients/src/api/instance/v1/marshalling.gen.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import type {
2929
CreateVolumeRequest,
3030
CreateVolumeResponse,
3131
Dashboard,
32+
ExportSnapshotRequest,
33+
ExportSnapshotResponse,
3234
GetBootscriptResponse,
3335
GetDashboardResponse,
3436
GetImageResponse,
@@ -645,6 +647,7 @@ const unmarshalSnapshot = (data: unknown) => {
645647
? unmarshalSnapshotBaseVolume(data.base_volume)
646648
: undefined,
647649
creationDate: unmarshalDate(data.creation_date),
650+
errorReason: data.error_reason,
648651
id: data.id,
649652
modificationDate: unmarshalDate(data.modification_date),
650653
name: data.name,
@@ -814,6 +817,18 @@ export const unmarshalCreateVolumeResponse = (data: unknown) => {
814817
} as CreateVolumeResponse
815818
}
816819

820+
export const unmarshalExportSnapshotResponse = (data: unknown) => {
821+
if (!isJSONObject(data)) {
822+
throw new TypeError(
823+
`Unmarshalling the type 'ExportSnapshotResponse' failed as data isn't a dictionary.`,
824+
)
825+
}
826+
827+
return {
828+
task: data.task ? unmarshalTask(data.task) : undefined,
829+
} as ExportSnapshotResponse
830+
}
831+
817832
export const unmarshalGetBootscriptResponse = (data: unknown) => {
818833
if (!isJSONObject(data)) {
819834
throw new TypeError(
@@ -1753,7 +1768,10 @@ export const marshalCreateSnapshotRequest = (
17531768
request: CreateSnapshotRequest,
17541769
defaults: DefaultValues,
17551770
): Record<string, unknown> => ({
1771+
bucket: request.bucket,
1772+
key: request.key,
17561773
name: request.name || randomName('snp'),
1774+
size: request.size,
17571775
tags: request.tags,
17581776
volume_id: request.volumeId,
17591777
volume_type: request.volumeType,
@@ -1806,6 +1824,14 @@ export const marshalCreateVolumeRequest = (
18061824
]),
18071825
})
18081826

1827+
export const marshalExportSnapshotRequest = (
1828+
request: ExportSnapshotRequest,
1829+
defaults: DefaultValues,
1830+
): Record<string, unknown> => ({
1831+
bucket: request.bucket,
1832+
key: request.key,
1833+
})
1834+
18091835
export const marshalServerActionRequest = (
18101836
request: ServerActionRequest,
18111837
defaults: DefaultValues,

packages/clients/src/api/instance/v1/types.gen.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export interface Dashboard {
167167
ipsUnused: number
168168
}
169169

170+
export interface ExportSnapshotResponse {
171+
task?: Task
172+
}
173+
170174
export interface GetBootscriptResponse {
171175
bootscript?: Bootscript
172176
}
@@ -707,6 +711,8 @@ export interface Snapshot {
707711
modificationDate?: Date
708712
/** The snapshot zone */
709713
zone: Zone
714+
/** The reason for the failed snapshot import */
715+
errorReason?: string
710716
}
711717

712718
/** Snapshot. base volume */
@@ -1074,6 +1080,12 @@ export type CreateSnapshotRequest = {
10741080
* the original volume will be used.
10751081
*/
10761082
volumeType: SnapshotVolumeType
1083+
/** Bucket name for snapshot imports */
1084+
bucket?: string
1085+
/** Object key for snapshot imports */
1086+
key?: string
1087+
/** Imported snapshot size, must be a multiple of 512 */
1088+
size?: number
10771089
}
10781090

10791091
export type GetSnapshotRequest = {
@@ -1090,6 +1102,17 @@ export type DeleteSnapshotRequest = {
10901102
snapshotId: string
10911103
}
10921104

1105+
export type ExportSnapshotRequest = {
1106+
/** Zone to target. If none is passed will use default zone from the config */
1107+
zone?: Zone
1108+
/** The snapshot ID */
1109+
snapshotId: string
1110+
/** S3 bucket name */
1111+
bucket: string
1112+
/** S3 object key */
1113+
key: string
1114+
}
1115+
10931116
export type ListVolumesRequest = {
10941117
/** Zone to target. If none is passed will use default zone from the config */
10951118
zone?: Zone

0 commit comments

Comments
 (0)