-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathregistry_provider.py
More file actions
306 lines (228 loc) · 8.8 KB
/
registry_provider.py
File metadata and controls
306 lines (228 loc) · 8.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
#!/usr/bin/env python3
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
"""
Registry Provider Individual Function Tests
This file provides individual test functions for each registry provider operation.
You can run specific functions to test individual parts of the API.
Functions available:
- test_list_simple() - Basic list test
- test_create_private() - Create a private provider
- test_create_public() - Create a public provider
- test_read_with_id() - Read a provider by ID
- test_delete_by_id() - Delete a provider by ID
Usage:
python registry_provider_individual.py
"""
import os
import random
import sys
# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from pytfe import TFEClient
from pytfe.models import (
RegistryName,
RegistryProviderCreateOptions,
RegistryProviderID,
RegistryProviderIncludeOps,
RegistryProviderListOptions,
RegistryProviderReadOptions,
)
def get_client_and_org():
"""Initialize client and get organization name."""
client = TFEClient()
organization_name = os.getenv("TFE_ORGANIZATION", "aayush-test")
return client, organization_name
def test_list_simple():
"""Test 1: Simple list of registry providers."""
print("=== Test 1: List Registry Providers ===")
client, org = get_client_and_org()
try:
providers = list(client.registry_providers.list(org))
print(f"Found {len(providers)} providers in organization '{org}'")
for i, provider in enumerate(providers[:5], 1):
print(f"{i}. {provider.name}")
print(f"Namespace: {provider.namespace}")
print(f"Registry: {provider.registry_name.value}")
print(f"ID: {provider.id}")
print(f"Can Delete: {provider.permissions.can_delete}")
print()
return providers
except Exception as e:
print(f"Error: {e}")
return []
def test_list_with_options():
"""Test 2: List with filtering options."""
print("=== Test 2: List with Options ===")
client, org = get_client_and_org()
try:
# Test with search
options = RegistryProviderListOptions(
search="test", registry_name=RegistryName.PRIVATE, page_size=5
)
providers = list(client.registry_providers.list(org, options))
print(f"Found {len(providers)} providers matching search 'test'")
# Test with include
include_options = RegistryProviderListOptions(
include=[RegistryProviderIncludeOps.REGISTRY_PROVIDER_VERSIONS]
)
detailed_providers = list(client.registry_providers.list(org, include_options))
print(f"Found {len(detailed_providers)} providers with version details")
return providers
except Exception as e:
print(f"Error: {e}")
return []
def test_create_private():
"""Test 3: Create a private registry provider."""
print("=== Test 3: Create Private Provider ===")
client, org = get_client_and_org()
try:
provider_name = f"test-provider-{random.randint(100000, 999999)}"
options = RegistryProviderCreateOptions(
name=provider_name,
namespace=org, # For private providers, namespace = org name
registry_name=RegistryName.PRIVATE,
)
provider = client.registry_providers.create(org, options)
print(f"Created private provider: {provider.name}")
print(f"ID: {provider.id}")
print(f"Namespace: {provider.namespace}")
print(f"Registry: {provider.registry_name.value}")
print(f"Created: {provider.created_at}")
return provider
except Exception as e:
print(f"Error creating private provider: {e}")
return None
def test_create_public():
"""Test 4: Create a public registry provider."""
print("=== Test 4: Create Public Provider ===")
client, org = get_client_and_org()
try:
provider_name = f"test-provider-{random.randint(100000, 999999)}"
namespace_name = f"test-namespace-{random.randint(1000, 9999)}"
options = RegistryProviderCreateOptions(
name=provider_name,
namespace=namespace_name,
registry_name=RegistryName.PUBLIC,
)
provider = client.registry_providers.create(org, options)
print(f"Created public provider: {provider.name}")
print(f"ID: {provider.id}")
print(f"Namespace: {provider.namespace}")
print(f"Registry: {provider.registry_name.value}")
print(f"Created: {provider.created_at}")
return provider
except Exception as e:
print(f"Error creating public provider: {e}")
return None
def test_read_with_id(provider_data):
"""Test 5: Read a provider by ID."""
print("=== Test 5: Read Provider by ID ===")
client, org = get_client_and_org()
if not provider_data:
print("No provider data provided")
return None
try:
provider_id = RegistryProviderID(
organization_name=org,
registry_name=provider_data.registry_name,
namespace=provider_data.namespace,
name=provider_data.name,
)
# Basic read
provider = client.registry_providers.read(provider_id)
print(f"Read provider: {provider.name}")
print(f"ID: {provider.id}")
print(f"Namespace: {provider.namespace}")
print(f"Registry: {provider.registry_name.value}")
print(f"Created: {provider.created_at}")
print(f"Updated: {provider.updated_at}")
print(f"Can Delete: {provider.permissions.can_delete}")
# Read with options
options = RegistryProviderReadOptions(
include=[RegistryProviderIncludeOps.REGISTRY_PROVIDER_VERSIONS]
)
detailed_provider = client.registry_providers.read(provider_id, options)
print(f"Read with options: {detailed_provider.name}")
if detailed_provider.registry_provider_versions:
print(f"Found {len(detailed_provider.registry_provider_versions)} versions")
else:
print("No versions found")
return provider
except Exception as e:
print(f"Error reading provider: {e}")
return None
def test_delete_by_id(provider_data):
"""Test 6: Delete a provider by ID."""
print("=== Test 6: Delete Provider by ID ===")
client, org = get_client_and_org()
if not provider_data:
print("No provider data provided")
return False
try:
provider_id = RegistryProviderID(
organization_name=org,
registry_name=provider_data.registry_name,
namespace=provider_data.namespace,
name=provider_data.name,
)
# Verify provider exists
provider = client.registry_providers.read(provider_id)
print(f"Found provider to delete: {provider.name}")
# Delete the provider
client.registry_providers.delete(provider_id)
print("Successfully called delete() for provider")
# Verify deletion (optional - may take time)
import time
time.sleep(2)
try:
client.registry_providers.read(provider_id)
print("Provider still exists (deletion may take time)")
except Exception:
print("Provider successfully deleted")
return True
except Exception as e:
print(f"Error deleting provider: {e}")
return False
def main():
"""Run all tests in sequence."""
print("REGISTRY PROVIDER INDIVIDUAL TESTS")
print("=" * 50)
# Test 1: List providers
providers = test_list_simple()
print()
# Test 2: List with options
test_list_with_options()
print()
# WARNING: Uncomment the following tests to create/delete providers
print("WARNING: Creation and deletion tests are commented out for safety")
print("Uncomment them in the code to test creation and deletion")
print()
# UNCOMMENT TO TEST CREATION:
# Test 3: Create private provider
private_provider = test_create_private()
print()
# Test 4: Create public provider
public_provider = test_create_public()
print()
# Test 5: Read provider
if private_provider:
test_read_with_id(private_provider)
print()
# Test 6: Delete provider (UNCOMMENT TO TEST DELETION)
if private_provider:
test_delete_by_id(private_provider)
print()
if public_provider:
test_delete_by_id(public_provider)
print()
# Test with existing provider if available
if providers:
print("=== Testing with Existing Provider ===")
existing_provider = providers[0]
test_read_with_id(existing_provider)
print()
print("Individual tests completed!")
print("To test creation/deletion, uncomment the relevant sections in the code")
if __name__ == "__main__":
main()