Unable to add Environment to Team

(Stan) #1

Hi There,

We have on-prem hosting and currently we use 2019.9.3 LTS

Just background for our environments….

We are using Azure IaaS to create environments for testing purposes (e.g. DEV,TEST, UAT etc)

Each environment contains set of servers (e.g. WEB,DB etc.)

Every environment set created on demand, used and destroyed at the end of the SDLS

We have script automation to create Octopus Deploy targets, environments and lifecycles based on every new created Azure environment. At the end during Azure environment decommission we delete all Octopus Deploy resources related to this environment

During lifecycle creation we add static environments (e.g. PREPROD, PROD etc.) that hosted on-prem

For automation we use Octopus.Client from powershell script

We have different roles and teams to control access to some environments.

Prior “Spaces” we used Octopus.Client API to assign allowed environments to teams and removed them during decommissioning:

$team = $repository.Teams.FindByName(“TEAM NAME”)
$team.Environments.Add/Remove($environmentId)
$repository.Teams.Modify($team)

But after “Spaces” implementation we found that “team” doesn’t have anymore “Environments” property

This is affects our automation and maintenance.

I did a lot of research on the web, even had some communication on Octopus Deploy forum but still no luck.

So, I have few questions:

  1. How to implement previous functionality with new Octopus.Client (v 7.1.5.0) or
  2. How to grant permission for the team for all environments except some particular environments

Thanks in advance,

Stan

(Michael Richardson) #3

Hey Stan, I’ve replied to your email :slight_smile:

For anyone else stumbling upon this thread…

The scopes (projects, environments, etc) which were previously applied to teams have been moved to the roles which belong to a team. This allows teams in Octopus to better represent real-world teams.
The specific class you are looking for is ScopedUserRoleResource. This class has a TeamId property and a UserRoleId property which links a team to its roles. It also contains an EnvironmentIds collection.

So your code would become something like (I apologize if this isn’t exactly correct):

$deployRole=$repository.UserRoles.FindByName("Project deployer")
$team=$repository.Teams.FindByName("TEAM NAME") $deployerScopedRole = $repository.Teams.GetScopedUserRoles($team) | where {$_.UserRoleId -eq $deployRole.Id}
$deployerScopedRole.EnvironmentIds.Add($environmentId)

The sample above assumes you are setting the environment scoping for the Project deployer role. This will need to be done for each role you are interested in.

(Stan) #4

Thanks Michael !!!
Work like a charm !!!