Object Reference Error When Adding Variable via API

Hello,

Our company has been a long term user of Octopus for our deployments and heavily use the API to interact with the server via Octopus.Client. We have recently gotten a brand new Octopus server on a newer version. Our previous version was version 3.3.27 and our new version is 2020.5.249.

We are testing out the API methods we previously used on the new server and found that we are no longer able to add variables to a library set. When doing so we receive the following error.

Error: Exception calling "Modify" with "1" argument(s): "Octopus Server returned an error: Object reference not set to an instance of an object."

Code Snippet

Add-Type -Path '.\lib\net452\Octopus.Client.dll';

$apikey = 'API-KEY' # Get this from your profile
$octopusURI = 'OCTO-SERVER' # Your server address

$libraryVariableSetId = "LibraryVariableSets-21" # Get this from /api/libraryvariablesets
$variableName = "FirstName" # Name of the new variable
$variableValue = "Michael" # Value of the new variable

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$libraryVariableSet = $repository.LibraryVariableSets.Get($libraryVariableSetId);
$variables = $repository.VariableSets.Get($libraryVariableSet.VariableSetId);

$myNewVariable = new-object Octopus.Client.Model.VariableResource
$myNewVariable.Name = $variableName
$myNewVariable.Value = $variableValue

$variables.Variables.Add($myNewVariable)
$repository.VariableSets.Modify($variables)

I even created a demo Octopus server on the absolutely newest version (Version 2021.1 Build 7236) and still got the same error.

Is this a known bug and if not what am I doing wrong here? Did the API syntax to add variables change?

Thanks,
Mike

Hi Mike,

Thanks for reaching out!

Great to hear you’re a long time user, sorry that you’re having troubles after the latest upgrade. I’d be happy to help figure out what’s going on here.

I’ve tested the script you’ve provided and wasn’t able to reproduce the error, just checking that this is this the only error message that you are seeing? If you modify LibraryVariableSets manually through the UI do you also receive an error? Also, could you please confirm that the Octopus.Client.dll you are using is from the newer version and that it works with other API scripts?

We have seen issues previously with upgrades between large jump in versions; it’s worth checking if any LibraryVariableSets are not mapped correctly to a VariableSet. Could you please execute the following SQL query against the database and let me know if it returns any results? It’s a simple query but please make sure to take a backup of the Database and Masterkey just in case!

SELECT *
FROM LibraryVariableSet
WHERE VariableSetId not in (SELECT id FROM VariableSet)

I’ll keep trying to reproduce the issue on my end and let you know if I require any further info. Feel free to reach out if you have any questions or additional info.

Best Regards,