Skip to content

Commit 372d548

Browse files
irataxykweinmeisterpattishin
authored
docs: add Live Stream assets and pools samples and tests (#3383)
* docs: add Live Stream assets and pools samples and tests * fix license check --------- Co-authored-by: Karl Weinmeister <[email protected]> Co-authored-by: Patti Shin <[email protected]>
1 parent 4b6f96b commit 372d548

File tree

7 files changed

+402
-2
lines changed

7 files changed

+402
-2
lines changed

media/livestream/createAsset.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(projectId, location, assetId, assetUri) {
19+
// [START livestream_create_asset]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// projectId = 'my-project-id';
24+
// location = 'us-central1';
25+
// assetId = 'my-asset';
26+
// assetUri = 'gs://my-bucket/my-video.mp4';
27+
28+
// Imports the Livestream library
29+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
30+
31+
// Instantiates a client
32+
const livestreamServiceClient = new LivestreamServiceClient();
33+
34+
async function createAsset() {
35+
// Construct request
36+
const request = {
37+
parent: livestreamServiceClient.locationPath(projectId, location),
38+
assetId: assetId,
39+
asset: {
40+
video: {
41+
uri: assetUri,
42+
},
43+
},
44+
};
45+
46+
// Run request
47+
const [operation] = await livestreamServiceClient.createAsset(request);
48+
const response = await operation.promise();
49+
const [asset] = response;
50+
console.log(`Asset: ${asset.name}`);
51+
}
52+
53+
createAsset();
54+
// [END livestream_create_asset]
55+
}
56+
57+
// node createAsset.js <projectId> <location> <assetId> <assetUri>
58+
process.on('unhandledRejection', err => {
59+
console.error(err.message);
60+
process.exitCode = 1;
61+
});
62+
main(...process.argv.slice(2));

media/livestream/deleteAsset.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(projectId, location, assetId) {
19+
// [START livestream_delete_asset]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// projectId = 'my-project-id';
24+
// location = 'us-central1';
25+
// assetId = 'my-asset';
26+
27+
// Imports the Livestream library
28+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
29+
30+
// Instantiates a client
31+
const livestreamServiceClient = new LivestreamServiceClient();
32+
33+
async function deleteAsset() {
34+
// Construct request
35+
const request = {
36+
name: livestreamServiceClient.assetPath(projectId, location, assetId),
37+
};
38+
39+
// Run request
40+
const [operation] = await livestreamServiceClient.deleteAsset(request);
41+
await operation.promise();
42+
console.log('Deleted asset');
43+
}
44+
45+
deleteAsset();
46+
// [END livestream_delete_asset]
47+
}
48+
49+
// node deleteAsset.js <projectId> <location> <assetId>
50+
process.on('unhandledRejection', err => {
51+
console.error(err.message);
52+
process.exitCode = 1;
53+
});
54+
main(...process.argv.slice(2));

media/livestream/getAsset.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(projectId, location, assetId) {
19+
// [START livestream_get_asset]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// projectId = 'my-project-id';
24+
// location = 'us-central1';
25+
// assetId = 'my-asset';
26+
27+
// Imports the Livestream library
28+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
29+
30+
// Instantiates a client
31+
const livestreamServiceClient = new LivestreamServiceClient();
32+
33+
async function getAsset() {
34+
// Construct request
35+
const request = {
36+
name: livestreamServiceClient.assetPath(projectId, location, assetId),
37+
};
38+
const [asset] = await livestreamServiceClient.getAsset(request);
39+
console.log(`Asset: ${asset.name}`);
40+
}
41+
42+
getAsset();
43+
// [END livestream_get_asset]
44+
}
45+
46+
// node getAsset.js <projectId> <location> <assetId>
47+
process.on('unhandledRejection', err => {
48+
console.error(err.message);
49+
process.exitCode = 1;
50+
});
51+
main(...process.argv.slice(2));

media/livestream/getPool.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(projectId, location, poolId) {
19+
// [START livestream_get_pool]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// projectId = 'my-project-id';
24+
// location = 'us-central1';
25+
// poolId = 'my-pool';
26+
27+
// Imports the Livestream library
28+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
29+
30+
// Instantiates a client
31+
const livestreamServiceClient = new LivestreamServiceClient();
32+
33+
async function getPool() {
34+
// Construct request
35+
const request = {
36+
name: livestreamServiceClient.poolPath(projectId, location, poolId),
37+
};
38+
const [pool] = await livestreamServiceClient.getPool(request);
39+
console.log(`Pool: ${pool.name}`);
40+
}
41+
42+
getPool();
43+
// [END livestream_get_pool]
44+
}
45+
46+
// node getPool.js <projectId> <location> <poolId>
47+
process.on('unhandledRejection', err => {
48+
console.error(err.message);
49+
process.exitCode = 1;
50+
});
51+
main(...process.argv.slice(2));

media/livestream/listAssets.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(projectId, location) {
19+
// [START livestream_list_assets]
20+
/**
21+
* TODO(developer): Uncomment these variables before running the sample.
22+
*/
23+
// projectId = 'my-project-id';
24+
// location = 'us-central1';
25+
26+
// Imports the Livestream library
27+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
28+
29+
// Instantiates a client
30+
const livestreamServiceClient = new LivestreamServiceClient();
31+
32+
async function listAssets() {
33+
const iterable = await livestreamServiceClient.listAssetsAsync({
34+
parent: livestreamServiceClient.locationPath(projectId, location),
35+
});
36+
console.info('Assets:');
37+
for await (const response of iterable) {
38+
console.log(response.name);
39+
}
40+
}
41+
42+
listAssets();
43+
// [END livestream_list_assets]
44+
}
45+
46+
// node listAssets.js <projectId> <location>
47+
process.on('unhandledRejection', err => {
48+
console.error(err.message);
49+
process.exitCode = 1;
50+
});
51+
main(...process.argv.slice(2));

media/livestream/test/livestream.test.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Google, Inc.
2+
* Copyright 2023 Google LLC
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -34,11 +34,16 @@ const channelId = `nodejs-test-livestream-channel-${uniqueID}`;
3434
const channelName = `projects/${projectId}/locations/${location}/channels/${channelId}`;
3535
const eventId = `nodejs-test-livestream-event-${uniqueID}`;
3636
const eventName = `projects/${projectId}/locations/${location}/channels/${channelId}/events/${eventId}`;
37+
const assetId = `nodejs-test-livestream-asset-${uniqueID}`;
38+
const assetName = `projects/${projectId}/locations/${location}/assets/${assetId}`;
39+
const assetUri = 'gs://cloud-samples-data/media/ForBiggerEscapes.mp4';
3740
const outputUri = `gs://${bucketName}/test-output-channel/`;
41+
const poolId = 'default';
42+
const poolName = `projects/${projectId}/locations/${location}/pools/${poolId}`;
3843
const cwd = path.join(__dirname, '..');
3944

4045
before(async () => {
41-
// Delete outstanding channels and inputs created more than 3 hours ago
46+
// Delete outstanding channels, inputs, and assets created more than 3 hours ago
4247
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
4348
const livestreamServiceClient = new LivestreamServiceClient();
4449
const THREE_HOURS_IN_SEC = 60 * 60 * 3;
@@ -87,6 +92,17 @@ before(async () => {
8792
await livestreamServiceClient.deleteInput(request);
8893
}
8994
}
95+
const [assets] = await livestreamServiceClient.listAssets({
96+
parent: livestreamServiceClient.locationPath(projectId, location),
97+
});
98+
for (const asset of assets) {
99+
if (asset.createTime.seconds < DATE_NOW_SEC - THREE_HOURS_IN_SEC) {
100+
const request = {
101+
name: asset.name,
102+
};
103+
await livestreamServiceClient.deleteAsset(request);
104+
}
105+
}
90106
});
91107

92108
describe('Input functions', () => {
@@ -304,3 +320,54 @@ describe('Channel event functions', () => {
304320
assert.ok(output.includes('Deleted channel event'));
305321
});
306322
});
323+
324+
describe('Asset functions', () => {
325+
it('should create an asset', () => {
326+
const output = execSync(
327+
`node createAsset.js ${projectId} ${location} ${assetId} ${assetUri}`,
328+
{cwd}
329+
);
330+
assert.ok(output.includes(assetName));
331+
});
332+
333+
it('should show a list of assets', () => {
334+
const output = execSync(`node listAssets.js ${projectId} ${location}`, {
335+
cwd,
336+
});
337+
assert.ok(output.includes(assetName));
338+
});
339+
340+
it('should get an asset', () => {
341+
const output = execSync(
342+
`node getAsset.js ${projectId} ${location} ${assetId}`,
343+
{cwd}
344+
);
345+
assert.ok(output.includes(assetName));
346+
});
347+
348+
it('should delete an asset', () => {
349+
const output = execSync(
350+
`node deleteAsset.js ${projectId} ${location} ${assetId}`,
351+
{cwd}
352+
);
353+
assert.ok(output.includes('Deleted asset'));
354+
});
355+
});
356+
357+
describe('Pool functions', () => {
358+
it('should get a pool', () => {
359+
const output = execSync(
360+
`node getPool.js ${projectId} ${location} ${poolId}`,
361+
{cwd}
362+
);
363+
assert.ok(output.includes(poolName));
364+
});
365+
366+
it('should update a pool', () => {
367+
const output = execSync(
368+
`node updatePool.js ${projectId} ${location} ${poolId} ''`,
369+
{cwd}
370+
);
371+
assert.ok(output.includes(poolName));
372+
});
373+
});

0 commit comments

Comments
 (0)