Skip to content

vm.detachDisk requires a device name #922

Closed

Description

@stephenplusplus

Right now, we're sending through a disk name, which !== device name.

Here's a fix that will find the device name if the provided device name automatically if a Disk object doesn't have it already:

/**
 * Custom error type for errors related to detaching a disk.
 *
 * @param {string} message - Custom error message.
 * @return {Error}
 */
var DetachDiskError = createErrorClass('DetachDiskError', function(message) {
  this.message = message;
});

VM.prototype.detachDisk = function(deviceName, callback) {
  var self = this;

  if (deviceName instanceof Disk) {
    var disk = deviceName;

    if (disk.metadata.deviceName) {
      self.detachDisk(disk.metadata.deviceName, callback);
      return;
    }

    this.getMetadata(function(err, metadata) {
      if (err) {
        callback(new DetachDiskError(err.message));
        return;
      }

      var deviceName;
      var baseUrl = 'https://www.googleapis.com/compute/v1/';

      // Try to find the deviceName by matching the source of the attached disks
      // to the name of the disk provided by the user.
      for (var i = 0; !deviceName && i < metadata.disks.length; i++) {
        var attachedDisk = metadata.disks[i];
        var source = attachedDisk.source.replace(baseUrl, '');

        if (source === disk.formattedName) {
          deviceName = attachedDisk.deviceName;
        }
      }

      if (deviceName) {
        self.detachDisk(deviceName, callback);
      } else {
        callback(new DetachDiskError('Could not find a deviceName for disk.'));
      }
    });
    return;
  }

  var query = {
    deviceName: deviceName
  };

  this.makeReq_('POST', '/detachDisk', query, null, callback || util.noop);
};

// system test...
it('should attach and detach a disk', function(done) {
  var disk = zone.disk(generateName());

  // This test waits on a lot of operations.
  this.timeout(90000);

  async.series([
    createDisk,
    attachDisk,
    detachDisk
  ], done);

  function createDisk(callback) {
    var config = {
      os: 'ubuntu'
    };

    disk.create(config, execAfterOperationComplete(callback));
  }

  function attachDisk(callback) {
    vm.attachDisk(disk, execAfterOperationComplete(callback));
  }

  function detachDisk(callback) {
    vm.detachDisk(disk, execAfterOperationComplete(callback));
  }
});

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    api: computeIssues related to the Compute Engine API.
    on Oct 30, 2015
  2. changed the title [-]zone.detachDisk requires a device name[/-] [+]vm.detachDisk requires a device name[/+] on Oct 30, 2015
  3. cromwellian commented on Dec 5, 2018

    @cromwellian

    This code fails if the disk is created with the Beta Compute API, then the disk base URL will be https://www.googleapis.com/compute/beta/

  4. added a commit that references this issue on Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: computeIssues related to the Compute Engine API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions