Skip to content

Commit 987c455

Browse files
author
Arun Bhati
committed
Move resource metadata parsing logic in helper function
1 parent d864d99 commit 987c455

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

patched-vscode/extensions/sagemaker-extension/src/constant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function getExpiryTime(cookie: SagemakerCookie): number {
8484
* Constructs the SMUS portal URL using domain, region, and project information
8585
* Returns null if not in SMUS environment or if required fields are missing
8686
*/
87-
export const getSmusVscodePortalUrl = (metadata: SagemakerResourceMetadata): string | null => {
87+
export const getSmusVscodePortalUrl = (metadata: SagemakerResourceMetadata | null): string | null => {
8888
if (process.env[SERVICE_NAME_ENV_VAR] !== SMUS_SERVICE_NAME) {
8989
return null;
9090
}

patched-vscode/extensions/sagemaker-extension/src/extension.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ const ENABLE_AUTO_UPDATE_COMMAND = 'workbench.extensions.action.enableAutoUpdate
2323
// Global redirect URL for SMUS environment
2424
let smusRedirectUrl: string | null = null;
2525

26+
function fetchMetadata(): SagemakerResourceMetadata | null {
27+
try {
28+
const data = fs.readFileSync(SAGEMAKER_METADATA_PATH, 'utf-8');
29+
return JSON.parse(data) as SagemakerResourceMetadata;
30+
} catch (error) {
31+
// fail silently not to block users
32+
console.error('Error reading metadata file:', error);
33+
return null;
34+
}
35+
}
36+
37+
function initializeSmusRedirectUrl() {
38+
smusRedirectUrl = getSmusVscodePortalUrl(fetchMetadata());
39+
}
40+
2641
function showWarningDialog() {
2742
vscode.commands.executeCommand(PARSE_SAGEMAKER_COOKIE_COMMAND).then(response => {
2843

@@ -107,22 +122,12 @@ function renewSession(sagemakerCookie: SagemakerCookie) {
107122
}
108123

109124
function updateStatusItemWithMetadata(context: vscode.ExtensionContext) {
110-
try {
111-
const data = fs.readFileSync(SAGEMAKER_METADATA_PATH, 'utf-8');
112-
const jsonData = JSON.parse(data) as SagemakerResourceMetadata;
113-
114-
if (jsonData?.SpaceName) {
115-
let spaceNameStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
116-
spaceNameStatusBarItem.text = `Space: ${jsonData.SpaceName}`;
117-
spaceNameStatusBarItem.show();
118-
context.subscriptions.push(spaceNameStatusBarItem);
119-
}
120-
121-
// Initialize SMUS redirect URL
122-
smusRedirectUrl = getSmusVscodePortalUrl(jsonData);
123-
} catch (error) {
124-
// fail silently not to block users
125-
console.error('Error reading metadata file:', error);
125+
const metadata = fetchMetadata();
126+
if (metadata?.SpaceName) {
127+
let spaceNameStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
128+
spaceNameStatusBarItem.text = `Space: ${metadata.SpaceName}`;
129+
spaceNameStatusBarItem.show();
130+
context.subscriptions.push(spaceNameStatusBarItem);
126131
}
127132
}
128133

@@ -162,15 +167,16 @@ export function activate(context: vscode.ExtensionContext) {
162167
// TODO: log activation of extension
163168
console.log('Activating Sagemaker Extension...');
164169

165-
// Initialize metadata first (which will set smusRedirectUrl if in SMUS environment)
166-
updateStatusItemWithMetadata(context);
170+
// First set smusRedirectUrl if we are in SMUS environment
171+
initializeSmusRedirectUrl();
167172

168173
// execute the get cookie command and save the data to cookies
169174
vscode.commands.executeCommand(PARSE_SAGEMAKER_COOKIE_COMMAND).then(r => {
170175

171176
const sagemakerCookie: SagemakerCookie = r as SagemakerCookie
172177

173178
initialize(sagemakerCookie);
179+
updateStatusItemWithMetadata(context);
174180
});
175181

176182
// render warning message regarding auto upgrade disabled

patches/sagemaker-extension-smus-support.patch

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-extension/src/constant.
3737
+ * Constructs the SMUS portal URL using domain, region, and project information
3838
+ * Returns null if not in SMUS environment or if required fields are missing
3939
+ */
40-
+export const getSmusVscodePortalUrl = (metadata: SagemakerResourceMetadata): string | null => {
40+
+export const getSmusVscodePortalUrl = (metadata: SagemakerResourceMetadata | null): string | null => {
4141
+ if (process.env[SERVICE_NAME_ENV_VAR] !== SMUS_SERVICE_NAME) {
4242
+ return null;
4343
+ }
@@ -77,17 +77,32 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-extension/src/extension
7777
} from "./constant";
7878
import * as console from "console";
7979

80-
@@ -19,6 +20,9 @@ import * as console from "console";
80+
@@ -19,6 +20,24 @@ import * as console from "console";
8181
const PARSE_SAGEMAKER_COOKIE_COMMAND = 'sagemaker.parseCookies';
8282
const ENABLE_AUTO_UPDATE_COMMAND = 'workbench.extensions.action.enableAutoUpdate';
8383

8484
+// Global redirect URL for SMUS environment
8585
+let smusRedirectUrl: string | null = null;
86+
+
87+
+function fetchMetadata(): SagemakerResourceMetadata | null {
88+
+ try {
89+
+ const data = fs.readFileSync(SAGEMAKER_METADATA_PATH, 'utf-8');
90+
+ return JSON.parse(data) as SagemakerResourceMetadata;
91+
+ } catch (error) {
92+
+ // fail silently not to block users
93+
+ console.error('Error reading metadata file:', error);
94+
+ return null;
95+
+ }
96+
+}
97+
+
98+
+function initializeSmusRedirectUrl() {
99+
+ smusRedirectUrl = getSmusVscodePortalUrl(fetchMetadata());
100+
+}
86101
+
87102
function showWarningDialog() {
88103
vscode.commands.executeCommand(PARSE_SAGEMAKER_COOKIE_COMMAND).then(response => {
89104

90-
@@ -59,11 +63,12 @@ function showWarningDialog() {
105+
@@ -59,11 +78,12 @@ function showWarningDialog() {
91106
}
92107

93108
function signInError(sagemakerCookie: SagemakerCookie) {
@@ -101,7 +116,7 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-extension/src/extension
101116
}
102117
});
103118
}
104-
@@ -94,32 +99,31 @@ function saveWorkspace() {
119+
@@ -94,32 +114,21 @@ function saveWorkspace() {
105120
});
106121
}
107122
function renewSession(sagemakerCookie: SagemakerCookie) {
@@ -131,43 +146,29 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-extension/src/extension
131146
- } catch (jsonError) {
132147
- // fail silently not to block users
133148
- }
134-
+ try {
135-
+ const data = fs.readFileSync(SAGEMAKER_METADATA_PATH, 'utf-8');
136-
+ const jsonData = JSON.parse(data) as SagemakerResourceMetadata;
137-
+
138-
+ if (jsonData?.SpaceName) {
139-
+ let spaceNameStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
140-
+ spaceNameStatusBarItem.text = `Space: ${jsonData.SpaceName}`;
141-
+ spaceNameStatusBarItem.show();
142-
+ context.subscriptions.push(spaceNameStatusBarItem);
143-
}
149+
- }
144150
- });
145-
+
146-
+ // Initialize SMUS redirect URL
147-
+ smusRedirectUrl = getSmusVscodePortalUrl(jsonData);
148-
+ } catch (error) {
149-
+ // fail silently not to block users
150-
+ console.error('Error reading metadata file:', error);
151+
+ const metadata = fetchMetadata();
152+
+ if (metadata?.SpaceName) {
153+
+ let spaceNameStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
154+
+ spaceNameStatusBarItem.text = `Space: ${metadata.SpaceName}`;
155+
+ spaceNameStatusBarItem.show();
156+
+ context.subscriptions.push(spaceNameStatusBarItem);
151157
+ }
152158
}
153159

154160
// Render warning message regarding auto upgrade disabled
155-
@@ -158,15 +162,25 @@ export function activate(context: vscode
161+
@@ -158,6 +167,9 @@ export function activate(context: vscode
156162
// TODO: log activation of extension
157163
console.log('Activating Sagemaker Extension...');
158164

159-
+ // Initialize metadata first (which will set smusRedirectUrl if in SMUS environment)
160-
+ updateStatusItemWithMetadata(context);
165+
+ // First set smusRedirectUrl if we are in SMUS environment
166+
+ initializeSmusRedirectUrl();
161167
+
162168
// execute the get cookie command and save the data to cookies
163169
vscode.commands.executeCommand(PARSE_SAGEMAKER_COOKIE_COMMAND).then(r => {
164170

165-
const sagemakerCookie: SagemakerCookie = r as SagemakerCookie
166-
167-
initialize(sagemakerCookie);
168-
- updateStatusItemWithMetadata(context);
169-
});
170-
171+
@@ -170,3 +182,11 @@ export function activate(context: vscode
171172
// render warning message regarding auto upgrade disabled
172173
renderExtensionAutoUpgradeDisabledNotification();
173174
}

0 commit comments

Comments
 (0)