async shareAuthorizeView()

in shared/bigqueryUtil.js [429:494]


    async shareAuthorizeView(sourceDatasetId, authorizeProject, authorizeDataset, authorizeView, recreateAuthorization) {
        console.log(`Authorizing dataset '${sourceDatasetId}' granting object '${authorizeProject}.${authorizeDataset}.${authorizeView}' access`);

        // We need to remove any views for which the authorized views no longer exist, otherwise we'll run into an exception when saving
        let metadata = await this.getDatasetMetadata(sourceDatasetId);
        let isViewAlreadyAdded = false;
        if (metadata.access) {
            let updatedRequired = false;
            // Remove and save authorized view
            let i = metadata.access.length;
            while (i--) {
                let a = metadata.access[i];
                if (a.view && a.view.projectId && a.view.datasetId && a.view.tableId) {
                    // If there is already an entry for the view that we're authorizing, it's stale and we need to remove it, and re-add it
                    if (a.view.projectId.toLowerCase() === authorizeProject.toLowerCase() && a.view.datasetId.toLowerCase() === authorizeDataset.toLowerCase() && a.view.tableId.toLowerCase() === authorizeView.toLowerCase()) {
                        if (recreateAuthorization) {
                            // Remove from array
                            updatedRequired = true;
                            metadata.access.splice(i, 1);

                            if (this.VERBOSE_MODE) {
                                console.log(`Removing authorization in dataset '${sourceDatasetId}' granting object '${authorizeProject}.${authorizeDataset}.${authorizeView}' access`);
                            }
                        }
                        else {
                            isViewAlreadyAdded = true;

                            if (this.VERBOSE_MODE) {
                                console.log(`Authorization in dataset '${sourceDatasetId}' granting object '${authorizeProject}.${authorizeDataset}.${authorizeView}' access already exists`);
                            }
                        }
                    }
                    else {
                        // Remove authorized views for which are no longer valid, otherwise there will be an error saving
                        const _viewExists = await this.viewExists(a.view.datasetId, a.view.tableId);
                        if (_viewExists === false) {
                            console.log(`View '${a.view.datasetId}.${a.view.tableId}' is no longer valid, will remove it from the authorized view list`);
                            updatedRequired = true;
                            metadata.access.splice(i, 1);
                        }
                        else {
                            console.log(`Authorized view '${a.view.datasetId}.${a.view.tableId}' in dataset '${sourceDatasetId}' is still valid`);
                        }
                    }
                }
            }

            if (updatedRequired === true) {
                console.log(`Updating authorized view records in dataset '${sourceDatasetId}'`);
                await this.setDatasetMetadata(sourceDatasetId, metadata);

                // We have to reload the metadata again before editing again.
                metadata = await this.getDatasetMetadata(sourceDatasetId);
            }
        }
        else {
            console.log("metadata.access array does not exist, creating now.");
            metadata.access = [];
        }

        if (!isViewAlreadyAdded) {
            metadata.access.push({ "view": { "projectId": authorizeProject, "datasetId": authorizeDataset, "tableId": authorizeView } });
            await this.setDatasetMetadata(sourceDatasetId, metadata);
            console.log(`Changes applied to authorize view '${authorizeProject}.${authorizeDataset}.${authorizeView}' in dataset '${sourceDatasetId}'`);
        }
    }