Skip to content

Commit 4940954

Browse files
committed
Add execution context and run command APIs
These are the still supported functionality from the REST API 1.2. https://docs.databricks.com/dev-tools/api/1.2/index.html
1 parent 5444cb8 commit 4940954

File tree

7 files changed

+466
-1
lines changed

7 files changed

+466
-1
lines changed

databricks_cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from databricks_cli.instance_pools.cli import instance_pools_group
4242
from databricks_cli.pipelines.cli import pipelines_group
4343
from databricks_cli.repos.cli import repos_group
44+
from databricks_cli.execution_context.cli import execution_context_group
4445

4546

4647
@click.group(context_settings=CONTEXT_SETTINGS)
@@ -67,6 +68,7 @@ def cli():
6768
cli.add_command(instance_pools_group, name="instance-pools")
6869
cli.add_command(pipelines_group, name='pipelines')
6970
cli.add_command(repos_group, name='repos')
71+
cli.add_command(execution_context_group, name='execution-context')
7072

7173
if __name__ == "__main__":
7274
cli()

databricks_cli/click_types.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ class PipelineIdClickType(ParamType):
112112
help = 'The pipeline ID.'
113113

114114

115+
class ExecutionContextIdClickType(ParamType):
116+
name = 'CONTEXT_ID'
117+
help = 'The execution context ID as returned from "databricks execution-context create".'
118+
119+
120+
class CommandIdClickType(ParamType):
121+
name = 'COMMAND_ID'
122+
help = 'The command ID as returned from "databricks execution-context command-execute".'
123+
124+
125+
class CommandStringType(ParamType):
126+
name = 'COMMAND'
127+
help = 'The command string to run.'
128+
129+
130+
class CommandOutputType(ParamType):
131+
name = 'FORMAT'
132+
help = 'Can be "JSON" or "TEXT". Set to TEXT by default.'
133+
134+
def convert(self, value, param, ctx):
135+
if value is None:
136+
return 'text'
137+
if value.lower() != 'json' and value.lower() != 'text':
138+
raise RuntimeError('output must be "json" or "text"')
139+
return value
140+
141+
@classmethod
142+
def is_json(cls, value):
143+
return value is not None and value.lower() == 'json'
144+
145+
115146
class OneOfOption(Option):
116147
def __init__(self, *args, **kwargs):
117148
self.one_of = kwargs.pop('one_of')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Databricks CLI
2+
# Copyright 2017 Databricks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"), except
5+
# that the use of services to which certain application programming
6+
# interfaces (each, an "API") connect requires that the user first obtain
7+
# a license for the use of the APIs from Databricks, Inc. ("Databricks"),
8+
# by creating an account at www.databricks.com and agreeing to either (a)
9+
# the Community Edition Terms of Service, (b) the Databricks Terms of
10+
# Service, or (c) another written agreement between Licensee and Databricks
11+
# for the use of the APIs.
12+
#
13+
# You may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# WARNING THIS FILE IS AUTOGENERATED
2+
#
3+
# Databricks CLI
4+
# Copyright 2017 Databricks, Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License"), except
7+
# that the use of services to which certain application programming
8+
# interfaces (each, an "API") connect requires that the user first obtain
9+
# a license for the use of the APIs from Databricks, Inc. ("Databricks"),
10+
# by creating an account at www.databricks.com and agreeing to either (a)
11+
# the Community Edition Terms of Service, (b) the Databricks Terms of
12+
# Service, or (c) another written agreement between Licensee and Databricks
13+
# for the use of the APIs.
14+
#
15+
# You may not use this file except in compliance with the License.
16+
# You may obtain a copy of the License at
17+
#
18+
# http://www.apache.org/licenses/LICENSE-2.0
19+
#
20+
# Unless required by applicable law or agreed to in writing, software
21+
# distributed under the License is distributed on an "AS IS" BASIS,
22+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
# See the License for the specific language governing permissions and
24+
# limitations under the License.
25+
#
26+
from typing import Dict, Any
27+
28+
from databricks_cli.sdk.service import ExecutionContextService
29+
30+
31+
class ExecutionContext(object):
32+
def __init__(self, api_client, cluster_id):
33+
self.client = ExecutionContextService(api_client)
34+
self.cluster_id = cluster_id
35+
self.id = None
36+
37+
def __enter__(self):
38+
if self.id is None:
39+
self.id = self.client.create_context(cluster_id=self.cluster_id)["id"]
40+
41+
return self
42+
43+
def __exit__(self, _type, _value, _traceback):
44+
self.client.delete_context(cluster_id=self.cluster_id, context_id=self.id)
45+
self.id = None
46+
47+
48+
class ExecutionContextApi(object):
49+
50+
def __init__(self, api_client):
51+
self.client = ExecutionContextService(api_client)
52+
53+
def create_context(self, cluster_id, language="python"):
54+
# type: (str, str, Dict[Any, Any]) -> Dict[Any, Any]
55+
return self.client.create_context(cluster_id, language)
56+
57+
def get_context_status(self, cluster_id, context_id):
58+
# type: (str, str, Dict[Any, Any]) -> Dict[Any, Any]
59+
return self.client.get_context_status(cluster_id, context_id)
60+
61+
def delete_context(self, cluster_id, context_id):
62+
# type: (str, str, Dict[Any, Any]) -> Any
63+
return self.client.delete_context(cluster_id, context_id)
64+
65+
def execute_command(self, cluster_id, context_id, command, language="python"):
66+
# type: (str, str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
67+
return self.client.execute_command(cluster_id, context_id, command, language)
68+
69+
def cancel_command(self, cluster_id, context_id, command_id):
70+
# type: (str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
71+
return self.client.cancel_command(cluster_id, context_id, command_id)
72+
73+
def get_command_status(self, cluster_id, context_id, command_id):
74+
# type: (str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
75+
return self.client.get_command_status(cluster_id, context_id, command_id)

0 commit comments

Comments
 (0)