awslabs.terraform-mcp-server 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of awslabs.terraform-mcp-server might be problematic. Click here for more details.

Files changed (32) hide show
  1. awslabs/__init__.py +2 -0
  2. awslabs/terraform_mcp_server/__init__.py +3 -0
  3. awslabs/terraform_mcp_server/impl/resources/__init__.py +11 -0
  4. awslabs/terraform_mcp_server/impl/resources/terraform_aws_provider_resources_listing.py +52 -0
  5. awslabs/terraform_mcp_server/impl/resources/terraform_awscc_provider_resources_listing.py +55 -0
  6. awslabs/terraform_mcp_server/impl/tools/__init__.py +15 -0
  7. awslabs/terraform_mcp_server/impl/tools/execute_terraform_command.py +206 -0
  8. awslabs/terraform_mcp_server/impl/tools/run_checkov_scan.py +359 -0
  9. awslabs/terraform_mcp_server/impl/tools/search_aws_provider_docs.py +677 -0
  10. awslabs/terraform_mcp_server/impl/tools/search_awscc_provider_docs.py +627 -0
  11. awslabs/terraform_mcp_server/impl/tools/search_specific_aws_ia_modules.py +444 -0
  12. awslabs/terraform_mcp_server/impl/tools/utils.py +558 -0
  13. awslabs/terraform_mcp_server/models/__init__.py +27 -0
  14. awslabs/terraform_mcp_server/models/models.py +260 -0
  15. awslabs/terraform_mcp_server/scripts/generate_aws_provider_resources.py +1224 -0
  16. awslabs/terraform_mcp_server/scripts/generate_awscc_provider_resources.py +1020 -0
  17. awslabs/terraform_mcp_server/scripts/scrape_aws_terraform_best_practices.py +129 -0
  18. awslabs/terraform_mcp_server/server.py +329 -0
  19. awslabs/terraform_mcp_server/static/AWSCC_PROVIDER_RESOURCES.md +3125 -0
  20. awslabs/terraform_mcp_server/static/AWS_PROVIDER_RESOURCES.md +3833 -0
  21. awslabs/terraform_mcp_server/static/AWS_TERRAFORM_BEST_PRACTICES.md +2523 -0
  22. awslabs/terraform_mcp_server/static/MCP_INSTRUCTIONS.md +126 -0
  23. awslabs/terraform_mcp_server/static/TERRAFORM_WORKFLOW_GUIDE.md +198 -0
  24. awslabs/terraform_mcp_server/static/__init__.py +22 -0
  25. awslabs/terraform_mcp_server/tests/__init__.py +1 -0
  26. awslabs/terraform_mcp_server/tests/run_tests.sh +35 -0
  27. awslabs/terraform_mcp_server/tests/test_parameter_annotations.py +207 -0
  28. awslabs/terraform_mcp_server/tests/test_tool_implementations.py +309 -0
  29. awslabs_terraform_mcp_server-0.0.1.dist-info/METADATA +97 -0
  30. awslabs_terraform_mcp_server-0.0.1.dist-info/RECORD +32 -0
  31. awslabs_terraform_mcp_server-0.0.1.dist-info/WHEEL +4 -0
  32. awslabs_terraform_mcp_server-0.0.1.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,627 @@
