Prüfen, ob eine Compute Engine-Instanz präemptiv ist
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Beispiel wird geprüft, ob eine bestimmte Compute Engine-Instanz präemptiv ist.
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python to check if a Compute Engine instance is preemptible.\u003c/p\u003e\n"],["\u003cp\u003eEach sample requires setting up Application Default Credentials for authentication and refers to Compute Engine API documentation for further details.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples use the Compute Engine client libraries to retrieve instance information, including the preemptible status.\u003c/p\u003e\n"],["\u003cp\u003eUsers need to provide the project ID, zone, and instance name as parameters for the provided functions in order to test their instance.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples also guide users on initial steps such as setting up compute engine, and finding sample code for other Google Cloud Products.\u003c/p\u003e\n"]]],[],null,["# Check if a Compute Engine instance is preemptible\n\nThis sample checks if a given Compute Engine instance is preemptible or not.\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 // printPreemtible prints if a given instance is preemptible or not.\n func printPreemtible(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 \tfmt.Fprintf(w, \"Is instance preemptible: %v\\n\", instance.GetScheduling().GetPreemptible())\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 IsPreemptible {\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 want to use.\n // zone: name of the zone you want to use. For example: \"us-west3-b\"\n // instanceName: name of the virtual machine to check.\n String projectId = \"your-project-id-or-number\";\n String zone = \"zone-name\";\n String instanceName = \"instance-name\";\n\n isPreemptible(projectId, zone, instanceName);\n }\n\n // Check if a given instance is preemptible or not.\n public static void isPreemptible(String projectId, String zone, String instanceName)\n throws IOException {\n\n try (Insthttps://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.htmltancesClient = Insthttps://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstancesClient.htmlate()) {\n Insthttps://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.htmltance = instancesClient.get(projectId, zone, instanceName);\n boolean isPreemptible = instinstance.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Instance.html#com_google_cloud_compute_v1_Instance_getScheduling__()Preemptible();\n\n System.out.printf(\"Preemptible status: %s\", isPreemptible);\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 printPreemptible() {\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\n console.log(`Is instance preemptible: ${instance.scheduling.preemptible}`);\n }\n\n printPreemptible();\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 is_preemptible(project_id: str, zone: str, instance_name: str) -\u003e bool:\n \"\"\"\n Check if a given instance is preemptible or not.\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 Returns:\n The preemptible status of the 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.scheduling.preemptible\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)."]]