Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't there any SDKs to call Cloud SQL API?

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

  • https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/export?hl=en

And I found libraries to call GCP APIs.

  • https://github.com/googleapis/google-cloud-node
  • https://github.com/googleapis/google-cloud-go

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?

like image 909
tsugitta Avatar asked Jun 05 '26 18:06

tsugitta


1 Answers

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.

like image 65
komarkovich Avatar answered Jun 08 '26 00:06

komarkovich