|
| 1 | +# Copyright 2024 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 | +# https://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 | +import os |
| 15 | + |
| 16 | +from google.cloud.tpu_v2alpha1 import CreateQueuedResourceRequest, Node |
| 17 | + |
| 18 | + |
| 19 | +def create_queued_resource_network( |
| 20 | + project_id: str, |
| 21 | + zone: str, |
| 22 | + tpu_name: str, |
| 23 | + tpu_type: str = "v2-8", |
| 24 | + runtime_version: str = "tpu-vm-tf-2.17.0-pjrt", |
| 25 | + queued_resource_name: str = "resource-name", |
| 26 | + network: str = "default", |
| 27 | +) -> Node: |
| 28 | + # [START tpu_queued_resources_network] |
| 29 | + from google.cloud import tpu_v2alpha1 |
| 30 | + |
| 31 | + # TODO(developer): Update and un-comment below lines |
| 32 | + # project_id = "your-project-id" |
| 33 | + # zone = "us-central1-b" |
| 34 | + # tpu_name = "tpu-name" |
| 35 | + # tpu_type = "v2-8" |
| 36 | + # runtime_version = "tpu-vm-tf-2.17.0-pjrt" |
| 37 | + # queued_resource_name = "resource-name" |
| 38 | + # network = "default" |
| 39 | + |
| 40 | + node = tpu_v2alpha1.Node() |
| 41 | + node.accelerator_type = tpu_type |
| 42 | + node.runtime_version = runtime_version |
| 43 | + # Setting network configuration |
| 44 | + node.network_config = tpu_v2alpha1.NetworkConfig( |
| 45 | + network=network, # Update if you want to use a specific network |
| 46 | + subnetwork="default", # Update if you want to use a specific subnetwork |
| 47 | + enable_external_ips=True, |
| 48 | + can_ip_forward=True, |
| 49 | + ) |
| 50 | + |
| 51 | + node_spec = tpu_v2alpha1.QueuedResource.Tpu.NodeSpec() |
| 52 | + node_spec.parent = f"projects/{project_id}/locations/{zone}" |
| 53 | + node_spec.node_id = tpu_name |
| 54 | + node_spec.node = node |
| 55 | + |
| 56 | + resource = tpu_v2alpha1.QueuedResource() |
| 57 | + resource.tpu = tpu_v2alpha1.QueuedResource.Tpu(node_spec=[node_spec]) |
| 58 | + |
| 59 | + request = CreateQueuedResourceRequest( |
| 60 | + parent=f"projects/{project_id}/locations/{zone}", |
| 61 | + queued_resource_id=queued_resource_name, |
| 62 | + queued_resource=resource, |
| 63 | + ) |
| 64 | + |
| 65 | + client = tpu_v2alpha1.TpuClient() |
| 66 | + operation = client.create_queued_resource(request=request) |
| 67 | + |
| 68 | + response = operation.result() |
| 69 | + print(response.name) |
| 70 | + print(response.tpu.node_spec[0].node.network_config) |
| 71 | + print(resource.tpu.node_spec[0].node.network_config.network == "default") |
| 72 | + # Example response: |
| 73 | + # network: "default" |
| 74 | + # subnetwork: "default" |
| 75 | + # enable_external_ips: true |
| 76 | + # can_ip_forward: true |
| 77 | + |
| 78 | + # [END tpu_queued_resources_network] |
| 79 | + return response |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 84 | + ZONE = "us-central1-b" |
| 85 | + create_queued_resource_network( |
| 86 | + project_id=PROJECT_ID, |
| 87 | + zone=ZONE, |
| 88 | + tpu_name="tpu-name", |
| 89 | + queued_resource_name="resource-name", |
| 90 | + ) |
0 commit comments