|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This sample walks a user through updating the number of clusters using the Dataproc |
| 16 | +# client library. |
| 17 | + |
| 18 | +# Usage: |
| 19 | +# python update_cluster.py --project_id <PROJECT_ID> --region <REGION> --cluster_name <CLUSTER_NAME> |
| 20 | + |
| 21 | +import sys |
| 22 | + |
| 23 | +# [START dataproc_update_cluster] |
| 24 | +from google.cloud import dataproc_v1 as dataproc |
| 25 | + |
| 26 | + |
| 27 | +def update_cluster(project_id, region, cluster_name, new_num_instances): |
| 28 | + """This sample walks a user through updating a Cloud Dataproc cluster |
| 29 | + using the Python client library. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_id (str): Project to use for creating resources. |
| 33 | + region (str): Region where the resources should live. |
| 34 | + cluster_name (str): Name to use for creating a cluster. |
| 35 | + """ |
| 36 | + |
| 37 | + # Create a client with the endpoint set to the desired cluster region. |
| 38 | + client = dataproc.ClusterControllerClient( |
| 39 | + client_options={"api_endpoint": f"{region}-dataproc.googleapis.com:443"} |
| 40 | + ) |
| 41 | + |
| 42 | + # Get cluster you wish to update. |
| 43 | + cluster = client.get_cluster( |
| 44 | + project_id=project_id, region=region, cluster_name=cluster_name |
| 45 | + ) |
| 46 | + |
| 47 | + # Update number of clusters |
| 48 | + mask = {"paths": {"config.worker_config.num_instances": str(new_num_instances)}} |
| 49 | + |
| 50 | + # Update cluster config |
| 51 | + cluster.config.worker_config.num_instances = new_num_instances |
| 52 | + |
| 53 | + # Update cluster |
| 54 | + operation = client.update_cluster( |
| 55 | + project_id=project_id, |
| 56 | + region=region, |
| 57 | + cluster=cluster, |
| 58 | + cluster_name=cluster_name, |
| 59 | + update_mask=mask, |
| 60 | + ) |
| 61 | + |
| 62 | + # Output a success message. |
| 63 | + updated_cluster = operation.result() |
| 64 | + print(f"Cluster was updated successfully: {updated_cluster.cluster_name}") |
| 65 | + |
| 66 | + |
| 67 | +# [END dataproc_update_cluster] |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + if len(sys.argv) < 5: |
| 72 | + sys.exit("python update_cluster.py project_id region cluster_name") |
| 73 | + |
| 74 | + project_id = sys.argv[1] |
| 75 | + region = sys.argv[2] |
| 76 | + cluster_name = sys.argv[3] |
| 77 | + new_num_instances = sys.argv[4] |
| 78 | + update_cluster(project_id, region, cluster_name) |
0 commit comments