Mencantumkan template instance dalam sebuah project
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Mendapatkan daftar template instance yang ditentukan dalam sebuah project.
Jelajahi Lebih Lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat referensi berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python to list instance templates defined in a Google Cloud project.\u003c/p\u003e\n"],["\u003cp\u003eThe samples demonstrate how to use the Compute Engine client libraries for each respective language to retrieve a list of available instance templates.\u003c/p\u003e\n"],["\u003cp\u003eEach code example includes instructions on setting up authentication via Application Default Credentials and provides links to the relevant API reference documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe content references the compute engine quickstart, to assist the user with setting up for using the compute engine client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe user can obtain more samples by using the google cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# List instance templates in a project\n\nGet a list of instance templates defined in a project.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Get, List, and Delete Instance Templates](/compute/docs/instance-templates/get-list-delete-instance-templates)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listInstanceTemplates prints a list of InstanceTemplate objects available in a project.\n func listInstanceTemplates(w io.Writer, projectID string) error {\n \t// projectID := \"your_project_id\"\n\n \tctx := context.Background()\n \tinstanceTemplatesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstanceTemplatesClient_NewInstanceTemplatesRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewInstanceTemplatesRESTClient: %w\", err)\n \t}\n \tdefer instanceTemplatesClient.Close()\n\n \treq := &computepb.ListInstanceTemplatesRequest{\n \t\tProject: projectID,\n \t}\n\n \tit := instanceTemplatesClient.List(ctx, req)\n \tfor {\n \t\tinstance, err := it.Next()\n \t\tif err == iterator.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_Operation_Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Fprintf(w, \"- %s %s\\n\", instance.GetName(), instance.GetProperties().GetMachineType())\n \t}\n\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplate.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html.ListPagedResponse;\n import java.io.IOException;\n\n public class ListInstanceTemplates {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // projectId: project ID or project number of the Cloud project you use.\n String projectId = \"your-project-id\";\n listInstanceTemplates(projectId);\n }\n\n // Get a list of InstanceTemplate objects available in a project.\n public static ListPagedResponse listInstanceTemplates(String projectId) throws IOException {\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html instanceTemplatesClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html.create()) {\n int count = 0;\n System.out.println(\"Listing instance templates...\");\n ListPagedResponse templates = instanceTemplatesClient.list(projectId);\n for (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplate.html instanceTemplate : templates.iterateAll()) {\n System.out.printf(\"%s. %s%n\", ++count, instanceTemplate.getName());\n }\n return templates;\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Node.js API\nreference documentation](/nodejs/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment and replace these variables before running the sample.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n\n const compute = require('https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html');\n\n // Print a list of instance template objects available in a project.\n async function listInstanceTemplates() {\n const instanceTemplatesClient = new compute.https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html();\n\n const instanceTemplates = instanceTemplatesClient.listAsync({\n project: projectId,\n });\n\n for await (const instanceTemplate of instanceTemplates) {\n console.log(` - ${instanceTemplate.name}`);\n }\n }\n\n listInstanceTemplates();\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n from collections.abc import Iterable\n\n from google.cloud import compute_v1\n\n\n def list_instance_templates(project_id: str) -\u003e Iterable[compute_v1.InstanceTemplate]:\n \"\"\"\n Get a list of InstanceTemplate objects available in a project.\n\n Args:\n project_id: project ID or project number of the Cloud project you use.\n\n Returns:\n Iterable list of InstanceTemplate objects.\n \"\"\"\n template_client = compute_v1.InstanceTemplatesClient()\n return template_client.list(project=project_id)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]