Skip to content

Commit 43848d0

Browse files
paulinawinsPaulina Nguyenkweinmeister
authored
feat: add fetch acl sample code for content warehouse (#3287)
* add fetch acl sample code for content warehouse * remove google related values from test * add document-warehouse directory codeowner * add document-warehouse blunderbuss issue and pr auto-assigner * fix some requested changes and lint issues * add document-ai-samples-contributors to directory * add license to header --------- Co-authored-by: Paulina Nguyen <[email protected]> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent ea6c1ec commit 43848d0

File tree

10 files changed

+247
-4
lines changed

10 files changed

+247
-4
lines changed

.github/blunderbuss.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ assign_issues_by:
3737
- 'api: dlp'
3838
to:
3939
- GoogleCloudPlatform/googleapis-dlp
40+
- labels:
41+
- 'api: contentwarehouse'
42+
- 'api: documentai'
43+
to:
44+
- GoogleCloudPlatform/document-ai-samples-contributors
4045

4146
assign_prs_by:
4247
- labels:
@@ -58,3 +63,8 @@ assign_prs_by:
5863
- 'api: dlp'
5964
to:
6065
- GoogleCloudPlatform/googleapis-dlp
66+
- labels:
67+
- 'api: contentwarehouse'
68+
- 'api: documentai'
69+
to:
70+
- GoogleCloudPlatform/document-ai-samples-contributors
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2023 Google LLC
2+
#
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+
name: document-warehouse
16+
on:
17+
push:
18+
branches:
19+
- main
20+
paths:
21+
- 'document-warehouse/**'
22+
- '.github/workflows/document-warehouse.yaml'
23+
pull_request:
24+
paths:
25+
- 'document-warehouse/**'
26+
- '.github/workflows/document-warehouse.yaml'
27+
pull_request_target:
28+
types: [labeled]
29+
paths:
30+
- 'document-warehouse/**'
31+
- '.github/workflows/document-warehouse.yaml'
32+
schedule:
33+
- cron: '0 0 * * 0'
34+
jobs:
35+
test:
36+
# Ref: https://github.com/google-github-actions/auth#usage
37+
permissions:
38+
contents: 'read'
39+
id-token: 'write'
40+
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
41+
uses: ./.github/workflows/test.yaml
42+
with:
43+
name: 'document-warehouse'
44+
path: 'document-warehouse'
45+
remove_label:
46+
# Ref: https://github.com/google-github-actions/auth#usage
47+
permissions:
48+
contents: 'read'
49+
id-token: 'write'
50+
if: |
51+
github.event.action == 'labeled' &&
52+
github.event.label.name == 'actions:force-run' &&
53+
always()
54+
uses: ./.github/workflows/remove-label.yaml
55+
flakybot:
56+
# Ref: https://github.com/google-github-actions/auth#usage
57+
permissions:
58+
contents: 'read'
59+
id-token: 'write'
60+
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
61+
uses: ./.github/workflows/flakybot.yaml
62+
needs: [test]

.github/workflows/utils/workflows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"discoveryengine",
4040
"dlp",
4141
"document-ai",
42+
"document-warehouse",
4243
"endpoints/getting-started",
4344
"endpoints/getting-started-grpc",
4445
"error-reporting",

CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ datalabeling @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-sample
5757
dialogflow @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
5858
dialogflow-cx @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
5959
discoveryengine @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
60-
document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
60+
document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors
61+
document-warehouse @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors
6162
mediatranslation @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
6263
notebooks @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
6364
speech @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers

document-warehouse/.eslintrc.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2023 Google LLC
2+
#
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+
rules:
16+
no-console: off
17+
node/no-unsupported-features/node-builtins: off

document-warehouse/fetch-acl.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/** Copyright 2023 Google LLC
2+
*
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+
async function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
location = 'YOUR_PROJECT_LOCATION',
21+
userId = 'user:[email protected]',
22+
documentId = 'YOUR_DOCUMENT_ID'
23+
) {
24+
// [START contentwarehouse_fetch_acl]
25+
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
* const projectId = 'YOUR_PROJECT_ID';
29+
* const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
30+
* const documentId = 'YOUR_DOCUMENT_ID',
31+
* const userId = "user:[email protected]" // Format is "user:[email protected]"
32+
*/
33+
34+
// Import from google cloud
35+
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;
36+
37+
// Create service client
38+
const serviceClient = new DocumentServiceClient();
39+
40+
// Fetches access control policies on project or document level.
41+
async function fetchACL() {
42+
// Initialize request argument(s)
43+
const request = {};
44+
if (documentId !== 'YOUR_DOCUMENT_ID') {
45+
// Full document resource name, e.g.:
46+
// projects/{project_id}/locations/{location}/documents/{document_id}
47+
request.resource = `projects/${projectId}/locations/${location}/documents/${documentId}`;
48+
request.requestMetadata = {userInfo: {id: userId}};
49+
} else {
50+
// Full document resource name, e.g.: projects/{project_id}
51+
request.resource = `projects/${projectId}`;
52+
request.projectOwner = true;
53+
}
54+
55+
// Make Request
56+
const response = serviceClient.fetchAcl(request);
57+
58+
// Print out response
59+
response.then(
60+
result => console.log(`Success! Response: \n${JSON.stringify(result)}`),
61+
error => console.log(`Failed! Response: \n${error}`)
62+
);
63+
}
64+
65+
// [END contentwarehouse_fetch_acl]
66+
await fetchACL();
67+
}
68+
69+
main(...process.argv.slice(2)).catch(err => {
70+
console.error(err);
71+
process.exitCode = 1;
72+
});

