Skip to content

Commit 3bbd25d

Browse files
authored
feat: add Cloud Compute instance samples (GoogleCloudPlatform#1309)
1 parent 5d59ecc commit 3bbd25d

File tree

15 files changed

+414
-2
lines changed

15 files changed

+414
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Google Compute Engine PHP Sample Application
1+
# Google Compute Engine PHP Samples
22

33
## Description
44
This is a simple web-based example of calling the Google Compute Engine API
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Google Compute Engine PHP Sample Application
22

3+
**NOTE: This sample is outdated. It is recommended you use the [ALPHA Compute
4+
Client](../../cloud-client/helloworld) instead**
5+
36
## Description
47
This is a simple web-based example of calling the Google Compute Engine API
58
in PHP.
File renamed without changes.
File renamed without changes.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-compute": "^0.1.0"
4+
}
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2021 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+
<phpunit bootstrap="../../../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="Google Compute Cloud Client Instances Tests">
20+
<directory>test</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
</logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">src</directory>
29+
<exclude>
30+
<directory>./vendor</directory>
31+
</exclude>
32+
</whitelist>
33+
</filter>
34+
</phpunit>

0 commit comments

Comments
 (0)