Getting Tenants with Octopus.Client

I’m looking to get the tenants using Octopus.Client. I previously asked a similar question but then I was working on Octopus 2018.8.8.

Now trying to do the same on a newer release 2021.3 (build 8275) and it’s giving me fits. Octopus.Client is reporting 13.0.3912.0.

When I do the following:

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint($server, $apiKey)
$client = New-Object Octopus.Client.OctopusClient($endpoint)
$tenants = $client.Repository.Tenants.FindAll()

I get ‘cannot call a method on a null-valued expression’

If I try

$client = New-Object Octopus.Client.OctopusRepository($endpoint)
$tenants = $client.Tenants.FindAll()

Then $tenants comes back empty.

Any suggestions appreciated.

Jamie

Hi @jamie.sidoti

Thanks for getting in touch, the jump to 2018.x to 2021.3 of Octopus introduced a number of new concepts; one of which was Spaces.

I tested a script to retrieve tenants below which caters for the default space, which was added to all Octopus instances running 2019.x or greater:

# You can get this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path "C:\octopus.client.13.0.3790\lib\net452\Octopus.Client.dll"

# Octopus variables
$octopusURL = "https://your.octopus.app"
$octopusAPIKey = "API-KEY"
$spaceName = "Default"

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURL, $octopusAPIKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$client = New-Object Octopus.Client.OctopusClient $endpoint

try
{
    # Get space
    $space = $repository.Spaces.FindByName($spaceName)
    $repositoryForSpace = $client.ForSpace($space)

    # Get tenants
    $tenants = $repositoryForSpace.Tenants.FindAll()
    $tenants | Format-Table
}
catch
{
    Write-Host $_.Exception.Message
}

You’ll need to change the instance URL and API key etc, but it should give you a good starting point.

I hope that helps!

Mark,

Thanks for the suggestions. I downloaded the Octopus.Client.dll 13.0.3790 version from

Running the supplied code step-by-step is working until I get to:

$space = $repository.Spaces.FindByName($spaceName)

where I get

You cannot call a method on a null-valued expression.
At line:1 char:1
+ $space = $repository.Spaces.FindByName($spaceName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

My $spaceName = “Default” (also tried our other space names with same result).

My API key was generated for a User who is a member of a Team with with the System Administrator and Space Manager user roles (Space Manager scoped to each of our three spaces, all project groups, projects, environments and tenants). Am I missing something?

Thanks.

Hi @jamie.sidoti

So slightly embarrassingly I edited the script to remove the following line (before the try catch)

$repository = New-Object Octopus.Client.OctopusRepository

I did this as I thought it wasn’t being used.

That’s why you are getting the error as you can’t call the Spaces property on the $repository object if it’s not there. In the words of Homer Simpson, doh!

I’ve since reversed the original script to the first revision that included the correct line back in place.

Can you run it again with the line above in and see if it works for you then?

My apologies for the inconvenience.

Best,

Mark,

Yes, that modification got me going. I was then able figure out how change my old (pre-spaces) script to be able to save an existing tag to a subset of the returned $tenants.

I’m still wrapping my head around the spaces changes and I appreciate your help.

Thanks!

Jamie

Hi @jamie.sidoti

Awesome, that’s good to hear.

Thanks for letting me know!

Best,