Skip to content

Commit 2552056

Browse files
author
Jonathan Simon
committed
- Updated API version references to be v1beta14
- Updated URLs pointing to global resources to include '/global' in the URL path - Added DEFAULT_ZONE_NAME and updated DEFAULT_ZONE to include it - Updated methods that now require a Zone parameter to use DEFAULT_ZONE_NAME - Updated DEFAULT_IMAGE to new image name since the old image name referenced an image resource that has been deprecated - Updated listOperations to be listGlobalOperations - Removed code referencing API Developer Key - Updated README.md file to include URL to Google API PHP Client - Updated README.md file to remove references to developer key - Updated README.md file to add instructions for Project ID
1 parent b25bd84 commit 2552056

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ the sample application.
1212
- PHP Curl extension [http://www.php.net/manual/en/intro.curl.php]
1313
- PHP JSON extension [http://php.net/manual/en/book.json.php]
1414
- The google-api-php-client library checked out locally
15+
[https://code.google.com/p/google-api-php-client/]
1516

1617
## Setup Authentication
1718
NOTE: This README assumes that you have enabled access to the Google Compute
@@ -26,18 +27,18 @@ generated any client IDs, or "Create another client ID..." if you have
2627
- Select "Web Application" as the "Application type"
2728
- Click "Create client ID"
2829
- Click "Edit settings..." for your new client ID
29-
- Under the redirect URI, enter the location of your JavaScript application
30+
- Under the redirect URI, enter the location of your application
3031
- Click "Update"
32+
- Click on "Overview" in the left column and note the Project ID
3133

32-
2) Update app.php with the redirect uri, consumer key, secret, and developer
33-
key obtained in step 1.
34+
2) Update app.php with the redirect uri, consumer key, secret, and Project ID
35+
obtained in step 1.
3436
- Update 'YOUR_CLIENT_ID' with your oauth2 client id.
3537
- Update 'YOUR_CLIENT_SECRET' with your oauth2 client secret.
3638
- Update 'YOUR_REDIRECT_URI' with the fully qualified
3739
redirect URI.
38-
- Update 'YOUR_DEVELOPER_KEY' with your developer key.
39-
This is listed under "Simple API Access" at the very bottom of the page,
40-
in the Google API Console.
40+
- Update 'YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT' with your Project ID from the
41+
API Console.
4142

4243
## Running the Sample Application
4344
3) Load app.php on your web server, and visit the appropriate website in

app.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
$client->setClientId('YOUR_CLIENT_ID');
3636
$client->setClientSecret('YOUR_CLIENT_SECRET');
3737
$client->setRedirectUri('YOUR_REDIRECT_URI');
38-
$client->setDeveloperKey('YOUR_DEVELOPER_KEY');
3938
$computeService = new Google_ComputeService($client);
4039

4140
/**
@@ -46,20 +45,21 @@
4645
/**
4746
* Constants for sample request parameters.
4847
*/
49-
define('API_VERSION', 'v1beta13');
48+
define('API_VERSION', 'v1beta14');
5049
define('BASE_URL', 'https://www.googleapis.com/compute/'.
5150
API_VERSION . '/projects/');
5251
define('GOOGLE_PROJECT', 'google');
5352
define('DEFAULT_PROJECT', $project);
5453
define('DEFAULT_NAME', 'new-node');
5554
define('DEFAULT_NAME_WITH_METADATA', 'new-node-with-metadata');
5655
define('DEFAULT_MACHINE_TYPE', BASE_URL . DEFAULT_PROJECT .
57-
'/machineTypes/n1-standard-1');
58-
define('DEFAULT_ZONE', BASE_URL . DEFAULT_PROJECT . '/zones/us-central1-a');
56+
'/global/machineTypes/n1-standard-1');
57+
define('DEFAULT_ZONE_NAME', 'us-central1-a');
58+
define('DEFAULT_ZONE', BASE_URL . DEFAULT_PROJECT . '/zones/' . DEFAULT_ZONE_NAME);
5959
define('DEFAULT_IMAGE', BASE_URL . GOOGLE_PROJECT .
60-
'/images/ubuntu-12-04-v20120912');
60+
'/global/images/gcel-12-04-v20130104');
6161
define('DEFAULT_NETWORK', BASE_URL . DEFAULT_PROJECT .
62-
'/networks/default');
62+
'/global/networks/default');
6363

6464
/**
6565
* Generates the markup for a specific Google Compute Engine API request.
@@ -119,7 +119,8 @@ function generateMarkup($apiRequestName, $apiResponse) {
119119
* Google Compute Engine API request to retrieve the list of instances in your
120120
* Google Compute Engine project.
121121
*/
122-
$instances = $computeService->instances->listInstances(DEFAULT_PROJECT);
122+
$instances = $computeService->instances->listInstances(DEFAULT_PROJECT,
123+
DEFAULT_ZONE_NAME);
123124
$instancesListMarkup = generateMarkup('List Instances', $instances);
124125

125126
/**
@@ -164,7 +165,7 @@ function generateMarkup($apiRequestName, $apiResponse) {
164165
*/
165166
$name = DEFAULT_NAME;
166167
$machineType = DEFAULT_MACHINE_TYPE;
167-
$zone = DEFAULT_ZONE;
168+
$zone = DEFAULT_ZONE_NAME;
168169
$image = DEFAULT_IMAGE;
169170

