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

compute/helloworld/README.md renamed to compute/README.md

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

compute/helloworld/api-client/README.md renamed to compute/api-client/helloworld/README.md

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.
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+
17+
<phpunit bootstrap="../../../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="Google Compute Cloud Client Instances Tests">
20+
<directory>testdirectory>
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">srcdirectory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
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+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
use Google\Cloud\Compute\V1\InstancesClient;
27+
use Google\Cloud\Compute\V1\AttachedDisk;
28+
use Google\Cloud\Compute\V1\AttachedDiskInitializeParams;
29+
use Google\Cloud\Compute\V1\Instance;
30+
use Google\Cloud\Compute\V1\NetworkInterface;
31+
32+
/**
33+
* Creates an instance.
34+
* Example:
35+
* ```
36+
* create_instance($projectId, $zone, $instanceName);
37+
* ```
38+
*
39+
* @param string $projectId Your Google Cloud project ID.
40+
* @param string $zone The zone to create the instance in (e.g. "us-central1-a")
41+
* @param string $instanceName The unique name for this Compute instance.
42+
* @param string $machineType Instance machine type
43+
* @param string $sourceImage Boot disk image name or family
44+
* @param string $networkName The Compute instance ID.
45+
*/
46+
function create_instance(
47+
string $projectId,
48+
string $zone,
49+
string $instanceName,
50+
string $machineType = 'n1-standard-1',
51+
string $sourceImage = 'projects/debian-cloud/global/images/family/debian-10',
52+
string $networkName = 'global/networks/default'
53+
) {
54+
// Set the machine type using the specified zone
55+
$machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType);
56+
57+
// Set the boot disk
58+
$diskInitializeParams = (new AttachedDiskInitializeParams())
59+
->setSourceImage($sourceImage);
60+
$disk = (new AttachedDisk())
61+
->setBoot(true)
62+
->setInitializeParams($diskInitializeParams);
63+
64+
// Set the network
65+
$network = (new NetworkInterface())
66+
->setName($networkName);
67+
68+
// Create the Instance message
69+
$instance = (new Instance())
70+
->setName($instanceName)
71+
->setDisks([$disk])
72+
->setMachineType($machineTypeFullName)
73+
->setNetworkInterfaces([$network]);
74+
75+
// Insert the new Compute Engine instance using the InstancesClient
76+
$instancesClient = new InstancesClient();
77+
$operation = $instancesClient->insert($instance, $projectId, $zone);
78+
79+
/** TODO: wait until operation completes */
80+
81+
printf('Created instance %s' . PHP_EOL, $instanceName);
82+
}
83+
84+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
85+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
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+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
use Google\Cloud\Compute\V1\InstancesClient;
27+
28+
/**
29+
* Creates an instance.
30+
* Example:
31+
* ```
32+
* delete_instance($projectId, $zone, $instanceName);
33+
* ```
34+
*
35+
* @param string $projectId Your Google Cloud project ID.
36+
* @param string $zone The zone to delete the instance in (e.g. "us-central1-a")
37+
* @param string $instanceName The unique name for the Compute instance to delete.
38+
*/
39+
function delete_instance(
40+
string $projectId,
41+
string $zone,
42+
string $instanceName
43+
) {
44+
// Delete the Compute Engine instance using the InstancesClient
45+
$instancesClient = new InstancesClient();
46+
$operation = $instancesClient->delete($instanceName, $projectId, $zone);
47+
48+
/** TODO: wait until operation completes */
49+
50+
printf('Deleted instance %s' . PHP_EOL, $instanceName);
51+
}
52+
53+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
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+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Compute;
25+
26+
use Google\Cloud\Compute\V1\InstancesClient;
27+
28+
/**
29+
* Creates an instance.
30+
* Example:
31+
* ```
32+
* list_instances($projectId, $zone);
33+
* ```
34+
*
35+
* @param string $projectId Your Google Cloud project ID.
36+
* @param string $zone The zone to list the instance in (e.g. "us-central1-a")
37+
*/
38+
function list_instances(string $projectId, string $zone)
39+
{
40+
// List the new Compute Engine instance using the InstancesClient
41+
$instancesClient = new InstancesClient();
42+
$instancesList = $instancesClient->list_($projectId, $zone);
43+
44+
printf('Instances for %s (%s)' . PHP_EOL, $projectId, $zone);
45+
foreach ($instancesList as $instance) {
46+
printf(' - %s' . PHP_EOL, $instance->getName());
47+
}
48+
}
49+
50+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)