Skip to content

Commit ee18a8d

Browse files
committed
Add labels support to packaging functions
1 parent 043516f commit ee18a8d

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

package/lib/compileFunctions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ module.exports = {
3737
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
3838
|| _.get(this, 'serverless.service.provider.timeout')
3939
|| '60s';
40+
funcTemplate.properties.labels = _.assign({},
41+
_.get(this, 'serverless.service.provider.labels') || {},
42+
_.get(funcObject, 'labels') || {},
43+
);
4044

4145
const eventType = Object.keys(funcObject.events[0])[0];
4246

package/lib/compileFunctions.test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ describe('CompileFunctions', () => {
120120
httpsTrigger: {
121121
url: 'foo',
122122
},
123+
labels: {},
123124
},
124125
}];
125126

@@ -153,6 +154,7 @@ describe('CompileFunctions', () => {
153154
httpsTrigger: {
154155
url: 'foo',
155156
},
157+
labels: {},
156158
},
157159
}];
158160

@@ -186,6 +188,7 @@ describe('CompileFunctions', () => {
186188
httpsTrigger: {
187189
url: 'foo',
188190
},
191+
labels: {},
189192
},
190193
}];
191194

@@ -219,6 +222,126 @@ describe('CompileFunctions', () => {
219222
httpsTrigger: {
220223
url: 'foo',
221224
},
225+
labels: {},
226+
},
227+
}];
228+
229+
return googlePackage.compileFunctions().then(() => {
230+
expect(consoleLogStub.calledOnce).toEqual(true);
231+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
232+
.toEqual(compiledResources);
233+
});
234+
});
235+
236+
it('should set the labels based on the functions configuration', () => {
237+
googlePackage.serverless.service.functions = {
238+
func1: {
239+
handler: 'func1',
240+
labels: {
241+
test: 'label'
242+
},
243+
events: [
244+
{ http: 'foo' },
245+
],
246+
},
247+
};
248+
249+
const compiledResources = [{
250+
type: 'cloudfunctions.v1beta2.function',
251+
name: 'my-service-dev-func1',
252+
properties: {
253+
location: 'us-central1',
254+
function: 'func1',
255+
availableMemoryMb: 256,
256+
timeout: '60s',
257+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
258+
httpsTrigger: {
259+
url: 'foo',
260+
},
261+
labels: {
262+
test: 'label'
263+
},
264+
},
265+
}];
266+
267+
return googlePackage.compileFunctions().then(() => {
268+
expect(consoleLogStub.calledOnce).toEqual(true);
269+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
270+
.toEqual(compiledResources);
271+
});
272+
});
273+
274+
it('should set the labels based on the provider configuration', () => {
275+
googlePackage.serverless.service.functions = {
276+
func1: {
277+
handler: 'func1',
278+
events: [
279+
{ http: 'foo' },
280+
],
281+
},
282+
};
283+
googlePackage.serverless.service.provider.labels = {
284+
test: 'label'
285+
};
286+
287+
const compiledResources = [{
288+
type: 'cloudfunctions.v1beta2.function',
289+
name: 'my-service-dev-func1',
290+
properties: {
291+
location: 'us-central1',
292+
function: 'func1',
293+
availableMemoryMb: 256,
294+
timeout: '60s',
295+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
296+
httpsTrigger: {
297+
url: 'foo',
298+
},
299+
labels: {
300+
test: 'label'
301+
},
302+
},
303+
}];
304+
305+
return googlePackage.compileFunctions().then(() => {
306+
expect(consoleLogStub.calledOnce).toEqual(true);
307+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
308+
.toEqual(compiledResources);
309+
});
310+
});
311+
312+
it('should set the labels based on the merged provider and function configuration', () => {
313+
googlePackage.serverless.service.functions = {
314+
func1: {
315+
handler: 'func1',
316+
events: [
317+
{ http: 'foo' },
318+
],
319+
labels: {
320+
test: 'functionLabel'
321+
}
322+
},
323+
};
324+
googlePackage.serverless.service.provider.labels = {
325+
test: 'providerLabel',
326+
secondTest: 'tested',
327+
};
328+
329+
const compiledResources = [{
330+
type: 'cloudfunctions.v1beta2.function',
331+
name: 'my-service-dev-func1',
332+
properties: {
333+
location: 'us-central1',
334+
function: 'func1',
335+
availableMemoryMb: 256,
336+
timeout: '60s',
337+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
338+
httpsTrigger: {
339+
url: 'foo',
340+
},
341+
labels: {
342+
test: 'functionLabel',
343+
secondTest: 'tested',
344+
},
222345
},
223346
}];
224347

@@ -251,6 +374,7 @@ describe('CompileFunctions', () => {
251374
httpsTrigger: {
252375
url: 'foo',
253376
},
377+
labels: {},
254378
},
255379
}];
256380

@@ -303,6 +427,7 @@ describe('CompileFunctions', () => {
303427
path: 'some-path',
304428
resource: 'some-resource',
305429
},
430+
labels: {},
306431
},
307432
},
308433
{
@@ -318,6 +443,7 @@ describe('CompileFunctions', () => {
318443
eventType: 'foo',
319444
resource: 'some-resource',
320445
},
446+
labels: {},
321447
},
322448
},
323449
];

0 commit comments

Comments
 (0)