.NET: no repository.TagResource[.FindByName()] support?

Hi there,

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.

Is there a simpler way to do this?

Thanks for you advice!

Cheers,
Eelko

Hi Eelko,

Thanks for getting in touch.

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();

I hope this helps!

Cheers,
Shane

1 Like

Hi Shane,

That’s a lot shorter. Me like!

I’ll use and study your version.

Thanks!
Eelko

Hi Shane,

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?

Thanks!
Eelko

Hi Eelko,

This is how I would approach adding common variables:

var templateTenant = repository.Tenants.FindByName("Template tenant");
var templateTenantVariables = repository.Tenants.GetVariables(templateTenant);

var newTenant = repository.Tenants.CreateOrModify("New tenant");
var newTenantVariables = repository.Tenants.GetVariables(newTenant.Instance);

foreach (var variableSetValue in templateTenantVariables.LibraryVariables.Values)
{
	var variableSet = repository.LibraryVariableSets.Get(variableSetValue.LibraryVariableSetId);

	foreach (var variable in variableSetValue.Variables)
	{
		var variableTemplate = variableSet.Templates.Single(t => t.Id.Equals(variable.Key));
		Console.WriteLine($"{variableTemplate.Name}: {variable.Value.Value}");
		newTenantVariables.LibraryVariables[variableSetValue.LibraryVariableSetId].Variables[variable.Key] = $"New value for {variableTemplate.Name}";
	}	
}
repository.Tenants.ModifyVariables(newTenant.Instance, newTenantVariables);

When you have finished your tenant cloning script would you mind sharing it so others can benefit? I would like to add it to https://github.com/OctopusDeploy/OctopusDeploy-Api.

Cheers,
Shane

Thanks again, Shane. You saved the world again.

I’ve shared it with you in a personal thread, so you can add it to the repository.

Cheers & thanks & happy waves to other Octopus power users,
Eelko

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.