0
votes

Using Google Cloud Sql API, I want to restart my instance programmatically using google apps script. I have enabled the API in console. Through the GUI in the API DOC, I am able to restart it successfully nd I get a response, bt I dnt know how to programmatically restart as there is no sample request body or REQUEST body provided in the API REF Doc.

How to write the code in GAS to restart it programmatically? AM I missing any procedures here?

https://developers.google.com/cloud-sql/docs/admin-api/v1beta3/instances/restart

Request body

Do not supply a request body with this method.

{

"kind": "sql#instancesRestart",

"operation": string

}

Plz help.

Thnks fr ur generous help in advance.

1

1 Answers

1
votes

Here's some code to get you started:

function myFunction() {
  var url = "https://www.googleapis.com/sql/v1beta3/projects/MY_PROJECT_ID/instances/MY_INSTANCE_NAME/restart";

   var headers = {"Accept":"application/json", 
                  "Content-Type":"application/json", 
                  "Authorization":"Bearer MY_TOKEN"
             };

   var options = {"method":"POST",
                  "headers": headers,
                  };
   var response = UrlFetchApp.fetch(url, options);
   Logger.log(response);  
}

You don't always need to send a payload/body with a request, and I do not do so here (it would be in the options variable if I did).

The trickier bit is getting the OAuth credentials. You can use the tutorial on external services to help here.

Hope this helps!