Skip to content

Commit 5f29a29

Browse files
[DECO-333] Assume you have all the permissions if can't access workspace confs (#232)
Right now we just log the error and let users continue using the extension.
1 parent 45e3650 commit 5f29a29

File tree

7 files changed

+101
-47
lines changed

7 files changed

+101
-47
lines changed

packages/databricks-sdk-js/src/context/Context.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,51 @@ export interface ContextItems {
66
logger?: NamedLogger;
77
opId?: string;
88
opName?: string;
9+
rootClassName?: string;
10+
rootFnName?: string;
911
cancellationToken?: CancellationToken;
1012
}
1113

1214
export class Context {
13-
private _logger?: NamedLogger;
15+
private _items: ContextItems = {};
16+
1417
get logger() {
15-
return this._logger;
18+
return this._items.logger;
1619
}
1720

18-
private _opId: string;
1921
get opId() {
20-
return this._opId;
22+
return this._items.opId;
2123
}
2224

23-
private _opName?: string;
2425
get opName() {
25-
return this._opName;
26+
return this._items.opName;
27+
}
28+
29+
get rootClassName() {
30+
return this._items.rootClassName;
31+
}
32+
33+
get rootFnName() {
34+
return this._items.rootFnName;
2635
}
2736

28-
private _cancelationToken?: CancellationToken;
2937
get cancellationToken() {
30-
return this._cancelationToken;
38+
return this._items?.cancellationToken;
3139
}
3240

3341
constructor(items: ContextItems = {}) {
34-
this._opId = randomUUID();
3542
this.setItems(items);
43+
this._items.opId = this._items.opId ?? randomUUID();
3644
}
3745

3846
setItems(items: ContextItems = {}) {
39-
this._cancelationToken =
40-
items.cancellationToken ?? this._cancelationToken;
41-
this._opId = items.opId ?? this._opId;
42-
this._opName = items.opName ?? this._opName;
43-
this._logger = items.logger ?? this._logger;
47+
this._items = {
48+
...this._items,
49+
...items,
50+
};
51+
}
52+
53+
copy() {
54+
return new Context(this._items);
4455
}
4556
}

packages/databricks-sdk-js/src/logging/NamedLogger.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ export class NamedLogger {
4848
return this._context?.opId;
4949
}
5050
get opName() {
51-
return this._context?.opName;
51+
const rootNames = [
52+
this._context?.rootClassName,
53+
this._context?.rootFnName,
54+
].filter((e) => e !== undefined);
55+
56+
return this._context?.opName ?? rootNames.length !== 0
57+
? rootNames.join(".")
58+
: undefined;
5259
}
5360

