Sample: Adding Tags to Content
This sample illustrates how to add a Migrated
tag to both data sources and workbooks.
Both the Python and C# transformer classes inherit from a base class that handles the core functionality, then create versions for IPublishableWorkbook
and IPublishableDataSource
.
To implement the tag addition in Python, you can utilize the following transformer class:
from typing import TypeVar
from tableau_migration import (
ContentTransformerBase,
IDataSource,
ITag,
IWorkbook)
T = TypeVar("T")
class MigratedTagTransformer(ContentTransformerBase[T]):
def transform(self, itemToTransform: T) -> T:
tag: str = "Migrated"
itemToTransform.tags.append(ITag(tag))
return itemToTransform
class MigratedTagTransformerForDataSources(MigratedTagTransformer[IDataSource]):
pass
class MigratedTagTransformerForWorkbooks(MigratedTagTransformer[IWorkbook]):
pass
Registration
plan_builder.transformers.add(MigratedTagTransformerForDataSources)
plan_builder.transformers.add(MigratedTagTransformerForWorkbooks)
See hook registration for more details.
In C#, the transformer class for adding tags is implemented as shown below:
public class MigratedTagTransformer<T> : ContentTransformerBase<T> where T : IContentReference, IWithTags
{
private readonly ILogger<IContentTransformer<T>>? _logger;
public MigratedTagTransformer(ISharedResourcesLocalizer localizer, ILogger<IContentTransformer<T>> logger) : base(localizer, logger)
{
_logger = logger;
}
public override async Task<T?> TransformAsync(T itemToTransform, CancellationToken cancel)
{
var tag = "Migrated";
// Add the tag to the content item.
itemToTransform.Tags.Add(new Tag(tag));
_logger?.LogInformation(
@"Added ""{Tag}"" tag to {ContentType} {ContentLocation}.",
tag,
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<MigratedTagTransformer<IPublishableDataSource>, IPublishableDataSource>();
_planBuilder.Transformers.Add<MigratedTagTransformer<IPublishableWorkbook>, IPublishableWorkbook>();
Dependency Injection
Learn more about dependency injection here.
services.AddScoped<MigratedTagTransformer<IPublishableDataSource>>();
services.AddScoped<MigratedTagTransformer<IPublishableWorkbook>>();