Skip to content

Commit 22dc3a3

Browse files
authored
feat(compute): add cloud-client compute sample (GoogleCloudPlatform#1305)
1 parent 91d2d90 commit 22dc3a3

File tree

7 files changed

+251
-39
lines changed

7 files changed

+251
-39
lines changed

compute/helloworld/README.md

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,9 @@
22

33
## Description
44
This is a simple web-based example of calling the Google Compute Engine API
5-
in PHP.
5+
in PHP. These samples include calling the API with both the
6+
[Google API client](api-client) and [Google Cloud Client](cloud-client) (alpha).
67

7-
## Prerequisites
8-
Please make sure that all of the following is installed before trying to run
9-
the sample application.
108

11-
- [PHP 5.2.x or higher](http://www.php.net/)
12-
- [PHP Curl extension](http://www.php.net/manual/en/intro.curl.php)
13-
- [PHP JSON extension](http://php.net/manual/en/book.json.php)
14-
- The [`google-api-php-client`](https://github.com/google/google-api-php-client)
15-
library checked out locally
16-
17-
## Setup Authentication
18-
NOTE: This README assumes that you have enabled access to the Google Compute
19-
Engine API via the Google API Console page.
20-
21-
1) Visit https://code.google.com/apis/console/?api=compute to register your
22-
application.
23-
- Click on "API Access" in the left column
24-
- Click the button labeled "Create an OAuth2 client ID..." if you have not
25-
generated any client IDs, or "Create another client ID..." if you have
26-
- Give your application a name and click "Next"
27-
- Select "Web Application" as the "Application type"
28-
- Click "Create client ID"
29-
- Click "Edit settings..." for your new client ID
30-
- Under the redirect URI, enter the location of your application
31-
- Click "Update"
32-
- Click on "Overview" in the left column and note the Project ID
33-
34-
2) Update app.php with the redirect uri, consumer key, secret, and Project ID
35-
obtained in step 1.
36-
- Update `YOUR_CLIENT_ID` with your oauth2 client id.
37-
- Update `YOUR_CLIENT_SECRET` with your oauth2 client secret.
38-
- Update `YOUR_REDIRECT_URI` with the fully qualified
39-
redirect URI.
40-
- Update `YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT` with your Project ID from the
41-
API Console.
42-
43-
## Running the Sample Application
44-
3) Load app.php on your web server, and visit the appropriate website in
45-
your web browser.
9+
* [Google Cloud Client](cloud-client) (**Recommended**): Under active development, currently in alpha.
10+
* [Google API client](api-client): Stable and generally available, but no longer under active development
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Google Compute Engine PHP Sample Application
2+
3+
## Description
4+
This is a simple web-based example of calling the Google Compute Engine API
5+
in PHP.
6+
7+
## Prerequisites
8+
Please make sure that all of the following is installed before trying to run
9+
the sample application.
10+
11+
- [PHP 5.2.x or higher](http://www.php.net/)
12+
- [PHP Curl extension](http://www.php.net/manual/en/intro.curl.php)
13+
- [PHP JSON extension](http://php.net/manual/en/book.json.php)
14+
- The [`google-api-php-client`](https://github.com/google/google-api-php-client)
15+
library checked out locally
16+
17+
## Setup Authentication
18+
NOTE: This README assumes that you have enabled access to the Google Compute
19+
Engine API via the Google API Console page.
20+
21+
1) Visit https://code.google.com/apis/console/?api=compute to register your
22+
application.
23+
- Click on "API Access" in the left column
24+
- Click the button labeled "Create an OAuth2 client ID..." if you have not
25+
generated any client IDs, or "Create another client ID..." if you have
26+
- Give your application a name and click "Next"
27+
- Select "Web Application" as the "Application type"
28+
- Click "Create client ID"
29+
- Click "Edit settings..." for your new client ID
30+
- Under the redirect URI, enter the location of your application
31+
- Click "Update"
32+
- Click on "Overview" in the left column and note the Project ID
33+
34+
2) Update app.php with the redirect uri, consumer key, secret, and Project ID
35+
obtained in step 1.
36+
- Update `YOUR_CLIENT_ID` with your oauth2 client id.
37+
- Update `YOUR_CLIENT_SECRET` with your oauth2 client secret.
38+
- Update `YOUR_REDIRECT_URI` with the fully qualified
39+
redirect URI.
40+
- Update `YOUR_GOOGLE_COMPUTE_ENGINE_PROJECT` with your Project ID from the
41+
API Console.
42+
43+
## Running the Sample Application
44+
3) Load app.php on your web server, and visit the appropriate website in
45+
your web browser.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"require": {
3+
"google/apiclient": "^2.9"
4+
},
5+
"scripts": {
6+
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
7+
},
8+
"extra": {
9+
"google/apiclient-services": [
10+
"Compute"
11+
]
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Google Compute Engine PHP Sample Application
2+
3+
## Description
4+
5+
This is a simple web-based example of calling the Google Compute Engine API
6+
in PHP.
7+
8+
## Prerequisites
9+
10+
* Run `composer install` in this directory to install the `google/cloud-compute`
11+
library.
12+
* Follow [Getting started with authentication](https://cloud.google.com/docs/authentication/getting-started) to authenticate with Service Account credentials.
13+
14+
## Running the Sample Application
15+
16+
Run app.php on your web server:
17+
18+
```
19+
php -S localhost:8080
20+
```
21+
22+
Visit the website (http://localhost:8080/app.php) in your web browser.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
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+
require_once 'vendor/autoload.php';
19+
20+
use Google\Cloud\Compute\V1\InstancesClient;
21+
use Google\Cloud\Compute\V1\ZonesClient;
22+
use Google\Cloud\Compute\V1\MachineTypesClient;
23+
use Google\Cloud\Compute\V1\ImagesClient;
24+
use Google\Cloud\Compute\V1\FirewallsClient;
25+
use Google\Cloud\Compute\V1\NetworksClient;
26+
use Google\Cloud\Compute\V1\DisksClient;
27+
use Google\Cloud\Compute\V1\GlobalOperationsClient;
28+
use Google\Protobuf\Internal\Message;
29+
30+
/**
31+
* Set these variables to your project and zone.
32+
*/
33+
$projectId = 'php-docs-samples-kokoro';
34+
$zoneName = 'us-central1-f';
35+
36+
// Instantiate clients for calling the Compute API.
37+
$instancesClient = new InstancesClient();
38+
$zonesClient = new ZonesClient();
39+
$disksClient = new DisksClient();
40+
$machineTypesClient = new MachineTypesClient();
41+
$imagesClient = new ImagesClient();
42+
$firewallsClient = new FirewallsClient();
43+
$networksClient = new NetworksClient();
44+
$globalOperationsClient = new GlobalOperationsClient();
45+
46+
/**
47+
* Helper function to pretty-print a Protobuf message.
48+
*/
49+
function print_message(Message $message)
50+
{
51+
return json_encode(
52+
json_decode($message->serializeToJsonString(), true),
53+
JSON_PRETTY_PRINT
54+
);
55+
}
56+
?>
57+
<!doctype html>
58+
<html>
59+
<head><meta charset="utf-8"></head>
60+
<body>
61+
<header><h1>Google Cloud Compute Sample App</h1></header>
62+
<div class="main-content">
63+
<h2 class="collapsible">List Instances</h2>
64+
<div id="listInstances" class="collapsible-content">
65+
<?php foreach ($instancesClient->list_($projectId, $zoneName) as $instance): ?>
66+
<pre><?= print_message($instance) ?></pre>
67+
<?php endforeach ?>
68+
</div>
69+
70+
<h2 class="collapsible">List Zones</h2>
71+
<div id="listZones" class="collapsible-content">
72+
<?php foreach ($zonesClient->list_($projectId) as $zone): ?>
73+
<pre><?= print_message($zone) ?></pre>
74+
<?php endforeach ?>
75+
</div>
76+
77+
<h2 class="collapsible">List Disks</h2>
78+
<div id="listDisks" class="collapsible-content">
79+
<?php foreach ($disksClient->list_($projectId, $zoneName) as $disk): ?>
80+
<pre><?= print_message($disk) ?></pre>
81+
<?php endforeach ?>
82+
</div>
83+
84+
<h2 class="collapsible">List Machine Types</h2>
85+
<div id="listMachineTypes" class="collapsible-content">
86+
<?php foreach ($machineTypesClient->list_($projectId, $zoneName) as $machineType): ?>
87+
<pre><?= print_message($machineType) ?></pre>
88+
<?php endforeach ?>
89+
</div>
90+
91+
<h2 class="collapsible">List Images</h2>
92+
<div id="listImages" class="collapsible-content">
93+
<?php foreach ($imagesClient->list_($projectId) as $image): ?>
94+
<pre><?= print_message($image) ?></pre>
95+
<?php endforeach ?>
96+
</div>
97+
98+
<h2 class="collapsible">List Firewalls</h2>
99+
<div id="listFirewalls" class="collapsible-content">
100+
<?php foreach ($firewalls = $firewallsClient->list_($projectId) as $firewall): ?>
101+
<pre><?= print_message($firewall) ?></pre>
102+
<?php endforeach ?>
103+
</div>
104+
105+
<h2 class="collapsible">List Networks</h2>
106+
<div id="listNetworks" class="collapsible-content">
107+
<?php foreach ($networksClient->list_($projectId) as $network): ?>
108+
<pre><?= print_message($network) ?></pre>
109+
<?php endforeach ?>
110+
</div>
111+
112+
<h2 class="collapsible">List Operations</h2>
113+
<div id="listGlobalOperations" class="collapsible-content">
114+
<?php foreach ($globalOperationsClient->list_($projectId) as $operation): ?>
115+
<pre><?= print_message($operation) ?></pre>
116+
<?php endforeach ?>
117+
</div>
118+
</div>
119+
</body>
120+
</html>
121+
122+
<style>
123+
.collapsible {
124+
background-color: #eee;
125+
color: #444;
126+
cursor: pointer;
127+
padding: 18px;
128+
width: 100%;
129+
border: none;
130+
text-align: left;
131+
outline: none;
132+
font-size: 15px;
133+
}
134+
135+
.active, .collapsible:hover {
136+
background-color: #ccc;
137+
}
138+
139+
.collapsible-content {
140+
padding: 0 18px;
141+
display: none;
142+
overflow: hidden;
143+
background-color: #f1f1f1;
144+
}
145+
</style>
146+
147+
<script>
148+
var coll = document.getElementsByClassName("collapsible");
149+
var i;
150+
151+
for (i = 0; i < coll.length; i++) {
152+
coll[i].addEventListener("click", function() {
153+
this.classList.toggle("active");
154+
var content = this.nextElementSibling;
155+
if (content.style.display === "block") {
156+
content.style.display = "none";
157+
} else {
158+
content.style.display = "block";
159+
}
160+
});
161+
}
162+
</script>
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+
}

0 commit comments

Comments
 (0)