document-warehouse/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "nodejs-document-warehouse-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"author": "Google LLC",
6+
"scripts": {
7+
"test": "c8 mocha test/*.js --timeout 600000"
8+
},
9+
"dependencies": {
10+
"@google-cloud/contentwarehouse": "^0.5.1",
11+
"assert": "^2.0.0"
12+
}
13+
}

document-warehouse/test/.eslintrc.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2023 Google LLC
2+
#
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+
env:
16+
mocha: true
17+
rules:
18+
node/no-extraneous-require: off
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/** Copyright 2023 Google LLC
2+
*
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+
const cp = require('child_process');
19+
const path = require('path');
20+
const assert = require('assert');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
const cwd = path.join(__dirname, '..');
24+
const PROJECT_ID_PASS = 'YOUR_PROJECT_ID';
25+
const PROJECT_ID_FAILED = 'PROJECT_ID_WITHOUT_ACL';
26+
const LOCATION = 'us';
27+
const USER_ID = 'user:[email protected]';
28+
const DOCUMENT_ID = 'YOUR_DOCUMENT_ID';
29+
30+
describe('Fetch document acl', () => {
31+
it('should get acl given only a projectId', async () => {
32+
const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_PASS} `, {cwd});
33+
assert(stdout.startsWith('Success!'));
34+
});
35+
36+
it('should get acl given a documentId', async () => {
37+
const stdout = execSync(
38+
`node ./fetch-acl.js ${PROJECT_ID_PASS} ${LOCATION} ${USER_ID} ${DOCUMENT_ID}`,
39+
{cwd}
40+
);
41+
assert(stdout.startsWith('Success!'));
42+
});
43+
44+
it('should fail given an incorrect projectId', async () => {
45+
const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_FAILED}`, {cwd});
46+
assert(stdout.startsWith('Failed!'));
47+
});
48+
});

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
"test": "echo 'Please run tests in each sample directory.' && exit 1",
1818
"generate-ci": "node .github/workflows/utils/generate.js"
1919
},
20-
"devDependencies": {
21-
"c8": "^7.13.0",
20+
"devDependencies": {
2221
"@typescript-eslint/eslint-plugin": "^5.59.0",
2322
"@typescript-eslint/parser": "^5.59.0",
23+
"c8": "^7.13.0",
24+
"eslint-config-prettier": "^8.8.0",
2425
"eslint-plugin-node": "^11.1.0",
2526
"eslint-plugin-prettier": "^4.2.1",
26-
"eslint-config-prettier": "^8.8.0",
2727
"gts": "^3.1.1",
28+
"mocha": "^10.2.0",
2829
"nunjucks": "^3.2.4",
2930
"prettier": "^2.8.7",
3031
"typescript": "^5.0.4"

0 commit comments

Comments
 (0)