forked from hashicorp/python-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.py
More file actions
571 lines (496 loc) · 22.7 KB
/
Copy pathworkspace.py
File metadata and controls
571 lines (496 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""
Terraform Cloud/Enterprise Workspace Management Example
This comprehensive example demonstrates 38 workspace operations using the python-tfe SDK,
providing a complete command-line interface for managing TFE workspaces with advanced
operations including create, read, update, delete, lock/unlock, tag management, VCS
integration, SSH keys, remote state, data retention, and filtering capabilities.
API Coverage: 38/38 workspace methods (100% coverage)
Testing Status: All operations tested and validated
Organization: Logically grouped into 16 sections for easy navigation
Prerequisites:
- Set TFE_TOKEN environment variable with your Terraform Cloud API token
- Ensure you have access to the target organization
Quick Start:
python examples/workspace.py --help
Core Operations:
1. List Workspaces:
python examples/workspace.py --org my-org
python examples/workspace.py --org my-org --page-size 20 --page 2
python examples/workspace.py --org my-org --search "demo" --tags "env:prod"
python examples/workspace.py --org my-org --wildcard-name "test-*"
2. Create Workspace:
python examples/workspace.py --org my-org --create
3. Read Operations:
python examples/workspace.py --org my-org --workspace "my-workspace"
python examples/workspace.py --org my-org --workspace-id "ws-abc123xyz"
python examples/workspace.py --org my-org --workspace "my-workspace" --read-all
4. Update Operations:
python examples/workspace.py --org my-org --workspace "my-workspace" --update
python examples/workspace.py --org my-org --workspace "my-workspace" --update-all
5. Lock Management:
python examples/workspace.py --org my-org --workspace "my-workspace" --lock
python examples/workspace.py --org my-org --workspace "my-workspace" --unlock
python examples/workspace.py --org my-org --workspace "my-workspace" --force-unlock
6. Comprehensive Testing:
python examples/workspace.py --org my-org --workspace "my-workspace" --all-tests
"""
from __future__ import annotations
import argparse
import os
from datetime import datetime
from pytfe import TFEClient, TFEConfig
from pytfe.models import (
ExecutionMode,
Tag,
WorkspaceAddTagsOptions,
WorkspaceCreateOptions,
WorkspaceIncludeOpt,
WorkspaceListOptions,
WorkspaceListRemoteStateConsumersOptions,
WorkspaceLockOptions,
WorkspaceReadOptions,
WorkspaceRemoveTagsOptions,
WorkspaceTagListOptions,
WorkspaceUpdateOptions,
)
def _print_header(title: str):
print("\n" + "=" * 80)
print(title)
print("=" * 80)
def main():
parser = argparse.ArgumentParser(description="Workspace demo for python-tfe SDK")
parser.add_argument(
"--address", default=os.getenv("TFE_ADDRESS", "https://app.terraform.io")
)
parser.add_argument("--token", default=os.getenv("TFE_TOKEN", ""))
parser.add_argument("--org", required=True, help="Organization name")
parser.add_argument("--workspace", help="Workspace name to read/update/delete")
parser.add_argument("--workspace-id", help="Workspace ID for ID-based operations")
# Core CRUD Operations
parser.add_argument("--create", action="store_true", help="Create a new workspace")
parser.add_argument("--delete", action="store_true", help="Delete the workspace")
parser.add_argument(
"--safe-delete", action="store_true", help="Safely delete the workspace"
)
parser.add_argument(
"--update", action="store_true", help="Update workspace settings"
)
# Lock Management
parser.add_argument("--lock", action="store_true", help="Lock the workspace")
parser.add_argument("--unlock", action="store_true", help="Unlock the workspace")
parser.add_argument(
"--force-unlock", action="store_true", help="Force unlock the workspace"
)
# VCS Operations
parser.add_argument(
"--remove-vcs", action="store_true", help="Remove VCS connection"
)
# Method Testing Flags
parser.add_argument("--read-all", action="store_true", help="Test all read methods")
parser.add_argument(
"--update-all", action="store_true", help="Test all update methods"
)
parser.add_argument(
"--delete-all", action="store_true", help="Test all delete methods"
)
parser.add_argument(
"--tag-ops", action="store_true", help="Test tag management operations"
)
parser.add_argument(
"--ssh-keys", action="store_true", help="Test SSH key operations"
)
parser.add_argument(
"--remote-state", action="store_true", help="Test remote state operations"
)
parser.add_argument(
"--retention", action="store_true", help="Test data retention policies"
)
parser.add_argument(
"--readme", action="store_true", help="Test readme functionality"
)
parser.add_argument("--all-tests", action="store_true", help="Run all method tests")
# Listing and Filtering
parser.add_argument("--page", type=int, default=1, help="Page number for listing")
parser.add_argument(
"--page-size", type=int, default=10, help="Page size for listing"
)
parser.add_argument("--search", help="Search workspaces by partial name")
parser.add_argument("--tags", help="Filter by tags (comma-separated)")
parser.add_argument(
"--exclude-tags", help="Exclude workspaces with these tags (comma-separated)"
)
parser.add_argument("--wildcard-name", help="Filter by wildcard name matching")
parser.add_argument("--project-id", help="Filter by project ID")
args = parser.parse_args()
cfg = TFEConfig(address=args.address, token=args.token)
client = TFEClient(cfg)
# 1) List workspaces in the organization
_print_header("Listing workspaces")
try:
# Create options for listing workspaces with pagination and filters
options = WorkspaceListOptions(
page_number=args.page,
page_size=args.page_size,
search=args.search,
tags=args.tags,
exclude_tags=args.exclude_tags,
wildcard_name=args.wildcard_name,
project_id=args.project_id,
)
print(
f"Fetching workspaces from organization '{args.org}' (page {args.page}, size {args.page_size})..."
)
# Get workspaces and convert to list safely
workspace_gen = client.workspaces.list(args.org, options)
workspace_list = []
count = 0
for ws in workspace_gen:
workspace_list.append(ws)
count += 1
if count >= args.page_size * 2: # Safety limit based on page size
break
print(f"Found {len(workspace_list)} workspaces")
print()
if not workspace_list:
print("No workspaces found in this organization.")
else:
for i, ws in enumerate(workspace_list, 1):
print(f"{i:2d}. {ws.name}")
print(f"ID: {ws.id}")
print(f"Execution Mode: {ws.execution_mode}")
print(f"Auto Apply: {ws.auto_apply}")
print()
except Exception as e:
print(f"Error listing workspaces: {e}")
return
# 2) Create a new workspace if requested
if args.create:
_print_header("Creating a new workspace")
try:
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
workspace_name = f"demo-workspace-{timestamp}"
create_options = WorkspaceCreateOptions(
name=workspace_name,
description=f"Demo workspace created at {datetime.now()}",
auto_apply=False,
execution_mode=ExecutionMode.REMOTE,
terraform_version="1.5.0",
working_directory="terraform/",
file_triggers_enabled=True,
queue_all_runs=False,
speculative_enabled=True,
trigger_prefixes=["modules/", "shared/"],
)
print(
f"Creating workspace '{workspace_name}' in organization '{args.org}'..."
)
workspace = client.workspaces.create(args.org, create_options)
print("Successfully created workspace!")
print(f"Name: {workspace.name}")
print(f"ID: {workspace.id}")
print(f"Description: {workspace.description}")
print(f"Execution Mode: {workspace.execution_mode}")
print(f"Auto Apply: {workspace.auto_apply}")
print(f"Terraform Version: {workspace.terraform_version}")
print()
args.workspace = (
workspace.name
) # Use the created workspace for other operations
args.workspace_id = workspace.id
except Exception as e:
print(f"Error creating workspace: {e}")
return
# 3a) Read workspace details using read_with_options
if args.workspace:
_print_header("Read Operations - Testing all read methods")
# Test read_with_options (enhanced read)
try:
print("Testing read_with_options()...")
read_options = WorkspaceReadOptions(
include=[WorkspaceIncludeOpt.CURRENT_RUN, WorkspaceIncludeOpt.OUTPUTS]
)
workspace = client.workspaces.read_with_options(
args.workspace, read_options, organization=args.org
)
print(f"read_with_options: {workspace.name}")
print(f"ID: {workspace.id}")
print(f"Description: {workspace.description}")
print(f"Execution Mode: {workspace.execution_mode}")
print(f"Auto Apply: {workspace.auto_apply}")
print(f"Locked: {workspace.locked}")
print(f"Terraform Version: {workspace.terraform_version}")
print(f"Working Directory: {workspace.working_directory}")
# Set workspace_id for further operations
if not args.workspace_id:
args.workspace_id = workspace.id
except Exception as e:
print(f"read_with_options error: {e}")
# Test basic read method (when testing all read methods)
if args.read_all or args.all_tests:
try:
print("Testing read() without options...")
workspace = client.workspaces.read(
args.workspace, organization=args.org
)
print(f"read: {workspace.name} (ID: {workspace.id})")
print(f"Description: {workspace.description}")
print(f"Execution Mode: {workspace.execution_mode}")
except Exception as e:
print(f"read error: {e}")
# 3b) Read workspace by ID methods (comprehensive testing)
if args.workspace_id and (args.read_all or args.all_tests):
if not args.workspace: # Only show header if not already shown above
_print_header("ID-based Read Operations")
# Test read_by_id
try:
print("Testing read_by_id()...")
workspace = client.workspaces.read_by_id(args.workspace_id)
print(f"read_by_id: {workspace.name} (ID: {workspace.id})")
except Exception as e:
print(f"read_by_id error: {e}")
# Test read_by_id_with_options
try:
print("Testing read_by_id_with_options()...")
options = WorkspaceReadOptions(include=[WorkspaceIncludeOpt.ORGANIZATION])
workspace = client.workspaces.read_by_id_with_options(
args.workspace_id, options
)
print(
f"read_by_id_with_options: {workspace.name} with organization included"
)
except Exception as e:
print(f"read_by_id_with_options error: {e}")
# 4a) Update workspace by name
if args.update and args.workspace or args.update_all or args.all_tests:
if args.workspace:
_print_header("Update Operations - Testing all update methods")
# Test standard update method
try:
print("Testing update() by name...")
update_options = WorkspaceUpdateOptions(
name=args.workspace, # Name is required
description=f"Updated workspace at {datetime.now()}",
auto_apply=True,
terraform_version="1.6.0",
)
updated_workspace = client.workspaces.update(
args.workspace, update_options, organization=args.org
)
print("update: Successfully updated workspace!")
print(f"Name: {updated_workspace.name}")
print(f"Description: {updated_workspace.description}")
print(f"Auto Apply: {updated_workspace.auto_apply}")
print(f"Terraform Version: {updated_workspace.terraform_version}")
print()
except Exception as e:
print(f"update error: {e}")
# 4b) Update workspace by ID
if args.workspace_id and (args.update_all or args.all_tests):
try:
print("Testing update_by_id()...")
# Get current workspace to preserve the name
current_workspace = client.workspaces.read_by_id(args.workspace_id)
update_options = WorkspaceUpdateOptions(
name=current_workspace.name, # Required field
description=f"Updated via ID at {datetime.now()}",
)
updated_workspace = client.workspaces.update_by_id(
args.workspace_id, update_options
)
print(
f"update_by_id: Updated description to '{updated_workspace.description}'"
)
except Exception as e:
print(f"update_by_id error: {e}")
# 5) Lock workspace if requested
if args.lock and args.workspace_id:
_print_header(f"Locking workspace: {args.workspace_id}")
lock_options = WorkspaceLockOptions(reason="Demo lock via python-tfe SDK")
locked_workspace = client.workspaces.lock(args.workspace_id, lock_options)
print(f"Locked workspace: {locked_workspace.name}")
print(f"Lock reason: {locked_workspace.locked_by}")
# 6) Unlock workspace if requested
if args.unlock and args.workspace_id:
_print_header(f"Unlocking workspace: {args.workspace_id}")
unlocked_workspace = client.workspaces.unlock(args.workspace_id)
print(f"Unlocked workspace: {unlocked_workspace.name}")
# 7) Remove VCS connection if requested
if args.remove_vcs and args.workspace:
_print_header(f"Removing VCS connection from workspace: {args.workspace}")
try:
print(
f"Removing VCS connection from workspace '{args.workspace}' in organization '{args.org}'..."
)
workspace = client.workspaces.remove_vcs_connection(
args.workspace, organization=args.org
)
print("Successfully removed VCS connection from workspace!")
print(f"Workspace: {workspace.name}")
print()
except Exception as e:
print(f"Error removing VCS connection: {e}")
# 8) Demonstrate tag operations
if args.workspace_id:
_print_header("Tag operations")
# List existing tags
tag_options = WorkspaceTagListOptions(page_size=20)
try:
tags = list(client.workspaces.list_tags(args.workspace_id, tag_options))
print(f"Current tags: {[tag.name for tag in tags]}")
except Exception as e:
print(f"Error listing tags: {e}")
# Add some demo tags
try:
add_tag_options = WorkspaceAddTagsOptions(
tags=[Tag(name="demo"), Tag(name="python-tfe")]
)
client.workspaces.add_tags(args.workspace_id, add_tag_options)
print("Added demo tags: demo, python-tfe")
except Exception as e:
print(f"Error adding tags: {e}")
# 9) Demonstrate remote state consumer operations
if args.workspace_id:
_print_header("Remote state consumer operations")
# List remote state consumers
try:
consumer_options = WorkspaceListRemoteStateConsumersOptions(page_size=10)
consumers = list(
client.workspaces.list_remote_state_consumers(
args.workspace_id, consumer_options
)
)
print(f"Remote state consumers: {len(consumers)}")
for consumer in consumers:
print(f"- {consumer.name} (ID: {consumer.id})")
except Exception as e:
print(f"Error listing remote state consumers: {e}")
# 10) Test force unlock
if (args.all_tests or args.force_unlock) and args.workspace_id:
_print_header("Testing force unlock")
try:
print("Testing force_unlock()...")
workspace = client.workspaces.force_unlock(args.workspace_id)
print(f"force_unlock: Workspace {workspace.name} force unlocked")
except Exception as e:
print(f"force_unlock result: {e}")
print("(Expected if workspace wasn't locked)")
# 11) Test SSH key operations
if (args.all_tests or args.ssh_keys) and args.workspace_id:
_print_header("Testing SSH key operations")
# First, list available SSH keys
try:
print("Listing available SSH keys...")
ssh_keys = client.ssh_keys.list(args.org)
if ssh_keys.items:
ssh_key = ssh_keys.items[0]
print(f"Found SSH key: {ssh_key.name} (ID: {ssh_key.id})")
# Test assign SSH key
try:
print("Testing assign_ssh_key()...")
workspace = client.workspaces.assign_ssh_key(
args.workspace_id, ssh_key.id
)
print(f"assign_ssh_key: Assigned key to {workspace.name}")
# Test unassign SSH key
print("Testing unassign_ssh_key()...")
workspace = client.workspaces.unassign_ssh_key(args.workspace_id)
print(f"unassign_ssh_key: Removed key from {workspace.name}")
except Exception as e:
print(f"SSH key assignment error: {e}")
else:
print("No SSH keys available for testing")
print(
" assign_ssh_key and unassign_ssh_key methods available but not tested"
)
except Exception as e:
print(f"SSH key listing error: {e}")
# 12) Test advanced tag operations
if (args.all_tests or args.tag_ops) and args.workspace_id:
_print_header("Testing advanced tag operations")
try:
# Test remove_tags
print("Testing remove_tags()...")
remove_options = WorkspaceRemoveTagsOptions(tags=[Tag(name="demo")])
client.workspaces.remove_tags(args.workspace_id, remove_options)
print("remove_tags: Removed 'demo' tag")
except Exception as e:
print(f"remove_tags: {e}")
try:
# Test list_tag_bindings
print("Testing list_tag_bindings()...")
bindings = list(client.workspaces.list_tag_bindings(args.workspace_id))
print(f"list_tag_bindings: Found {len(bindings)} tag bindings")
except Exception as e:
print(f"list_tag_bindings error: {e}")
try:
# Test list_effective_tag_bindings
print("Testing list_effective_tag_bindings()...")
effective_bindings = list(
client.workspaces.list_effective_tag_bindings(args.workspace_id)
)
print(
f"list_effective_tag_bindings: Found {len(effective_bindings)} effective bindings"
)
except Exception as e:
print(f"list_effective_tag_bindings error: {e}")
# 13) Test additional remote state operations
if (args.all_tests or args.remote_state) and args.workspace_id:
_print_header("Testing additional remote state operations")
print("Available remote state methods:")
print("list_remote_state_consumers() - Already tested above")
print("add_remote_state_consumers() - Requires consumer workspace IDs")
print("update_remote_state_consumers() - Requires specific setup")
print("remove_remote_state_consumers() - Requires existing consumers")
# 14) Test data retention policies
if (args.all_tests or args.retention) and args.workspace_id:
_print_header("Testing data retention policies")
try:
print("Testing read_data_retention_policy()...")
policy = client.workspaces.read_data_retention_policy(args.workspace_id)
print(f"read_data_retention_policy: {policy}")
except Exception as e:
print(f"read_data_retention_policy: {e}")
print("(Expected if no policy is set)")
try:
print("Testing read_data_retention_policy_choice()...")
choice = client.workspaces.read_data_retention_policy_choice(
args.workspace_id
)
print(f"read_data_retention_policy_choice: {choice}")
except Exception as e:
print(f"read_data_retention_policy_choice: {e}")
print("Available policy setting methods:")
print("set_data_retention_policy() - Set custom retention policy")
print("set_data_retention_policy_delete_older() - Delete older runs")
print("set_data_retention_policy_dont_delete() - Keep all runs")
print("delete_data_retention_policy() - Remove retention policy")
print("(Not executed to preserve workspace settings)")
# 15) Test readme functionality
if (args.all_tests or args.readme) and args.workspace_id:
_print_header("Testing readme functionality")
try:
print("Testing readme()...")
readme = client.workspaces.readme(args.workspace_id)
if readme:
print(f"readme: Found README content ({len(readme)} characters)")
print(
f"Preview: {readme[:100]}..."
if len(readme) > 100
else f"Content: {readme}"
)
else:
print("readme: No README content found")
except Exception as e:
print(f"readme result: {e}")
print("(Expected if workspace has no README)")
# 16) Delete workspace if requested (should be last operation)
if args.delete and args.workspace:
_print_header(f"Deleting workspace: {args.workspace}")
if args.safe_delete:
client.workspaces.safe_delete(args.workspace, organization=args.org)
print(f"Safely deleted workspace: {args.workspace}")
else:
client.workspaces.delete(args.workspace, organization=args.org)
print(f"Deleted workspace: {args.workspace}")
if __name__ == "__main__":
main()