Verifica se un'istanza Compute Engine è preemptible
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo esempio controlla se una determinata istanza Compute Engine è preemptible o meno.
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]