Clemente Madrassi
Clemente Madrassi

Reputation: 21

Updating BigQuery dataset access from C#

I am working with BigQuery. I create a DataSet and I want to define access rights with C# language. It's not clear to me how to do it. In GOOGLE web page https://cloud.google.com/bigquery/docs/dataset-access-controls is explained how to do it with some example for Java and Pyton (see below), but no example is provided for c#.

example in pyton:
dataset = client.get_dataset(dataset_id)  # Make an API request.

entry = bigquery.AccessEntry(
    role="READER",
    entity_type="userByEmail",
    entity_id="[email protected]",
)

entries = list(dataset.access_entries)
entries.append(entry)
dataset.access_entries = entries

dataset = client.update_dataset(dataset, ["access_entries"])  # Make an API request.

full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id)

Can anybody help please?

Upvotes: 2

Views: 619

Answers (2)

Clemente Madrassi
Clemente Madrassi

Reputation: 21

At the and I managed to make it work. I used the same solution suggested by Jon Skeet, using the Patch method. I attach my code here by.

public static bool GrantDatasetAccess(string dataSetId, bool online, string role, string email, string bigQueryJsonPath, string bigQueryScope, string projectId, clsLog log) {

        try
        {
            BigQueryClient client = GetBigQueryClient(online, bigQueryJsonPath, bigQueryScope, projectId);
            BigQueryDataset dataset = GetDataSet(online, dataSetId, bigQueryJsonPath, bigQueryScope, projectId, log);

            List<AccessData> accessList = new List<AccessData>();
            var accessData = new AccessData()
            {
                Role = role,
                GroupByEmail = null,
                UserByEmail = email,
                SpecialGroup = null,
                IamMember = null,
                Domain = null,
                View = null

            };
            accessList.Add(accessData);
            dataset.Resource.Access = accessList;
            dataset.Patch(dataset.Resource, true);
        }
        catch (Exception e)
        {
            log.ManageError("Error GetDataSet: {0}\nError: {1}", dataSetId, string.Concat(e.Message, "\n", e.StackTrace));
        }
        return true;
    }

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502286

It's probably best to use the BigQueryDataset.Patch method, from the Google.Cloud.BigQuery.V2 package:

// Fetch the existing dataset
var client = BigQueryClient.Create(projectId);
var dataset = client.GetDataset(datasetId);

var accessList = dataset.Resource.Access ?? new List<AccessData>();
accessList.Add(new AccessData
{
    Role = "Reader",
    UserByEmail = "[email protected]"
});
var patchedResource = new Dataset { Access = accessList };

// Push the changes back up to BigQuery
dataset.Patch(patchedResource, matchETag: true);

As an alternative, you can use Update to update the replace the dataset resource completely:

// Fetch the existing dataset
var client = BigQueryClient.Create(projectId);
var dataset = client.GetDataset(datasetId);

// Modify it in memory
var resource = dataset.Resource;
if (resource.Access is null)
{
    // If there's no access list yet, create one.
    resource.Access = new List<AccessData>();
}
var newAccess = new AccessData
{
    Role = "Reader",
    UserByEmail = "[email protected]"
};
resource.Access.Add(newAccess);

// Push the changes back up to BigQuery
dataset.Update();

Upvotes: 3

Related Questions