1
+ """Implementation of AWSCC provider documentation search tool."""
2
+
3
+ import re
4
+ import requests
5
+ import sys
6
+ import time
7
+ from awslabs.terraform_mcp_server.models import TerraformAWSCCProviderDocsResult
8
+ from loguru import logger
9
+ from pathlib import Path
10
+ from typing import Any, Dict, List, Literal, Optional, Tuple, cast
11
+
12
+
13
+ # Configure logger for enhanced diagnostics with stacktraces
14
+ logger.configure(
15
+ handlers=[
16
+ {
17
+ 'sink': sys.stderr,
18
+ 'backtrace': True,
19
+ 'diagnose': True,
20
+ 'format': '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>',
21
+ }
22
+ ]
23
+ )
24
+
25
+ # Path to the static markdown file
26
+ STATIC_RESOURCES_PATH = (
27
+ Path(__file__).parent.parent.parent / 'static' / 'AWSCC_PROVIDER_RESOURCES.md'
28
+ )
29
+
30
+ # Base URLs for AWSCC provider documentation
31
+ AWSCC_DOCS_BASE_URL = 'https://registry.terraform.io/providers/hashicorp/awscc/latest/docs'
32
+ GITHUB_RAW_BASE_URL = (
33
+ 'https://raw.githubusercontent.com/hashicorp/terraform-provider-awscc/main/docs'
34
+ )
35
+
36
+ # Simple in-memory cache
37
+ _GITHUB_DOC_CACHE = {}
38
+
39
+
40
+ def resource_to_github_path(
41
+ asset_name: str, asset_type: str = 'resource', correlation_id: str = ''
42
+ ) -> Tuple[str, str]:
43
+ """Convert AWSCC resource type to GitHub documentation file path.
44
+
45
+ Args:
46
+ asset_name: The name of the asset to search (e.g., 'awscc_s3_bucket')
47
+ asset_type: Type of asset to search for - 'resource' or 'data_source'
48
+ correlation_id: Identifier for tracking this request in logs
49
+
50
+ Returns:
51
+ A tuple of (path, url) for the GitHub documentation file
52
+ """
53
+ # Validate input parameters
54
+ if not isinstance(asset_name, str) or not asset_name:
55
+ logger.error(f'[{correlation_id}] Invalid asset_name: {asset_name}')
56
+ raise ValueError('asset_name must be a non-empty string')
57
+
58
+ # Sanitize asset_name to prevent path traversal and URL manipulation
59
+ # Only allow alphanumeric characters, underscores, and hyphens
60
+ sanitized_name = asset_name
61
+ if not re.match(r'^[a-zA-Z0-9_-]+$', sanitized_name.replace('awscc_', '')):
62
+ logger.error(f'[{correlation_id}] Invalid characters in asset_name: {asset_name}')
63
+ raise ValueError('asset_name contains invalid characters')
64
+
65
+ # Validate asset_type
66
+ valid_asset_types = ['resource', 'data_source', 'both']
67
+ if asset_type not in valid_asset_types:
68
+ logger.error(f'[{correlation_id}] Invalid asset_type: {asset_type}')
69
+ raise ValueError(f'asset_type must be one of {valid_asset_types}')
70
+
71
+ # Remove the 'awscc_' prefix if present
72
+ if sanitized_name.startswith('awscc_'):
73
+ resource_name = sanitized_name[6:]
74
+ logger.trace(f"[{correlation_id}] Removed 'awscc_' prefix: {resource_name}")
75
+ else:
76
+ resource_name = sanitized_name
77
+ logger.trace(f"[{correlation_id}] No 'awscc_' prefix to remove: {resource_name}")
78
+
79
+ # Determine document type based on asset_type parameter
80
+ if asset_type == 'data_source':
81
+ doc_type = 'data-sources' # data sources
82
+ elif asset_type == 'resource':
83
+ doc_type = 'resources' # resources
84
+ else:
85
+ # For "both" or any other value, determine based on name pattern
86
+ # Data sources typically have 'data' in the name or follow other patterns
87
+ is_data_source = 'data' in sanitized_name.lower()
88
+ doc_type = 'data-sources' if is_data_source else 'resources'
89
+
90
+ # Create the file path for the markdown documentation
91
+ file_path = f'{doc_type}/{resource_name}.md'
92
+ logger.trace(f'[{correlation_id}] Constructed GitHub file path: {file_path}')
93
+
94
+ # Create the full URL to the raw GitHub content
95
+ github_url = f'{GITHUB_RAW_BASE_URL}/{file_path}'
96
+ logger.trace(f'[{correlation_id}] GitHub raw URL: {github_url}')
97
+
98
+ return file_path, github_url
99
+
100
+
101
+ def fetch_github_documentation(
102
+ asset_name: str, asset_type: str, cache_enabled: bool, correlation_id: str = ''
103
+ ) -> Optional[Dict[str, Any]]:
104
+ """Fetch documentation from GitHub for a specific resource type.
105
+
106
+ Args:
107
+ asset_name: The asset name (e.g., 'awscc_s3_bucket')
108
+ asset_type: Either 'resource' or 'data_source'
109
+ cache_enabled: Whether local cache is enabled or not
110
+ correlation_id: Identifier for tracking this request in logs
111
+
112
+ Returns:
113
+ Dictionary with markdown content and metadata, or None if not found
114
+ """
115
+ start_time = time.time()
116
+ logger.info(f"[{correlation_id}] Fetching documentation from GitHub for '{asset_name}'")
117
+
118
+ # Create a cache key that includes both asset_name and asset_type
119
+ # Use a hash function to ensure the cache key is safe
120
+ cache_key = f'{asset_name}_{asset_type}'
121
+
122
+ # Check cache first
123
+ if cache_enabled:
124
+ if cache_key in _GITHUB_DOC_CACHE:
125
+ logger.info(
126
+ f"[{correlation_id}] Using cached documentation for '{asset_name}' (asset_type: {asset_type})"
127
+ )
128
+ return _GITHUB_DOC_CACHE[cache_key]
129
+
130
+ try:
131
+ # Convert resource type to GitHub path and URL
132
+ # This will validate and sanitize the input
133
+ try:
134
+ _, github_url = resource_to_github_path(asset_name, asset_type, correlation_id)
135
+ except ValueError as e:
136
+ logger.error(f'[{correlation_id}] Invalid input parameters: {str(e)}')
137
+ return None
138
+
139
+ # Validate the constructed URL to ensure it points to the expected domain
140
+ if not github_url.startswith(GITHUB_RAW_BASE_URL):
141
+ logger.error(f'[{correlation_id}] Invalid GitHub URL constructed: {github_url}')
142
+ return None
143
+
144
+ # Fetch the markdown content from GitHub
145
+ logger.info(f'[{correlation_id}] Fetching from GitHub: {github_url}')
146
+ response = requests.get(github_url, timeout=10)
147
+
148
+ if response.status_code != 200:
149
+ logger.warning(
150
+ f'[{correlation_id}] GitHub request failed: HTTP {response.status_code}'
151
+ )
152
+ return None
153
+
154
+ markdown_content = response.text
155
+ content_length = len(markdown_content)
156
+ logger.debug(f'[{correlation_id}] Received markdown content: {content_length} bytes')
157
+
158
+ if content_length > 0:
159
+ preview_length = min(200, content_length)
160
+ logger.trace(
161
+ f'[{correlation_id}] Markdown preview: {markdown_content[:preview_length]}...'
162
+ )
163
+
164
+ # Parse the markdown content
165
+ result = parse_markdown_documentation(
166
+ markdown_content, asset_name, github_url, correlation_id
167
+ )
168
+
169
+ # Cache the result with the composite key
170
+ if cache_enabled:
171
+ _GITHUB_DOC_CACHE[cache_key] = result
172
+
173
+ fetch_time = time.time() - start_time
174
+ logger.info(f'[{correlation_id}] GitHub documentation fetched in {fetch_time:.2f} seconds')
175
+ return result
176
+
177
+ except requests.exceptions.Timeout as e:
178
+ logger.warning(f'[{correlation_id}] Timeout error fetching from GitHub: {str(e)}')
179
+ return None
180
+ except requests.exceptions.RequestException as e:
181
+ logger.warning(f'[{correlation_id}] Request error fetching from GitHub: {str(e)}')
182
+ return None
183
+ except Exception as e:
184
+ logger.error(
185
+ f'[{correlation_id}] Unexpected error fetching from GitHub: {type(e).__name__}: {str(e)}'
186
+ )
187
+ # Don't log the full stack trace to avoid information disclosure
188
+ return None
189
+
190
+
191
+ def parse_markdown_documentation(
192
+ content: str,
193
+ asset_name: str,
194
+ url: str,
195
+ correlation_id: str = '',
196
+ ) -> Dict[str, Any]:
197
+ """Parse markdown documentation content for a resource.
198
+
199
+ Args:
200
+ content: The markdown content
201
+ asset_name: The asset name
202
+ url: The source URL for this documentation
203
+ correlation_id: Identifier for tracking this request in logs
204
+
205
+ Returns:
206
+ Dictionary with parsed documentation details
207
+ """
208
+ start_time = time.time()
209
+ logger.debug(f"[{correlation_id}] Parsing markdown documentation for '{asset_name}'")
210
+
211
+ try:
212
+ # Find the title (typically the first heading)
213
+ title_match = re.search(r'^#\s+(.*?)$', content, re.MULTILINE)
214
+ if title_match:
215
+ title = title_match.group(1).strip()
216
+ logger.debug(f"[{correlation_id}] Found title: '{title}'")
217
+ else:
218
+ title = f'AWS {asset_name}'
219
+ logger.debug(f"[{correlation_id}] No title found, using default: '{title}'")
220
+
221
+ # Find the main resource description section (all content after resource title before next heading)
222
+ description = ''
223
+ resource_heading_pattern = re.compile(
224
+ rf'# {re.escape(asset_name)}\s+\(Resource\)\s*(.*?)(?=\n#|\Z)', re.DOTALL
225
+ )
226
+ resource_match = resource_heading_pattern.search(content)
227
+
228
+ if resource_match:
229
+ # Extract the description text and clean it up
230
+ description = resource_match.group(1).strip()
231
+ logger.debug(
232
+ f"[{correlation_id}] Found resource description section: '{description[:100]}...'"
233
+ )
234
+ else:
235
+ # Fall back to the description found on the starting markdown table of each github markdown page
236
+ desc_match = re.search(r'description:\s*\|-\n(.*?)\n---', content, re.MULTILINE)
237
+ if desc_match:
238
+ description = desc_match.group(1).strip()
239
+ logger.debug(
240
+ f"[{correlation_id}] Using fallback description: '{description[:100]}...'"
241
+ )
242
+ else:
243
+ description = f'Documentation for AWSCC {asset_name}'
244
+ logger.debug(f'[{correlation_id}] No description found, using default')
245
+
246
+ # Find all example snippets
247
+ example_snippets = []
248
+
249
+ # First try to extract from the Example Usage section
250
+ example_section_match = re.search(r'## Example Usage\n([\s\S]*?)(?=\n## |\Z)', content)
251
+
252
+ if example_section_match:
253
+ # logger.debug(f"example_section_match: {example_section_match.group()}")
254
+ example_section = example_section_match.group(1).strip()
255
+ logger.debug(
256
+ f'[{correlation_id}] Found Example Usage section ({len(example_section)} chars)'
257
+ )
258
+
259
+ # Find all subheadings in the Example Usage section with a more robust pattern
260
+ subheading_list = list(
261
+ re.finditer(r'### (.*?)[\r\n]+(.*?)(?=###|\Z)', example_section, re.DOTALL)
262
+ )
263
+ logger.debug(
264
+ f'[{correlation_id}] Found {len(subheading_list)} subheadings in Example Usage section'
265
+ )
266
+ subheading_found = False
267
+
268
+ # Check if there are any subheadings
269
+ for match in subheading_list:
270
+ # logger.info(f"subheading match: {match.group()}")
271
+ subheading_found = True
272
+ title = match.group(1).strip()
273
+ subcontent = match.group(2).strip()
274
+
275
+ logger.debug(
276
+ f"[{correlation_id}] Found subheading '{title}' with {len(subcontent)} chars content"
277
+ )
278
+
279
+ # Find code blocks in this subsection - pattern to match terraform code blocks
280
+ code_match = re.search(r'```(?:terraform|hcl)?\s*(.*?)```', subcontent, re.DOTALL)
281
+ if code_match:
282
+ code_snippet = code_match.group(1).strip()
283
+ example_snippets.append({'title': title, 'code': code_snippet})
284
+ logger.debug(
285
+ f"[{correlation_id}] Added example snippet for '{title}' ({len(code_snippet)} chars)"
286
+ )
287
+
288
+ # If no subheadings were found, look for direct code blocks under Example Usage
289
+ if not subheading_found:
290
+ logger.debug(
291
+ f'[{correlation_id}] No subheadings found, looking for direct code blocks'
292
+ )
293
+ # Improved pattern for code blocks
294
+ code_blocks = re.finditer(
295
+ r'```(?:terraform|hcl)?\s*(.*?)```', example_section, re.DOTALL
296
+ )
297
+ code_found = False
298
+
299
+ for code_match in code_blocks:
300
+ code_found = True
301
+ code_snippet = code_match.group(1).strip()
302
+ example_snippets.append({'title': 'Example Usage', 'code': code_snippet})
303
+ logger.debug(
304
+ f'[{correlation_id}] Added direct example snippet ({len(code_snippet)} chars)'
305
+ )
306
+
307
+ if not code_found:
308
+ logger.debug(
309
+ f'[{correlation_id}] No code blocks found in Example Usage section'
310
+ )
311
+ else:
312
+ logger.debug(f'[{correlation_id}] No Example Usage section found')
313
+
314
+ if example_snippets:
315
+ logger.info(f'[{correlation_id}] Found {len(example_snippets)} example snippets')
316
+ else:
317
+ logger.debug(f'[{correlation_id}] No example snippets found')
318
+
319
+ # Extract Schema section
320
+ schema_arguments = []
321
+ schema_section_match = re.search(r'## Schema\n([\s\S]*?)(?=\n## |\Z)', content)
322
+ if schema_section_match:
323
+ schema_section = schema_section_match.group(1).strip()
324
+ logger.debug(f'[{correlation_id}] Found Schema section ({len(schema_section)} chars)')
325
+
326
+ # DO NOT Look for schema arguments directly under the main Schema section
327
+ # args_under_main_section_match = re.search(r'(.*?)(?=\n###|\n##|$)', schema_section, re.DOTALL)
328
+ # if args_under_main_section_match:
329
+ # args_under_main_section = args_under_main_section_match.group(1).strip()
330
+ # logger.debug(
331
+ # f'[{correlation_id}] Found arguments directly under the Schema section ({len(args_under_main_section)} chars)'
332
+ # )
333
+
334
+ # # Find arguments in this subsection
335
+ # arg_matches = re.finditer(
336
+ # r'-\s+`([^`]+)`\s+(.*?)(?=\n-\s+`|$)',
337
+ # args_under_main_section,
338
+ # re.DOTALL,
339
+ # )
340
+ # arg_list = list(arg_matches)
341
+ # logger.debug(
342
+ # f'[{correlation_id}] Found {len(arg_list)} arguments directly under the Argument Reference section'
343
+ # )
344
+
345
+ # for match in arg_list:
346
+ # arg_name = match.group(1).strip()
347
+ # arg_desc = match.group(2).strip() if match.group(2) else None
348
+ # # Do not add arguments that do not have a description
349
+ # if arg_name is not None and arg_desc is not None:
350
+ # schema_arguments.append({'name': arg_name, 'description': arg_desc, 'schema_section': "main"})
351
+ # logger.debug(
352
+ # f"[{correlation_id}] Added argument '{arg_name}': '{arg_desc[:50]}...' (truncated)"
353
+ # )
354
+
355
+ # Now, Find all subheadings in the Argument Reference section with a more robust pattern
356
+ subheading_list = list(
357
+ re.finditer(r'### (.*?)[\r\n]+(.*?)(?=###|\Z)', schema_section, re.DOTALL)
358
+ )
359
+ logger.debug(
360
+ f'[{correlation_id}] Found {len(subheading_list)} subheadings in Argument Reference section'
361
+ )
362
+ subheading_found = False
363
+
364
+ # Check if there are any subheadings
365
+ for match in subheading_list:
366
+ subheading_found = True
367
+ title = match.group(1).strip()
368
+ subcontent = match.group(2).strip()
369
+ logger.debug(
370
+ f"[{correlation_id}] Found subheading '{title}' with {len(subcontent)} chars content"
371
+ )
372
+
373
+ # Find arguments in this subsection
374
+ arg_matches = re.finditer(
375
+ r'-\s+`([^`]+)`\s+(.*?)(?=\n-\s+`|$)',
376
+ subcontent,
377
+ re.DOTALL,
378
+ )
379
+ arg_list = list(arg_matches)
380
+ logger.debug(
381
+ f'[{correlation_id}] Found {len(arg_list)} arguments in subheading {title}'
382
+ )
383
+
384
+ for match in arg_list:
385
+ arg_name = match.group(1).strip()
386
+ arg_desc = match.group(2).strip() if match.group(2) else None
387
+ # Do not add arguments that do not have a description
388
+ if arg_name is not None and arg_desc is not None:
389
+ schema_arguments.append(
390
+ {'name': arg_name, 'description': arg_desc, 'argument_section': title}
391
+ )
392
+ else:
393
+ logger.debug(
394
+ f"[{correlation_id}] Added argument '{arg_name}': '{arg_desc[:50] if arg_desc else 'No description found'}...' (truncated)"
395
+ )
396
+
397
+ schema_arguments = schema_arguments if schema_arguments else None
398
+ if schema_arguments:
399
+ logger.info(
400
+ f'[{correlation_id}] Found {len(schema_arguments)} arguments across all sections'
401
+ )
402
+ else:
403
+ logger.debug(f'[{correlation_id}] No Schema section found')
404
+
405
+ # Return the parsed information
406
+ parse_time = time.time() - start_time
407
+ logger.debug(f'[{correlation_id}] Markdown parsing completed in {parse_time:.2f} seconds')
408
+
409
+ return {
410
+ 'title': title,
411
+ 'description': description,
412
+ 'example_snippets': example_snippets if example_snippets else None,
413
+ 'url': url,
414
+ 'schema_arguments': schema_arguments,
415
+ }
416
+
417
+ except Exception as e:
418
+ logger.exception(f'[{correlation_id}] Error parsing markdown content')
419
+ logger.error(f'[{correlation_id}] Error type: {type(e).__name__}, message: {str(e)}')
420
+
421
+ # Return partial info if available
422
+ return {
423
+ 'title': f'AWSCC {asset_name}',
424
+ 'description': f'Documentation for AWSCC {asset_name} (Error parsing details: {str(e)})',
425
+ 'url': url,
426
+ 'example_snippets': None,
427
+ 'schema_arguments': None,
428
+ }
429
+
430
+
431
+ async def search_awscc_provider_docs_impl(
432
+ asset_name: str, asset_type: str = 'resource', cache_enabled: bool = False
433
+ ) -> List[TerraformAWSCCProviderDocsResult]:
434
+ """Search AWSCC provider documentation for resources and data sources.
435
+
436
+ This tool searches the Terraform AWSCC provider documentation for information about
437
+ specific assets, which can either be resources or data sources. It retrieves comprehensive details including
438
+ descriptions, example code snippets, and schema information.
439
+
440
+ The AWSCC provider is based on the AWS Cloud Control API and provides a more consistent interface to AWS resources compared to the standard AWS provider.
441
+
442
+ The implementation fetches documentation directly from the official Terraform AWSCC provider
443
+ GitHub repository to ensure the most up-to-date information. Results are cached for
444
+ improved performance on subsequent queries.
445
+
446
+ The tool retrieves comprehensive details including descriptions, example code snippets,
447
+ and schema information (required, optional, and read-only attributes). It also handles
448
+ nested schema structures for complex attributes.
449
+
450
+ The tool will automatically handle prefixes - you can search for either 'awscc_s3_bucket' or 's3_bucket'.
451
+
452
+ Examples:
453
+ - To get documentation for an S3 bucket resource:
454
+ search_awscc_provider_docs_impl(resource_type='awscc_s3_bucket')
455
+
456
+ - To find information about a specific attribute:
457
+ search_awscc_provider_docs_impl(resource_type='awscc_lambda_function', attribute='code')
458
+
459
+ - Without the prefix:
460
+ search_awscc_provider_docs_impl(resource_type='ec2_instance')
461
+
462
+ Parameters:
463
+ asset_name: Name of the AWSCC Provider resource or data source to look for (e.g., 'awscc_s3_bucket', 'awscc_lambda_function')
464
+ asset_type: Type of documentation to search - 'resource' (default), 'data_source', or 'both'. Some resources and data sources share the same name
465
+
466
+ Returns:
467
+ A list of matching documentation entries with details including:
468
+ - Resource name and description
469
+ - URL to the official documentation
470
+ - Example code snippets
471
+ - Schema information (required, optional, read-only, and nested structures attributes)
472
+ """
473
+ start_time = time.time()
474
+ correlation_id = f'search-{int(start_time * 1000)}'
475
+ logger.info(f"[{correlation_id}] Starting AWSCC provider docs search for '{asset_name}'")
476
+
477
+ # Validate input parameters
478
+ if not isinstance(asset_name, str) or not asset_name:
479
+ logger.error(f'[{correlation_id}] Invalid asset_name parameter: {asset_name}')
480
+ return [
481
+ TerraformAWSCCProviderDocsResult(
482
+ asset_name='Error',
483
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
484
+ description='Invalid asset_name parameter. Must be a non-empty string.',
485
+ url=None,
486
+ example_usage=None,
487
+ schema_arguments=None,
488
+ )
489
+ ]
490
+
491
+ # Validate asset_type
492
+ valid_asset_types = ['resource', 'data_source', 'both']
493
+ if asset_type not in valid_asset_types:
494
+ logger.error(f'[{correlation_id}] Invalid asset_type parameter: {asset_type}')
495
+ return [
496
+ TerraformAWSCCProviderDocsResult(
497
+ asset_name='Error',
498
+ asset_type=cast(Literal['both', 'resource', 'data_source'], 'resource'),
499
+ description=f'Invalid asset_type parameter. Must be one of {valid_asset_types}.',
500
+ url=None,
501
+ example_usage=None,
502
+ schema_arguments=None,
503
+ )
504
+ ]
505
+
506
+ search_term = asset_name.lower()
507
+
508
+ try:
509
+ # Try fetching from GitHub
510
+ logger.info(f'[{correlation_id}] Fetching from GitHub')
511
+
512
+ results = []
513
+
514
+ # If asset_type is "both", try both resource and data source paths
515
+ if asset_type == 'both':
516
+ logger.info(f'[{correlation_id}] Searching for both resources and data sources')
517
+
518
+ # First try as a resource
519
+ github_result = fetch_github_documentation(
520
+ search_term, 'resource', cache_enabled, correlation_id
521
+ )
522
+ if github_result:
523
+ logger.info(f'[{correlation_id}] Found documentation as a resource')
524
+ # Create result object
525
+ description = github_result['description']
526
+
527
+ result = TerraformAWSCCProviderDocsResult(
528
+ asset_name=asset_name,
529
+ asset_type='resource',
530
+ description=description,
531
+ url=github_result['url'],
532
+ example_usage=github_result.get('example_snippets'),
533
+ schema_arguments=github_result.get('schema_arguments'),
534
+ )
535
+ results.append(result)
536
+
537
+ # Then try as a data source
538
+ data_result = fetch_github_documentation(
539
+ search_term, 'data_source', cache_enabled, correlation_id
540
+ )
541
+ if data_result:
542
+ logger.info(f'[{correlation_id}] Found documentation as a data source')
543
+ # Create result object
544
+ description = data_result['description']
545
+
546
+ result = TerraformAWSCCProviderDocsResult(
547
+ asset_name=asset_name,
548
+ asset_type='data_source',
549
+ description=description,
550
+ url=data_result['url'],
551
+ example_usage=data_result.get('example_snippets'),
552
+ schema_arguments=data_result.get('schema_arguments'),
553
+ )
554
+ results.append(result)
555
+
556
+ if results:
557
+ logger.info(f'[{correlation_id}] Found {len(results)} documentation entries')
558
+ end_time = time.time()
559
+ logger.info(
560
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (GitHub source)'
561
+ )
562
+ return results
563
+ else:
564
+ # Search for either resource or data source based on asset_type parameter
565
+ github_result = fetch_github_documentation(
566
+ search_term, asset_type, cache_enabled, correlation_id
567
+ )
568
+ if github_result:
569
+ logger.info(f'[{correlation_id}] Successfully found GitHub documentation')
570
+
571
+ # Create result object
572
+ description = github_result['description']
573
+ result = TerraformAWSCCProviderDocsResult(
574
+ asset_name=asset_name,
575
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
576
+ description=description,
577
+ url=github_result['url'],
578
+ example_usage=github_result.get('example_snippets'),
579
+ schema_arguments=github_result.get('schema_arguments'),
580
+ )
581
+
582
+ end_time = time.time()
583
+ logger.info(
584
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (GitHub source)'
585
+ )
586
+ return [result]
587
+
588
+ # If GitHub approach fails, return a "not found" result
589
+ logger.warning(f"[{correlation_id}] Documentation not found on GitHub for '{search_term}'")
590
+
591
+ # Return a "not found" result
592
+ logger.warning(f'[{correlation_id}] No documentation found for asset {asset_name}')
593
+ end_time = time.time()
594
+ logger.info(
595
+ f'[{correlation_id}] Search completed in {end_time - start_time:.2f} seconds (no results)'
596
+ )
597
+ return [
598
+ TerraformAWSCCProviderDocsResult(
599
+ asset_name='Not found',
600
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
601
+ description=f"No documentation found for resource type '{asset_name}'.",
602
+ url=None,
603
+ example_usage=None,
604
+ schema_arguments=None,
605
+ )
606
+ ]
607
+
608
+ except Exception as e:
609
+ logger.error(
610
+ f'[{correlation_id}] Error searching AWSCC provider docs: {type(e).__name__}: {str(e)}'
611
+ )
612
+ # Don't log the full stack trace to avoid information disclosure
613
+
614
+ end_time = time.time()
615
+ logger.info(f'[{correlation_id}] Search failed in {end_time - start_time:.2f} seconds')
616
+
617
+ # Return a generic error message without exposing internal details
618
+ return [
619
+ TerraformAWSCCProviderDocsResult(
620
+ asset_name='Error',
621
+ asset_type=cast(Literal['both', 'resource', 'data_source'], asset_type),
622
+ description='Failed to search AWSCC provider documentation. Please check your input and try again.',
623
+ url=None,
624
+ example_usage=None,
625
+ schema_arguments=None,
626
+ )
627
+ ]