How to get a list of Deployment Targets and Environments for specific Tenant in PowerShell Step?

I have tenanted environments and deployments.

Lets say I have:

  • ProjectA

  • TenantA which is connected to ProjectA

  • DeploymentTarget1 with IP 10.10.10.1 with role AppServer and for TenantA for ENV TEST

  • DeploymentTarget2 with IP 10.10.10.2 with role AppServer and for TenantA for ENV DEV

  • DeploymentTarget3 with IP 10.10.10.3 with role AppServer and for TenantA for ENV PROD

  • DeploymentTarget4 with IP 10.10.10.4 with role WebServer and for TenantA for ENV UAT

  • DeploymentTarget5 with IP 10.10.10.5 with role WebServer and for TenantA for ENV PROD

So, once I am doing the deployment of ProjectA to DeploymentTarget4 to UAT I should somehow get a list of all deployment targets with role AppServers where Environment is equal to TEST or DEV, using preferably some Octopus parameters to retrieve those values where the link is that it is the same Tenant, Environments are in (TEST, DEV) and Role of the target is (AppServer)
So in my PowerShell script I want to retrieve those values for that specific TenantA:
**Environment TEST, target: 10.10.10.1 **
Environment DEV, target: 10.10.10.2
I need this because I need to propagate this kind of information further more in my installation.

In similar example, once I am doing the deployment of ProjectA to DeploymentTarget5 to PROD I should somehow get a list of all deployment targets with role AppServers where Environment is equal to PROD, using preferably some Octopus parameters to retrieve those values where the link is that it is the same Tenant, Environments is (PROD) and Role of the target is (AppServer.)
So in my PowerShell script I want to retrieve those values for that specific TenantA:
Environment PROD, target: 10.10.10.3.

I hope it is clear.

Are there specific Octopus Parameters, System Variables which can help to retrieve this kind of information in my PowerShell script for current deployment?

Thanks!

Hi @veljkosbbb,

I think the best way to do this would be via API calls. If you do an API call to /api/machines/all, each machine will have all of the information that you are looking for as part of the JSON machine list. You could iterate through the list of machines matching the desired parameters and create an array of the data you need. You could grab the Tenant ID from Octopus.Deployment.Tenant.Id. From there you will just need to match the Tenant ID, Check the Environment and Role with some if/and/or logic, then build out your array to do what you need to with it.

You can actually see every variable(including system variables) available to you in a step in a deployment by enabling Variable Logging. While this tool is incredibly useful for setting something up, it’s advised to turn it off once you are finished as the logs it creates are very big and it can slow your deployments down. Here is a list of our System Variables as well in case you need to reference that to build your logic.

If you need to see what API calls are available to you, you just need to add /swaggerui to the end of your octopus url in a web browser and it will take you to the SwaggerUI which has all the API calls. If you insert your API keys at the top where it says Authorize, you can run API calls and look at the results and get an idea on how to build this out. For what it’s worth I believe all the information you need to do this is inside of /api/machines/all. Combined with variable logging to see how to reference the space/tenant/environment Id for matching against that JSON should get you where you need to go.

Here is a repo with a bunch of API samples. You may be able to find something there to help you if you get stuck.

Please let me know if this works for you or if you need any further assistance or help.

Thanks,
Jeremy

1 Like

Thanks Jeremy.

This is pretty cool, I managed with two API calls to resolve the whole mine complexity!

1 Like

Hi @veljkosbbb,

That’s great news, I’m glad you got it working. Thanks for updating me and have a great weekend.

Thanks,
Jeremy

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