Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.js #617

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,30 @@ 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';
const METADATA_PROJECT_ID_URL = 'http://metadata.google.internal/computeMetadata/v1/project/project-id';

function getExternalIp () {
function getProjectId () {
const options = {
headers: {
'Metadata-Flavor': 'Google'
},
json: true
}
};

return request(METADATA_NETWORK_INTERFACE_URL, options)
return request(METADATA_PROJECT_ID_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';
console.log('Error while talking to metadata server.');
return 'Unknown_Project_ID';
}
return Promise.reject(err);
});
}

app.get('/', (req, res, next) => {
getExternalIp()
.then((externalIp) => {
res.status(200).send(`External IP: ${externalIp}`).end();
getProjectId()
.then((projectId) => {
res.status(200).send(`Project ID: ${projectId}`).end();
})
.catch(next);
});
Expand Down
12 changes: 5 additions & 7 deletions appengine/metadata/standard/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "appengine-metadata",
"name": "appengine-std-metadata",
"description": "Sample for accessing the Compute metadata server on GAE Standard Environment.",
"version": "0.0.1",
"private": true,
Expand All @@ -13,9 +13,10 @@
"node": "8.x.x"
},
"scripts": {
"lint": "samples lint",
"start": "node app.js",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"system-test": "samples test app",
"system-test": "repo-tools test app",
"test": "npm run system-test"
},
"dependencies": {
Expand All @@ -28,10 +29,7 @@
"cloud-repo-tools": {
"test": {
"app": {
"msg": "External IP:",
"args": [
"server.js"
]
"msg": "Project ID:"
}
},
"requiresKeyFile": false,
Expand Down
3 changes: 2 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ 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/metadata/flexible"
- node scripts/build "appengine/metadata/standard"
- node scripts/build "appengine/static-files"
- GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage"
- node scripts/build "auth"
Expand Down