Obtenir l'état de protection contre la suppression d'une VM
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Déterminez si la protection contre la suppression est activée pour une VM spécifiée.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python for determining if deletion protection is enabled for a specific virtual machine (VM) instance.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples utilize the Compute Engine client libraries for each respective language to interact with the Google Cloud Compute Engine API.\u003c/p\u003e\n"],["\u003cp\u003eEach code example requires users to authenticate using Application Default Credentials and provides links on how to do this, as well as setting up compute engine client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples retrieve the VM instance details, including the deletion protection flag, by specifying the project ID, zone, and instance name.\u003c/p\u003e\n"],["\u003cp\u003eThe result of the code execution is the state of the deletion protection, which will print or return a true/false boolean value to the user.\u003c/p\u003e\n"]]],[],null,["# Get the deletion protection status of a VM\n\nDetermine whether deletion protection is enabled for a specified VM.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Prevent accidental VM deletion](/compute/docs/instances/preventing-accidental-vm-deletion)\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 // getDeleteProtection prints the state of delete protection flag of given instance.\n func getDeleteProtection(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, \"Instance %s has DeleteProtection value: %v\", instanceName, instance.GetDeletionProtection())\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 GetDeleteProtection {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // project: 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 new virtual machine.\n String projectId = \"your-project-id-or-number\";\n String zone = \"zone-name\";\n String instanceName = \"instance-name\";\n getDeleteProtection(projectId, zone, instanceName);\n }\n\n // Returns the state of delete protection flag of given instance.\n public static boolean getDeleteProtection(String projectId, String zone,\n String instanceName) 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 deleteProtection = 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_getDeletionProtection__() System.out.printf(\"Retrieved Delete Protection setting for instance: %s : %s\", instanceName,\n deleteProtection);\n\n return deleteProtection;\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 // Print the state of delete protection flag of given instance.\n async function getDeleteProtection() {\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(\n `Instance ${instanceName} has deletionProtection value: ${instance.deletionProtection}`\n );\n }\n\n getDeleteProtection();\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_delete_protection(project_id: str, zone: str, instance_name: str) -\u003e bool:\n \"\"\"\n Returns the state of delete protection flag of given instance.\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 boolean value of the delete protection setting.\n \"\"\"\n instance_client = comphttps://cloud.google.com/python/docs/reference/compute/latest/thttps://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient.html instance = instinstance_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 project=project_id, zone=zone, instance=instance_name\n )\n return instance.deletion_protection\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)."]]