forked from hashicorp/python-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_module.py
More file actions
857 lines (723 loc) · 31.8 KB
/
registry_module.py
File metadata and controls
857 lines (723 loc) · 31.8 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#!/usr/bin/env python3
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
"""
Complete Registry Module Testing Suite
This file contains individual tests for all 15 registry module functions:
PUBLIC FUNCTIONS AVAILABLE FOR TESTING:
1. list() - List registry modules in organization
2. list_commits() - List commits for a VCS-connected module
3. create() - Create a new registry module
4. create_version() - Create a new version of an existing module
5. create_with_vcs_connection() - Create module with VCS connection
6. read() - Read a specific registry module
7. read_version() - Read a specific version of a module
8. read_terraform_registry_module() - Read public Terraform Registry module
9. delete() - Delete a module by organization and name
10. delete_by_name() - Delete a module using RegistryModuleID
11. delete_provider() - Delete all modules for a provider
12. delete_version() - Delete a specific version of a module
13. update() - Update module configuration
14. upload() - Upload module content for a version
15. upload_tar_gzip() - Upload tar.gz archive to upload URL
USAGE:
- Uncomment specific test sections to test individual functions
- Tests 1-6 have been pre-tested and are commented out (preserve this logic)
- Modify test data (module names, versions, etc.) as needed for your environment
- Ensure you have proper TFE credentials and organization access
"""
import io
import os
import random
import sys
import tarfile
import tempfile
import time
# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from pytfe import TFEClient, TFEConfig
from pytfe.errors import NotFound
from pytfe.models import (
AgentExecutionMode,
RegistryModuleCreateOptions,
RegistryModuleCreateVersionOptions,
RegistryModuleCreateWithVCSConnectionOptions,
RegistryModuleID,
RegistryModuleListOptions,
RegistryModuleUpdateOptions,
RegistryModuleVCSRepoOptions,
RegistryName,
TestConfig,
)
def main():
"""Test all registry module functions individually."""
print("=" * 80)
print("REGISTRY MODULE COMPLETE TESTING SUITE")
print("=" * 80)
print("Testing ALL 15 functions in src/tfe/resources/registry_module.py")
print("Comprehensive test coverage for all registry module operations")
print("=" * 80)
# Initialize the TFE client
client = TFEClient(TFEConfig.from_env())
organization_name = "aayush-test" # Replace with your organization
# Variables to store created resources for dependent tests
created_module = None
created_version = None
version_object = None # Store the version object with upload URL
# =====================================================
# TEST 1: LIST REGISTRY MODULES [TESTED - COMMENTED]
# =====================================================
print("\n1. Testing list() function:")
try:
options = RegistryModuleListOptions(
organization_name=organization_name, registry_name=RegistryName.PRIVATE
)
modules = list(client.registry_modules.list(organization_name, options))
print(f"Found {len(modules)} registry modules")
for i, module in enumerate(modules[:3], 1):
print(f"{i}. {module.name}/{module.provider} (ID: {module.id})")
except NotFound:
print(
" No modules found (organization may not exist or no private modules available)"
)
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 2: CREATE REGISTRY MODULE WITH VCS CONNECTION [TESTED - COMMENTED]
# =====================================================
print("\n2. Testing create_with_vcs_connection() function:")
created_module = None
try:
unique_suffix = f"{int(time.time())}-{random.randint(1000, 9999)}"
vcs_options = RegistryModuleVCSRepoOptions(
identifier="aayushsingh2502/dummy-repo", # Required
**{"display-identifier": "dummy-aws"}, # Required (using alias)
**{"oauth-token-id": "ot-gAGuPJPTRrSdqjZA"}, # Optional
**{"organization-name": organization_name}, # Required when using branch
branch="main", # Optional: for branch-based modules
tags=False, # Cannot be True when branch is specified
**{"source-directory": ""}, # Optional
**{"tag-prefix": "v"}, # Optional
)
test_config = TestConfig(
tests_enabled=True, agent_execution_mode=AgentExecutionMode.REMOTE
)
vcs_create_options = RegistryModuleCreateWithVCSConnectionOptions(
**{"vcs-repo": vcs_options},
name=f"dummy-repo-{unique_suffix}",
provider="aws",
**{"registry-name": "private"},
**{"initial-version": "1.0.0"},
**{"test-config": test_config},
)
created_module = client.registry_modules.create_with_vcs_connection(
vcs_create_options
)
print(
f" Created VCS module: {created_module.name}/{created_module.provider}"
)
print(f"ID: {created_module.id}")
print(f"Status: {created_module.status}")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 3: READ REGISTRY MODULE [TESTED - COMMENTED]
# =====================================================
if created_module:
print("\n3. Testing read() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
read_module = client.registry_modules.read(module_id)
print(f"Read module: {read_module.name}")
print(f"Status: {read_module.status}")
print(f"Created: {read_module.created_at}")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 4: LIST COMMITS [TESTED - COMMENTED]
# =====================================================
if created_module:
print("\n4. Testing list_commits() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
commits = client.registry_modules.list_commits(module_id)
commit_list = list(commits.items) if hasattr(commits, "items") else []
print(f"Found {len(commit_list)} commits")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 5: CREATE VERSION [TESTED - COMMENTED]
# =====================================================
created_version = None
if created_module:
print("\n5. Testing create_version() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
version_options = RegistryModuleCreateVersionOptions(
version="1.0.0", commit_sha="dummy-sha-123456789abcdef"
)
version = client.registry_modules.create_version(module_id, version_options)
created_version = version.version
print(f"Created version: {version.version}")
print(f"Status: {version.status}")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 6: READ VERSION [TESTED - COMMENTED]
# =====================================================
if created_module and created_version:
print("\n6. Testing read_version() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
read_version = client.registry_modules.read_version(
module_id, created_version
)
print(f"Read version: {read_version.version}")
print(f"Status: {read_version.status}")
print(f"ID: {read_version.id}")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 7: READ PUBLIC TERRAFORM REGISTRY MODULE
# =====================================================
print("\n7. Testing read_terraform_registry_module() function:")
try:
# Create a RegistryModuleID for a public module
public_module_id = RegistryModuleID(
namespace="terraform-aws-modules", # Use namespace for public modules
name="vpc",
provider="aws",
registry_name=RegistryName.PUBLIC,
)
# Read a specific version of the public module
version = "5.0.0" # Use a known stable version
public_module = client.registry_modules.read_terraform_registry_module(
public_module_id, version
)
print(f"Read public module: {public_module.name}")
print(f"Version: {version}")
print(f"Downloads: {getattr(public_module, 'downloads', 'N/A')}")
print(f"Verified: {getattr(public_module, 'verified', 'N/A')}")
print(f"Source: {getattr(public_module, 'source', 'N/A')}")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 8: CREATE SIMPLE REGISTRY MODULE (Non-VCS)
# =====================================================
print("\n8. Testing create() function (non-VCS module):")
print("NOTE: Non-VCS modules start in PENDING status until content is uploaded")
try:
unique_suffix = f"{int(time.time())}-{random.randint(1000, 9999)}"
create_options = RegistryModuleCreateOptions(
name=f"test-module-{unique_suffix}",
provider="aws",
registry_name=RegistryName.PRIVATE,
)
created_simple_module = client.registry_modules.create(
organization_name, create_options
)
print(
f"Created simple module: {created_simple_module.name}/{created_simple_module.provider}"
)
print(f"ID: {created_simple_module.id}")
print(
f"Status: {created_simple_module.status} (PENDING until content uploaded)"
)
print(f"No Code: {created_simple_module.no_code}")
# Store for later tests (will be overridden by upload test module)
created_module = created_simple_module
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 8A: LIST VERSIONS
# =====================================================
if created_module:
print("\n8A. Testing list_versions() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
versions = client.registry_modules.list_versions(module_id)
versions_list = list(versions) if hasattr(versions, "__iter__") else []
print(f"Found {len(versions_list)} versions")
for i, version in enumerate(versions_list[:3], 1):
print(f"{i}. Version {version.version} (Status: {version.status})")
except Exception as e:
print(f"Error: {e}")
# =====================================================
# TEST 8B: UPDATE MODULE
# =====================================================
if created_module:
print("\n8B. Testing update() function:")
print("NOTE: Update functionality may vary by TFE version")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
# First check current module status
current_module = client.registry_modules.read(module_id)
print(f"Current module no_code setting: {current_module.no_code}")
# Try to update no_code setting
update_options = RegistryModuleUpdateOptions(
no_code=True # Set to no-code module
)
updated_module = client.registry_modules.update(module_id, update_options)
print(f"Updated module: {updated_module.name}")
print(f"No Code: {updated_module.no_code}")
print(f"Status: {updated_module.status}")
except Exception as e:
print(f"Update may not be supported: {e}")
# =====================================================
# TEST 9: CREATE MODULE FOR UPLOAD TESTING
# =====================================================
print("\n9. Creating test module for upload function testing:")
try:
# Create a module specifically for upload testing
create_options = RegistryModuleCreateOptions(
name=f"upload-test-{random.randint(100000, 999999)}",
provider="aws",
registry_name=RegistryName.PRIVATE,
)
created_module = client.registry_modules.create(
organization_name, create_options
)
print(f"Created test module: {created_module.name}")
print(f"Provider: {created_module.provider}")
print(f"Status: {created_module.status}")
except Exception as e:
print(f"Error creating module: {e}")
return
# =====================================================
# TEST 10: CREATE VERSION FOR UPLOAD TESTING
# =====================================================
created_version = None
version_object = None
if created_module:
print("\n10. Creating version for upload testing:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
version_options = RegistryModuleCreateVersionOptions(version="1.0.0")
version = client.registry_modules.create_version(module_id, version_options)
created_version = version.version
version_object = version
print(f"Created version: {created_version}")
print(f"Status: {version.status}")
# Check if upload URL is available
upload_url = (
version.links.get("upload") if hasattr(version, "links") else None
)
print(f"Upload URL available: {'Yes' if upload_url else 'No'}")
except Exception as e:
print(f"Error creating version: {e}")
# =====================================================
# TEST 11: UPLOAD_TAR_GZIP FUNCTION TESTING
# =====================================================
if created_module and created_version and version_object:
print("\n11. Testing upload_tar_gzip() function:")
print("This will change module status from PENDING to SETUP_COMPLETE")
try:
# Create a simple module structure in memory
tar_buffer = io.BytesIO()
with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
# Create main.tf content
main_tf_content = """
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "name" {
description = "Name of the resource"
type = string
default = "upload-test"
}
resource "aws_s3_bucket" "example" {
bucket = var.name
}
output "bucket_name" {
description = "The name of the S3 bucket"
value = aws_s3_bucket.example.bucket
}
""".strip()
# Add main.tf to archive
main_tf_info = tarfile.TarInfo(name="main.tf")
main_tf_info.size = len(main_tf_content.encode("utf-8"))
tar.addfile(main_tf_info, io.BytesIO(main_tf_content.encode("utf-8")))
# Create README.md content
readme_content = f"""
# {created_module.name}
A test module created for upload function testing.
## Usage
```hcl
module "example" {{
source = "app.terraform.io/{{organization_name}}/{{created_module.name}}/{{created_module.provider}}"
name = "my-resource"
}}
```
""".strip()
# Add README.md to archive
readme_info = tarfile.TarInfo(name="README.md")
readme_info.size = len(readme_content.encode("utf-8"))
tar.addfile(readme_info, io.BytesIO(readme_content.encode("utf-8")))
tar_buffer.seek(0)
# Get upload URL from the version object
upload_url = (
version_object.links.get("upload")
if hasattr(version_object, "links")
else None
)
if upload_url:
client.registry_modules.upload_tar_gzip(upload_url, tar_buffer)
print("Successfully uploaded tar.gz content using upload_tar_gzip()")
# Wait for processing
print("Waiting 5 seconds for processing...")
time.sleep(5)
# Check module status after upload
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
updated_module = client.registry_modules.read(module_id)
print(f"Updated Module Status: {updated_module.status}")
if updated_module.status.value != "pending":
print(
f"SUCCESS: Module status changed from PENDING to {updated_module.status}"
)
else:
print("Module still processing - may take longer")
else:
print(" No upload URL available in version links")
except Exception as e:
print(f"Error in upload_tar_gzip test: {e}")
# =====================================================
# TEST 12: UPLOAD FUNCTION TESTING
# =====================================================
if created_module and created_version and version_object:
print("\n12. Testing upload() function:")
print("NOTE: This function uploads from a local file path")
try:
# Create a temporary directory with module structure
with tempfile.TemporaryDirectory() as temp_dir:
# Create main.tf file
main_tf_path = os.path.join(temp_dir, "main.tf")
with open(main_tf_path, "w") as f:
f.write(
"""
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "test_var" {
description = "A test variable for upload() function"
type = string
default = "upload-test"
}
resource "aws_s3_bucket" "upload_test" {
bucket = var.test_var
}
output "bucket_name" {
description = "The name of the S3 bucket"
value = aws_s3_bucket.upload_test.bucket
}
""".strip()
)
# Create variables.tf file
variables_tf_path = os.path.join(temp_dir, "variables.tf")
with open(variables_tf_path, "w") as f:
f.write(
"""
variable "environment" {
description = "Environment name"
type = string
default = "dev"
}
variable "region" {
description = "AWS region"
type = string
default = "us-west-2"
}
""".strip()
)
# Create outputs.tf file
outputs_tf_path = os.path.join(temp_dir, "outputs.tf")
with open(outputs_tf_path, "w") as f:
f.write(
"""
output "module_info" {
description = "Information about this module"
value = {
name = "upload-test-module"
environment = var.environment
region = var.region
}
}
""".strip()
)
print(f"Created temporary module files in: {temp_dir}")
print(f"Files: {os.listdir(temp_dir)}")
# Check if upload URL is available
upload_url = (
version_object.links.get("upload")
if hasattr(version_object, "links")
else None
)
if upload_url:
print("Upload URL available: Yes")
# Try the upload function
try:
client.registry_modules.upload(version_object, temp_dir)
print("Successfully uploaded using upload() function")
# Wait and check status
print("Waiting 5 seconds for processing...")
time.sleep(5)
module_id = RegistryModuleID(
organization=organization_name,
name=created_module.name,
provider=created_module.provider,
registry_name=RegistryName.PRIVATE,
)
updated_module = client.registry_modules.read(module_id)
print(f"Updated Module Status: {updated_module.status}")
except NotImplementedError as nie:
print(f"upload() function not fully implemented: {nie}")
print("This is expected - the function is a placeholder")
# Fallback to upload_tar_gzip
print("Trying fallback: upload_tar_gzip()...")
tar_buffer = io.BytesIO()
with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
for file_name in os.listdir(temp_dir):
file_path = os.path.join(temp_dir, file_name)
if os.path.isfile(file_path):
with open(file_path) as file_content:
content = file_content.read()
tarinfo = tarfile.TarInfo(name=file_name)
tarinfo.size = len(content.encode("utf-8"))
tar.addfile(
tarinfo, io.BytesIO(content.encode("utf-8"))
)
tar_buffer.seek(0)
client.registry_modules.upload_tar_gzip(upload_url, tar_buffer)
print(
"Successfully uploaded using upload_tar_gzip() as fallback"
)
except Exception as upload_error:
print(f"upload() function error: {upload_error}")
else:
print(" No upload URL available - cannot test upload function")
except Exception as e:
print(f"Error in upload() test: {e}")
# =====================================================
# TEST 13: DELETE VERSION
# =====================================================
# Create a test module and version for delete testing
print("\n13. Testing delete_version() function:")
print("Creating test module and version for deletion...")
test_module_for_deletion = None
test_version_for_deletion = None
try:
# Create a module specifically for delete testing
delete_create_options = RegistryModuleCreateOptions(
name=f"delete-test-{random.randint(100000, 999999)}",
provider="aws",
registry_name=RegistryName.PRIVATE,
)
test_module_for_deletion = client.registry_modules.create(
organization_name, delete_create_options
)
print(f"Created test module: {test_module_for_deletion.name}")
# Create a version for deletion testing
module_id = RegistryModuleID(
organization=organization_name,
name=test_module_for_deletion.name,
provider=test_module_for_deletion.provider,
registry_name=RegistryName.PRIVATE,
)
version_options = RegistryModuleCreateVersionOptions(version="1.0.0")
version = client.registry_modules.create_version(module_id, version_options)
test_version_for_deletion = version.version
print(f"Created test version: {test_version_for_deletion}")
# Now test version deletion
print(f"Testing deletion of version {test_version_for_deletion}...")
# Delete the version
client.registry_modules.delete_version(module_id, test_version_for_deletion)
print(
f"Successfully called delete_version() for version: {test_version_for_deletion}"
)
# Verify deletion by trying to read it
try:
client.registry_modules.read_version(
organization=organization_name,
registry_name=RegistryName.PRIVATE,
namespace=organization_name,
name=test_module_for_deletion.name,
provider=test_module_for_deletion.provider,
version=test_version_for_deletion,
)
print(
"Warning: Version still exists after deletion (may take time to process)"
)
except Exception:
print(" Confirmed: Version no longer exists")
except Exception as e:
print(f"Error in delete_version test: {e}")
# =====================================================
# TEST 14: DELETE BY NAME
# =====================================================
if test_module_for_deletion:
print("\n14. Testing delete_by_name() function:")
try:
module_id = RegistryModuleID(
organization=organization_name,
name=test_module_for_deletion.name,
provider=test_module_for_deletion.provider,
registry_name=RegistryName.PRIVATE,
)
# Check module exists before deletion
try:
client.registry_modules.read(module_id)
print(
f"Module {test_module_for_deletion.name}/{test_module_for_deletion.provider} exists"
)
# Delete the module
client.registry_modules.delete_by_name(module_id)
print(
f"Successfully called delete_by_name() for module: {test_module_for_deletion.name}"
)
# Verify deletion
try:
client.registry_modules.read(module_id)
print(
"Warning: Module still exists after deletion (may take time to process)"
)
except Exception:
print("Confirmed: Module no longer exists")
except Exception as read_error:
print(f"Module not found: {read_error}")
except Exception as e:
print(f"Error in delete_by_name test: {e}")
# =====================================================
# TEST 15: DELETE (Alternative delete method)
# =====================================================
print("\n15. Testing delete() function:")
print("NOTE: Testing with non-existent module to avoid conflicts")
try:
# This function takes organization and name directly
# We'll test with a non-existent module to avoid conflicts
test_name = "non-existent-module-for-testing"
print(f"Testing delete with non-existent module: {test_name}")
client.registry_modules.delete(organization_name, test_name)
print(
"Delete function executed successfully (may return 404 for non-existent module)"
)
except Exception as e:
print(f"Expected error for non-existent module: {e}")
# =====================================================
# TEST 16: DELETE PROVIDER (SAFE VERSION - CREATES TEST PROVIDER)
# =====================================================
print("\n16. Testing delete_provider() function:")
print("Creating a test provider specifically for deletion testing...")
try:
# Create a test module with a valid provider for deletion testing
# Use simple alphanumeric names to avoid validation issues
test_provider_name = f"testprovider{random.randint(1000, 9999)}"
delete_provider_options = RegistryModuleCreateOptions(
name=f"testmodule{random.randint(1000, 9999)}",
provider=test_provider_name,
registry_name=RegistryName.PRIVATE,
)
test_provider_module = client.registry_modules.create(
organization_name, delete_provider_options
)
print(f"Created test module with provider: {test_provider_name}")
# Now test delete_provider function
test_provider_module_id = RegistryModuleID(
organization=organization_name,
name=test_provider_module.name, # Name doesn't matter for provider deletion
provider=test_provider_name,
registry_name=RegistryName.PRIVATE,
)
print(f"Testing delete_provider() for provider: {test_provider_name}")
client.registry_modules.delete_provider(test_provider_module_id)
print(
f"Successfully called delete_provider() for provider: {test_provider_name}"
)
# Verify deletion by trying to read the module
try:
client.registry_modules.read(test_provider_module_id)
print(
"Warning: Module still exists after provider deletion (may take time to process)"
)
except Exception:
print("Confirmed: All modules for provider have been deleted")
except Exception as e:
print(f"Error in delete_provider test: {e}")
# =====================================================
# TESTING SUMMARY
# =====================================================
print("\n" + "=" * 80)
print("REGISTRY MODULE TESTING COMPLETED!")
print("=" * 80)
print("Summary of ALL 15 Functions Tested:")
print(" list() - List registry modules in organization")
print(" create_with_vcs_connection() - Create module with VCS connection")
print(" read() - Read module details")
print(" list_commits() - List VCS commits for module")
print(" create_version() - Create new module version")
print(" read_version() - Read specific version details")
print(" read_terraform_registry_module() - Read public registry module")
print(" create() - Create simple module")
print(" list_versions() - List all versions of a module")
print(" update() - Update module settings")
print(" upload_tar_gzip() - Upload tar.gz archive to upload URL")
print(" upload() - Upload from local directory path (placeholder)")
print(" delete_version() - Delete a specific version")
print(" delete_by_name() - Delete entire module by name")
print(" delete() - Delete module by organization and name")
print(" delete_provider() - Delete all modules for a provider")
if created_module:
print(f"Created test module: {created_module.name}")
print("=" * 80)
print(" ALL 15 REGISTRY MODULE FUNCTIONS HAVE BEEN TESTED!")
print("=" * 80)
if __name__ == "__main__":
main()