Skip to content

Commit 176f7bd

Browse files
Adds examples documentation for enabling/disabling tenants. (#2564)
* Include tenant enabled/disabled state * Fix example scripts * Update wording * Fix deactivate tenant path
1 parent 4ba269d commit 176f7bd

File tree

5 files changed

+278
-3
lines changed

5 files changed

+278
-3
lines changed

src/pages/docs/octopus-rest-api/examples/deployment-targets/enable-disable-machine.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: src/layouts/Default.astro
33
pubDate: 2023-01-01
44
modDate: 2023-01-01
5-
title: Enable/disable machine
5+
title: Deactivate machines
66
description: An example script that enables or disables a machine in Octopus.
77
---
88
import EnableDisableMachineScripts from 'src/shared-content/scripts/enable-disable-machine-scripts.include.md';

src/pages/docs/octopus-rest-api/examples/projects/enable-disable-project.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: src/layouts/Default.astro
33
pubDate: 2023-01-01
44
modDate: 2023-01-01
5-
title: Enable/disable project
5+
title: Deactivate projects
66
description: An example script that enables or disables a project in Octopus.
77
---
88
import EnableDisableProjectScripts from 'src/shared-content/scripts/enable-disable-project-scripts.include.md';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: src/layouts/Default.astro
3+
pubDate: 2024-11-18
4+
modDate: 2024-11-18
5+
title: Deactivate tenants
6+
description: An example script that activates or deactivates a tenant in Octopus.
7+
---
8+
import DeactivateTenantScripts from 'src/shared-content/scripts/deactivate-tenant-scripts.include.md';
9+
10+
In 2025.1 Octopus has added support for deactivating tenants. Inactive tenants do not allow deployments or runbook runs but are able to be edited. They are also removed from license calculations allowing you to effectively archive unused tenants and re-enable them in the future.
11+
Inactive tenants are highlighted with grayed out text and are not available for selection on the deployment or runbook run pages. If deployments are created for inactive tenants via the API or CLI an exception will be thrown.
12+
13+
This script demonstrates how to programmatically deactivate a tenant.
14+
15+
## Usage
16+
17+
Provide values for the following:
18+
19+
- Octopus URL
20+
- Octopus API Key
21+
- Name of the space to use
22+
- Name of the tenant
23+
- Boolean value for enabled
24+
25+
## Script
26+
27+
<DeactivateTenantScripts />

src/pages/docs/octopus-rest-api/examples/tenants/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ hideInThisSectionHeader: true
1010
You can use the REST API to create and manage Server tasks in Octopus. Typical tasks might include:
1111

1212
- [Create a tenant](/docs/octopus-rest-api/examples/tenants/create-tenant)
13-
- [Update tenant variables](/docs/octopus-rest-api/examples/tenants/update-tenant-variable)
13+
- [Update tenant variables](/docs/octopus-rest-api/examples/tenants/update-tenant-variable)
14+
- [Deactivate tenants](/docs/octopus-rest-api/examples/tenants/deactivate-tenant)
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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

Comments
 (0)