Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fetch acl sample code for content warehouse #3287

Merged
merged 13 commits into from
Jun 24, 2023
Merged
10 changes: 10 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ assign_issues_by:
- 'api: dlp'
to:
- GoogleCloudPlatform/googleapis-dlp
- labels:
- 'api: contentwarehouse'
- 'api: documentai'
to:
- GoogleCloudPlatform/document-ai-samples-contributors

assign_prs_by:
- labels:
Expand All @@ -58,3 +63,8 @@ assign_prs_by:
- 'api: dlp'
to:
- GoogleCloudPlatform/googleapis-dlp
- labels:
- 'api: contentwarehouse'
- 'api: documentai'
to:
- GoogleCloudPlatform/document-ai-samples-contributors
62 changes: 62 additions & 0 deletions .github/workflows/document-warehouse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: document-warehouse
on:
push:
branches:
- main
paths:
- 'document-warehouse/**'
- '.github/workflows/document-warehouse.yaml'
pull_request:
paths:
- 'document-warehouse/**'
- '.github/workflows/document-warehouse.yaml'
pull_request_target:
types: [labeled]
paths:
- 'document-warehouse/**'
- '.github/workflows/document-warehouse.yaml'
schedule:
- cron: '0 0 * * 0'
jobs:
test:
# Ref: https://github.com/google-github-actions/auth#usage
permissions:
contents: 'read'
id-token: 'write'
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
uses: ./.github/workflows/test.yaml
with:
name: 'document-warehouse'
path: 'document-warehouse'
remove_label:
# Ref: https://github.com/google-github-actions/auth#usage
permissions:
contents: 'read'
id-token: 'write'
if: |
github.event.action == 'labeled' &&
github.event.label.name == 'actions:force-run' &&
always()
uses: ./.github/workflows/remove-label.yaml
flakybot:
# Ref: https://github.com/google-github-actions/auth#usage
permissions:
contents: 'read'
id-token: 'write'
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
uses: ./.github/workflows/flakybot.yaml
needs: [test]
1 change: 1 addition & 0 deletions .github/workflows/utils/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"discoveryengine",
"dlp",
"document-ai",
"document-warehouse",
"endpoints/getting-started",
"endpoints/getting-started-grpc",
"error-reporting",
Expand Down
3 changes: 2 additions & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ datalabeling @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-sample
dialogflow @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
dialogflow-cx @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
discoveryengine @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors
document-warehouse @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors
mediatranslation @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
notebooks @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
speech @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions document-warehouse/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
rules:
no-console: off
node/no-unsupported-features/node-builtins: off
72 changes: 72 additions & 0 deletions document-warehouse/fetch-acl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/** Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

async function main(
projectId = 'YOUR_PROJECT_ID',
location = 'YOUR_PROJECT_LOCATION',
userId = 'user:xxxx@example.com',
documentId = 'YOUR_DOCUMENT_ID'
) {
// [START contentwarehouse_fetch_acl]

/**
* TODO(developer): Uncomment these variables before running the sample.
* const projectId = 'YOUR_PROJECT_ID';
* const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
* const documentId = 'YOUR_DOCUMENT_ID',
* const userId = "user:xxxx@example.com" // Format is "user:xxxx@example.com"
*/

// Import from google cloud
const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1;

// Create service client
const serviceClient = new DocumentServiceClient();

// Fetches access control policies on project or document level.
async function fetchACL() {
// Initialize request argument(s)
const request = {};
if (documentId !== 'YOUR_DOCUMENT_ID') {
// Full document resource name, e.g.:
// projects/{project_id}/locations/{location}/documents/{document_id}
request.resource = `projects/${projectId}/locations/${location}/documents/${documentId}`;
request.requestMetadata = {userInfo: {id: userId}};
} else {
// Full document resource name, e.g.: projects/{project_id}
request.resource = `projects/${projectId}`;
request.projectOwner = true;
}

// Make Request
const response = serviceClient.fetchAcl(request);

// Print out response
response.then(
result => console.log(`Success! Response: \n${JSON.stringify(result)}`),
error => console.log(`Failed! Response: \n${error}`)
);
}

// [END contentwarehouse_fetch_acl]
await fetchACL();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
13 changes: 13 additions & 0 deletions document-warehouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "nodejs-document-warehouse-samples",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
"scripts": {
"test": "c8 mocha test/*.js --timeout 600000"
},
"dependencies": {
"@google-cloud/contentwarehouse": "^0.5.1",
"assert": "^2.0.0"
}
}
4 changes: 4 additions & 0 deletions document-warehouse/test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
env:
mocha: true
rules:
node/no-extraneous-require: off
48 changes: 48 additions & 0 deletions document-warehouse/test/fetch-acl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/** Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const cp = require('child_process');
const path = require('path');
const assert = require('assert');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const cwd = path.join(__dirname, '..');
const PROJECT_ID_PASS = 'YOUR_PROJECT_ID';
const PROJECT_ID_FAILED = 'PROJECT_ID_WITHOUT_ACL';
const LOCATION = 'us';
const USER_ID = 'user:xxxx@example.com';
const DOCUMENT_ID = 'YOUR_DOCUMENT_ID';

describe('Fetch document acl', () => {
it('should get acl given only a projectId', async () => {
const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_PASS} `, {cwd});
assert(stdout.startsWith('Success!'));
});

it('should get acl given a documentId', async () => {
const stdout = execSync(
`node ./fetch-acl.js ${PROJECT_ID_PASS} ${LOCATION} ${USER_ID} ${DOCUMENT_ID}`,
{cwd}
);
assert(stdout.startsWith('Success!'));
});

it('should fail given an incorrect projectId', async () => {
const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_FAILED}`, {cwd});
assert(stdout.startsWith('Failed!'));
});
});
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"test": "echo 'Please run tests in each sample directory.' && exit 1",
"generate-ci": "node .github/workflows/utils/generate.js"
},
"devDependencies": {
"c8": "^7.13.0",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"c8": "^7.13.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-config-prettier": "^8.8.0",
"gts": "^3.1.1",
"mocha": "^10.2.0",
"nunjucks": "^3.2.4",
"prettier": "^2.8.7",
"typescript": "^5.0.4"
Expand Down