I was wondering if there was a way to delete tenants via the Octopus.Client .NET interface?
I’ve created dozens of tenants in a test setup but removing them is a very cumbersome task. Writing a small tool to delete all tenants matching a certain regexp is easy, as long as the interface allows me to delete a tenant.
The Tenants repository in our Octopus.Client package conforms to the IDelete<TenantResponse> interface, so if you have a Tenant resource object, you should be able to call repository.Tenants.Delete(yourTenantResourceHere).
For example
async Task MainAsync()
{
var server = "http://YOUR_OCTOPUS_SERVER";
var apiKey = "API-YOUR_API_KEY";
var tenantNameToDelete = "YOUR_TENANT_NAME";
using(var client = await OctopusAsyncClient.Create(new OctopusServerEndpoint(server, apiKey)))
{
var tenant = await client.Repository.Tenants.FindByName(tenantNameToDelete);
if (tenant == null) {
throw new Exception($"Failed to find tenant with name: {tenantNameToDelete}");
}
await client.Repository.Tenants.Delete(tenant);
Console.WriteLine($"Tenant {tenant.Name} has been successfully deleted.");
}
}
I’ve been able to create my delete-tenant commandline tool that looks for and deletes all expression-matching tenants. Very useful with my tests, in which I’ve created hundreds of tenants.