Update Tenant Variable that comes from a Library Set?

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

$y.LibraryVariables.‘LibraryVariableSets-322’.Variables.'b5d93262-24d6-4695-a81c-703239dac516’

Can I update the value of this Library Variable for this Tenant only using a PUT? I haven’t been able to work out the syntax.

Thanks.

Hi,

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

And secondly, to just add a value:

private void AddValue(TenantVariableResource.Library library, string templateName, PropertyValueResource value)
        {
            var template = library.Templates.Single(t => t.Name == templateName);
            library.Variables.Add(template.Id, value);
        }

I hope this helps get you going! Let me know if you have any questions or concerns moving forward. :slight_smile:

Best regards,

Kenny

Kenny,

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'

Thanks.

Jamie

Finally located a PowerShell example that does what I’m looking for.
Here’s the link for anyone else looking for something similar.

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