Skip to content

Commit 0af61b9

Browse files
feat(samples): add samples (#229)
1 parent 0b0b16a commit 0af61b9

24 files changed

+2462
-586
lines changed

packages/google-cloud-datacatalog/.jsdoc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Google LLC
1+
// Copyright 2021 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ module.exports = {
4040
includePattern: '\\.js$'
4141
},
4242
templates: {
43-
copyright: 'Copyright 2020 Google LLC',
43+
copyright: 'Copyright 2021 Google LLC',
4444
includeDate: false,
4545
sourceFiles: false,
4646
systemName: '@google-cloud/datacatalog',

packages/google-cloud-datacatalog/CONTRIBUTING.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ accept your pull requests.
3737
1. Title your pull request following [Conventional Commits](https://www.conventionalcommits.org/) styling.
3838
1. Submit a pull request.
3939

40+
### Before you begin
41+
42+
1. [Select or create a Cloud Platform project][projects].
43+
1. [Enable billing for your project][billing].
44+
1. [Enable the Data Catalog API][enable_api].
45+
1. [Set up authentication with a service account][auth] so you can access the
46+
API from your local workstation.
47+
48+
4049
## Running the tests
4150

4251
1. [Prepare your environment for Node.js setup][setup].
@@ -51,15 +60,17 @@ accept your pull requests.
5160
npm test
5261

5362
# Run sample integration tests.
54-
gcloud auth application-default login
5563
npm run samples-test
5664

5765
# Run all system tests.
58-
gcloud auth application-default login
5966
npm run system-test
6067

6168
1. Lint (and maybe fix) any changes:
6269

6370
npm run fix
6471

6572
[setup]: https://cloud.google.com/nodejs/docs/setup
73+
[projects]: https://console.cloud.google.com/project
74+
[billing]: https://support.google.com/cloud/answer/6293499#enable-billing
75+
[enable_api]: https://console.cloud.google.com/flows/enableapi?apiid=datacatalog.googleapis.com
76+
[auth]: https://cloud.google.com/docs/authentication/getting-started

packages/google-cloud-datacatalog/README.md

+171-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Google APIs Client Libraries, in [Client Libraries Explained][explained].
3232
* [Quickstart](#quickstart)
3333
* [Before you begin](#before-you-begin)
3434
* [Installing the client library](#installing-the-client-library)
35-
36-
35+
* [Using the client library](#using-the-client-library)
36+
* [Samples](#samples)
3737
* [Versioning](#versioning)
3838
* [Contributing](#contributing)
3939
* [License](#license)
@@ -55,6 +55,174 @@ npm install @google-cloud/datacatalog
5555
```
5656

5757

58+
### Using the client library
59+
60+
```javascript
61+
// Import the Google Cloud client library and create a client.
62+
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1;
63+
const datacatalog = new DataCatalogClient();
64+
65+
async function quickstart() {
66+
// Common fields.
67+
let request;
68+
let responses;
69+
70+
/**
71+
* TODO(developer): Uncomment the following lines before running the sample.
72+
*/
73+
// const projectId = 'my_project'; // Google Cloud Platform project
74+
// const datasetId = 'demo_dataset';
75+
// const tableId = 'trips';
76+
77+
// Currently, Data Catalog stores metadata in the
78+
// us-central1 region.
79+
const location = 'us-central1';
80+
81+
// Create Fields.
82+
const fieldSource = {
83+
displayName: 'Source of data asset',
84+
type: {
85+
primitiveType: 'STRING',
86+
},
87+
};
88+
89+
const fieldNumRows = {
90+
displayName: 'Number of rows in data asset',
91+
type: {
92+
primitiveType: 'DOUBLE',
93+
},
94+
};
95+
96+
const fieldHasPII = {
97+
displayName: 'Has PII',
98+
type: {
99+
primitiveType: 'BOOL',
100+
},
101+
};
102+
103+
const fieldPIIType = {
104+
displayName: 'PII type',
105+
type: {
106+
enumType: {
107+
allowedValues: [
108+
{
109+
displayName: 'EMAIL',
110+
},
111+
{
112+
displayName: 'SOCIAL SECURITY NUMBER',
113+
},
114+
{
115+
displayName: 'NONE',
116+
},
117+
],
118+
},
119+
},
120+
};
121+
122+
// Create Tag Template.
123+
const tagTemplateId = 'demo_tag_template';
124+
125+
const tagTemplate = {
126+
displayName: 'Demo Tag Template',
127+
fields: {
128+
source: fieldSource,
129+
num_rows: fieldNumRows,
130+
has_pii: fieldHasPII,
131+
pii_type: fieldPIIType,
132+
},
133+
};
134+
135+
const tagTemplatePath = datacatalog.tagTemplatePath(
136+
projectId,
137+
location,
138+
tagTemplateId
139+
);
140+
141+
// Delete any pre-existing Template with the same name.
142+
try {
143+
request = {
144+
name: tagTemplatePath,
145+
force: true,
146+
};
147+
await datacatalog.deleteTagTemplate(request);
148+
console.log(`Deleted template: ${tagTemplatePath}`);
149+
} catch (error) {
150+
console.log(`Cannot delete template: ${tagTemplatePath}`);
151+
}
152+
153+
// Create the Tag Template request.
154+
const locationPath = datacatalog.locationPath(projectId, location);
155+
156+
request = {
157+
parent: locationPath,
158+
tagTemplateId: tagTemplateId,
159+
tagTemplate: tagTemplate,
160+
};
161+
162+
// Execute the request.
163+
responses = await datacatalog.createTagTemplate(request);
164+
const createdTagTemplate = responses[0];
165+
console.log(`Created template: ${createdTagTemplate.name}`);
166+
167+
// Lookup Data Catalog's Entry referring to the table.
168+
responses = await datacatalog.lookupEntry({
169+
linkedResource:
170+
'//bigquery.googleapis.com/projects/' +
171+
`${projectId}/datasets/${datasetId}/tables/${tableId}`,
172+
});
173+
const entry = responses[0];
174+
console.log(`Entry name: ${entry.name}`);
175+
console.log(`Entry type: ${entry.type}`);
176+
console.log(`Linked resource: ${entry.linkedResource}`);
177+
178+
// Attach a Tag to the table.
179+
const tag = {
180+
name: entry.name,
181+
template: createdTagTemplate.name,
182+
fields: {
183+
source: {
184+
stringValue: 'copied from tlc_yellow_trips_2017',
185+
},
186+
num_rows: {
187+
doubleValue: 113496874,
188+
},
189+
has_pii: {
190+
boolValue: false,
191+
},
192+
pii_type: {
193+
enumValue: {
194+
displayName: 'NONE',
195+
},
196+
},
197+
},
198+
};
199+
200+
request = {
201+
parent: entry.name,
202+
tag: tag,
203+
};
204+
205+
// Create the Tag.
206+
await datacatalog.createTag(request);
207+
console.log(`Tag created for entry: ${entry.name}`);
208+
}
209+
quickstart();
210+
211+
```
212+
213+
214+
215+
## Samples
216+
217+
Samples are in the [`samples/`](https://github.com/googleapis/nodejs-datacatalog/tree/master/samples) directory. Each sample's `README.md` has instructions for running its sample.
218+
219+
| Sample | Source Code | Try it |
220+
| --------------------------- | --------------------------------- | ------ |
221+
| Create Custom Entry | [source code](https://github.com/googleapis/nodejs-datacatalog/blob/master/samples/createCustomEntry.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datacatalog&page=editor&open_in_editor=samples/createCustomEntry.js,samples/README.md) |
222+
| Create Fileset | [source code](https://github.com/googleapis/nodejs-datacatalog/blob/master/samples/createFileset.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datacatalog&page=editor&open_in_editor=samples/createFileset.js,samples/README.md) |
223+
| Grant Tag Template User Role | [source code](https://github.com/googleapis/nodejs-datacatalog/blob/master/samples/grantTagTemplateUserRole.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datacatalog&page=editor&open_in_editor=samples/grantTagTemplateUserRole.js,samples/README.md) |
224+
| Quickstart | [source code](https://github.com/googleapis/nodejs-datacatalog/blob/master/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datacatalog&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
225+
| Search Assets | [source code](https://github.com/googleapis/nodejs-datacatalog/blob/master/samples/searchAssets.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datacatalog&page=editor&open_in_editor=samples/searchAssets.js,samples/README.md) |
58226

59227

60228

@@ -67,7 +235,7 @@ Our client libraries follow the [Node.js release schedule](https://nodejs.org/en
67235
Libraries are compatible with all current _active_ and _maintenance_ versions of
68236
Node.js.
69237

70-
Client libraries targetting some end-of-life versions of Node.js are available, and
238+
Client libraries targeting some end-of-life versions of Node.js are available, and
71239
can be installed via npm [dist-tags](https://docs.npmjs.com/cli/dist-tag).
72240
The dist-tags follow the naming convention `legacy-(version)`.
73241

packages/google-cloud-datacatalog/protos/protos.d.ts

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)