-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first steps in adding sample * consistent formatting with create_cluster.py * test first draft * update_cluster sample complete * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * docs: add update cluster sample - fixing formatting * Update samples/snippets/update_cluster.py Co-authored-by: Bu Sun Kim <[email protected]> * Update samples/snippets/update_cluster.py Co-authored-by: Bu Sun Kim <[email protected]> * updated test, still fine-tuning * added get_cluster to test * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * another attempt at writing test * new test pattern * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * updated static for new_num_instances and fixed linting error Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Bu Sun Kim <[email protected]>
- Loading branch information
1 parent
0f29c12
commit b11a14b
Showing
8 changed files
with
212 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/google-cloud-dataproc/samples/snippets/update_cluster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2021 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 | ||
# | ||
# 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. | ||
|
||
# This sample walks a user through updating the number of clusters using the Dataproc | ||
# client library. | ||
|
||
# Usage: | ||
# python update_cluster.py --project_id <PROJECT_ID> --region <REGION> --cluster_name <CLUSTER_NAME> | ||
|
||
import sys | ||
|
||
# [START dataproc_update_cluster] | ||
from google.cloud import dataproc_v1 as dataproc | ||
|
||
|
||
def update_cluster(project_id, region, cluster_name, new_num_instances): | ||
"""This sample walks a user through updating a Cloud Dataproc cluster | ||
using the Python client library. | ||
Args: | ||
project_id (str): Project to use for creating resources. | ||
region (str): Region where the resources should live. | ||
cluster_name (str): Name to use for creating a cluster. | ||
""" | ||
|
||
# Create a client with the endpoint set to the desired cluster region. | ||
client = dataproc.ClusterControllerClient( | ||
client_options={"api_endpoint": f"{region}-dataproc.googleapis.com:443"} | ||
) | ||
|
||
# Get cluster you wish to update. | ||
cluster = client.get_cluster( | ||
project_id=project_id, region=region, cluster_name=cluster_name | ||
) | ||
|
||
# Update number of clusters | ||
mask = {"paths": {"config.worker_config.num_instances": str(new_num_instances)}} | ||
|
||
# Update cluster config | ||
cluster.config.worker_config.num_instances = new_num_instances | ||
|
||
# Update cluster | ||
operation = client.update_cluster( | ||
project_id=project_id, | ||
region=region, | ||
cluster=cluster, | ||
cluster_name=cluster_name, | ||
update_mask=mask, | ||
) | ||
|
||
# Output a success message. | ||
updated_cluster = operation.result() | ||
print(f"Cluster was updated successfully: {updated_cluster.cluster_name}") | ||
|
||
|
||
# [END dataproc_update_cluster] | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 5: | ||
sys.exit("python update_cluster.py project_id region cluster_name") | ||
|
||
project_id = sys.argv[1] | ||
region = sys.argv[2] | ||
cluster_name = sys.argv[3] | ||
new_num_instances = sys.argv[4] | ||
update_cluster(project_id, region, cluster_name) |
80 changes: 80 additions & 0 deletions
80
packages/google-cloud-dataproc/samples/snippets/update_cluster_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2021 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 | ||
# | ||
# 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. | ||
|
||
# This sample walks a user through updating the number of clusters using the Dataproc | ||
# client library. | ||
|
||
|
||
import os | ||
import uuid | ||
|
||
from google.cloud.dataproc_v1.services.cluster_controller.client import ( | ||
ClusterControllerClient, | ||
) | ||
import pytest | ||
|
||
import update_cluster | ||
|
||
|
||
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
REGION = "us-central1" | ||
CLUSTER_NAME = f"py-cc-test-{str(uuid.uuid4())}" | ||
NEW_NUM_INSTANCES = 5 | ||
CLUSTER = { | ||
"project_id": PROJECT_ID, | ||
"cluster_name": CLUSTER_NAME, | ||
"config": { | ||
"master_config": {"num_instances": 1, "machine_type_uri": "n1-standard-2"}, | ||
"worker_config": {"num_instances": 2, "machine_type_uri": "n1-standard-2"}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup_teardown(cluster_client): | ||
# Create the cluster. | ||
operation = cluster_client.create_cluster( | ||
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER} | ||
) | ||
operation.result() | ||
|
||
yield | ||
|
||
cluster_client.delete_cluster( | ||
request={ | ||
"project_id": PROJECT_ID, | ||
"region": REGION, | ||
"cluster_name": CLUSTER_NAME, | ||
} | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def cluster_client(): | ||
cluster_client = ClusterControllerClient( | ||
client_options={"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)} | ||
) | ||
return cluster_client | ||
|
||
|
||
def test_update_cluster(capsys, cluster_client: ClusterControllerClient): | ||
# Wrapper function for client library function | ||
update_cluster.update_cluster(PROJECT_ID, REGION, CLUSTER_NAME, NEW_NUM_INSTANCES) | ||
new_num_cluster = cluster_client.get_cluster( | ||
project_id=PROJECT_ID, region=REGION, cluster_name=CLUSTER_NAME | ||
) | ||
|
||
out, _ = capsys.readouterr() | ||
assert CLUSTER_NAME in out | ||
assert new_num_cluster.config.worker_config.num_instances == NEW_NUM_INSTANCES |