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: Create Data Catalog search sample #1799

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
15 changes: 14 additions & 1 deletion .kokoro/datacatalog.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@ env_vars: {
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-docs-samples/.kokoro/build.sh"
}
}

# This must be different from GCLOUD_PROJECT.
# Since build.sh overrides the value for GCLOUD_PROJECT.
env_vars: {
key: "GCLOUD_ORGANIZATION_PROJECT"
value: "project-a"
}

# Set organization used by org scoped samples.
env_vars: {
key: "GCLOUD_ORGANIZATION"
value: "1081635000895"
}
5 changes: 4 additions & 1 deletion datacatalog/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ Run the following command to install the library dependencies for Node.js:
# Running the sample

Commands:
createEntryGroup.js <projectId> <entryGroupId> Create a entry group.
createFilesetEntry.js <projectId> <entryGroupId> <entryId> Create a fileset entry.
lookupEntry.js <projectId> <datasetId> Lookup a dataset entry.

searchCatalogProject.js <projectId> <query> Search Catalog with project scope.
searchCatalogOrg.js <organizationId> <query> Search Catalog with organization scope.

For more information, see https://cloud.google.com/data-catalog/docs/
3 changes: 2 additions & 1 deletion datacatalog/cloud-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"devDependencies": {
"mocha": "^8.0.0",
"uuid": "^8.0.0"
"uuid": "^8.0.0",
"@google-cloud/bigquery": "^4.0.0"
},
"dependencies": {
"@google-cloud/datacatalog": "^2.0.0"
Expand Down
52 changes: 52 additions & 0 deletions datacatalog/cloud-client/searchCatalogOrg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020 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.

// [START datacatalog_search_org]
// This application demonstrates how to perform search operations with the
// Cloud Data Catalog API.

const main = async (
organizationId = process.env.GCLOUD_ORGANIZATION,
query
) => {

// -------------------------------
// Import required modules.
// -------------------------------
const { DataCatalogClient } = require('@google-cloud/datacatalog').v1;
const datacatalog = new DataCatalogClient();

// Create request.
const scope = {
includeOrgIds: [organizationId],
// Alternatively, search using project scopes.
// includeProjectIds: ['my-project'],
};

const request = {
scope: scope,
query: query,
};

const [response] = await datacatalog.searchCatalog(request);
console.log(response);
return response;
}

// node searchCatalogOrg.js <organizationId> <query>
// sample values:
// organizationId = 111111000000;
// query = 'type=dataset'
main(...process.argv.slice(2));
// [END datacatalog_search_org]
52 changes: 52 additions & 0 deletions datacatalog/cloud-client/searchCatalogProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020 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.

// [START datacatalog_search_project]
// This application demonstrates how to perform search operations with the
// Cloud Data Catalog API.

const main = async (
projectId = process.env.GCLOUD_PROJECT,
query
) => {

// -------------------------------
// Import required modules.
// -------------------------------
const { DataCatalogClient } = require('@google-cloud/datacatalog').v1;
const datacatalog = new DataCatalogClient();

// Create request.
const scope = {
includeProjectIds: [projectId],
// Alternatively, search using org scopes.
// includeOrgIds: [organizationId]
};

const request = {
scope: scope,
query: query,
};

const [response] = await datacatalog.searchCatalog(request);
console.log(response);
return response;
}

// node searchCatalogProject.js <projectId> <query>
// sample values:
// projectId = 'my-project';
// query = 'type=dataset'
main(...process.argv.slice(2));
// [END datacatalog_search_project]
68 changes: 68 additions & 0 deletions datacatalog/cloud-client/system-test/searchCatalogOrg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2020 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 path = require('path');
const assert = require('assert');
const cwd = path.join(__dirname, '..');
const {exec} = require('child_process');
const uuid = require('uuid');
const generateUuid = () => `datacatalog-tests-${uuid.v4()}`.replace(/-/gi, '_');
const datasetId = generateUuid();
const {BigQuery} = require('@google-cloud/bigquery');

const bigquery = new BigQuery();

before(async () => {
assert(
process.env.GCLOUD_ORGANIZATION,
`Must set GCLOUD_ORGANIZATION environment variable!`
);
assert(
process.env.GCLOUD_ORGANIZATION_PROJECT,
`Must set GCLOUD_ORGANIZATION_PROJECT environment variable!`
);
assert(
process.env.GOOGLE_APPLICATION_CREDENTIALS,
`Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!`
);

await bigquery.createDataset(datasetId);
});
after(async () => {
await bigquery
.dataset(datasetId)
.delete({force: true})
.catch(console.warn);
});

describe('searchCatalog org', () => {
it('should return a dataset entry', (done) => {
const organizationId = process.env.GCLOUD_ORGANIZATION;
const projectId = process.env.GCLOUD_ORGANIZATION_PROJECT;
const query = 'type=dataset';
const expectedLinkedResource = `//bigquery.googleapis.com/projects/${projectId}/datasets/${datasetId}`;
exec(
`node searchCatalogOrg.js ${organizationId} ${query}`,
{cwd},
(err, stdout) => {
// ADD line to test return on kokoro build
console.log(`search-org: ${stdout} ${err}`)
assert.ok(stdout.includes(expectedLinkedResource));
done();
}
);
});
});
63 changes: 63 additions & 0 deletions datacatalog/cloud-client/system-test/searchCatalogProject.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 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 path = require('path');
const assert = require('assert');
const cwd = path.join(__dirname, '..');
const {exec} = require('child_process');
const uuid = require('uuid');
const generateUuid = () => `datacatalog-tests-${uuid.v4()}`.replace(/-/gi, '_');
const datasetId = generateUuid();
const {BigQuery} = require('@google-cloud/bigquery');

const bigquery = new BigQuery();

before(async () => {
assert(
process.env.GCLOUD_PROJECT,
`Must set GCLOUD_PROJECT environment variable!`
);
assert(
process.env.GOOGLE_APPLICATION_CREDENTIALS,
`Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!`
);

await bigquery.createDataset(datasetId);
});
after(async () => {
await bigquery
.dataset(datasetId)
.delete({force: true})
.catch(console.warn);
});

describe('searchCatalog project', () => {
it('should return a dataset entry', (done) => {
const projectId = process.env.GCLOUD_PROJECT;
const query = 'type=dataset';
const expectedLinkedResource = `//bigquery.googleapis.com/projects/${projectId}/datasets/${datasetId}`;
exec(
`node searchCatalogProject.js ${projectId} ${query}`,
{cwd},
(err, stdout) => {
// ADD line to test return on kokoro build
console.log(`search-project: ${stdout} ${err}`)
assert.ok(stdout.includes(expectedLinkedResource));
done();
}
);
});
});