This sample demonstrates how to encrypt workbook and data source extracts, irrespective of their original state.
Both the Python and C# transformer classes inherit from a base class responsible for the core functionality, then generate versions for IPublishableWorkbook
and IPublishableDataSource
.
To encrypt extracts in Python, you can use the following transformer class:
from typing import TypeVar
from tableau_migration import (
ContentTransformerBase,
IPublishableWorkbook,
IPublishableDataSource)
T = TypeVar("T")
class EncryptExtractTransformer(ContentTransformerBase[T]):
def transform(self, itemToTransform: T) -> T:
itemToTransform.encrypt_extracts = True
return itemToTransform
class EncryptExtractTransformerForDataSources(EncryptExtractTransformer[IPublishableDataSource]):
pass
class EncryptExtractTransformerForWorkbooks(EncryptExtractTransformer[IPublishableWorkbook]):
pass
Registration
plan_builder.transformers.add(EncryptExtractTransformerForDataSources)
plan_builder.transformers.add(EncryptExtractTransformerForWorkbooks)
See hook registration for more details.
In C#, the transformer class for encrypting extracts is implemented as follows:
public class EncryptExtractsTransformer<T> : ContentTransformerBase<T> where T : IContentReference, IFileContent, IExtractContent
{
private readonly ILogger<IContentTransformer<T>>? _logger;
public EncryptExtractsTransformer(ISharedResourcesLocalizer localizer, ILogger<IContentTransformer<T>> logger) : base(localizer, logger)
{
_logger = logger;
}
public override async Task<T?> TransformAsync(T itemToTransform, CancellationToken cancel)
{
itemToTransform.EncryptExtracts = true;
_logger?.LogInformation(
@"Setting encrypt extract to true for {ContentType} {ContentLocation}",
typeof(T).Name,
itemToTransform.Location);
return await Task.FromResult(itemToTransform);
}
public async Task<IPublishableWorkbook?> TransformAsync(IPublishableWorkbook ctx, CancellationToken cancel)
=> await TransformAsync(ctx, cancel);
public async Task<IPublishableDataSource?> TransformAsync(IPublishableDataSource ctx, CancellationToken cancel)
=> await TransformAsync(ctx, cancel);
}
Registration
To register the transformer in C#, follow the guidance provided in the documentation.
_planBuilder.Transformers.Add<EncryptExtractsTransformer<IPublishableDataSource>, IPublishableDataSource>();
_planBuilder.Transformers.Add<EncryptExtractsTransformer<IPublishableWorkbook>, IPublishableWorkbook>();
Dependency Injection
Learn more about dependency injection here.
services.AddScoped<EncryptExtractsTransformer<IPublishableDataSource>>();
services.AddScoped<EncryptExtractsTransformer<IPublishableWorkbook>>();