Compute Engine インスタンスのホスト名を取得する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このサンプルでは、特定の Compute Engine インスタンスのホスト名を取得します。
コードサンプル
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python for retrieving the hostname of a specified Compute Engine instance.\u003c/p\u003e\n"],["\u003cp\u003eThe samples require setting up Application Default Credentials for authentication and specify a project ID, zone, and instance name.\u003c/p\u003e\n"],["\u003cp\u003eEach code example demonstrates how to use the respective language's Google Cloud client library to fetch and display the hostname of the given VM instance.\u003c/p\u003e\n"],["\u003cp\u003eIf a custom hostname has not been set for an instance, the respective methods will return an empty string in Go, be undefined in Node.js and Java, and output as a null value in Python.\u003c/p\u003e\n"]]],[],null,["# Get the hostname of a Compute Engine instance\n\nThis sample retrieves the hostname of a given Compute Engine instance.\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 )\n\n // getInstanceHostname prints the hostname of the Google Cloud VM instance.\n func getInstanceHostname(w io.Writer, projectID, zone, instanceName string) error {\n \t// projectID := \"your_project_id\"\n \t// zone := \"europe-central2-b\"\n \t// instanceName := \"your_instance_name\"\n\n \tctx := context.Background()\n \tinstancesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstancesClient_NewInstancesRESTClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewInstancesRESTClient: %w\", err)\n \t}\n \tdefer instancesClient.Close()\n\n \treq := &computepb.GetInstanceRequest{\n \t\tProject: projectID,\n \t\tZone: zone,\n \t\tInstance: instanceName,\n \t}\n\n \tinstance, err := instancesClient.Get(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to get instance: %w\", err)\n \t}\n\n \t// If a custom hostname is not set, the output for instance.GetHostname() will be \"\"\n \tfmt.Fprintf(w, \"Instance %v has hostname: %v\", instanceName, instance.GetHostname())\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.Instance.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html;\n import java.io.IOException;\n\n public class GetInstanceHostname {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String project = \"your-project-id\";\n String zone = \"zone-name\"; // eg: \"us-central1-a\"\n String instanceName = \"instance-name\"; // Name of the VM instance to retrieve.\n\n getInstanceHostname(project, zone, instanceName);\n }\n\n // Retrieves the hostname of the Google Cloud VM instance.\n public static void getInstanceHostname(String projectId, String zone, String instanceName)\n throws IOException {\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html instancesClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.html.create()) {\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html instance = instancesClient.get(projectId, zone, instanceName);\n\n if (instance.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html#com_google_cloud_compute_v1_Instance_hasHostname__()) {\n // If a custom hostname is not set, the output for instance.getHostname() will be undefined.\n System.out.printf(\"Custom Hostname for the instance %s is: %s\", instanceName,\n instance.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html#com_google_cloud_compute_v1_Instance_getHostname__());\n }\n }\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 // const zone = 'europe-central2-b'\n // const instanceName = 'YOUR_INSTANCE_NAME'\n\n const compute = require('https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html');\n\n async function getInstanceHostname() {\n const instancesClient = new compute.https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html();\n\n const [instance] = await instancesClient.get({\n project: projectId,\n zone,\n instance: instanceName,\n });\n // If a custom hostname is not set, the output for instance.hostname will be undefined\n console.log(`Instance ${instanceName} has hostname: ${instance.hostname}`);\n }\n\n getInstanceHostname();\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 google.cloud import https://cloud.google.com/python/docs/reference/compute/latest/\n\n\n def get_hostname(project_id: str, zone: str, instance_name: str) -\u003e str:\n \"\"\"\n Retrieve the hostname of given instance.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n zone: name of the zone you want to use. For example: \"us-west3-b\"\n instance_name: name of the virtual machine to check.\n\n Returns:\n The hostname of an instance.\n \"\"\"\n instance_client = https://cloud.google.com/python/docs/reference/compute/latest/.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient.html()\n instance = instance_client.https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient.html#google_cloud_compute_v1_services_instances_InstancesClient_get(\n project=project_id, zone=zone, instance=instance_name\n )\n return instance.hostname\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)."]]