Search code examples
google-apigoogle-compute-enginegoogle-api-python-client

Invalid value for field 'disk' when trying to set disk Auto Delete option


On Google Compute Engine, I have a running instance named my-inst with its root persistent disk pd-my-inst attached. I want to set the AutoDelete option of the disk to False using the Python API client. I know I could set this option at instance creation or by using gcloud compute tool, but my application requires the ability to set it after instance creation with Python API. The code I use to send the request is:

request = gce_service.instances().setDiskAutoDelete(autoDelete=False,
    deviceName='pd-my-inst', project=PROJECT_ID, instance='my-inst', zone=DEFAULT_ZONE)
response = gce_service.execute(http=http_auth)

The response I get is as detailed in the API Reference so it was properly sent. I then use the zoneOperations().get() method until the ['status'] field of the response is 'DONE', similarly to the _blocking_call() function provided in the Google Compute API Python Guide. The zone operation object I get is then (some parts eluded for privacy):

{
u'status': u'DONE',
u'kind': u'compute#operation',
u'name': u'operation-1412647209801-504cb8f221529-...',
u'zone': u'https://www.googleapis.com/compute/v1/projects/MY-PROJECT-NAME/zones/us-central1-a',
u'startTime': u'2014-10-06T19:00:10.094-07:00',
u'httpErrorMessage': u'BAD REQUEST',
u'insertTime': u'2014-10-06T19:00:09.908-07:00',
u'targetId': u'1543069760...',
u'targetLink': u'https://www.googleapis.com/compute/v1/projects/MY-PROJECT-NAME/zones/us-central1-a/instances/my-inst',
u'operationType': u'setDiskAutoDelete',
u'error':
    {
    u'errors':
        [{
        u'message': u"Invalid value for field 'disk': 'pd-my-inst'. ",
        u'code': u'INVALID_FIELD_VALUE'
        }]
    },
u'progress': 100,
u'endTime': u'2014-10-06T19:00:10.464-07:00',
u'httpErrorStatusCode': 400,
u'id': u'998580128...',
u'selfLink': u'https://www.googleapis.com/compute/v1/projects/MY-PROJECT-NAME/zones/us-central1-a/operations/operation-1412647209801-504cb8f221529-...',
u'user': u'MY_USERNAME'}

I also tried to send a request using the Web-based APIs Explorer for the setDiskAutoDelete() method and I got the exact same error.

I don't understand why I get the INVALID_FIELD_VALUE error because I provided the disk device name as requested by the API Reference. Is there something I'm missing about this field? Is the device name different from the name given to it at instance creation time?

Thank you for your help! :)


Solution

  • The DeviceName field should contain the disk name as seen by the VM instance (persistent-disk-0 for this example), which is different from the name given to the persistent disk when creating it (pd-my-inst). The DeviceName is in the json instance object obtained with the instances().get() or instances().list() methods:

    {
    u'status': u'RUNNING',
    u'kind': u'compute#instance',
    u'machineType': ...
    u'name': u'my-inst',
    u'zone': u'https://www.googleapis.com/compute/v1/projects/MY-PROJECT-NAME/zones/us-central1-a',
    u'tags': {u'fingerprint': u'42WmSpB8rSM='},
    u'disks':
        [{
        u'deviceName': u'persistent-disk-0',
        u'kind': u'compute#attachedDisk',
        u'autoDelete': True,
        u'index': 0,
        u'boot': True,
        u'source': u'https://www.googleapis.com/compute/v1/projects/MY-PROJECT-NAME/zones/us-central1-a/disks/pd-my-inst',
        u'type': u'PERSISTENT'
        }],
    u'scheduling': {u'automaticRestart': True, u'onHostMaintenance': u'MIGRATE'},
    u'canIpForward': False,
    ...
    }
    

    It can be retreived using: instance['disks'][DISK_INDEX]['deviceName']