|
| 1 | +<details data-group="deactivate-tenant-scripts"> |
| 2 | +<summary>PowerShell (REST API)</summary> |
| 3 | + |
| 4 | +```powershell |
| 5 | +$ErrorActionPreference = "Stop"; |
| 6 | +
|
| 7 | +# Define working variables |
| 8 | +$octopusURL = "https://your-octopus-url" |
| 9 | +$octopusAPIKey = "API-YOUR-KEY" |
| 10 | +$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } |
| 11 | +$tenantName = "MyTenant" |
| 12 | +$tenantEnabled = $true |
| 13 | +
|
| 14 | +# Get space |
| 15 | +$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName} |
| 16 | +
|
| 17 | +# Get tenant |
| 18 | +$tenant = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/tenants/all" -Headers $header) | Where-Object {$_.Name -eq $tenantName} |
| 19 | +
|
| 20 | +# Enable/disable tenant |
| 21 | +$tenant.IsDisabled = !$tenantEnabled |
| 22 | +
|
| 23 | +# Update tenant |
| 24 | +Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/tenants/$($tenant.Id)" -Headers $header -Body ($tenant | ConvertTo-Json -Depth 10) |
| 25 | +``` |
| 26 | + |
| 27 | +</details> |
| 28 | +<details data-group="deactivate-tenant-scripts"> |
| 29 | +<summary>PowerShell (Octopus.Client)</summary> |
| 30 | + |
| 31 | +```powershell |
| 32 | +# Load octopus.client assembly |
| 33 | +Add-Type -Path "c:\octopus.client\Octopus.Client.dll" |
| 34 | +
|
| 35 | +# Octopus variables |
| 36 | +$octopusURL = "https://your-octopus-url" |
| 37 | +$octopusAPIKey = "API-YOUR-KEY" |
| 38 | +$spaceName = "default" |
| 39 | +$tenantName = "MyTenant" |
| 40 | +$tenantEnabled = $true |
| 41 | +
|
| 42 | +$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURL, $octopusAPIKey |
| 43 | +$repository = New-Object Octopus.Client.OctopusRepository $endpoint |
| 44 | +$client = New-Object Octopus.Client.OctopusClient $endpoint |
| 45 | +
|
| 46 | +try |
| 47 | +{ |
| 48 | + # Get space |
| 49 | + $space = $repository.Spaces.FindByName($spaceName) |
| 50 | + $repositoryForSpace = $client.ForSpace($space) |
| 51 | +
|
| 52 | + # Get tenant |
| 53 | + $tenant = $repositoryForSpace.Tenants.FindByName($tenantName) |
| 54 | +
|
| 55 | + # Enable/disable tenant |
| 56 | + $tenant.IsDisabled = !$tenantEnabled |
| 57 | +
|
| 58 | + # Update tenant |
| 59 | + $repositoryForSpace.Tenants.Modify($tenant) |
| 60 | +} |
| 61 | +catch |
| 62 | +{ |
| 63 | + Write-Host $_.Exception.Message |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +</details> |
| 68 | +<details data-group="deactivate-tenant-scripts"> |
| 69 | +<summary>C#</summary> |
| 70 | + |
| 71 | +```csharp |
| 72 | +// If using .net Core, be sure to add the NuGet package of System.Security.Permissions |
| 73 | +#r "path\to\Octopus.Client.dll" |
| 74 | + |
| 75 | +using Octopus.Client; |
| 76 | +using Octopus.Client.Model; |
| 77 | + |
| 78 | +// Declare working variables |
| 79 | +var octopusURL = "https://your-octopus-url"; |
| 80 | +var octopusAPIKey = "API-YOUR-KEY"; |
| 81 | +var spaceName = "default"; |
| 82 | +var tenantName = "MyTenant"; |
| 83 | +bool enabled = false; |
| 84 | + |
| 85 | +// Create repository object |
| 86 | +var endpoint = new OctopusServerEndpoint(octopusURL, octopusAPIKey); |
| 87 | +var repository = new OctopusRepository(endpoint); |
| 88 | +var client = new OctopusClient(endpoint); |
| 89 | + |
| 90 | +try |
| 91 | +{ |
| 92 | + // Get space |
| 93 | + var space = repository.Spaces.FindByName(spaceName); |
| 94 | + var repositoryForSpace = client.ForSpace(space); |
| 95 | + |
| 96 | + // Get tenant |
| 97 | + var tenant = repositoryForSpace.Tenants.FindByName(tenantName); |
| 98 | + |
| 99 | + // Enable/disable tenant |
| 100 | + tenant.IsDisabled = !enabled; |
| 101 | + |
| 102 | + //update tenant |
| 103 | + repositoryForSpace.Tenants.Modify(tenant); |
| 104 | +} |
| 105 | +catch (Exception ex) |
| 106 | +{ |
| 107 | + Console.WriteLine(ex.Message); |
| 108 | + return; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +</details> |
| 113 | +<details data-group="deactivate-tenant-scripts"> |
| 114 | +<summary>Python3</summary> |
| 115 | + |
| 116 | +```python |
| 117 | +import json |
| 118 | +import requests |
| 119 | + |
| 120 | +octopus_server_uri = 'https://your-octopus-url/api' |
| 121 | +octopus_api_key = 'API-YOUR-KEY' |
| 122 | +headers = {'X-Octopus-ApiKey': octopus_api_key} |
| 123 | + |
| 124 | + |
| 125 | +def get_octopus_resource(uri): |
| 126 | + response = requests.get(uri, headers=headers) |
| 127 | + response.raise_for_status() |
| 128 | + |
| 129 | + return json.loads(response.content.decode('utf-8')) |
| 130 | + |
| 131 | + |
| 132 | +def get_by_name(uri, name): |
| 133 | + resources = get_octopus_resource(uri) |
| 134 | + return next((x for x in resources if x['Name'] == name), None) |
| 135 | + |
| 136 | + |
| 137 | +space_name = 'Default' |
| 138 | +tenant_name = 'Your Tenant Name' |
| 139 | +disable_tenant = False |
| 140 | + |
| 141 | +space = get_by_name('{0}/spaces/all'.format(octopus_server_uri), space_name) |
| 142 | +tenant = get_by_name('{0}/{1}/tenants/all'.format(octopus_server_uri, space['Id']), tenant_name) |
| 143 | + |
| 144 | +tenant['IsDisabled'] = disable_tenant |
| 145 | + |
| 146 | +uri = '{0}/{1}/tenants/{2}'.format(octopus_server_uri, space['Id'], tenant['Id']) |
| 147 | +response = requests.put(uri, headers=headers, json=tenant) |
| 148 | +response.raise_for_status() |
| 149 | +``` |
| 150 | + |
| 151 | +</details> |
| 152 | +<details data-group="deactivate-tenant-scripts"> |
| 153 | +<summary>Go</summary> |
| 154 | + |
| 155 | +```go |
| 156 | +package main |
| 157 | + |
| 158 | +import ( |
| 159 | + "log" |
| 160 | + "net/url" |
| 161 | + |
| 162 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" |
| 163 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces" |
| 164 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants" |
| 165 | +) |
| 166 | + |
| 167 | +func main() { |
| 168 | + apiURL, err := url.Parse("https://your-octopus-url") |
| 169 | + if err != nil { |
| 170 | + log.Println(err) |
| 171 | + } |
| 172 | + APIKey := "API-YOUR-KEY" |
| 173 | + spaceName := "Default" |
| 174 | + tenantName := "MyTenant" |
| 175 | + enabled := true |
| 176 | + |
| 177 | + space := GetSpace(apiURL, APIKey, spaceName) |
| 178 | + if space == nil { |
| 179 | + log.Println(err) |
| 180 | + } |
| 181 | + |
| 182 | + client := octopusAuth(apiURL, APIKey, space.ID) |
| 183 | + |
| 184 | + tenant := GetTenantByName(client, tenantName) |
| 185 | + if tenant == nil { |
| 186 | + log.Println(err) |
| 187 | + } |
| 188 | + |
| 189 | + tenant.IsDisabled = !enabled |
| 190 | + updatedTenant, err := client.Tenants.Update(tenant) |
| 191 | + if err != nil { |
| 192 | + log.Println(err) |
| 193 | + } |
| 194 | + |
| 195 | + log.Printf("Tenant '%s' updated successfully. IsDisabled: %v", updatedTenant.Name, updatedTenant.IsDisabled) |
| 196 | +} |
| 197 | + |
| 198 | +func octopusAuth(octopusURL *url.URL, APIKey string, spaceID string) *client.Client { |
| 199 | + client, err := client.NewClient(nil, octopusURL, APIKey, spaceID) |
| 200 | + if err != nil { |
| 201 | + log.Println(err) |
| 202 | + } |
| 203 | + return client |
| 204 | +} |
| 205 | + |
| 206 | +func GetSpace(octopusURL *url.URL, APIKey string, spaceName string) *spaces.Space { |
| 207 | + client := octopusAuth(octopusURL, APIKey, "") |
| 208 | + |
| 209 | + spaceQuery := spaces.SpacesQuery{ |
| 210 | + PartialName: spaceName, |
| 211 | + } |
| 212 | + |
| 213 | + spaces, err := client.Spaces.Get(spaceQuery) |
| 214 | + if err != nil { |
| 215 | + log.Println(err) |
| 216 | + } |
| 217 | + |
| 218 | + for _, space := range spaces.Items { |
| 219 | + if space.Name == spaceName { |
| 220 | + return space |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return nil |
| 225 | +} |
| 226 | + |
| 227 | +func GetTenantByName(client *client.Client, tenantName string) *tenants.Tenant { |
| 228 | + tenantQuery := tenants.TenantsQuery{ |
| 229 | + Name: tenantName, |
| 230 | + } |
| 231 | + |
| 232 | + tenants, err := client.Tenants.Get(tenantQuery) |
| 233 | + if err != nil { |
| 234 | + log.Println(err) |
| 235 | + } |
| 236 | + |
| 237 | + if len(tenants.Items) == 1 { |
| 238 | + return tenants.Items[0] |
| 239 | + } |
| 240 | + |
| 241 | + return nil |
| 242 | +} |
| 243 | + |
| 244 | + |
| 245 | +``` |
| 246 | + |
| 247 | +</details> |
0 commit comments