5461
get loggingFnName() {
@@ -114,17 +121,15 @@ export class NamedLogger {
114121
this.log(LEVELS.error, message, {error: obj});
115122
}
116123

117-
withContext<T>({
124+
withContext({
118125
context,
119126
loggingFnName,
120-
fn,
121127
}: {
122128
context: Context;
123129
loggingFnName: string;
124-
fn: () => T;
125130
}) {
126131
this._context = context;
127132
this._loggingFnName = loggingFnName;
128-
return fn();
133+
return this;
129134
}
130135
}

packages/databricks-sdk-js/src/logging/loggingDecorators.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "..";
22
import {NamedLogger} from "./NamedLogger";
33
import {getContextParamIndex, Context, ContextItems} from "../context";
44

5-
export function withLogContext(name: string) {
5+
export function withLogContext(name: string, opName?: string) {
66
return function (
77
_target: any,
88
_propertyKey: string,
@@ -17,23 +17,28 @@ export function withLogContext(name: string) {
1717
while (args.length <= contextParamIndex) {
1818
args.push(undefined);
1919
}
20-
const contextParam =
20+
const contextParam = (
2121
(args[contextParamIndex] as Context | undefined) ??
22-
new Context();
22+
new Context()
23+
).copy();
2324

24-
const items: ContextItems = {logger};
25-
if (contextParam.opName === undefined) {
26-
items.opName = `${this.constructor.name}.${_propertyKey}`;
27-
}
25+
const items: ContextItems = {
26+
logger,
27+
opName: contextParam.opName ?? opName,
28+
rootClassName:
29+
contextParam.rootClassName ?? this.constructor.name,
30+
rootFnName: contextParam.rootFnName ?? _propertyKey,
31+
};
2832
contextParam.setItems(items);
2933

3034
args[contextParamIndex] = contextParam;
3135

32-
return logger.withContext({
36+
logger.withContext({
3337
context: contextParam,
3438
loggingFnName: `${this.constructor.name}.${_propertyKey}`,
35-
fn: () => method.apply(this, args),
3639
});
40+
41+
return method.apply(this, args);
3742
};
3843
};
3944
}

packages/databricks-sdk-js/src/services/WorkspaceConf.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {ApiClient, CancellationToken, WorkspaceConfService} from "..";
2-
import {Context} from "../context";
1+
import {ApiClient, WorkspaceConfService} from "..";
2+
import {context, Context} from "../context";
3+
import {ExposedLoggers, withLogContext} from "../logging";
34

45
type StringBool = "true" | "false" | "";
56

@@ -40,27 +41,26 @@ export interface WorkspaceConfProps {
4041
export class WorkspaceConf {
4142
constructor(private readonly client: ApiClient) {}
4243

44+
@withLogContext(ExposedLoggers.SDK)
4345
async getStatus(
4446
keys: Array<keyof WorkspaceConfProps>,
45-
cancellationToken?: CancellationToken
47+
@context ctx?: Context
4648
): Promise<Partial<WorkspaceConfProps>> {
4749
const wsConfApi = new WorkspaceConfService(this.client);
4850
return await wsConfApi.getStatus(
4951
{
5052
keys: keys.join(","),
5153
},
52-
new Context({cancellationToken})
54+
ctx
5355
);
5456
}
5557

58+
@withLogContext(ExposedLoggers.SDK)
5659
async setStatus(
5760
request: Partial<WorkspaceConfProps>,
58-
cancellationToken?: CancellationToken
61+
@context ctx?: Context
5962
): Promise<Partial<WorkspaceConfProps>> {
6063
const wsConfApi = new WorkspaceConfService(this.client);
61-
return await wsConfApi.setStatus(
62-
request,
63-
new Context({cancellationToken})
64-
);
64+
return await wsConfApi.setStatus(request, ctx);
6565
}
6666
}

packages/databricks-vscode/src/configuration/DatabricksWorkspace.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
WorkspaceConf,
77
WorkspaceConfProps,
88
} from "@databricks/databricks-sdk";
9-
import {Uri} from "vscode";
9+
import {Context, context} from "@databricks/databricks-sdk/dist/context";
10+
import {withLogContext} from "@databricks/databricks-sdk/dist/logging";
11+
import {Uri, window} from "vscode";
12+
import {Loggers} from "../logger";
1013

1114
export class DatabricksWorkspace {
1215
constructor(
@@ -63,18 +66,37 @@ export class DatabricksWorkspace {
6366
}
6467
}
6568

66-
static async load(client: ApiClient, profile: string) {
69+
@withLogContext(Loggers.Extension, "DatabricksWorkspace.load")
70+
static async load(
71+
client: ApiClient,
72+
profile: string,
73+
@context ctx?: Context
74+
) {
6775
const host = Uri.parse((await client.host).toString());
6876

6977
const scimApi = new CurrentUserService(client);
70-
const me = await scimApi.me();
78+
const me = await scimApi.me(ctx);
7179

7280
const wsConfApi = new WorkspaceConf(client);
73-
const state = await wsConfApi.getStatus([
74-
"enableProjectTypeInWorkspace",
75-
"enableWorkspaceFilesystem",
76-
]);
81+
let state: WorkspaceConfProps = {
82+
enableProjectTypeInWorkspace: "true",
83+
enableWorkspaceFilesystem: "true",
84+
};
85+
try {
86+
state = {
87+
...state,
88+
...(await wsConfApi.getStatus(
89+
[
90+
"enableProjectTypeInWorkspace",
91+
"enableWorkspaceFilesystem",
92+
],
93+
ctx
94+
)),
95+
};
96+
} catch (e) {
97+
ctx?.logger?.error("Can't fetch workspace confs", e);
98+
}
7799

78-
return new DatabricksWorkspace(host, me, state as any, profile);
100+
return new DatabricksWorkspace(host, me, state, profile);
79101
}
80102
}

packages/databricks-vscode/src/logger/outputConsoleTransport.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,24 @@ export function getOutputConsoleTransport(outputChannel: OutputChannel) {
6666

6767
info[MESSAGE] =
6868
info.level === "error"
69-
? inspect(stripped, false, 1000)
69+
? inspect(
70+
{
71+
...stripped,
72+
level: info.level,
73+
message: info.message,
74+
},
75+
false,
76+
1000
77+
)
7078
: inspect(
7179
{
7280
...recursiveTruncate(
7381
stripped,
7482
workspaceConfigs.truncationDepth
7583
),
7684
timestamp: new Date().toLocaleString(),
85+
level: info.level,
86+
message: info.message,
7787
},
7888
false,
7989
workspaceConfigs.truncationDepth

packages/databricks-vscode/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"sourceMap": true,
1010
"rootDir": "src",
1111
"strict": true /* enable all strict type-checking options */,
12-
"forceConsistentCasingInFileNames": true
12+
"forceConsistentCasingInFileNames": true,
13+
"experimentalDecorators": true
1314
},
1415
"include": ["src"]
1516
}

0 commit comments

Comments
 (0)