async function performTableMetadataUpdate()

in api/v1/src/lib/appliers/bigQueryApplier.js [152:222]


async function performTableMetadataUpdate(projectId, datasetId, tableId, accounts) {
    console.log(`Begin metadata update for table: ${datasetId}.${tableId}`);
    const bigqueryUtil = new BigQueryUtil(projectId);
    const exists = await bigqueryUtil.tableExists(datasetId, tableId);
    if (!exists) {
        console.warn(`Skipping metadata update for non-existant table: ${datasetId}.${tableId}`);
        return false;
    }
    const viewerRole = await runtimeConfig.bigQueryDataViewerRole(projectId);
    let isDirty = false;
    const tablePolicy = await bigqueryUtil.getTableIamPolicy(projectId, datasetId, tableId);
    let readBinding = {};
    let bindingExists = false;
    if (tablePolicy.bindings) {
        const viewerRoleBinding = underscore.findWhere(tablePolicy.bindings, { role: viewerRole });
        if (viewerRoleBinding) {
            readBinding = viewerRoleBinding;
            bindingExists = true;
            let i = readBinding.members.length;
            while (i--) {
                let member = readBinding.members[i];
                let arr = member.split(':');
                let type = arr[0];
                let email = arr[1];
                if (cfg.managedIamAccessTypes.includes(type)) {
                    const shouldHaveAccess = underscore.findWhere(accounts, { email: email, emailType: type });
                    if (!shouldHaveAccess) {
                        console.log(`Deleting user: ${type}:${email} from table: ${datasetId}.${tableId}`);
                        readBinding.members.splice(i, 1);
                        isDirty = true;
                    }
                }
            }
        }
    } else {
        tablePolicy.bindings = [];
    }

    if (!bindingExists) {
        readBinding.role = viewerRole;
        readBinding.members = [];
        tablePolicy.bindings.push(readBinding);
    }

    accounts.forEach(account => {
        if (account.email && account.emailType) {
            const identifier = `${account.emailType}:${account.email}`;
            const accessRecordExists = readBinding.members.includes(identifier);
            if (!accessRecordExists) {
                readBinding.members.push(identifier);
                isDirty = true;
                console.log(`Adding access record to tableId: ${datasetId}.${tableId}: ${JSON.stringify(account)}`);
            }
        }
    });

    if (isDirty === true) {
        try {
            await bigqueryUtil.setTableIamPolicy(projectId, datasetId, tableId, tablePolicy, 'bindings');
            console.info(`Policy set successfully for table '${datasetId}'`);
        } catch (err) {
            console.error(`Failed to set policy for table '${datasetId}.${tableId}' with error '${err}' and payload: ${JSON.stringify(tablePolicy)}`);
            throw err;
        }
    } else {
        console.info(`Metadata is already up to date for table: '${datasetId}.${tableId}'`);
    }

    console.log(`End metadata update for table: ${datasetId}.${tableId}`);
    return isDirty;
}