How to update tenant project variables via octopus.client

I’m trying to set a tenant’s variables via the client/API. I’ve gotten this far before things go sideways:

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint ($octoserver, $key)
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$tenant = $repository.Tenants.FindByName($tenantname)

$variableset = $repository.VariableSets.Get($tenant.links.variables)

I’m getting back the variable set but it doesn’t appear to have any variables (or I did something wrong). I have the specific name of each variable I wish to set. How can I access the variable, set the value, and update the variable set?

Hi,

Thanks for getting in touch! Tenant’s variables are accessible via Tenants property. If you replace the last line of your script with the following line then everything should work as expected.
$variables = $repository.Tenants.GetVariables($tenant)

Please, let me know if this doesn’t solve your problem.

Regards

Pawel

That gets me a TenantVariableResource which doesn’t have the actual variables I wish to modify in the collection so no, that most definitely does not get me anywhere close to what I need. Frankly, your api and library are completely unfriendly. I see a ProjectVariables dictionary and I can find an ActionTemplateParameterResource object with the variable name I want but updating the DefaultValue property (which looks suspect to me anyway) does nothing (after updating the updated tenant via the repository).

Please give me a better answer.

I have setup a Project Variable Template with a variable “foo” per http://docs.octopusdeploy.com/display/OD/Working+with+tenant-specific+variables . I wish to use the api to set this variable as part of my automated deployment. Please show me how to do this in detail; a gist would be great.

BTW - if you sense a bit of frustration with your api/documentation in my tone you are absolutely correct.

I found a way to do this but it was anything but straightforward. This is working but wow.

Here’s the dirty version against a test tenant with a single project template variable:

var endpoint = new OctopusServerEndpoint(server, key);
var repository = new OctopusRepository(endpoint);
var tenant = repository.Tenants.FindByName("MyTenant");
var tenantVariableResource = repository.Tenants.GetVariables(tenant);
var property = new PropertyValueResource("it works");
var id = tenantVariableResource.ProjectVariables["MyProject"].Templates.FirstOrDefault().Id;
var projectVariables = tenantVariableResource.ProjectVariables["MyProject"].Variables["MyEnvironment"];
if (projectVariables.ContainsKey(id)) projectVariables.Remove(id);
projectVariables.Add(id, property);
repository.Tenants.ModifyVariables(tenant, tenantVariableResource)

Hi,

I’m glad you managed to come up with a solution. Our .NET API has still a few rough edges and we will be taking care of them as we discover them. Thank you for getting back to me with your solution. It will help us make the API better in the future. I created an issue so you track progress of this improvement.

Regards

Pawel