2

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="sample.bigquery.dev@gmail.com",
)

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?

2 Answers2

3

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 = "sample.bigquery.dev@gmail.com"
});
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 = "sample.bigquery.dev@gmail.com"
};
resource.Access.Add(newAccess);

// Push the changes back up to BigQuery
dataset.Update();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hello Jon Skeet, Thank you very much for your advice. I think your solution is good. Unfurtunatelly I didn't read it before, but I have implemented something very similar and it worked. The only difference is that I use the Patch function insead of Update. I will publish my method. – Clemente Madrassi Mar 09 '20 at 09:31
  • @ClementeMadrassi: Note the first of my examples uses Patch too. – Jon Skeet Mar 09 '20 at 09:40
  • @Debasish22: Please don't use comments on one post to get attention to a different one. I'll have a look now, but it's really not an appropriate use of comments. – Jon Skeet Mar 05 '22 at 17:15
  • @JonSkeet sure . Sorry about that – Debasish22 Mar 06 '22 at 06:08
0

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;
    }