Illia (1) [Avatar] Offline
#1
The first code example needs revising.

Looks like gcloud package has been deprecated:
npm WARN deprecated gcloud@0.37.0: gcloud has been renamed to google-cloud. To get new features and bug fixes, you must use the new package.

Also, the first example will not work without setting up the default credentials.

One more thing. I am using gcloud@0.37.0. In this version of the package operation.onComplete is not defined.
Therefore, instead of

operation.onComplete(function(err) {
        console.log('Stopped!');
});


It should be:
operation.on('complete', function(err) {
        console.log('Stopped!');
});




Thanks.
JJ Geewax (19) [Avatar] Offline
#2
Awesome - thanks for the feedback. I'm going to give another pass to Chapter 1 (and a few others) with updates to the install (npm install google-cloud) and code snippets (.on('complete'))
460757 (3) [Avatar] Offline
#3
FWIW, I was going to post similar comments about using npm -g on windows and the step about creating the default credentials.

Also, going back to section 1.6.1 since I am posting this; by default I only had the API section available on my new free project dashboard; I had to go to "Customize" and turn on the Compute Engine section to continue with that project.
rafalgorzkowski (30) [Avatar] Offline
#4
I found the same issue, so I moved to google-cloud library. Here is my code snippet (for Listing 1.9. script.js):

const Compute = require('@google-cloud/compute');

const compute = new Compute({
    projectId: 'my-project-id'
});

function getVmsExample(callback) {
  const options = {
    maxResults: 1
  };
  compute.getVMs(options, (err, vms) => {
    if (err) {
      return callback(err);
    }

    console.log('VMs:', vms);
    callback(null, vms);
  });
}

exports.main = cb => {
  getVmsExample(cb);
};

if (module === require.main) {
  exports.main(console.log);
}

rafalgorzkowski (30) [Avatar] Offline
#5
Turning off the VM using node:

const Compute = require('@google-cloud/compute');

const compute = new Compute({
    projectId: 'my-project-id'
});

function stopInstance(callback) {
    var zone = compute.zone('us-central1-b'); // vm zone
    var vm = zone.vm('learning-cloud-demo'); // your vm name
    vm.stop(function (err, operation, apiResponse) {
        callback('instance has been stopped');
    });
}

exports.main = cb => {
    stopInstance(cb)
};

if (module === require.main) {
    exports.main(console.log);
}
JJ Geewax (19) [Avatar] Offline
#6
Thanks for all the feedback everyone. I've updated the code snippets and clarified the specific version to make sure the code is always correct so long as you use the version mentioned in the text.

Sorry for the trouble and thanks again!
146066 (3) [Avatar] Offline
#7
I'm not sure if this is the right place to ask for help, but I'm trying to run the script.js on node and am getting the following error and thought other MEAP readers might get the same
Error: Cannot find module '@google-cloud/compute'
I can use gcloud on the command line without a problem and have run through all the installation and authentication steps in the book a couple of times.
JJ Geewax (19) [Avatar] Offline
#8
Did you run `npm install @google-cloud/compute@0.7.1` as it says in Listing 1.9? That should fix your problem.
146066 (3) [Avatar] Offline
#9
Thank you for your rapid reply. I had run the npm command a couple of times to no avail, but I tried again and it fixed the problem