VM
Source: vm.
An Instance object allows you to interact with a Google Compute Engine instance.
Methods
new VM(zone, name)
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
Parameters
| Name | Type | Optional | Description |
|---|---|---|---|
|
zone |
|
|
Zone object this instance belongs to. |
|
name |
|
|
Name of the instance. |
- See also
Properties
id string
name string
zone Zone
The parent Zone instance of this VM instance.
Methods
attachDisk(disk[, options], callback)
Attach a disk to the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
const disk = zone.disk('my-disk');
function callback(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
}
vm.attachDisk(disk, callback);
//-
// Provide an options object to customize the request.
//-
const options = {
autoDelete: true,
readOnly: true
};
vm.attachDisk(disk, options, callback);
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.attachDisk(disk, options).then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
disk |
module:compute/disk |
|
The disk to attach. |
||||||||||||||||
|
options |
object |
Yes |
See the Instances: attachDisk request body. Values in
|
||||||||||||||||
|
callback |
function() |
|
The callback function. Values in
|
- See also
- Throws
-
Errorif a {module:compute/disk} is not provided.
create(config)
Create a virtual machine.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
const config = {
// ...
};
vm.create(config, function(err, vm, operation, apiResponse) {
// `vm` is a VM object.
// `operation` is an Operation object that can be used to check the
// status of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.create(config).then(function(data) {
const vm = data[0];
const operation = data[1];
const apiResponse = data[2];
});
Parameter
| Name | Type | Optional | Description |
|---|---|---|---|
|
config |
object |
|
See {Zone#createVM}. |
delete([callback])
Delete the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.delete(function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.delete().then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
Yes |
The callback function. Values in
|
- See also
detachDisk(deviceName[, callback])
Detach a disk from the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
const disk = zone.disk('my-disk');
vm.detachDisk(disk, function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.detachDisk(disk).then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
deviceName |
(module:compute/disk or string) |
|
The device name of the disk to detach. If a Disk object is provided, we try to find the device name automatically by searching through the attached disks on the instance. |
||||||||||||||||
|
callback |
function() |
Yes |
The callback function. Values in
|
exists(callback)
Check if the vm exists.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.exists(function(err, exists) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.exists().then(function(data) {
const exists = data[0];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
|
The callback function. Values in
|
get([options])
Get a virtual machine if it exists.
You may optionally use this to "get or create" an object by providing an
object with autoCreate set to true. Any extra configuration that is
normally required for the create method must be contained within this
object as well.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.get(function(err, vm, apiResponse) {
// `vm` is a VM object.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.get().then(function(data) {
const vm = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
options |
options |
Yes |
Configuration object. Values in
|
getMetadata([callback])
Get the instance's metadata.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.getMetadata(function(err, metadata, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.getMetadata().then(function(data) {
// Representation of this VM as the API sees it.
const metadata = data[0];
const apiResponse = data[1];
// Custom metadata and predefined keys.
const customMetadata = metadata.metadata;
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
Yes |
The callback function. Values in
|
getSerialPortOutput([port], callback)
Returns the serial port output for the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.getSerialPortOutput(function(err, output, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.getSerialPortOutput().then(function(data) {
const output = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
port |
number |
Yes |
The port from which the output is retrieved (1-4).
Default: |
||||||||||||||||
|
callback |
function() |
|
The callback function. Values in
|
getTags(callback)
Get the instance's tags and their fingerprint.
This method wraps {module:compute/vm#getMetadata}, returning only the tags
property.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.getTags(function(err, tags, fingerprint, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.getTags().then(function(data) {
const tags = data[0];
const fingerprint = data[1];
const apiResponse = data[2];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
|
The callback function. Values in
|
reset([callback])
Reset the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.reset(function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.reset().then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
Yes |
The callback function. Values in
|
- See also
resize(machineType[, options], callback)
Set the machine type for this instance, stopping and restarting the VM as necessary.
For a list of the standard, high-memory, and high-CPU machines you may choose from, see Predefined machine types.
In order to change the machine type, the VM must not be running. This method will automatically stop the VM if it is running before changing the machine type. After it is sucessfully changed, the VM will be started.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.resize('n1-standard-1', function(err, apiResponse) {
if (!err) {
// The VM is running and its machine type was changed successfully.
}
});
//-
// By default, calling `resize` will start your server after updating its
// machine type. If you want to leave it stopped, set `options.start` to
// `false`.
//-
const options = {
start: false
};
vm.resize('ns-standard-1', options, function(err, apiResponse) {
if (!err) {
// The VM is stopped and its machine type was changed successfully.
}
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.resize('ns-standard-1', options).then(function(data) {
const apiResponse = data[0];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
machineType |
string |
|
Full or partial machine type. See a list of predefined machine types here. |
||||||||||||
|
options |
object |
Yes |
Configuration object. Values in
|
||||||||||||
|
callback |
function() |
|
The callback function. Values in
|
setMetadata(metadata[, callback])
Set the custom metadata for this instance.
This will combine the metadata key/value pairs with any pre-existing
metadata. Any changes will override pre-existing keys. To remove a
pre-existing key, explicitly set the key's value to null.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
const metadata = {
'startup-script': '...',
customKey: null // Setting `null` will remove the `customKey` property.
};
vm.setMetadata(metadata, function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.setMetadata(metadata).then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
metadata |
object |
|
New metadata. |
||||||||||||||||
|
callback |
function() |
Yes |
The callback function. Values in
|
setTags(tags, fingerprint[, callback])
Set the instance's tags.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.getTags(function(err, tags, fingerprint) {
tags.push('new-tag');
vm.setTags(tags, fingerprint, function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the
// status of the request.
});
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.getTags().then(function(data) {
const tags = data[0];
const fingerprint = data[1];
tags.push('new-tag');
return vm.setTags(tags, fingerprint);
}).then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
tags |
Array of string |
|
The new tags for the instance. |
||||||||||||||||
|
fingerprint |
string |
|
The current tags fingerprint. An up-to-date fingerprint must be provided. |
||||||||||||||||
|
callback |
function() |
Yes |
The callback function. Values in
|
- See also
start([callback])
Start the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.start(function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.start().then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
Yes |
The callback function. Values in
|
- See also
stop([callback])
Stop the instance.
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.stop(function(err, operation, apiResponse) {
// `operation` is an Operation object that can be used to check the status
// of the request.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.stop().then(function(data) {
const operation = data[0];
const apiResponse = data[1];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
callback |
function() |
Yes |
The callback function. Values in
|
- See also
waitFor(status[, options], callback)
This function will callback when the VM is in the specified state.
Will time out after the specified time (default: 300 seconds).
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');
vm.waitFor('RUNNING', function(err, metadata) {
if (!err) {
// The VM is running.
}
});
//-
// By default, `waitFor` will timeout after 300 seconds while waiting for the
// desired state to occur. This can be changed to any number between 0 and
// 600. If the timeout is set to 0, it will poll once for status and then
// timeout if the desired state is not reached.
//-
const options = {
timeout: 600
};
vm.waitFor('TERMINATED', options, function(err, metadata) {
if (!err) {
// The VM is terminated.
}
});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.waitFor('RUNNING', options).then(function(data) {
const metadata = data[0];
});
Parameters
| Name | Type | Optional | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
status |
string |
|
The status to wait for. This can be: - "PROVISIONING" - "STAGING" - "RUNNING" - "STOPPING" - "SUSPENDING" - "SUSPENDED" - "TERMINATED" |
||||||||||||
|
options |
object |
Yes |
Configuration object. Values in
|
||||||||||||
|
callback |
function() |
|
The callback function. Values in
|