I’ve got a Tenanted Project that makes use of a Library Variable Set. In the library set the variable contains a default value but as it is a templated variable it can be overridden at the Tenant level, thus making the value unique for each Tenant.
Using PowerShell, the Invoke-RestMethod and a GET I can load the Library Variable and see the value for a particular Tenant. It looks something like:
$y = Invoke-RestMethod -Uri “$OctopusURL/api/tenants/Tenants-1561/variables” -Method GET -Headers $header
Thanks for getting in touch! You certainly can do this, though it’s more complex than it would seem at first glance. Below I’ve pasted a sample script pulled from how we accomplish this internally. I’ve scrubbed some info, and it’s in C# but hopefully this provides a good starting point on approaching this.
var TenantName = Repository.Tenants.Create(new TenantResource()
{
Name = TenantName,
ProjectEnvironments = allProjects.Select(p => p.Id).ToDictionary(projectId => projectId, projectId => new ReferenceCollection(environment.Id)),
TenantTags = new ReferenceCollection(new [] {$"{prefix}{TagSetName}/TenantTagName", "TenantTagSetName/TenantTagName"})
});
var variables = Repository.Tenants.GetVariables(TenantName);
var Instance = variables.LibraryVariables.Single(pair => pair.Value.LibraryVariableSetName == $"{prefix}Instance").Value;
AddValue(variable, "Value", new PropertyValueResource(Value));
Repository.Tenants.ModifyVariables(TenantName, variables);
I appreciate the reply, but I’m not well versed in C# so translating that into something PowerShell is eluding me.
In my OP I was trying to use the API to update a library set variable for a particular Tenant. I’d prefer to use the API so I don’t have to deploy anything else to the remote servers where I’m running my script. (We’re using Octopus Deploy which is very PowerShell oriented.)
$y = Invoke-RestMethod -Uri “$OctopusURL/api/tenants/Tenants-1561/variables” -Method GET -Headers $header
# THIS IS THE VARIABLE I WISH TO UPDATE FOR THIS TENANT
$y.LibraryVariables.‘LibraryVariableSets-322’.Variables.'b5d93262-24d6-4695-a81c-703239dac516’
I tried again, this time using Octopus.Client but I’m kind of stuck at the same point. I can identify the variable I want, I can see the current value for my particular Tenant, but I’m not clear on how to update that variable for the Tenant.
Add-Type -Path 'C:\dev\octopus.client.4.42.6\lib\net45\Octopus.Client.dll'
$apikey = "MYAPIKEY"
$OctopusURI = "https://MYOCTOPUSURL"
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI,$apikey
$repo = new-object Octopus.Client.OctopusRepository $endpoint
$id = "LibraryVariableSets-322"
$varset = $repo.LibraryVariableSets.Get($id)
$tenantName = "MYTENANTNAME"
$tenant = $repo.Tenants.FindByName($tenantName)
$tVars = $repo.Tenants.GetVariables($tenant)
# THIS IS THE VARIABLE I WISH TO UPDATE FOR THIS TENANT
$tVars.LibraryVariables.'LibraryVariableSets-322'.Variables.'b5d93262-24d6-4695-a81c-703239dac516'