This code is a set of functions for interacting with Google Cloud's Compute Engine API, using the google-auth-library
and googleapis
libraries to authenticate and make requests to the API. The functions, including listUrlMaps
, listTargetProxies
, and listGlobalForwards
, follow a common pattern to retrieve metadata from the API and return promises that resolve to objects with the desired data.
npm run import -- "list google bucket url map"
var path = require('path');
var importer = require('../Core');
var authorizeGoogle = importer.import("authorize google service");
function listUrlMaps(project, urlMap) {
var params = {};
if(urlMap) {
params.filter = `name="${urlMap}"`
}
return authorizeGoogle()
.then(client => client.request({
url: `https://www.googleapis.com/compute/v1/projects/${project}/global/urlMaps`,
params
}))
.then(res => {
return (res.data.items || []).reduce((obj, cur) => {
obj[cur.name] = {
hostRules: cur.hostRules,
pathMatchers: cur.pathMatchers,
fingerprint: cur.fingerprint
// defaultService: 'https://www.googleapis.com/compute/v1/projects/spahaha-ea443/global/backendServices/web-map-backend-service',
};
return obj;
}, {})
});
}
function listTargetProxies(project, urlMap) {
var params = {};
if(urlMap) {
params.filter = `urlMap="https://www.googleapis.com/compute/v1/projects/${project}/global/urlMaps/${urlMap}"`
}
return authorizeGoogle()
.then(client => client.request({
url: `https://www.googleapis.com/compute/v1/projects/${project}/global/targetHttpsProxies`,
params
}))
.then(res => (res.data.items || []).reduce((obj, p) => {
obj[p.name] = p.sslCertificates;
return obj
}, {}));
}
function listGlobalForwards(project, proxy, ip) {
var params = {};
if(proxy) {
params['filter'] = `target="https://www.googleapis.com/compute/v1/projects/${project}/global/targetHttpsProxies/${proxy}"`
}
if(ip) {
params['filter'] = `IPAddress="https://www.googleapis.com/compute/v1/projects/${project}/global/addresses/${ip}"`;
}
return authorizeGoogle()
.then(client => client.request({
url: `https://www.googleapis.com/compute/v1/projects/${project}/global/forwardingRules`,
params
}))
.then(res => {
return (res.data.items || []).reduce((obj, cur) => {
obj[cur.name] = {
IPAddress: cur.IPAddress,
target: cur.target
}
return obj;
}, {})
});
}
function listBackendBuckets(project, bucketName) {
var params = {};
if(bucketName) {
params.filter = `bucketName="${bucketName}"`;
}
return authorizeGoogle()
.then(client => client.request({
url: `https://www.googleapis.com/compute/v1/projects/${project}/global/backendBuckets`,
params
}))
.then(res => {
return (res.data.items || []).reduce((obj, cur) => {
obj[cur.name] = cur.bucketName;
return obj;
}, {})
});
}
function listSslCertificates(project, certName) {
var params = {};
if(certName) {
params.filter = `name="${certName}"`;
}
return authorizeGoogle()
.then(client => client.request({
url: `https://www.googleapis.com/compute/v1/projects/${project}/global/sslCertificates`,
params
}))
.then(res => {
return (res.data.items || []).reduce((obj, cur) => {
obj[cur.name] = cur.description;
return obj;
}, {})
});
}
module.exports = {
listUrlMaps,
listTargetProxies,
listGlobalForwards,
listBackendBuckets,
listSslCertificates
};
```javascript
const { google } = require('googleapis');
const authorizeGoogle = async () => {
const auth = new google.auth.GoogleAuth({
// TODO: Replace with service account credentials
// For more information, see: https://developers.google.com/identity/protocols/oauth2/service-account
});
const client = await auth.getClient();
return client.authorize();
};
const compute = async (auth) => {
return google.compute('v1').authorize(auth);
};
const makeRequest = async (auth, url, params = {}) => {
const compute = await compute(auth);
const res = await compute.projects().global.get(url, { params });
return res.data;
};
const listUrlMaps = async (project, urlMap) => {
const url = `https://www.googleapis.com/compute/v1/projects/${project}/global/urlMaps`;
const params = urlMap? { filter: `name="${urlMap}"` } : {};
const res = await makeRequest(await authorizeGoogle(), url, params);
return (res.items || []).reduce((obj, cur) => {
obj[cur.name] = {
hostRules: cur.hostRules,
pathMatchers: cur.pathMatchers,
fingerprint: cur.fingerprint,
};
return obj;
}, {});
};
const listTargetProxies = async (project, urlMap) => {
const url = `https://www.googleapis.com/compute/v1/projects/${project}/global/targetHttpsProxies`;
const params = urlMap? { filter: `urlMap="https://www.googleapis.com/compute/v1/projects/${project}/global/urlMaps/${urlMap}"` } : {};
const res = await makeRequest(await authorizeGoogle(), url, params);
return (res.items || []).reduce((obj, p) => {
obj[p.name] = p.sslCertificates;
return obj;
}, {});
};
const listGlobalForwards = async (project, proxy, ip) => {
const url = `https://www.googleapis.com/compute/v1/projects/${project}/global/forwardingRules`;
const params = proxy? { filter: `target="https://www.googleapis.com/compute/v1/projects/${project}/global/targetHttpsProxies/${proxy}"` } : {};
if (ip) {
params.filter = `IPAddress="https://www.googleapis.com/compute/v1/projects/${project}/global/addresses/${ip}"`;
}
const res = await makeRequest(await authorizeGoogle(), url, params);
return (res.items || []).reduce((obj, cur) => {
obj[cur.name] = {
IPAddress: cur.IPAddress,
target: cur.target,
};
return obj;
}, {});
};
const listBackendBuckets = async (project, bucketName) => {
const url = `https://www.googleapis.com/compute/v1/projects/${project}/global/backendBuckets`;
const params = bucketName? { filter: `bucketName="${bucketName}"` } : {};
const res = await makeRequest(await authorizeGoogle(), url, params);
return (res.items || []).reduce((obj, cur) => {
obj[cur.name] = cur.bucketName;
return obj;
}, {});
};
const listSslCertificates = async (project, certName) => {
const url = `https://www.googleapis.com/compute/v1/projects/${project}/global/sslCertificates`;
const params = certName? { filter: `name="${certName}"` } : {};
const res = await makeRequest(await authorizeGoogle(), url, params);
return (res.items || []).reduce((obj, cur) => {
obj[cur.name] = cur.description;
return obj;
}, {});
};
module.exports = {
listUrlMaps,
listTargetProxies,
listGlobalForwards,
listBackendBuckets,
listSslCertificates,
};
```
This code is a set of functions for interacting with Google Cloud's Compute Engine API. It uses the google-auth-library
and googleapis
libraries to authenticate and make requests to the API.
listUrlMaps(project, urlMap)
project
: the ID of the projecturlMap
: the name of the URL Map to filter by (optional)listTargetProxies(project, urlMap)
project
: the ID of the projecturlMap
: the name of the URL Map associated with the Target Proxy (optional)listGlobalForwards(project, proxy, ip)
project
: the ID of the projectproxy
: the name of the Target Proxy associated with the Forwarding Rule (optional)ip
: the IP address associated with the Forwarding Rule (optional)All three functions follow a similar pattern:
authorizeGoogle
function to authenticate and get a client object.This code uses the google-auth-library
and googleapis
libraries, which are not included in this snippet. You would need to have these libraries installed and imported in your project for this code to work.