diff --git a/appengine/metadata/package.json b/appengine/metadata/package.json new file mode 100644 index 0000000000..ac9745196b --- /dev/null +++ b/appengine/metadata/package.json @@ -0,0 +1,41 @@ +{ + "name": "appengine-metadata", + "description": "Sample for accessing the Compute metadata server on GAE.", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=4" + }, + "scripts": { + "lint": "samples lint", + "pretest": "npm run lint", + "system-test": "samples test app", + "test": "npm run system-test", + "e2e-test": "samples test deploy" + }, + "dependencies": { + "express": "4.15.4", + "got": "7.1.0" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "1.4.17" + }, + "cloud-repo-tools": { + "test": { + "app": { + "msg": "External IP:", + "args": [ + "server.js" + ] + } + }, + "requiresKeyFile": false, + "requiresProjectId": false + } +} diff --git a/appengine/metadata/server.js b/appengine/metadata/server.js new file mode 100644 index 0000000000..e2192abcb0 --- /dev/null +++ b/appengine/metadata/server.js @@ -0,0 +1,60 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +// [START appengine_metadata] +const express = require('express'); +const request = require('got'); + +const app = express(); +app.enable('trust proxy'); + +const METADATA_NETWORK_INTERFACE_URL = 'http://metadata/computeMetadata/v1/' + +'/instance/network-interfaces/0/access-configs/0/external-ip'; + +function getExternalIp () { + const options = { + headers: { + 'Metadata-Flavor': 'Google' + }, + json: true + }; + + return request(METADATA_NETWORK_INTERFACE_URL, options) + .then((response) => response.body) + .catch((err) => { + if (err || err.statusCode !== 200) { + console.log('Error while talking to metadata server, assuming localhost'); + return 'localhost'; + } + return Promise.reject(err); + }); +} + +app.get('/', (req, res, next) => { + getExternalIp() + .then((externalIp) => { + res.status(200).send(`External IP: ${externalIp}`).end(); + }) + .catch(next); +}); + +const PORT = process.env.PORT || 8080; +app.listen(PORT, () => { + console.log(`App listening on port ${PORT}`); + console.log('Press Ctrl+C to quit.'); +}); +// [END appengine_metadata] diff --git a/circle.yml b/circle.yml index 2f18d06496..e67a8eb9d8 100644 --- a/circle.yml +++ b/circle.yml @@ -82,6 +82,7 @@ deployment: - node scripts/build "appengine/errorreporting" - node scripts/build "appengine/hello-world" - node scripts/build "appengine/mailjet" + - node scripts/build "appengine/metadata" - node scripts/build "appengine/static-files" - GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage" - node scripts/build "auth"