Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions packages/core/src/shared/clients/sagemaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ import { yes, no, continueText, cancel } from '../localizedText'
import { AwsCredentialIdentity } from '@aws-sdk/types'
import globals from '../extensionGlobals'

const appTypeSettingsMap: Record<string, string> = {
[AppType.JupyterLab as string]: 'JupyterLabAppSettings',
[AppType.CodeEditor as string]: 'CodeEditorAppSettings',
} as const

export interface SagemakerSpaceApp extends SpaceDetails {
App?: AppDetails
DomainSpaceKey: string
Expand Down Expand Up @@ -136,13 +141,13 @@ export class SagemakerClient extends ClientWrapper<SageMakerClient> {

// Get app type
const appType = spaceDetails.SpaceSettings?.AppType
if (appType !== 'JupyterLab' && appType !== 'CodeEditor') {
if (!appType || !(appType in appTypeSettingsMap)) {
throw new ToolkitError(`Unsupported AppType "${appType}" for space "${spaceName}"`)
}

// Get app resource spec
const requestedResourceSpec =
appType === 'JupyterLab'
appType === AppType.JupyterLab
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still prone to errors if someone updates the appTypeSettingsMap but does not update this condition. But fine for now. Ideal change would have been to use a case condition here as well.

? spaceDetails.SpaceSettings?.JupyterLabAppSettings?.DefaultResourceSpec
: spaceDetails.SpaceSettings?.CodeEditorAppSettings?.DefaultResourceSpec

Expand Down Expand Up @@ -181,16 +186,30 @@ export class SagemakerClient extends ClientWrapper<SageMakerClient> {
instanceType = InstanceTypeMinimum
}

// Get remote access flag
if (!spaceDetails.SpaceSettings?.RemoteAccess || spaceDetails.SpaceSettings?.RemoteAccess === 'DISABLED') {
// First, update the space if needed
const needsRemoteAccess =
!spaceDetails.SpaceSettings?.RemoteAccess || spaceDetails.SpaceSettings?.RemoteAccess === 'DISABLED'
const instanceTypeChanged = requestedResourceSpec?.InstanceType !== instanceType

if (needsRemoteAccess || instanceTypeChanged) {
const updateSpaceRequest: UpdateSpaceCommandInput = {
DomainId: domainId,
SpaceName: spaceName,
SpaceSettings: {
...(needsRemoteAccess && { RemoteAccess: 'ENABLED' }),
...(instanceTypeChanged && {
[appTypeSettingsMap[appType]]: {
DefaultResourceSpec: {
InstanceType: instanceType,
},
},
}),
},
}

try {
await this.updateSpace({
DomainId: domainId,
SpaceName: spaceName,
SpaceSettings: {
RemoteAccess: 'ENABLED',
},
})
getLogger().debug('SagemakerClient: Updating space: domainId=%s, spaceName=%s', domainId, spaceName)
await this.updateSpace(updateSpaceRequest)
await this.waitForSpaceInService(spaceName, domainId)
} catch (err) {
throw this.handleStartSpaceError(err)
Expand All @@ -214,6 +233,7 @@ export class SagemakerClient extends ClientWrapper<SageMakerClient> {
? { ...resourceSpec, EnvironmentArn: undefined, EnvironmentVersionArn: undefined }
: resourceSpec

// Second, create the App
const createAppRequest: CreateAppCommandInput = {
DomainId: domainId,
SpaceName: spaceName,
Expand All @@ -223,6 +243,7 @@ export class SagemakerClient extends ClientWrapper<SageMakerClient> {
}

try {
getLogger().debug('SagemakerClient: Creating app: domainId=%s, spaceName=%s', domainId, spaceName)
await this.createApp(createAppRequest)
} catch (err) {
throw this.handleStartSpaceError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ describe('SagemakerClient.startSpace', function () {
getTestWindow().getFirstMessage().selectItem('Yes')

await promise
sinon.assert.calledOnce(updateSpaceStub)
sinon.assert.calledOnce(createAppStub)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "The space is updated upon creation of a new app with the requested settings"
}
Loading