tidpe
tidpe

Reputation: 345

GCE - Switch ON and OFF a VM from API request

I'm figuring out if there is a way to switch ON and OFF an instance on Google Compute Engine directly by API (hosted on app engine with PHP).

I've found this documentation: https://cloud.google.com/compute/docs/reference/rest/v1/instances/start

But can't understand if this documentation is what really i need or not :/

EDIT:

$client = new Google_Client();
$client->setApplicationName('Google-ComputeSample/0.1');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');

$service = new Google_Service_Compute($client);



// Project ID for this request.
$project = 'my-project'; 

// The name of the zone for this request.
$zone = 'europe-west3-c';  

// Name of the instance resource to start.
$instance = 'name-instance';  

$response = $service->instances->start($project, $zone, $instance);


print_r($response)

Get this error: Uncaught exception 'DomainException' with message 'Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information'

I'm trying to run this code from localhost, i think this is the problem.

If i run this code from an app engine inside my project, i suppose it will work. But the google-php-client folder for the APIs has > 10000 files inside, so I can't push it on an app engine versione and try it :/

Upvotes: 0

Views: 173

Answers (1)

Annie
Annie

Reputation: 56

The document that you cite is correct for what you want to achieve, so using that API call you will start a VM instance and with the method instances.stop you will stop them. The examples for PHP are in the same document, to start [1] and to stop [2]. I hope this information helps.

Upvotes: 1

Related Questions