Skip to content

Commit 4460bed

Browse files
authored
adds flex metadata example (GoogleCloudPlatform#309)
1 parent 6fe4488 commit 4460bed

File tree

9 files changed

+2905
-0
lines changed

9 files changed

+2905
-0
lines changed

appengine/flexible/metadata/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Compute Metadata on App Engine Flexible Environment
2+
3+
This sample application demonstrates how to access
4+
[Compute Metadata](https://cloud.google.com/compute/docs/storing-retrieving-metadata)
5+
from App Engine flexible environment.
6+
7+
## Setup
8+
9+
Before running this sample:
10+
11+
### Register your application
12+
13+
- Go to
14+
[Google Developers Console](https://console.developers.google.com/project)
15+
and create a new project.
16+
17+
### Prerequisites
18+
19+
- Install [`composer`](https://getcomposer.org)
20+
- Install dependencies by running:
21+
22+
```sh
23+
composer install
24+
```
25+
26+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
27+
- Initialize the SDK by running `gcloud init`
28+
29+
## Run Locally
30+
31+
This sample is designed to run in App Engine flexible environment.
32+
This application will fail to reach the Metadata server if run locally.
33+
34+
## Deploy to App Engine
35+
36+
**Deploy with gcloud**
37+
38+
```
39+
gcloud config set project YOUR_PROJECT_ID
40+
gcloud app deploy
41+
gcloud app browse
42+
```
43+
44+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
45+
in your browser.

appengine/flexible/metadata/app.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* Copyright 2017 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+
# [START app]
18+
use Silex\Application;
19+
20+
# [START metadata]
21+
function get_external_ip_using_google_cloud()
22+
{
23+
$metadata = new Google\Cloud\Compute\Metadata();
24+
$externalIp = $metadata->get(
25+
'instance/network-interfaces/0/access-configs/0/external-ip');
26+
27+
return $externalIp;
28+
}
29+
30+
function get_external_ip_using_curl()
31+
{
32+
$url = 'http://metadata.google.internal/computeMetadata/v1/' .
33+
'instance/network-interfaces/0/access-configs/0/external-ip';
34+
35+
$ch = curl_init();
36+
curl_setopt($ch, CURLOPT_URL, $url);
37+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Metadata-Flavor: Google'));
38+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
39+
return curl_exec($ch);
40+
}
41+
# [END metadata]
42+
43+
// create the Silex application
44+
$app = new Application();
45+
46+
$app->get('/', function () use ($app) {
47+
if (!$externalIp = get_external_ip_using_google_cloud()) {
48+
return 'Unable to reach Metadata server - are you running locally?';
49+
}
50+
return sprintf('External IP: %s', $externalIp);
51+
});
52+
53+
$app->get('/curl', function () use ($app) {
54+
if (!$externalIp = get_external_ip_using_curl()) {
55+
return 'Unable to reach Metadata server - are you running locally?';
56+
}
57+
return sprintf('External IP: %s', $externalIp);
58+
});
59+
# [END app]
60+
61+
return $app;

appengine/flexible/metadata/app.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runtime: php
2+
env: flex
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"google/cloud": "^0.20"
5+
},
6+
"require-dev": {
7+
"symfony/browser-kit": "^3.0",
8+
"google/cloud-tools":"^0.6"
9+
}
10+
}

0 commit comments

Comments
 (0)