Skip to content
Closed
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
11 changes: 5 additions & 6 deletions logs/lib/retrieveLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ module.exports = {

getLogs() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
let func = this.options.function;
const pageSize = this.options.count || 10;

func = getGoogleCloudFunctionName(this.serverless.service.functions, func);


// Actually the function name on GCP is service-stage-handler
return this.provider.request('logging', 'entries', 'list', {
filter: `Function execution ${func} ${region}`,
filter: `resource.labels.function_name="${func}" AND NOT textPayload=""`,
orderBy: 'timestamp desc',
resourceNames: [`projects/${project}`],
pageSize,
Expand All @@ -40,7 +40,7 @@ module.exports = {
}

let output = logs.entries.reduce(
(p, c) => (p += `${chalk.grey(`${c.timestamp}:`)} ${c.textPayload}\n`),
(p, c) => (p += `${chalk.grey(`${c.timestamp}:`)} | Execution ID: ${c.labels.execution_id} | ${c.textPayload}\n`),
''
); //eslint-disable-line

Expand All @@ -62,6 +62,5 @@ const getGoogleCloudFunctionName = (serviceFunctions, func) => {
].join('');
throw new Error(errorMessage);
}

return serviceFunctions[func].handler;
return serviceFunctions[func].name;
};
12 changes: 7 additions & 5 deletions logs/lib/retrieveLogs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ describe('RetrieveLogs', () => {

it('should return a default amount of logs for the function if the "count" option is not given', () => {
googleLogs.options.function = 'func1';
let func = googleLogs.options.function;
func = googleLogs.getGoogleCloudFunctionName(serverless.service.functions, func);

return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'Function execution foo us-central1',
filter: `resource.labels.function_name="${func}" AND NOT textPayload=""`,
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: 10,
Expand Down Expand Up @@ -117,13 +119,13 @@ describe('RetrieveLogs', () => {
it('should print the received execution result log on the console', () => {
const logs = {
entries: [
{ timestamp: '1970-01-01 00:00', textPayload: 'Entry 1' },
{ timestamp: '1970-01-01 00:01', textPayload: 'Entry 2' },
{ timestamp: '1970-01-01 00:00', textPayload: 'Entry 1', labels: {execution_id: 'foo'}, },
{ timestamp: '1970-01-01 00:01', textPayload: 'Entry 2', labels: {execution_id: 'bar'}, },
],
};

const logEntry1 = `${chalk.grey('1970-01-01 00:00:')} Entry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')} Entry 2`;
const logEntry1 = `${chalk.grey('1970-01-01 00:00:')} | Execution ID: foo | Entry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')} | Execution ID: bar | Entry 2`;
const expectedOutput = `Displaying the 2 most recent log(s):\n\n${logEntry1}\n${logEntry2}`;

return googleLogs.printLogs(logs).then(() => {
Expand Down
4 changes: 2 additions & 2 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = {
funcTemplate.properties.runtime =
_.get(funcObject, 'runtime') ||
_.get(this, 'serverless.service.provider.runtime') ||
'nodejs8';
'nodejs10';
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables = _.merge(
Expand Down Expand Up @@ -165,7 +165,7 @@ const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl)
properties: {
parent: `projects/${projectName}/locations/${region}`,
availableMemoryMb: 256,
runtime: 'nodejs8',
runtime: 'nodejs10',
timeout: '60s',
entryPoint: funcObject.handler,
function: funcObject.name,
Expand Down
44 changes: 22 additions & 22 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 1024,
runtime: 'nodejs8',
runtime: 'nodejs10',
events: [{ http: 'foo' }],
},
};
Expand All @@ -109,7 +109,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 1024,
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 1024,
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -521,7 +521,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand Down Expand Up @@ -576,7 +576,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 256,
Expand All @@ -595,7 +595,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func2',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func2',
entryPoint: 'func2',
availableMemoryMb: 256,
Expand Down Expand Up @@ -623,7 +623,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
},
Expand All @@ -635,7 +635,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand Down Expand Up @@ -663,7 +663,7 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
maxInstances: 10,
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
Expand All @@ -676,7 +676,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand Down Expand Up @@ -705,14 +705,14 @@ describe('CompileFunctions', () => {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'foo' }],
},
func2: {
handler: 'func2',
memorySize: 128,
runtime: 'nodejs8',
runtime: 'nodejs10',
maxInstances: 10,
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [{ http: 'bar' }],
Expand All @@ -725,7 +725,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func1',
entryPoint: 'func1',
availableMemoryMb: 128,
Expand All @@ -743,7 +743,7 @@ describe('CompileFunctions', () => {
name: 'my-service-dev-func2',
properties: {
parent: 'projects/myProject/locations/us-central1',
runtime: 'nodejs8',
runtime: 'nodejs10',
function: 'my-service-dev-func2',
entryPoint: 'func2',
availableMemoryMb: 128,
Expand Down
38 changes: 22 additions & 16 deletions provider/googleProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class GoogleProvider {

const authClient = this.getAuthClient();

return authClient.authorize().then(() => {
return authClient.getClient().then(() => {
const requestParams = { auth: authClient };

// merge the params from the request call into the base functionParams
Expand Down Expand Up @@ -105,21 +105,27 @@ class GoogleProvider {
}

getAuthClient() {
let credentials =
this.serverless.service.provider.credentials || process.env.GOOGLE_APPLICATION_CREDENTIALS;
const credParts = credentials.split(path.sep);

if (credParts[0] === '~') {
credParts[0] = os.homedir();
credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
}

const keyFileContent = fs.readFileSync(credentials).toString();
const key = JSON.parse(keyFileContent);

return new google.auth.JWT(key.client_email, null, key.private_key, [
'https://www.googleapis.com/auth/cloud-platform',
]);
let credentials = this.serverless.service.provider.credentials;

if (credentials) {
const credParts = this.serverless.service.provider.credentials.split(path.sep);
if (credParts[0] === '~') {
credParts[0] = os.homedir();
credentials = credParts.reduce((memo, part) => path.join(memo, part), '');
}

const auth = new google.auth.GoogleAuth({
keyFile: credentials.toString(),
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
return auth
}

const auth = new google.auth.GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform'
});
return auth

}

isServiceSupported(service) {
Expand Down
2 changes: 0 additions & 2 deletions provider/googleProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe('GoogleProvider', () => {
const authClient = googleProvider.getAuthClient();

expect(readFileSyncStub.calledWithExactly('/root/.gcloud/project-1234.json')).toEqual(true);
expect(authClient).toBeInstanceOf(google.auth.JWT);
});

it('should expand tilde characters in credentials file paths', () => {
Expand All @@ -148,7 +147,6 @@ describe('GoogleProvider', () => {

expect(homedirStub.calledOnce).toEqual(true);
expect(readFileSyncStub.calledWithExactly('/root/.gcloud/project-1234.json')).toEqual(true);
expect(authClient).toBeInstanceOf(google.auth.JWT);
});
});

Expand Down