|
| 1 | +Google Cloud Compute PHP Instances Samples |
| 2 | +========================================== |
| 3 | + |
| 4 | +[![Open in Cloud Shell][shell_img]][shell_link] |
| 5 | + |
| 6 | +[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg |
| 7 | +[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=compute/cloud-client/instances |
| 8 | + |
| 9 | +This directory contains samples for calling [Google Cloud Compute][compute] |
| 10 | +from PHP. Specifically, they show how to manage your Compute Engine [instances][instances]. |
| 11 | + |
| 12 | +[compute]: https://cloud.google.com/compute/docs/apis |
| 13 | +[instances]: https://cloud.google.com/compute/docs/instances/stop-start-instance |
| 14 | + |
| 15 | +## Setup |
| 16 | + |
| 17 | +### Authentication |
| 18 | + |
| 19 | +Authentication is typically done through [Application Default Credentials][adc] |
| 20 | +which means you do not have to change the code to authenticate as long as |
| 21 | +your environment has credentials. You have a few options for setting up |
| 22 | +authentication: |
| 23 | + |
| 24 | +1. When running locally, use the [Google Cloud SDK][google-cloud-sdk] |
| 25 | + |
| 26 | + gcloud auth application-default login |
| 27 | + |
| 28 | +1. When running on App Engine or Compute Engine, credentials are already |
| 29 | + set-up. However, you may need to configure your Compute Engine instance |
| 30 | + with [additional scopes][additional_scopes]. |
| 31 | + |
| 32 | +1. You can create a [Service Account key file][service_account_key_file]. This file can be used to |
| 33 | + authenticate to Google Cloud Platform services from any environment. To use |
| 34 | + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to |
| 35 | + the path to the key file, for example: |
| 36 | + |
| 37 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json |
| 38 | + |
| 39 | +[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow |
| 40 | +[additional_scopes]: https://cloud.google.com/compute/docs/authentication#using |
| 41 | +[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount |
| 42 | + |
| 43 | +## Install Dependencies |
| 44 | + |
| 45 | +1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). |
| 46 | + Run `php composer.phar install` (if composer is installed locally) or `composer install` |
| 47 | + (if composer is installed globally). |
| 48 | + |
| 49 | +1. Create a service account at the |
| 50 | +[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/) |
| 51 | + |
| 52 | +1. Download the json key file of the service account. |
| 53 | + |
| 54 | +1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to that file. |
| 55 | + |
| 56 | +## Samples |
| 57 | + |
| 58 | +To run the Compute Samples, run any of the files in `src/` on the CLI to print |
| 59 | +the usage instructions: |
| 60 | + |
| 61 | +``` |
| 62 | +$ php src/list_instances.php |
| 63 | +
|
| 64 | +Usage: list_instances.php $projectId $zone |
| 65 | +
|
| 66 | + @param string $projectId Your Google Cloud project ID. |
| 67 | + @param string $zone The zone to create the instance in (e.g. "us-central1-a") |
| 68 | +``` |
| 69 | + |
| 70 | +### Create an instance |
| 71 | + |
| 72 | +``` |
| 73 | +$ php src/create_instance.php $YOUR_PROJECT_ID "us-central1-a" "my-new-instance-name" |
| 74 | +Created instance my-new-instance-name |
| 75 | +``` |
| 76 | + |
| 77 | +### List instances |
| 78 | + |
| 79 | +``` |
| 80 | +$ php src/list_instances.php $YOUR_PROJECT_ID "us-central1-a" |
| 81 | +Instances for YOUR_PROJECT_ID (us-central1-a) |
| 82 | + - my-new-instance-name |
| 83 | +``` |
| 84 | + |
| 85 | +### Delete an instance |
| 86 | + |
| 87 | +``` |
| 88 | +$ php src/delete_instance.php $YOUR_PROJECT_ID "us-central1-a" "my-new-instance-name" |
| 89 | +Deleted instance my-new-instance-name |
| 90 | +``` |
| 91 | + |
| 92 | +## Troubleshooting |
| 93 | + |
| 94 | +If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID: |
| 95 | + |
| 96 | +``` |
| 97 | +[Google\Cloud\Core\Exception\GoogleException] |
| 98 | +No project ID was provided, and we were unable to detect a default project ID. |
| 99 | +``` |
| 100 | + |
| 101 | +## The client library |
| 102 | + |
| 103 | +This sample uses the [Google Cloud Compute Client Library for PHP][google-cloud-php]. |
| 104 | +You can read the documentation for more details on API usage and use GitHub |
| 105 | +to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues]. |
| 106 | + |
| 107 | +[google-cloud-php]: https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.152.0/compute/readme |
| 108 | +[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php |
| 109 | +[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues |
| 110 | +[google-cloud-sdk]: https://cloud.google.com/sdk/ |
0 commit comments