|
| 1 | +import time |
| 2 | +import traceback |
| 3 | + |
| 4 | +from tfe import TFEClient, TFEConfig |
| 5 | +from tfe.models.run_task import ( |
| 6 | + RunTaskCreateOptions, |
| 7 | + RunTaskIncludeOptions, |
| 8 | + RunTaskListOptions, |
| 9 | + RunTaskReadOptions, |
| 10 | + RunTaskUpdateOptions, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def run_task_list(client, org_name): |
| 15 | + """Test run task list with all options combined.""" |
| 16 | + print(f"=== Testing Run Task List Comprehensive Options for '{org_name}' ===") |
| 17 | + |
| 18 | + # List run tasks with all options |
| 19 | + print("\n1. Listing Run Tasks with All Options Combined:") |
| 20 | + try: |
| 21 | + options = RunTaskListOptions( |
| 22 | + page_number=1, |
| 23 | + page_size=10, |
| 24 | + include=[ |
| 25 | + RunTaskIncludeOptions.RUN_TASK_WORKSPACE_TASKS, |
| 26 | + RunTaskIncludeOptions.RUN_TASK_WORKSPACE, |
| 27 | + ], |
| 28 | + ) |
| 29 | + |
| 30 | + run_task_list = client.run_tasks.list(org_name, options) |
| 31 | + run_tasks = list(run_task_list) |
| 32 | + print(f" ✓ Found {len(run_tasks)} run tasks with comprehensive options") |
| 33 | + |
| 34 | + for i, task in enumerate(run_tasks, 1): |
| 35 | + print(f" {i:2d}. {task.name}") |
| 36 | + print(f" URL: {task.url}") |
| 37 | + print(f" Category: {task.category}") |
| 38 | + print(f" Enabled: {task.enabled}") |
| 39 | + |
| 40 | + # Show description if available |
| 41 | + if task.description: |
| 42 | + print(f" Description: {task.description}") |
| 43 | + |
| 44 | + # Show global configuration details |
| 45 | + if task.global_configuration: |
| 46 | + gc = task.global_configuration |
| 47 | + print(" Global Config:") |
| 48 | + print(f" - Enabled: {gc.enabled}") |
| 49 | + print(f" - Enforcement: {gc.enforcement_level.value}") |
| 50 | + if gc.stages: |
| 51 | + stages = [stage.value for stage in gc.stages] |
| 52 | + print(f" - Stages: {', '.join(stages)}") |
| 53 | + |
| 54 | + # Show relationships |
| 55 | + if task.organization: |
| 56 | + print(f" Organization: {task.organization.id}") |
| 57 | + |
| 58 | + if task.workspace_run_tasks: |
| 59 | + print( |
| 60 | + f" Workspace Run Tasks: {len(task.workspace_run_tasks)} items" |
| 61 | + ) |
| 62 | + |
| 63 | + if task.agent_pool: |
| 64 | + print(f" Agent Pool: {task.agent_pool.id}") |
| 65 | + |
| 66 | + except Exception as e: |
| 67 | + print(f" Error listing run tasks comprehensively: {e}") |
| 68 | + traceback.print_exc() |
| 69 | + |
| 70 | + |
| 71 | +def run_task_create(client, org_name): |
| 72 | + """Create a comprehensive run task that demonstrates all available features.""" |
| 73 | + print("\n=== Creating Comprehensive Demonstration Run Task ===") |
| 74 | + |
| 75 | + try: |
| 76 | + timestamp = int(time.time()) |
| 77 | + |
| 78 | + # Create the most comprehensive example possible |
| 79 | + options = RunTaskCreateOptions( |
| 80 | + name=f"comprehensive-demo-{timestamp}", |
| 81 | + url="https://httpbin.org/post", |
| 82 | + category="task", |
| 83 | + description="A comprehensive demonstration task showcasing all available features and configurations", |
| 84 | + enabled=True, |
| 85 | + hmac_key=f"demo-secret-key-{timestamp}", |
| 86 | + ) |
| 87 | + |
| 88 | + print("\n2. Creating task with the following configuration:") |
| 89 | + created_task = client.run_tasks.create(org_name, options) |
| 90 | + |
| 91 | + print("\n ✓ Successfully created comprehensive run task!") |
| 92 | + print(f" Task Name: {created_task.name}") |
| 93 | + print(f" Task ID: {created_task.id}") |
| 94 | + print(f" URL: {created_task.url}") |
| 95 | + print(f" Enabled: {created_task.enabled}") |
| 96 | + print(f" Description: {created_task.description}") |
| 97 | + |
| 98 | + # Display additional details |
| 99 | + if created_task.organization: |
| 100 | + print(f" Organization: {created_task.organization.id}") |
| 101 | + |
| 102 | + if created_task.hmac_key: |
| 103 | + print(" HMAC Key: ***configured***") |
| 104 | + |
| 105 | + return created_task.id, created_task.name |
| 106 | + |
| 107 | + except Exception as e: |
| 108 | + print(f" ✗ Error creating comprehensive run task: {e}") |
| 109 | + return None, None |
| 110 | + |
| 111 | + |
| 112 | +def run_task_read(client, task_id, task_name): |
| 113 | + """Read and display details of a specific run task.""" |
| 114 | + try: |
| 115 | + print(f"\n4. Reading Run Task '{task_name}' (ID: {task_id})") |
| 116 | + read_task = client.run_tasks.read(task_id) |
| 117 | + |
| 118 | + print("\n ✓ Successfully read run task:") |
| 119 | + print(f" Task Name: {read_task.name}") |
| 120 | + print(f" Task ID: {read_task.id}") |
| 121 | + print(f" URL: {read_task.url}") |
| 122 | + print(f" Category: {read_task.category}") |
| 123 | + print(f" Enabled: {read_task.enabled}") |
| 124 | + print(f" Description: {read_task.description or 'None'}") |
| 125 | + print(f" HMAC Key: {'[SET]' if read_task.hmac_key else 'None'}") |
| 126 | + |
| 127 | + if read_task.organization: |
| 128 | + print(f" Organization: {read_task.organization.id}") |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + print(f" ✗ Error reading run task '{task_name}': {e}") |
| 132 | + traceback.print_exc() |
| 133 | + |
| 134 | + |
| 135 | +def run_task_read_with_options(client, task_id, task_name): |
| 136 | + """Read a specific run task with include options.""" |
| 137 | + try: |
| 138 | + options = RunTaskReadOptions( |
| 139 | + include=[RunTaskIncludeOptions.RUN_TASK_WORKSPACE_TASKS] |
| 140 | + ) |
| 141 | + print( |
| 142 | + f"\n5. Reading Run Task '{task_name}' (ID: {task_id}) with includes: {options}" |
| 143 | + ) |
| 144 | + read_task_with_option = client.run_tasks.read_with_options(task_id, options) |
| 145 | + |
| 146 | + print("\n ✓ Successfully read run task with includes:") |
| 147 | + print(f" Task Name: {read_task_with_option.name}") |
| 148 | + print(f" Task ID: {read_task_with_option.id}") |
| 149 | + print(f" URL: {read_task_with_option.url}") |
| 150 | + print(f" Category: {read_task_with_option.category}") |
| 151 | + |
| 152 | + if RunTaskIncludeOptions.RUN_TASK_WORKSPACE_TASKS in options.include: |
| 153 | + print( |
| 154 | + " (Workspace tasks relationship data would be included in API response)" |
| 155 | + ) |
| 156 | + |
| 157 | + if RunTaskIncludeOptions.RUN_TASK_WORKSPACE in options.include: |
| 158 | + print(" (Workspace data would be included in API response)") |
| 159 | + |
| 160 | + except Exception as e: |
| 161 | + print(f" ✗ Error reading run task '{task_name}' with includes: {e}") |
| 162 | + traceback.print_exc() |
| 163 | + |
| 164 | + |
| 165 | +def run_task_update(client, task_id): |
| 166 | + """Update various fields of a specific run task.""" |
| 167 | + print(f"\n=== Updating Run Task (ID: {task_id}) with Various Configurations ===") |
| 168 | + |
| 169 | + try: |
| 170 | + # Update basic fields |
| 171 | + print("\n3. Updating basic fields (name, description, url)...") |
| 172 | + update_options = RunTaskUpdateOptions( |
| 173 | + name=f"updated-name-{int(time.time())}", |
| 174 | + description="Updated description for the run task", |
| 175 | + url="https://httpbin.org/anything", |
| 176 | + ) |
| 177 | + updated_task = client.run_tasks.update(task_id, update_options) |
| 178 | + |
| 179 | + print(" Successfully updated basic fields:") |
| 180 | + print(f" Name: {updated_task.name}") |
| 181 | + print(f" Description: {updated_task.description}") |
| 182 | + print(f" URL: {updated_task.url}") |
| 183 | + |
| 184 | + except Exception as e: |
| 185 | + print(f" Error updating basic fields: {e}") |
| 186 | + |
| 187 | + |
| 188 | +def run_task_delete(client, task_id, task_name): |
| 189 | + """Delete a specific run task.""" |
| 190 | + try: |
| 191 | + print(f"\n6. Deleting Run Task '{task_name}' (ID: {task_id})") |
| 192 | + client.run_tasks.delete(task_id) |
| 193 | + print(f"\n ✓ Successfully deleted run task: {task_name} (ID: {task_id})") |
| 194 | + return True |
| 195 | + |
| 196 | + except Exception as e: |
| 197 | + print(f" ✗ Error deleting run task '{task_name}': {e}") |
| 198 | + return False |
| 199 | + |
| 200 | + |
| 201 | +def main(): |
| 202 | + """Main function to demonstrate comprehensive run task list operations.""" |
| 203 | + print("Run Task List - Comprehensive Example") |
| 204 | + print("=" * 50) |
| 205 | + |
| 206 | + # Initialize client |
| 207 | + config = TFEConfig() |
| 208 | + client = TFEClient(config) |
| 209 | + |
| 210 | + # Replace 'your-org-name' with an actual organization name |
| 211 | + org_name = "your-org-name" |
| 212 | + |
| 213 | + print(f"Using organization: {org_name}") |
| 214 | + |
| 215 | + try: |
| 216 | + # Test comprehensive list operations |
| 217 | + run_task_list(client, org_name) |
| 218 | + task_id, task_name = run_task_create(client, org_name) |
| 219 | + if task_id: |
| 220 | + run_task_update(client, task_id) |
| 221 | + if task_id and task_name: |
| 222 | + run_task_read(client, task_id, task_name) |
| 223 | + run_task_read_with_options(client, task_id, task_name) |
| 224 | + run_task_delete(client, task_id, task_name) |
| 225 | + |
| 226 | + except Exception as e: |
| 227 | + print(f"\n Example failed: {e}") |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + main() |
0 commit comments