moznion
moznion

Reputation: 57

How can I update a network-interface of GCE instance by nodejs SDK?

I want to know the way to do the equivalent to gcloud compute instances network-interfaces update command with node.js. https://cloud.google.com/sdk/gcloud/reference/compute/instances/network-interfaces/update

I explored the document of @google-cloud/compute and the repository, but I couldn't find out any method.

Is this possible?

Upvotes: 0

Views: 595

Answers (1)

dhauptman
dhauptman

Reputation: 1024

You can use the Compute Engine API instances.updateNetworkInterface method. In the same documentation you can find the following code sample to use it with node.js:

// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Compute Engine API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/compute
// 2. This sample uses Application Default Credentials for authentication.
//    If not already done, install the gcloud CLI from
//    https://cloud.google.com/sdk and run
//    `gcloud beta auth application-default login`.
//    For more information, see
//    https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install the Node.js client library by running
//    `npm install googleapis --save`

const {google} = require('googleapis');
var compute = google.compute('v1');

authorize(function(authClient) {
  var request = {
    // Project ID for this request.
    project: 'my-project',  // TODO: Update placeholder value.

    // The name of the zone for this request.
    zone: 'my-zone',  // TODO: Update placeholder value.

    // The instance name for this request.
    instance: 'my-instance',  // TODO: Update placeholder value.

    // The name of the network interface to update.
    networkInterface: '',  // TODO: Update placeholder value.

    resource: {
      // TODO: Add desired properties to the request body. Only these properties
      // will be changed.
    },

    auth: authClient,
  };

  compute.instances.updateNetworkInterface(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
  google.auth.getApplicationDefault(function(err, authClient) {
    if (err) {
      console.error('authentication failed: ', err);
      return;
    }
    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
      authClient = authClient.createScoped(scopes);
    }
    callback(authClient);
  });
}

Upvotes: 1

Related Questions