3
votes

I'd like to call Cloud SQL API described below from Cloud Functions.

And I found libraries to call GCP APIs.

However, there does not seem to be any modules for Cloud SQL.

I'm wondering why it's not implemented. is the reason that the APIs are relatively new? or that I misunderstand the purpose of the libraries and actually it shouldn't be implemented in the libraries?

2

2 Answers

4
votes

At the bottom of the page that you linked you will find a sample code in different languages to call that API by building a client. For example a sample code for Node.js would look like this:

const {google} = require('googleapis');
var sqlAdmin = google.sqladmin('v1beta4');

authorize(function(authClient) {
  var request = {
    // Project ID of the project that contains the instance to be exported.
    project: 'my-project',  // TODO: Update placeholder value.

    // Cloud SQL instance ID. This does not include the project ID.
    instance: 'my-instance',  // TODO: Update placeholder value.

    resource: {
      // TODO: Add desired properties to the request body.
    },

    auth: authClient,
  };

  sqlAdmin.instances.export(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);
  });
}

To connect to a Cloud SQL instance from Cloud Function follow the documentation here.

0
votes

You can use the gcloud sdk to call the the apis. If you simply wish to export database please have a look at this documentation: https://cloud.google.com/sdk/gcloud/reference/sql/instances/export