|
10 | 10 | from typing import Optional |
11 | 11 | import os |
12 | 12 |
|
13 | | -from .base_provider import BaseCloudProvider |
| 13 | +from .base_provider import BaseCloudProvider, BackendParams |
14 | 14 |
|
15 | 15 | # Azure imports |
16 | 16 | try: |
@@ -343,19 +343,19 @@ def delete_ssh_key(self, key_name: str, region: str) -> None: |
343 | 343 | """ |
344 | 344 | self.logger.info("Azure: No cloud service keys to delete") |
345 | 345 |
|
346 | | - def update_backend_config(self, backend_params: dict, backend_file_path: str) -> None: |
| 346 | + def write_backend_config(self, backend_params: BackendParams, backend_file_path: str) -> None: |
347 | 347 | """ |
348 | 348 | Update or create backend.tf file with Azure backend configuration. |
349 | 349 |
|
350 | | - :param backend_params: Dictionary containing backend parameters |
| 350 | + :param backend_params: Backend parameters |
351 | 351 | :param backend_file_path: Path to the backend.tf file |
352 | 352 | """ |
353 | | - storage_account_name = backend_params['storage_account_name'] |
354 | | - container_name = backend_params['container_name'] |
355 | | - resource_group_name = backend_params['resource_group_name'] |
356 | | - location = backend_params['location'] |
357 | | - attack_range_id = backend_params.get('attack_range_id', 'unknown') |
358 | | - config_source = backend_params.get('config_source', 'template/config file') |
| 353 | + storage_account_name = backend_params.azure_storage_account_name |
| 354 | + container_name = backend_params.azure_container_name |
| 355 | + resource_group_name = backend_params.azure_resource_group_name |
| 356 | + location = backend_params.azure_location or backend_params.region |
| 357 | + attack_range_id = backend_params.attack_range_id or 'unknown' |
| 358 | + config_source = backend_params.config_source or 'template/config file' |
359 | 359 |
|
360 | 360 | backend_config = f'''# This file is AUTO-GENERATED based on the template/config file. |
361 | 361 | # DO NOT EDIT MANUALLY - changes will be overwritten. |
@@ -383,3 +383,28 @@ def update_backend_config(self, backend_params: dict, backend_file_path: str) -> |
383 | 383 | f.write(backend_config) |
384 | 384 |
|
385 | 385 | self.logger.info(f"Backend configuration written to {backend_file_path} (generated from {config_source})") |
| 386 | + |
| 387 | + def get_backend_params(self, attack_range_id: str, config_source: str = "template/config file") -> BackendParams: |
| 388 | + """ |
| 389 | + Get backend parameters for Azure (Storage Account + Container). |
| 390 | +
|
| 391 | + :param attack_range_id: The attack range ID for naming |
| 392 | + :param config_source: Source config file name for backend.tf comments |
| 393 | + :return: Backend parameters |
| 394 | + """ |
| 395 | + backend_name = f"terraformstate{attack_range_id.replace('-', '')}" |
| 396 | + storage_account_name = self.sanitize_name(backend_name) |
| 397 | + container_name = "tfstate" |
| 398 | + resource_group_name = f"rg-terraform-state-{attack_range_id}" |
| 399 | + location = self.get_region(required=True) |
| 400 | + |
| 401 | + return BackendParams( |
| 402 | + backend_name=backend_name, |
| 403 | + azure_storage_account_name=storage_account_name, |
| 404 | + azure_container_name=container_name, |
| 405 | + azure_resource_group_name=resource_group_name, |
| 406 | + azure_location=location, |
| 407 | + region=location, |
| 408 | + attack_range_id=attack_range_id, |
| 409 | + config_source=config_source |
| 410 | + ) |
0 commit comments