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 appengine/metadata/standard sample #624

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions appengine/metadata/standard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "samples test app",
"system-test": "samples test app -- ./server.js",
"test": "npm run system-test"
},
"dependencies": {
Expand All @@ -28,7 +28,7 @@
"cloud-repo-tools": {
"test": {
"app": {
"msg": "External IP:",
"msg": "Project ID:",
"args": [
"server.js"
]
Expand Down
19 changes: 9 additions & 10 deletions appengine/metadata/standard/server.js
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
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