|
| 1 | +
|
| 2 | +/* |
| 3 | + * Copyright 2012 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Follow the instructions on https://code.google/com/p/google-api-php-client |
| 20 | + * to download, extract and include the Google APIs client library for PHP into |
| 21 | + * your project. |
| 22 | + */ |
| 23 | +require_once 'google-api-php-client/src/Google_Client.php'; |
| 24 | +require_once 'google-api-php-client/src/contrib/Google_ComputeService.php'; |
| 25 | + |
| 26 | +session_start(); |
| 27 | + |
| 28 | +/** |
| 29 | + * Visit https://code.google.com/apis/console to generate your |
| 30 | + * oauth2_client_id, oauth2_client_secret, and to register your |
| 31 | + * oauth2_redirect_uri. |
| 32 | + */ |
| 33 | +$client = new Google_Client(); |
| 34 | +$client->setApplicationName("Google Compute Engine PHP Starter Application"); |
| 35 | +$client->setClientId('YOUR_CLIENT_ID'); |
| 36 | +$client->setClientSecret('YOUR_CLIENT_SECRET'); |
| 37 | +$client->setRedirectUri('YOUR_REDIRECT_URI'); |
| 38 | +$client->setDeveloperKey('YOUR_DEVELOPER_KEY'); |
| 39 | +$computeService = new Google_ComputeService($client); |
| 40 | + |
| 41 | +/** |
| 42 | + * The name of your Google Compute Engine Project. |
| 43 | + */ |
| 44 | +$project = 'YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT'; |
| 45 | + |
| 46 | +/** |
| 47 | + * Constants for sample request parameters. |
| 48 | + */ |
| 49 | +define('API_VERSION', 'v1beta13'); |
| 50 | +define('BASE_URL', 'https://www.googleapis.com/compute/'. |
| 51 | + API_VERSION . '/projects/'); |
| 52 | +define('GOOGLE_PROJECT', 'google'); |
| 53 | +define('DEFAULT_PROJECT', $project); |
| 54 | +define('DEFAULT_NAME', 'new-node'); |
| 55 | +define('DEFAULT_NAME_WITH_METADATA', 'new-node-with-metadata'); |
| 56 | +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'); |
| 59 | +define('DEFAULT_IMAGE', BASE_URL . GOOGLE_PROJECT . |
| 60 | + '/images/ubuntu-12-04-v20120912'); |
| 61 | +define('DEFAULT_NETWORK', BASE_URL . DEFAULT_PROJECT . |
| 62 | + '/networks/default'); |
| 63 | + |
| 64 | +/** |
| 65 | + * Generates the markup for a specific Google Compute Engine API request. |
| 66 | + * @param string $apiRequestName The name of the API request to process. |
| 67 | + * @param string $apiResponse The API response to process. |
| 68 | + * @return string Markup for the specific Google Compute Engine API request. |
| 69 | + */ |
| 70 | +function generateMarkup($apiRequestName, $apiResponse) { |
| 71 | + $apiRequestMarkup = ''; |
| 72 | + $apiRequestMarkup .= "" . $apiRequestName . ""; |
| 73 | + |
| 74 | + if ($apiResponse['items'] == '' ) { |
| 75 | + |
| 76 | + $apiRequestMarkup .= print_r(json_decode(json_encode($apiResponse), true), true); |
| 77 | + $apiRequestMarkup .= ""; |
| 78 | + } else { |
| 79 | + foreach($apiResponse['items'] as $response) { |
| 80 | + |
| 81 | + $apiRequestMarkup .= print_r(json_decode(json_encode($response), true), true); |
| 82 | + $apiRequestMarkup .= ""; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $apiRequestMarkup; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Clear access token whenever a logout is requested. |
| 91 | + */ |
| 92 | +if (isset($_REQUEST['logout'])) { |
| 93 | + unset($_SESSION['access_token']); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Authenticate and set client access token. |
| 98 | + */ |
| 99 | +if (isset($_GET['code'])) { |
| 100 | + $client->authenticate($_GET['code']); |
| 101 | + $_SESSION['access_token'] = $client->getAccessToken(); |
| 102 | + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 103 | + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Set client access token. |
| 108 | + */ |
| 109 | +if (isset($_SESSION['access_token'])) { |
| 110 | + $client->setAccessToken($_SESSION['access_token']); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * If all authentication has been successfully completed, make Google Compute |
| 115 | + * Engine API requests. |
| 116 | + */ |
| 117 | +if ($client->getAccessToken()) { |
| 118 | + /** |
| 119 | + * Google Compute Engine API request to retrieve the list of instances in your |
| 120 | + * Google Compute Engine project. |
| 121 | + */ |
| 122 | + $instances = $computeService->instances->listInstances(DEFAULT_PROJECT); |
| 123 | + $instancesListMarkup = generateMarkup('List Instances', $instances); |
| 124 | + |
| 125 | + /** |
| 126 | + * Google Compute Engine API request to retrieve the list of all data center |
| 127 | + * locations associated with your Google Compute Engine project. |
| 128 | + */ |
| 129 | + $zones = $computeService->zones->listZones(DEFAULT_PROJECT); |
| 130 | + $zonesListMarkup = generateMarkup('List Zones', $zones); |
| 131 | + |
| 132 | + /** |
| 133 | + * Google Compute Engine API request to retrieve the list of all machine types |
| 134 | + * associated associated with your Google Compute Engine project. |
| 135 | + */ |
| 136 | + $machineTypes = $computeService->machineTypes->listMachineTypes(DEFAULT_PROJECT); |
| 137 | + $machineTypesListMarkup = generateMarkup('List Machine Types', |
| 138 | + $machineTypes); |
| 139 | + |
| 140 | + /** |
| 141 | + * Google Compute Engine API request to retrieve the list of all image types |
| 142 | + * associated associated with your Google Compute Engine project. |
| 143 | + */ |
| 144 | + $images = $computeService->images->listImages(GOOGLE_PROJECT); |
| 145 | + $imagesListMarkup = generateMarkup('List Images', $images); |
| 146 | + |
| 147 | + /** |
| 148 | + * Google Compute Engine API request to retrieve the list of all firewalls |
| 149 | + * associated associated with your Google Compute Engine project. |
| 150 | + */ |
| 151 | + $firewalls = $computeService->firewalls->listFirewalls(DEFAULT_PROJECT); |
| 152 | + $firewallsListMarkup = generateMarkup('List Firewalls', $firewalls); |
| 153 | + |
| 154 | + /** |
| 155 | + * Google Compute Engine API request to retrieve the list of all networks |
| 156 | + * associated associated with your Google Compute Engine project. |
| 157 | + */ |
| 158 | + $networks = $computeService->networks->listNetworks(DEFAULT_PROJECT); |
| 159 | + $networksListMarkup = generateMarkup('List Networks', $networks);; |
| 160 | + |
| 161 | + /** |
| 162 | + * Google Compute Engine API request to insert a new instance into your Google |
| 163 | + * Compute Engine project. |
| 164 | + */ |
| 165 | + $name = DEFAULT_NAME; |
| 166 | + $machineType = DEFAULT_MACHINE_TYPE; |
| 167 | + $zone = DEFAULT_ZONE; |
| 168 | + $image = DEFAULT_IMAGE; |
| 169 | + |
| 170 | + $googleNetworkInterfaceObj = new Google_NetworkInterface(); |
| 171 | + $network = DEFAULT_NETWORK; |
| 172 | + $googleNetworkInterfaceObj->setNetwork($network); |
| 173 | + |
| 174 | + $new_instance = new Google_Instance(); |
| 175 | + $new_instance->setName($name); |
| 176 | + $new_instance->setImage($image); |
| 177 | + $new_instance->setZone($zone); |
| 178 | + $new_instance->setMachineType($machineType); |
| 179 | + $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj)); |
| 180 | + |
| 181 | + $insertInstance = $computeService->instances->insert(DEFAULT_PROJECT, |
| 182 | + $new_instance); |
| 183 | + $insertInstanceMarkup = generateMarkup('Insert Instance', $insertInstance); |
| 184 | + |
| 185 | + /** |
| 186 | + * Google Compute Engine API request to insert a new instance (with metadata) |
| 187 | + * into your Google Compute Engine project. |
| 188 | + */ |
| 189 | + $name = DEFAULT_NAME_WITH_METADATA; |
| 190 | + $machineType = DEFAULT_MACHINE_TYPE; |
| 191 | + $zone = DEFAULT_ZONE; |
| 192 | + $image = DEFAULT_IMAGE; |
| 193 | + |
| 194 | + $googleNetworkInterfaceObj = new Google_NetworkInterface(); |
| 195 | + $network = DEFAULT_NETWORK; |
| 196 | + $googleNetworkInterfaceObj->setNetwork($network); |
| 197 | + |
| 198 | + $metadataItemsObj = new Google_MetadataItems(); |
| 199 | + $metadataItemsObj->setKey('startup-script'); |
| 200 | + $metadataItemsObj->setValue('apt-get install apache2'); |
| 201 | + |
| 202 | + $metadata = new Google_Metadata(); |
| 203 | + $metadata->setItems(array($metadataItemsObj)); |
| 204 | + |
| 205 | + $new_instance = new Google_Instance(); |
| 206 | + $new_instance->setName($name); |
| 207 | + $new_instance->setImage($image); |
| 208 | + $new_instance->setZone($zone); |
| 209 | + $new_instance->setMachineType($machineType); |
| 210 | + $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj)); |
| 211 | + $new_instance->setMetadata($metadata); |
| 212 | + |
| 213 | + $insertInstanceWithMetadata = $computeService->instances->insert( |
| 214 | + DEFAULT_PROJECT, $new_instance); |
| 215 | + $insertInstanceWithMetadataMarkup = generateMarkup( |
| 216 | + 'Insert Instance With Metadata', $insertInstanceWithMetadata); |
| 217 | + |
| 218 | + /** |
| 219 | + * Google Compute Engine API request to get an instance matching the outlined |
| 220 | + * parameters from your Google Compute Engine project. |
| 221 | + */ |
| 222 | + $getInstance = $computeService->instances->get(DEFAULT_PROJECT, DEFAULT_NAME); |
| 223 | + $getInstanceMarkup = generateMarkup('Get Instance', $getInstance); |
| 224 | + |
| 225 | + /** |
| 226 | + * Google Compute Engine API request to get an instance matching the outlined |
| 227 | + * parameters from your Google Compute Engine project. |
| 228 | + */ |
| 229 | + $getInstanceWithMetadata = $computeService->instances->get(DEFAULT_PROJECT, |
| 230 | + DEFAULT_NAME_WITH_METADATA); |
| 231 | + $getInstanceWithMetadataMarkup = generateMarkup('Get Instance With Metadata', |
| 232 | + $getInstanceWithMetadata); |
| 233 | + |
| 234 | + /** |
| 235 | + * Google Compute Engine API request to delete an instance matching the |
| 236 | + * outlined parameters from your Google Compute Engine project. |
| 237 | + */ |
| 238 | + $deleteInstance = $computeService->instances->delete(DEFAULT_PROJECT, DEFAULT_NAME); |
| 239 | + $deleteInstanceMarkup = generateMarkup('Delete Instance', $deleteInstance); |
| 240 | + |
| 241 | + /** |
| 242 | + * Google Compute Engine API request to delete an instance matching the |
| 243 | + * outlined parameters from your Google Compute Engine project. |
| 244 | + */ |
| 245 | + $deleteInstanceWithMetadata = $computeService->instances->delete(DEFAULT_PROJECT, |
| 246 | + DEFAULT_NAME_WITH_METADATA); |
| 247 | + $deleteInstanceWithMetadataMarkup = generateMarkup( |
| 248 | + 'Delete Instance With Metadata', $deleteInstanceWithMetadata); |
| 249 | + |
| 250 | + /** |
| 251 | + * Google Compute Engine API request to retrieve the list of all operations |
| 252 | + * associated with your Google Compute Engine project. |
| 253 | + */ |
| 254 | + $operations = $computeService->operations->listOperations(DEFAULT_PROJECT); |
| 255 | + $operationsListMarkup = generateMarkup('List Operations', $operations); |
| 256 | + |
| 257 | + // The access token may have been updated lazily. |
| 258 | + $_SESSION['access_token'] = $client->getAccessToken(); |
| 259 | +} else { |
| 260 | + $authUrl = $client->createAuthUrl(); |
| 261 | +} |
| 262 | +?> |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | + Google Compute Engine Sample App |
| 270 | + |
| 271 | + if(isset($instancesListMarkup)): ?> |
| 272 | + print $instancesListMarkup ?> |
| 273 | + endif ?> |
| 274 | + |
| 275 | + if(isset($zonesListMarkup)): ?> |
| 276 | + print $zonesListMarkup ?> |
| 277 | + endif ?> |
| 278 | + |
| 279 | + if(isset($machineTypesListMarkup)): ?> |
| 280 | + print $machineTypesListMarkup ?> |
| 281 | + endif ?> |
| 282 | + |
| 283 | + if(isset($imagesListMarkup)): ?> |
| 284 | + print $imagesListMarkup ?> |
| 285 | + endif ?> |
| 286 | + |
| 287 | + if(isset($firewallsListMarkup)): ?> |
| 288 | + print $firewallsListMarkup ?> |
| 289 | + endif ?> |
| 290 | + |
| 291 | + if(isset($networksListMarkup)): ?> |
| 292 | + print $networksListMarkup ?> |
| 293 | + endif ?> |
| 294 | + |
| 295 | + if(isset($getInstanceWithMetadataMarkup)): ?> |
| 296 | + |
| 297 | + print $getInstanceWithMetadataMarkup ?> |
| 298 | + |
| 299 | + endif ?> |
| 300 | + |
| 301 | + if(isset($getInstanceMarkup)): ?> |
| 302 | + print $getInstanceMarkup ?> |
| 303 | + endif ?> |
| 304 | + |
| 305 | + if(isset($deleteInstanceMarkup)): ?> |
| 306 | + print $deleteInstanceMarkup ?> |
| 307 | + endif ?> |
| 308 | + |
| 309 | + if(isset($deleteInstanceWithMetadataMarkup)): ?> |
| 310 | + |
| 311 | + print $deleteInstanceWithMetadataMarkup ?> |
| 312 | + |
| 313 | + endif ?> |
| 314 | + |
| 315 | + if(isset($insertInstanceMarkup)): ?> |
| 316 | + print $insertInstanceMarkup ?> |
| 317 | + endif ?> |
| 318 | + |
| 319 | + if(isset($insertInstanceWithMetadataMarkup)): ?> |
| 320 | + |
| 321 | + print $insertInstanceWithMetadataMarkup?> |
| 322 | + |
| 323 | + endif ?> |
| 324 | + |
| 325 | + if(isset($operationsListMarkup)): ?> |
| 326 | + print $operationsListMarkup ?> |
| 327 | + endif ?> |
| 328 | + |
| 329 | + |
| 330 | + if(isset($authUrl)) { |
| 331 | + |
| 332 | + } else { |
| 333 | + |
| 334 | + } |
| 335 | + ?> |
| 336 | + |
| 337 | + |
| 338 | + |
0 commit comments