From 380c4d8dc7c3ad2d20ef7c35cae05a777ceb027c Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Tue, 17 Dec 2019 16:07:36 -0300 Subject: [PATCH] Add delete fileset Entry sample --- datacatalog/quickstart/deleteFilesetEntry.js | 79 +++++++++++++++++++ datacatalog/quickstart/package.json | 22 ++++++ .../system-test/deleteFilesetEntry.test.js | 67 ++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 datacatalog/quickstart/deleteFilesetEntry.js create mode 100644 datacatalog/quickstart/package.json create mode 100644 datacatalog/quickstart/system-test/deleteFilesetEntry.test.js diff --git a/datacatalog/quickstart/deleteFilesetEntry.js b/datacatalog/quickstart/deleteFilesetEntry.js new file mode 100644 index 0000000000..ff71f3a023 --- /dev/null +++ b/datacatalog/quickstart/deleteFilesetEntry.js @@ -0,0 +1,79 @@ +/* eslint-disable no-warning-comments */ + +// Copyright 2019 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'; + +/** + * This application demonstrates how to delete a fileset Entry and an + * Entry Group with the Cloud Data Catalog API. + + * For more information, see the README.md under /datacatalog and the + * documentation at https://cloud.google.com/data-catalog/docs. + */ +const main = async ( + projectId = process.env.GCLOUD_PROJECT, + entryGroupId, + entryId +) => { + // [START datacatalog_delete_fileset_quickstart_tag] + // ------------------------------- + // Import required modules. + // ------------------------------- + const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1; + const datacatalog = new DataCatalogClient(); + + // ------------------------------- + // Currently, Data Catalog stores metadata in the + // us-central1 region. + // ------------------------------- + const location = 'us-central1'; + + // TODO(developer): Uncomment the following lines before running the sample. + // const projectId = 'my-project' + // const entryGroupId = 'my-entry-group' + // const entryId = 'my-entry' + + // ------------------------------- + // 1. Delete a Fileset Entry. + // ------------------------------- + const formattedName = datacatalog.entryPath( + projectId, + location, + entryGroupId, + entryId + ); + await datacatalog.deleteEntry({name: formattedName}); + console.log(`Deleted entry ${formattedName}`); + + // ------------------------------- + // 2. Delete an Entry Group. + // ------------------------------- + try { + const formattedName = datacatalog.entryGroupPath( + projectId, + location, + entryGroupId + ); + await datacatalog.deleteEntryGroup({name: formattedName}); + console.log(`Deleted entry group ${formattedName}`); + } catch (err) { + console.log('Entry Group does not exist or is not empty.'); + } +}; +// [END datacatalog_delete_fileset_quickstart_tag] + +// node deleteFilesetEntry.js +main(...process.argv.slice(2)); diff --git a/datacatalog/quickstart/package.json b/datacatalog/quickstart/package.json new file mode 100644 index 0000000000..ddadf932fc --- /dev/null +++ b/datacatalog/quickstart/package.json @@ -0,0 +1,22 @@ +{ + "name": "@google-cloud/datacatalog-quickstart", + "version": "0.0.1", + "private": true, + "description": "Quickstart guides for the Cloud Data Catalog client library for Node.js", + "license": "Apache-2.0", + "author": "Google LLC", + "repository": "GoogleCloudPlatform/nodejs-docs-samples", + "engines": { + "node": ">=8.0.0" + }, + "scripts": { + "test": "mocha system-test/*.test.js --timeout=60000" + }, + "devDependencies": { + "mocha": "^6.0.0", + "uuid": "^3.1.0" + }, + "dependencies": { + "@google-cloud/datacatalog": "^1.5.0" + } +} diff --git a/datacatalog/quickstart/system-test/deleteFilesetEntry.test.js b/datacatalog/quickstart/system-test/deleteFilesetEntry.test.js new file mode 100644 index 0000000000..b92149f944 --- /dev/null +++ b/datacatalog/quickstart/system-test/deleteFilesetEntry.test.js @@ -0,0 +1,67 @@ +// Copyright 2019 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 uuid = require('uuid'); +const cwd = path.join(__dirname, '..'); +const {exec} = require('child_process'); + +const projectId = process.env.GCLOUD_PROJECT; +const location = 'us-central1'; +// Use unique id to avoid conflicts between concurrent test runs +const entryGroupId = `fileset_entry_group_${uuid.v4().substr(0, 8)}`; +const entryId = `fileset_entry_id_${uuid.v4().substr(0, 8)}`; + +const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1; +const datacatalog = new DataCatalogClient(); + +before(() => { + assert( + process.env.GCLOUD_PROJECT, + 'Must set GCLOUD_PROJECT environment variable!' + ); + assert( + process.env.GOOGLE_APPLICATION_CREDENTIALS, + 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!' + ); +}); + +describe('deleteFilesetEntry', () => { + before(done => { + // Must create an entry to be deleted. + exec( + `node createFilesetEntry.js ${projectId} ${entryGroupId} ${entryId}`, + {cwd}, + done + ); + }); + + it('should delete a fileset entry', done => { + exec( + `node deleteFilesetEntry.js ${projectId} ${entryGroupId} ${entryId}`, + {cwd}, + (err, stdout) => { + assert.ok( + stdout.includes( + datacatalog.entryGroupPath(projectId, location, entryGroupId) + ) + ); + done(); + } + ); + }); +});