|
| 1 | +# Copyright 2023 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 | +# [START vmwareengine_create_custom_cluster] |
| 16 | +from google.api_core import operation |
| 17 | +from google.cloud import vmwareengine_v1 |
| 18 | + |
| 19 | + |
| 20 | +def create_custom_cluster( |
| 21 | + project_id: str, |
| 22 | + zone: str, |
| 23 | + private_cloud_name: str, |
| 24 | + cluster_name: str, |
| 25 | + node_count: int = 4, |
| 26 | + core_count: int = 28, |
| 27 | +) -> operation.Operation: |
| 28 | + """ |
| 29 | + Create a new cluster with custom number of cores in its nodes |
| 30 | + in a private cloud. |
| 31 | +
|
| 32 | + Creation of a new cluster is a long-running operation and it might take well over 15 minutes. |
| 33 | +
|
| 34 | + Args: |
| 35 | + project_id: name of the project you want to use. |
| 36 | + zone: region in which your private cloud is located. |
| 37 | + private_cloud_name: name of the private cloud hosting the new cluster. |
| 38 | + cluster_name: name of the new cluster. |
| 39 | + node_count: number of nodes in the new cluster. |
| 40 | + core_count: number of CPU cores in the new cluster nodes. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + An Operation object related to started cluster creation operation. |
| 44 | +
|
| 45 | + Raises: |
| 46 | + ValueError in case an incorrect number of nodes is provided. |
| 47 | + """ |
| 48 | + if node_count < 3: |
| 49 | + raise ValueError("Cluster needs to have at least 3 nodes") |
| 50 | + |
| 51 | + request = vmwareengine_v1.CreateClusterRequest() |
| 52 | + request.parent = ( |
| 53 | + f"projects/{project_id}/locations/{zone}/privateClouds/{private_cloud_name}" |
| 54 | + ) |
| 55 | + |
| 56 | + request.cluster = vmwareengine_v1.Cluster() |
| 57 | + request.cluster.name = cluster_name |
| 58 | + |
| 59 | + # Currently standard-72 is the only supported node type. |
| 60 | + request.cluster.node_type_configs = { |
| 61 | + "standard-72": vmwareengine_v1.NodeTypeConfig() |
| 62 | + } |
| 63 | + request.cluster.node_type_configs["standard-72"].node_count = node_count |
| 64 | + request.cluster.node_type_configs["standard-72"].custom_core_count = core_count |
| 65 | + |
| 66 | + client = vmwareengine_v1.VmwareEngineClient() |
| 67 | + return client.create_cluster(request) |
| 68 | + |
| 69 | + |
| 70 | +# [END vmwareengine_create_custom_cluster] |
0 commit comments