|
| 1 | +# Databricks CLI |
| 2 | +# Copyright 2021 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. |
| 23 | +import copy |
| 24 | + |
| 25 | +from databricks_cli.sdk.version import OLD_API_VERSION |
| 26 | + |
| 27 | + |
| 28 | +class APIV1Client(object): |
| 29 | + def __init__(self, client): |
| 30 | + self.v1_client = copy.deepcopy(client) |
| 31 | + self.v1_client.api_version = OLD_API_VERSION |
| 32 | + |
| 33 | + |
| 34 | +class CommandService(APIV1Client): |
| 35 | + def get_command_status(self, cluster_id, context_id, command_id): |
| 36 | + return self.v1_client.perform_query( |
| 37 | + method="GET", path="/commands/status", data={ |
| 38 | + "clusterId": cluster_id, |
| 39 | + "contextId": context_id, |
| 40 | + "commandId": command_id, |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + def cancel_command(self, cluster_id, context_id, command_id): |
| 45 | + return self.v1_client.perform_query( |
| 46 | + method="POST", path="/commands/cancel", data={ |
| 47 | + "clusterId": cluster_id, |
| 48 | + "contextId": context_id, |
| 49 | + "commandId": command_id, |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + def execute_command(self, language, cluster_id, context_id, command): |
| 54 | + return self.v1_client.perform_query( |
| 55 | + method="POST", path="/commands/execute", data={ |
| 56 | + "language": language, |
| 57 | + "clusterId": cluster_id, |
| 58 | + "contextId": context_id, |
| 59 | + "command": command |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +class ExecutionContextService(APIV1Client): |
| 65 | + def get_context_status(self, cluster_id, context_id): |
| 66 | + return self.v1_client.perform_query( |
| 67 | + method="GET", path="/contexts/status", data={ |
| 68 | + "clusterId": cluster_id, |
| 69 | + "contextId": context_id, |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | + def create_context(self, language, cluster_id): |
| 74 | + return self.v1_client.perform_query( |
| 75 | + method="POST", path="/contexts/create", data={ |
| 76 | + "language": language, |
| 77 | + "clusterId": cluster_id, |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + def destroy_context(self, cluster_id, context_id): |
| 82 | + return self.v1_client.perform_query( |
| 83 | + method="POST", path="/contexts/destroy", data={ |
| 84 | + "contextId": context_id, |
| 85 | + "clusterId": cluster_id, |
| 86 | + } |
| 87 | + ) |
0 commit comments