170171
$googleNetworkInterfaceObj = new Google_NetworkInterface();
@@ -174,12 +175,11 @@ function generateMarkup($apiRequestName, $apiResponse) {
174175
$new_instance = new Google_Instance();
175176
$new_instance->setName($name);
176177
$new_instance->setImage($image);
177-
$new_instance->setZone($zone);
178178
$new_instance->setMachineType($machineType);
179179
$new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
180180

181181
$insertInstance = $computeService->instances->insert(DEFAULT_PROJECT,
182-
$new_instance);
182+
$zone, $new_instance);
183183
$insertInstanceMarkup = generateMarkup('Insert Instance', $insertInstance);
184184

185185
/**
@@ -188,7 +188,7 @@ function generateMarkup($apiRequestName, $apiResponse) {
188188
*/
189189
$name = DEFAULT_NAME_WITH_METADATA;
190190
$machineType = DEFAULT_MACHINE_TYPE;
191-
$zone = DEFAULT_ZONE;
191+
$zone = DEFAULT_ZONE_NAME;
192192
$image = DEFAULT_IMAGE;
193193

194194
$googleNetworkInterfaceObj = new Google_NetworkInterface();
@@ -205,54 +205,55 @@ function generateMarkup($apiRequestName, $apiResponse) {
205205
$new_instance = new Google_Instance();
206206
$new_instance->setName($name);
207207
$new_instance->setImage($image);
208-
$new_instance->setZone($zone);
209208
$new_instance->setMachineType($machineType);
210209
$new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
211210
$new_instance->setMetadata($metadata);
212211

213212
$insertInstanceWithMetadata = $computeService->instances->insert(
214-
DEFAULT_PROJECT, $new_instance);
213+
DEFAULT_PROJECT, $zone, $new_instance);
215214
$insertInstanceWithMetadataMarkup = generateMarkup(
216215
'Insert Instance With Metadata', $insertInstanceWithMetadata);
217216

218217
/**
219218
* Google Compute Engine API request to get an instance matching the outlined
220219
* parameters from your Google Compute Engine project.
221220
*/
222-
$getInstance = $computeService->instances->get(DEFAULT_PROJECT, DEFAULT_NAME);
221+
$getInstance = $computeService->instances->get(DEFAULT_PROJECT,
222+
DEFAULT_ZONE_NAME, DEFAULT_NAME);
223223
$getInstanceMarkup = generateMarkup('Get Instance', $getInstance);
224224

225225
/**
226226
* Google Compute Engine API request to get an instance matching the outlined
227227
* parameters from your Google Compute Engine project.
228228
*/
229229
$getInstanceWithMetadata = $computeService->instances->get(DEFAULT_PROJECT,
230-
DEFAULT_NAME_WITH_METADATA);
230+
DEFAULT_ZONE_NAME, DEFAULT_NAME_WITH_METADATA);
231231
$getInstanceWithMetadataMarkup = generateMarkup('Get Instance With Metadata',
232232
$getInstanceWithMetadata);
233233

234234
/**
235235
* Google Compute Engine API request to delete an instance matching the
236236
* outlined parameters from your Google Compute Engine project.
237237
*/
238-
$deleteInstance = $computeService->instances->delete(DEFAULT_PROJECT, DEFAULT_NAME);
238+
$deleteInstance = $computeService->instances->delete(DEFAULT_PROJECT,
239+
DEFAULT_ZONE_NAME, DEFAULT_NAME);
239240
$deleteInstanceMarkup = generateMarkup('Delete Instance', $deleteInstance);
240241

241242
/**
242243
* Google Compute Engine API request to delete an instance matching the
243244
* outlined parameters from your Google Compute Engine project.
244245
*/
245246
$deleteInstanceWithMetadata = $computeService->instances->delete(DEFAULT_PROJECT,
246-
DEFAULT_NAME_WITH_METADATA);
247+
DEFAULT_ZONE_NAME, DEFAULT_NAME_WITH_METADATA);
247248
$deleteInstanceWithMetadataMarkup = generateMarkup(
248249
'Delete Instance With Metadata', $deleteInstanceWithMetadata);
249250

250251
/**
251-
* Google Compute Engine API request to retrieve the list of all operations
252-
* associated with your Google Compute Engine project.
252+
* Google Compute Engine API request to retrieve the list of all global
253+
* operations associated with your Google Compute Engine project.
253254
*/
254-
$operations = $computeService->operations->listOperations(DEFAULT_PROJECT);
255-
$operationsListMarkup = generateMarkup('List Operations', $operations);
255+
$globalOperations = $computeService->globalOperations->listGlobalOperations(DEFAULT_PROJECT);
256+
$operationsListMarkup = generateMarkup('List Global Operations', $globalOperations);
256257

257258
// The access token may have been updated lazily.
258259
$_SESSION['access_token'] = $client->getAccessToken();
@@ -323,7 +324,7 @@ function generateMarkup($apiRequestName, $apiResponse) {
323324
endif ?>
324325

325326
if(isset($operationsListMarkup)): ?>
326-
listOperations"> print $operationsListMarkup ?>
327+
listGlobalOperations"> print $operationsListMarkup ?>
327328
endif ?>
328329

329330

0 commit comments

Comments
 (0)