Skip to content

Commit

Permalink
Add execution context and run command APIs
Browse files Browse the repository at this point in the history
These are the still supported functionality from the REST API 1.2.
https://docs.databricks.com/dev-tools/api/1.2/index.html
  • Loading branch information
fjakobs committed May 4, 2022
1 parent 5444cb8 commit 3cc62f6
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 1 deletion.
2 changes: 2 additions & 0 deletions databricks_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from databricks_cli.instance_pools.cli import instance_pools_group
from databricks_cli.pipelines.cli import pipelines_group
from databricks_cli.repos.cli import repos_group
from databricks_cli.execution_context.cli import execution_context_group


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

if __name__ == "__main__":
cli()
31 changes: 31 additions & 0 deletions databricks_cli/click_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ class PipelineIdClickType(ParamType):
help = 'The pipeline ID.'


class ExecutionContextIdClickType(ParamType):
name = 'CONTEXT_ID'
help = 'The execution context ID as returned from "databricks execution-context create".'


class CommandIdClickType(ParamType):
name = 'COMMAND_ID'
help = 'The command ID as returned from "databricks execution-context command-execute".'


class CommandStringType(ParamType):
name = 'COMMAND'
help = 'The command string to run.'


class CommandOutputType(ParamType):
name = 'FORMAT'
help = 'Can be "JSON" or "TEXT". Set to TEXT by default.'

def convert(self, value, param, ctx):
if value is None:
return 'text'
if value.lower() != 'json' and value.lower() != 'text':
raise RuntimeError('output must be "json" or "text"')
return value

@classmethod
def is_json(cls, value):
return value is not None and value.lower() == 'json'


class OneOfOption(Option):
def __init__(self, *args, **kwargs):
self.one_of = kwargs.pop('one_of')
Expand Down
22 changes: 22 additions & 0 deletions databricks_cli/execution_context/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Databricks CLI
# Copyright 2017 Databricks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"), except
# that the use of services to which certain application programming
# interfaces (each, an "API") connect requires that the user first obtain
# a license for the use of the APIs from Databricks, Inc. ("Databricks"),
# by creating an account at www.databricks.com and agreeing to either (a)
# the Community Edition Terms of Service, (b) the Databricks Terms of
# Service, or (c) another written agreement between Licensee and Databricks
# for the use of the APIs.
#
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
76 changes: 76 additions & 0 deletions databricks_cli/execution_context/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# WARNING THIS FILE IS AUTOGENERATED
#
# Databricks CLI
# Copyright 2017 Databricks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"), except
# that the use of services to which certain application programming
# interfaces (each, an "API") connect requires that the user first obtain
# a license for the use of the APIs from Databricks, Inc. ("Databricks"),
# by creating an account at www.databricks.com and agreeing to either (a)
# the Community Edition Terms of Service, (b) the Databricks Terms of
# Service, or (c) another written agreement between Licensee and Databricks
# for the use of the APIs.
#
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
from databricks_cli.sdk.service import ExecutionContextService
if sys.version_info > (3, 5):
from typing import Dict, Any # noqa


class ExecutionContext(object):
def __init__(self, api_client, cluster_id):
self.client = ExecutionContextService(api_client)
self.cluster_id = cluster_id
self.id = None

def __enter__(self):
if self.id is None:
self.id = self.client.create_context(cluster_id=self.cluster_id)["id"]

return self

def __exit__(self, _type, _value, _traceback):
self.client.delete_context(cluster_id=self.cluster_id, context_id=self.id)
self.id = None


class ExecutionContextApi(object):

def __init__(self, api_client):
self.client = ExecutionContextService(api_client)

def create_context(self, cluster_id, language="python"):
# type: (str, str, Dict[Any, Any]) -> Dict[Any, Any]
return self.client.create_context(cluster_id, language)

def get_context_status(self, cluster_id, context_id):
# type: (str, str, Dict[Any, Any]) -> Dict[Any, Any]
return self.client.get_context_status(cluster_id, context_id)

def delete_context(self, cluster_id, context_id):
# type: (str, str, Dict[Any, Any]) -> Any
return self.client.delete_context(cluster_id, context_id)

def execute_command(self, cluster_id, context_id, command, language="python"):
# type: (str, str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
return self.client.execute_command(cluster_id, context_id, command, language)

def cancel_command(self, cluster_id, context_id, command_id):
# type: (str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
return self.client.cancel_command(cluster_id, context_id, command_id)

def get_command_status(self, cluster_id, context_id, command_id):
# type: (str, str, str, Dict[Any, Any]) -> Dict[Any, Any]
return self.client.get_command_status(cluster_id, context_id, command_id)
Loading

0 comments on commit 3cc62f6

Please sign in to comment.