Generally, the Cloud SQL Admin API for Java is used for operations such as the one you are looking for. If you are using Maven, you can add the library to your project adding the following lines of code to the pom.xml
configuration file:
<project>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sqladmin</artifactId>
<version>v1beta4-rev48-1.23.0</version>
</dependency>
</dependencies>
</project>
EDIT:
As far as I can see in the documentation, the underlying API uses the Instance.Patch
method for starting and stopping instances, although I cannot find any specific information about how to do it. However, you can find more relevant information yourself in the Instances:Patch page. I will keep looking for more information and in case I find something relevant, I will post a comment to this answer below.
EDIT 2:
I have been performing some tests using the Google APIs Explorer, using the PROJECT_ID
, SQL_INSTANCE_ID
and a JSON body such as this one:
{
"settings": {
"activationPolicy": "YOUR_PREFERED_STATE"
}
}
According to the documentation:
The activation policy specifies when the instance is activated; it is
applicable only when the instance state is RUNNABLE. Valid values:
ALWAYS: The instance is on, and remains so even in the absence of
connection requests. NEVER: The instance is off; it is not activated,
even if a connection request arrives. ON_DEMAND: First Generation
instances only. The instance responds to incoming requests, and turns
itself off when not in use. Instances with PER_USE pricing turn off
after 15 minutes of inactivity. Instances with PER_PACKAGE pricing
turn off after 12 hours of inactivity.
I have tried running the API with the NEVER and ALWAYS states, and my Cloud SQL instance stopped and started accordingly. So in your case, and going back to the Admin API for Java, you should be looking at the Settings of your instance, specifically at this method:
public Settings setActivationPolicy(java.lang.String activationPolicy)
Changing the Activation Policy to NEVER or ALWAYS should be what you need here, although you can have a look at the other possible instance states in case they fit your requirements better.