|
| 1 | +import cuid from 'cuid' |
| 2 | +import { google } from '@google-cloud/notebooks/build/protos/protos' |
| 3 | +import { |
| 4 | + GcpAiPlatformNotebook, |
| 5 | + GcpAiPlatformNotebookDisk, |
| 6 | +} from '../../types/generated' |
| 7 | +import { enumKeyToString, formatKeyValueMap, formatLabelsFromMap } from '../../utils/format' |
| 8 | +import { toISOString } from '../../utils/dateutils' |
| 9 | +import { RawGcpAiPlatformNotebook } from './data' |
| 10 | + |
| 11 | + |
| 12 | +// TODO disk object is not available in the v1beta1 version. |
| 13 | +// This definition is base on unused type of v1 version and SDK document: |
| 14 | +// https://cloud.google.com/vertex-ai/docs/workbench/reference/rest/v1/projects.locations.instances |
| 15 | +const formatDisk = (disk: google.cloud.notebooks.v1.Instance.IDisk): GcpAiPlatformNotebookDisk => { |
| 16 | + const { |
| 17 | + autoDelete, |
| 18 | + boot, |
| 19 | + deviceName, |
| 20 | + diskSizeGb, |
| 21 | + guestOsFeatures = [], |
| 22 | + index, |
| 23 | + kind, |
| 24 | + licenses = [], |
| 25 | + mode, |
| 26 | + source, |
| 27 | + type, |
| 28 | + } = disk |
| 29 | + |
| 30 | + return { |
| 31 | + id: cuid(), |
| 32 | + autoDelete, |
| 33 | + boot, |
| 34 | + deviceName, |
| 35 | + diskSizeGb: diskSizeGb?.toString() || '0', |
| 36 | + guestOsFeaturesTypes: guestOsFeatures?.map(f => f?.type), |
| 37 | + index: index?.toString() || '0', |
| 38 | + kind, |
| 39 | + licenses, |
| 40 | + mode, |
| 41 | + source, |
| 42 | + type, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// TODO object definition is based on the v1beta1 version |
| 47 | +// Couple fields are missing from the document |
| 48 | +// Need to update the SDK once a stable version is released |
| 49 | +export default ({ |
| 50 | + service, |
| 51 | + account, |
| 52 | + region, |
| 53 | +}: { |
| 54 | + service: RawGcpAiPlatformNotebook |
| 55 | + account: string |
| 56 | + region: string |
| 57 | +}): GcpAiPlatformNotebook => { |
| 58 | + const { |
| 59 | + id, |
| 60 | + projectId, |
| 61 | + Labels = {}, |
| 62 | + name, |
| 63 | + vmImage = {}, |
| 64 | + containerImage = {}, |
| 65 | + postStartupScript, |
| 66 | + proxyUri, |
| 67 | + instanceOwners = [], |
| 68 | + serviceAccount, |
| 69 | + // serviceAccountScopes = [], // v1 |
| 70 | + machineType, |
| 71 | + acceleratorConfig = {}, |
| 72 | + state, |
| 73 | + installGpuDriver, |
| 74 | + customGpuDriverPath, |
| 75 | + bootDiskType, |
| 76 | + bootDiskSizeGb, |
| 77 | + dataDiskType, |
| 78 | + dataDiskSizeGb, |
| 79 | + noRemoveDataDisk, |
| 80 | + diskEncryption, |
| 81 | + // disks = [], // v1 |
| 82 | + // shieldedInstanceConfig, // v1 |
| 83 | + noPublicIp, |
| 84 | + noProxyAccess, |
| 85 | + // network, |
| 86 | + // subnet, |
| 87 | + metadata, |
| 88 | + // tags = [], // v1 |
| 89 | + // upgradeHistory, // v1 |
| 90 | + // nicType, // v1 |
| 91 | + // reservationAffinity, // v1 |
| 92 | + createTime, |
| 93 | + updateTime, |
| 94 | + } = service |
| 95 | + |
| 96 | + return { |
| 97 | + id: id || cuid(), |
| 98 | + projectId, |
| 99 | + region, |
| 100 | + labels: formatLabelsFromMap(Labels), |
| 101 | + name, |
| 102 | + vmImageProject: vmImage?.project || '', |
| 103 | + vmImageImageName: vmImage?.imageName || '', |
| 104 | + vmImageImageFamily: vmImage?.imageFamily || '', |
| 105 | + containerImageRepository: containerImage?.repository || '', |
| 106 | + containerImageTag: containerImage?.tag || '', |
| 107 | + postStartupScript, |
| 108 | + proxyUri, |
| 109 | + instanceOwners, |
| 110 | + serviceAccount, |
| 111 | + machineType, |
| 112 | + acceleratorConfigType: enumKeyToString(google.cloud.notebooks.v1beta1.Instance.AcceleratorType, acceleratorConfig?.type), |
| 113 | + acceleratorConfigCoreCount: acceleratorConfig?.coreCount?.toString() || '0', |
| 114 | + state: enumKeyToString(google.cloud.notebooks.v1.Instance.State, state), |
| 115 | + installGpuDriver, |
| 116 | + customGpuDriverPath, |
| 117 | + bootDiskType: enumKeyToString(google.cloud.notebooks.v1.Instance.DiskType, bootDiskType), |
| 118 | + bootDiskSizeGb: bootDiskSizeGb?.toString() || '0', |
| 119 | + dataDiskType: enumKeyToString(google.cloud.notebooks.v1.Instance.DiskType, dataDiskType), |
| 120 | + dataDiskSizeGb: dataDiskSizeGb?.toString() || '0', |
| 121 | + noRemoveDataDisk, |
| 122 | + diskEncryption: enumKeyToString(google.cloud.notebooks.v1.Instance.DiskEncryption, diskEncryption), |
| 123 | + noPublicIp, |
| 124 | + noProxyAccess, |
| 125 | + metadata: formatKeyValueMap(metadata), |
| 126 | + createTime: toISOString(createTime?.seconds?.toString()), |
| 127 | + updateTime: toISOString(updateTime?.seconds?.toString()), |
| 128 | + // kmsKey - connection |
| 129 | + // network - connection |
| 130 | + // subnet - connection |
| 131 | + } |
| 132 | +} |
0 commit comments