-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cluster - Client library API migration changes #177
base: main
Are you sure you want to change the base?
Changes from 17 commits
14a7916
75e1576
c8f9643
654b621
9659c02
3708794
25e25c1
cdec0d7
2a37f2c
d539d90
1d60038
611fbac
cd84677
2b7b95b
9b71492
312902a
68c21db
4d70971
65d004f
2eb3d98
cb818d0
8a6072a
c5ae698
6daa124
7f64795
50f2d2d
a734f1e
ff65fda
7580728
36dfd9e
a3edf3d
d8e3567
f87ce93
f656955
218ecde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://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 json | ||
from jupyter_server.base.handlers import APIHandler | ||
import tornado | ||
from dataproc_jupyter_plugin import credentials | ||
from dataproc_jupyter_plugin.services import cluster | ||
|
||
|
||
class ClusterListPageController(APIHandler): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Completely remove this method. It duplicates the |
||
@tornado.web.authenticated | ||
async def get(self): | ||
try: | ||
page_token = self.get_argument("pageToken") | ||
page_size = self.get_argument("pageSize") | ||
client = cluster.Client( | ||
await credentials.get_cached(), self.log | ||
) | ||
cluster_list = await client.list_clusters(page_size, page_token) | ||
self.finish(json.dumps(cluster_list)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching cluster list") | ||
self.finish({"error": str(e)}) | ||
|
||
|
||
class ClusterDetailController(APIHandler): | ||
@tornado.web.authenticated | ||
async def get(self): | ||
try: | ||
cluster_selected = self.get_argument("clusterSelected") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change all instances of |
||
client = cluster.Client( | ||
await credentials.get_cached(), self.log | ||
) | ||
get_cluster = await client.get_cluster_detail(cluster_selected) | ||
self.finish(json.dumps(get_cluster)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching get cluster") | ||
self.finish({"error": str(e)}) | ||
|
||
|
||
class StopClusterController(APIHandler): | ||
@tornado.web.authenticated | ||
async def post(self): | ||
try: | ||
cluster_selected = self.get_argument("clusterSelected") | ||
client = cluster.Client( | ||
await credentials.get_cached(), self.log | ||
) | ||
stop_cluster = await client.stop_cluster(cluster_selected) | ||
self.finish(json.dumps(stop_cluster)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching stop cluster") | ||
self.finish({"error": str(e)}) | ||
|
||
|
||
class StartClusterController(APIHandler): | ||
@tornado.web.authenticated | ||
async def post(self): | ||
try: | ||
cluster_selected = self.get_argument("clusterSelected") | ||
client = cluster.Client( | ||
await credentials.get_cached(), self.log | ||
) | ||
start_cluster = await client.start_cluster(cluster_selected) | ||
self.finish(json.dumps(start_cluster)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching start cluster") | ||
self.finish({"error": str(e)}) | ||
|
||
class DeleteClusterController(APIHandler): | ||
@tornado.web.authenticated | ||
async def delete(self): | ||
try: | ||
cluster_selected = self.get_argument("clusterSelected") | ||
client = cluster.Client( | ||
await credentials.get_cached(), self.log | ||
) | ||
delete_cluster = await client.delete_cluster(cluster_selected) | ||
self.finish(json.dumps(delete_cluster)) | ||
except Exception as e: | ||
self.log.exception(f"Error deleting cluster") | ||
self.finish({"error": str(e)}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
from dataproc_jupyter_plugin.controllers import ( | ||
airflow, | ||
bigquery, | ||
cluster, | ||
composer, | ||
dataproc, | ||
executor, | ||
|
@@ -193,6 +194,11 @@ def full_path(name): | |
"dagRunTask": airflow.DagRunTaskController, | ||
"dagRunTaskLogs": airflow.DagRunTaskLogsController, | ||
"clusterList": dataproc.ClusterListController, | ||
"clusterListPage": cluster.ClusterListPageController, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop this line entirely; there's no justification for having two different endpoints that make the exact same API call. |
||
"clusterDetail": cluster.ClusterDetailController, | ||
"stopCluster": cluster.StopClusterController, | ||
"startCluster": cluster.StartClusterController, | ||
"deleteCluster": cluster.DeleteClusterController, | ||
"runtimeList": dataproc.RuntimeController, | ||
"createJobScheduler": executor.ExecutorController, | ||
"dagList": airflow.DagListController, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Copyright 2023 Google LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a file in the Move all of the methods from this file into that one and then delete this entire file. |
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://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. | ||
|
||
from google.cloud import dataproc_v1 as dataproc | ||
import proto | ||
import json | ||
import google.oauth2.credentials as oauth2 | ||
from google.protobuf.empty_pb2 import Empty | ||
|
||
class Client: | ||
def __init__(self, credentials, log): | ||
self.log = log | ||
if not ( | ||
("access_token" in credentials) | ||
and ("project_id" in credentials) | ||
and ("region_id" in credentials) | ||
): | ||
self.log.exception("Missing required credentials") | ||
raise ValueError("Missing required credentials") | ||
self._access_token = credentials["access_token"] | ||
self.project_id = credentials["project_id"] | ||
self.region_id = credentials["region_id"] | ||
|
||
async def list_clusters(self, page_size, page_token): | ||
try: | ||
# Create a client | ||
client = dataproc.ClusterControllerAsyncClient( | ||
client_options={ | ||
"api_endpoint": f"us-central1-dataproc.googleapis.com:443" | ||
}, | ||
credentials=oauth2.Credentials(self._access_token), | ||
) | ||
|
||
# Initialize request argument(s) | ||
request = dataproc.ListClustersRequest( | ||
project_id=self.project_id, | ||
page_size=int(page_size), | ||
page_token=page_token, | ||
region=self.region_id, | ||
) | ||
|
||
# Make the request | ||
page_result = await client.list_clusters(request=request) | ||
clusters_list = [] | ||
|
||
# Handle the response | ||
async for response in page_result: | ||
clusters_list.append(json.loads(proto.Message.to_json(response))) | ||
|
||
return clusters_list | ||
except Exception as e: | ||
self.log.exception(f"Error fetching cluster list") | ||
return {"error": str(e)} | ||
|
||
async def get_cluster_detail(self, cluster_selected): | ||
try: | ||
# Create a client | ||
client = dataproc.ClusterControllerAsyncClient( | ||
client_options={ | ||
"api_endpoint": f"us-central1-dataproc.googleapis.com:443" | ||
}, | ||
credentials=oauth2.Credentials(self._access_token), | ||
) | ||
|
||
# Initialize request argument(s) | ||
request = dataproc.GetClusterRequest( | ||
project_id=self.project_id, | ||
region=self.region_id, | ||
cluster_name=cluster_selected, | ||
) | ||
|
||
# Make the request | ||
response = await client.get_cluster(request=request) | ||
|
||
# Handle the response | ||
return json.loads(proto.Message.to_json(response)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching cluster detail") | ||
return {"error": str(e)} | ||
|
||
async def stop_cluster(self, cluster_selected): | ||
try: | ||
# Create a client | ||
client = dataproc.ClusterControllerAsyncClient( | ||
client_options={ | ||
"api_endpoint": f"us-central1-dataproc.googleapis.com:443" | ||
}, | ||
credentials=oauth2.Credentials(self._access_token), | ||
) | ||
|
||
# Initialize request argument(s) | ||
request = dataproc.StopClusterRequest( | ||
project_id=self.project_id, | ||
region=self.region_id, | ||
cluster_name=cluster_selected, | ||
) | ||
|
||
operation = await client.stop_cluster(request=request) | ||
|
||
response = await operation.result() | ||
# Handle the response | ||
return json.loads(proto.Message.to_json(response)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching stop cluster") | ||
return {"error": str(e)} | ||
|
||
async def start_cluster(self, cluster_selected): | ||
try: | ||
# Create a client | ||
client = dataproc.ClusterControllerAsyncClient( | ||
client_options={ | ||
"api_endpoint": f"us-central1-dataproc.googleapis.com:443" | ||
}, | ||
credentials=oauth2.Credentials(self._access_token), | ||
) | ||
|
||
# Initialize request argument(s) | ||
request = dataproc.StartClusterRequest( | ||
project_id=self.project_id, | ||
region=self.region_id, | ||
cluster_name=cluster_selected, | ||
) | ||
|
||
operation = await client.start_cluster(request=request) | ||
|
||
response = await operation.result() | ||
# Handle the response | ||
return json.loads(proto.Message.to_json(response)) | ||
except Exception as e: | ||
self.log.exception(f"Error fetching start cluster") | ||
return {"error": str(e)} | ||
|
||
async def delete_cluster(self, cluster_selected): | ||
try: | ||
# Create a client | ||
client = dataproc.ClusterControllerAsyncClient( | ||
client_options={ | ||
"api_endpoint": f"us-central1-dataproc.googleapis.com:443" | ||
}, | ||
credentials=oauth2.Credentials(self._access_token), | ||
) | ||
|
||
# Initialize request argument(s) | ||
request = dataproc.DeleteClusterRequest( | ||
project_id=self.project_id, | ||
region=self.region_id, | ||
cluster_name=cluster_selected, | ||
) | ||
|
||
operation = await client.delete_cluster(request=request) | ||
|
||
response = await operation.result() | ||
# Handle the response | ||
if isinstance(response, Empty): | ||
return "Deleted successfully" | ||
else: | ||
return json.loads(proto.Message.to_json(response)) | ||
except Exception as e: | ||
self.log.exception(f"Error deleting cluster") | ||
return {"error": str(e)} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ dependencies = [ | |
"pendulum>=3.0.0", | ||
"pydantic~=1.10.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we pinning the minor versions in these packages? I.E. why "~=.." instead of ">=.."? |
||
"bigframes~=0.22.0", | ||
"aiohttp~=3.9.5" | ||
"aiohttp~=3.9.5", | ||
"google-cloud-dataproc~=5.10.2" | ||
] | ||
dynamic = ["version", "description", "authors", "urls", "keywords"] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a file in the
controllers
directory for Dataproc calleddataproc
.Move all of the methods from this file into that one and then delete this entire file.