I’m trying to create a tenant-copy script in .NET, which works fine apart from the tags.
When I retrieve the tags from the source tenant, I get a ReferenceCollection back:
var refColTags = template_tenant.TenantTags;
When I retrieve one of the items from this collection, I get a string back:
var tag_str = refColTags.ElementAt(q);
It contains the tag-set name and the name of the tag itself, e.g. “Hosting/Staging env”
Now, when I want to add this tag to the new tenant, all I can use is:
new_tenant.WithTag(tagResourceObject);
But I cannot retrieve the tagResource object from the repository through the name of the tag I received! There is no repository.TagResource let along repository.TagResource .FindByName(“foobar/bla”)
It looks like I will have to:
strip the tag string up to the “/” and only keep “Hosting”,
retrieve the matching TagSetResource (which is available through repository.TagSetResource.FindByName()),
walk through the set of TagResources that are returned
when I finally find the matching TagResource, add it to the new tenant.
I think your assessment is correct, you need to go through your tag sets and find the matching tags in order to get a TagResource. Here is how I would approach the problem:
var templateTenant = repository.Tenants.FindByName("Template tenant");
var templateTags = templateTenant.TenantTags;
var newTenant = repository.Tenants.CreateOrModify("New tenant");
var tagSets = repository.TagSets.GetAll();
var tags = tagSets.SelectMany(tagSet => tagSet.Tags.Where(tag => templateTags.Contains(tag.CanonicalTagName)));
foreach (var tag in tags) {
newTenant.WithTag(tag);
}
newTenant.Save();
On the same branch, I’m now trying to set the common variables of the new (target) tenant, according to the template (source) tenant’s common variables. The only thing: I also want to adjust these common variables before storing them into the target-tenant.
Here’s what I have so far. I can see each id and value of the variables of the source tenant.
TenantResource source_tenant = repository.Tenants.FindByName(template_tenant_name);
TenantResource target_tenant = repository.Tenants.FindByName(new_tenant_name);
TenantVariableResource list = repository.Tenants.GetVariables(source_tenant);
foreach (KeyValuePair<string, TenantVariableResource.Library> kvp in list.LibraryVariables)
{
foreach (KeyValuePair<string, PropertyValueResource> varkvp in kvp.Value.Variables)
{
string variable_id = varkvp.Key;
PropertyValueResource propvalres = varkvp.Value;
// Now set these variables in the target_tenant..!?
// Can we use: repository.Tenants.ModifyVariables(target_tenant, list);
}
}
With the variable’s id I would like to retrieve the name of the variable and then -according to the name- adjust the value and store it as one of the target tenant’s common variables.
I’m stuck at this point. I can’t seem to retrieve the variableResource with the ids I found, and I can’t store them into the target tenant. #sob Can you help me out?