maproom 0.2.0__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.
- maproom/__init__.py +14 -0
- maproom/__main__.py +10 -0
- maproom/analyze/__init__.py +1 -0
- maproom/analyze/accel_families.py +45 -0
- maproom/analyze/account_azure_activity.py +260 -0
- maproom/analyze/account_azure_quota_requests.py +177 -0
- maproom/analyze/account_cloudtrail.py +316 -0
- maproom/analyze/account_quota_requests.py +180 -0
- maproom/analyze/analyze_v1_orchestration.py +1194 -0
- maproom/analyze/catalog.py +353 -0
- maproom/analyze/change_detection.py +652 -0
- maproom/analyze/context_client.py +99 -0
- maproom/analyze/context_lookup.py +142 -0
- maproom/analyze/cover_analyze.py +344 -0
- maproom/analyze/cover_client.py +65 -0
- maproom/analyze/cover_lookup.py +242 -0
- maproom/analyze/engine.py +644 -0
- maproom/analyze/fleetfit_endpoints.py +217 -0
- maproom/analyze/fleetfit_policy.py +66 -0
- maproom/analyze/instance_price_analyze.py +221 -0
- maproom/analyze/instance_price_client.py +108 -0
- maproom/analyze/instance_price_lookup.py +60 -0
- maproom/analyze/local_reads.py +484 -0
- maproom/analyze/lookup_http.py +280 -0
- maproom/analyze/metric_format.py +20 -0
- maproom/analyze/migration_method.py +130 -0
- maproom/analyze/move_list.py +93 -0
- maproom/analyze/operator_context.py +156 -0
- maproom/analyze/performance_rank.py +97 -0
- maproom/analyze/primary_recommendation.py +422 -0
- maproom/analyze/provider_comparison.py +401 -0
- maproom/analyze/quota_analyze.py +481 -0
- maproom/analyze/quota_client.py +75 -0
- maproom/analyze/quota_lookup.py +110 -0
- maproom/analyze/quota_outcome_steer.py +105 -0
- maproom/analyze/recommend_batch.py +199 -0
- maproom/analyze/recommend_client.py +362 -0
- maproom/analyze/recommend_share.py +118 -0
- maproom/analyze/recommendation.py +591 -0
- maproom/analyze/reservation_pack.py +883 -0
- maproom/analyze/reservation_read.py +461 -0
- maproom/analyze/safe_analyze.py +1134 -0
- maproom/analyze/safe_move.py +195 -0
- maproom/analyze/safe_moves.py +571 -0
- maproom/analyze/safe_moves_catalog.py +127 -0
- maproom/analyze/safe_moves_client.py +70 -0
- maproom/analyze/series_analyze.py +491 -0
- maproom/analyze/series_client.py +106 -0
- maproom/analyze/series_lookup.py +663 -0
- maproom/analyze/shape.py +254 -0
- maproom/analyze/size_analyze.py +883 -0
- maproom/analyze/size_client.py +81 -0
- maproom/analyze/size_keep.py +409 -0
- maproom/analyze/size_lookup.py +368 -0
- maproom/analyze/specs.py +325 -0
- maproom/analyze/spot_analyze.py +392 -0
- maproom/analyze/spot_client.py +74 -0
- maproom/analyze/spot_lookup.py +213 -0
- maproom/analyze/token_client.py +97 -0
- maproom/analyze/utilization.py +190 -0
- maproom/analyze/workload.py +104 -0
- maproom/analyze/workset.py +762 -0
- maproom/aws_region.py +78 -0
- maproom/cache/__init__.py +1 -0
- maproom/cache/catalog.py +372 -0
- maproom/cache/commands.py +215 -0
- maproom/cache/instances.py +460 -0
- maproom/cli.py +2084 -0
- maproom/cli_argv.py +329 -0
- maproom/cli_floors.py +89 -0
- maproom/cliio/__init__.py +1 -0
- maproom/cliio/registry.py +377 -0
- maproom/cliio/runner.py +489 -0
- maproom/consent/__init__.py +33 -0
- maproom/consent/gate.py +238 -0
- maproom/consent/policy.py +20 -0
- maproom/consent/prompt.py +61 -0
- maproom/consent/service_upload_gate.py +318 -0
- maproom/consent/service_upload_prompt.py +62 -0
- maproom/consent/stdin.py +45 -0
- maproom/consent/storage.py +24 -0
- maproom/consent/write_token.py +29 -0
- maproom/contracts/__init__.py +6 -0
- maproom/contracts/schema_diff.py +119 -0
- maproom/contracts/validate.py +113 -0
- maproom/data/__init__.py +1 -0
- maproom/data/clear.py +76 -0
- maproom/data/commands.py +14 -0
- maproom/doctor/__init__.py +1 -0
- maproom/doctor/aws_session.py +226 -0
- maproom/doctor/checks.py +701 -0
- maproom/doctor/context.py +32 -0
- maproom/doctor/display.py +96 -0
- maproom/doctor/inventory_display.py +377 -0
- maproom/doctor/runner.py +1467 -0
- maproom/errors.py +43 -0
- maproom/exitcodes.py +70 -0
- maproom/inventory/__init__.py +1 -0
- maproom/inventory/account_identity.py +79 -0
- maproom/inventory/accounts.py +155 -0
- maproom/inventory/command.py +961 -0
- maproom/inventory/messages.py +264 -0
- maproom/inventory/reservations_refresh.py +37 -0
- maproom/mock_environment/__init__.py +28 -0
- maproom/mock_environment/load.py +241 -0
- maproom/mock_environment/runner.py +387 -0
- maproom/output/__init__.py +1 -0
- maproom/output/json_envelope.py +241 -0
- maproom/output/redact.py +11 -0
- maproom/output/terminal.py +129 -0
- maproom/parsers.py +284 -0
- maproom/paths.py +105 -0
- maproom/plan/__init__.py +1 -0
- maproom/plan/accept.py +59 -0
- maproom/plan/analysis_context.py +78 -0
- maproom/plan/analyze_selection.py +279 -0
- maproom/plan/commands.py +586 -0
- maproom/plan/current.py +59 -0
- maproom/plan/errors.py +10 -0
- maproom/plan/export.py +36 -0
- maproom/plan/formatting.py +91 -0
- maproom/plan/ids.py +13 -0
- maproom/plan/migration_options.py +75 -0
- maproom/plan/model.py +265 -0
- maproom/plan/names.py +24 -0
- maproom/plan/store.py +193 -0
- maproom/plan/tty.py +34 -0
- maproom/pricing/__init__.py +5 -0
- maproom/pricing/prices.py +119 -0
- maproom/privacy/__init__.py +6 -0
- maproom/privacy/commands.py +159 -0
- maproom/privacy/delete_uploads.py +204 -0
- maproom/privacy/fleetfit_policy.py +125 -0
- maproom/privacy/fleetfit_statement.py +104 -0
- maproom/privacy/format.py +76 -0
- maproom/privacy/manifests.py +342 -0
- maproom/privacy/statement.py +58 -0
- maproom/quotas/__init__.py +21 -0
- maproom/quotas/aws.py +204 -0
- maproom/quotas/azure.py +75 -0
- maproom/quotas/commands.py +240 -0
- maproom/quotas/family_coverage.py +122 -0
- maproom/quotas/fetch.py +124 -0
- maproom/quotas/load.py +41 -0
- maproom/quotas/model.py +178 -0
- maproom/quotas/outcome_ledger.py +245 -0
- maproom/quotas/quota_request_sync.py +221 -0
- maproom/quotas/show.py +73 -0
- maproom/quotas/status.py +206 -0
- maproom/quotas/store.py +161 -0
- maproom/redaction.py +157 -0
- maproom/reference_data/__init__.py +1 -0
- maproom/reference_data/aws/aws_prices.csv +25562 -0
- maproom/reference_data/aws/capabilities.csv +1346 -0
- maproom/reference_data/aws/instance_types.csv +1346 -0
- maproom/reference_data/azure/azure_prices.csv +1 -0
- maproom/reference_data/azure/vm_types.csv +1 -0
- maproom/reference_data/benchmarks/cpu-lscpu-v2-instances.json +27 -0
- maproom/reference_data/benchmarks/safe-moves-v1.json +328 -0
- maproom/reference_data/manifest.json +174 -0
- maproom/reference_data/manifest.py +69 -0
- maproom/reservations/__init__.py +17 -0
- maproom/reservations/aws.py +76 -0
- maproom/reservations/azure.py +36 -0
- maproom/reservations/commands.py +149 -0
- maproom/reservations/load.py +41 -0
- maproom/reservations/model.py +316 -0
- maproom/reservations/show.py +64 -0
- maproom/reservations/status.py +181 -0
- maproom/reservations/store.py +391 -0
- maproom/resources.py +54 -0
- maproom/root_help.py +251 -0
- maproom/saving_plans/__init__.py +15 -0
- maproom/saving_plans/aws.py +73 -0
- maproom/saving_plans/commands.py +122 -0
- maproom/saving_plans/load.py +41 -0
- maproom/saving_plans/model.py +101 -0
- maproom/saving_plans/show.py +64 -0
- maproom/saving_plans/status.py +118 -0
- maproom/saving_plans/store.py +173 -0
- maproom/secret_egress_gate.py +108 -0
- maproom/service/install_id.py +60 -0
- maproom/settings/__init__.py +1 -0
- maproom/settings/defaults.py +73 -0
- maproom/settings/init.py +143 -0
- maproom/settings/model.py +105 -0
- maproom/settings/product_version_change.py +343 -0
- maproom/settings/product_versions.py +68 -0
- maproom/settings/set.py +347 -0
- maproom/settings/show.py +227 -0
- maproom/settings/write.py +273 -0
- maproom/snapshot_time.py +27 -0
- maproom-0.2.0.dist-info/METADATA +263 -0
- maproom-0.2.0.dist-info/RECORD +199 -0
- maproom-0.2.0.dist-info/WHEEL +5 -0
- maproom-0.2.0.dist-info/entry_points.txt +2 -0
- maproom-0.2.0.dist-info/licenses/LICENSE +201 -0
- maproom-0.2.0.dist-info/licenses/NOTICE +72 -0
- maproom-0.2.0.dist-info/top_level.txt +1 -0
maproom/errors.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Typed errors for cloud CLI response parsing (Journey error states)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MaproomParseError(Exception):
|
|
7
|
+
"""Base parse / shape error."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
message: str,
|
|
12
|
+
*,
|
|
13
|
+
api_id: str | None = None,
|
|
14
|
+
cli_version: str | None = None,
|
|
15
|
+
path: str | None = None,
|
|
16
|
+
) -> None:
|
|
17
|
+
self.api_id = api_id
|
|
18
|
+
self.cli_version = cli_version
|
|
19
|
+
self.path = path
|
|
20
|
+
parts = [message]
|
|
21
|
+
if api_id:
|
|
22
|
+
parts.append(f"api_id={api_id}")
|
|
23
|
+
if cli_version:
|
|
24
|
+
parts.append(f"cli_version={cli_version}")
|
|
25
|
+
if path:
|
|
26
|
+
parts.append(f"path={path}")
|
|
27
|
+
super().__init__("; ".join(parts))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class NotLoggedInError(MaproomParseError):
|
|
31
|
+
"""AWS/Azure CLI has no usable credentials (e.g. exit 253)."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class AccessDeniedError(MaproomParseError):
|
|
35
|
+
"""Caller lacks permission (UnauthorizedOperation / AccessDenied)."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class NotFoundError(MaproomParseError):
|
|
39
|
+
"""Target resource id does not exist / is malformed-not-found."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ShapeValidationError(MaproomParseError):
|
|
43
|
+
"""Fixture/payload missing a required field for the shape contract."""
|
maproom/exitcodes.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Exit-code registry for maproom CLI (docs/OUTPUT_CONTRACT.md section 5)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
SUCCESS = 0
|
|
10
|
+
RUNTIME = 1
|
|
11
|
+
USAGE = 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def repo_root() -> Path:
|
|
15
|
+
"""Repository root (parent of the ``maproom`` package directory)."""
|
|
16
|
+
return Path(__file__).resolve().parents[1]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def contracts_output_dir() -> Path:
|
|
20
|
+
return repo_root() / "contracts" / "output"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def working_schema_path() -> Path:
|
|
24
|
+
return contracts_output_dir() / "maproom_output.v1.schema.json"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def released_schema_path() -> Path:
|
|
28
|
+
return contracts_output_dir() / "released" / "maproom_output.v1.schema.json"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def working_plan_schema_path() -> Path:
|
|
32
|
+
return contracts_output_dir() / "maproom_plan.v1.schema.json"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def released_plan_schema_path() -> Path:
|
|
36
|
+
return contracts_output_dir() / "released" / "maproom_plan.v1.schema.json"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def exit_codes_json_path() -> Path:
|
|
40
|
+
return contracts_output_dir() / "exit_codes.json"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def exit_code_for_error(code: str) -> int:
|
|
44
|
+
"""Return the process exit code registered for ``error.code``."""
|
|
45
|
+
payload = json.loads(exit_codes_json_path().read_text(encoding="utf-8"))
|
|
46
|
+
mapping = payload["error_code_map"]
|
|
47
|
+
if code not in mapping:
|
|
48
|
+
raise KeyError(f"error code {code!r} is not in exit_codes.json")
|
|
49
|
+
return int(mapping[code])
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def exit_usage() -> int:
|
|
53
|
+
"""Return the usage exit code (for argparse handlers and tests)."""
|
|
54
|
+
return USAGE
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def exit_runtime() -> int:
|
|
58
|
+
return RUNTIME
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def exit_success() -> int:
|
|
62
|
+
return SUCCESS
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def raise_usage() -> None:
|
|
66
|
+
raise SystemExit(USAGE)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def raise_runtime() -> None:
|
|
70
|
+
raise SystemExit(RUNTIME)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Inventory fetch messaging shared across doctor and get commands."""
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Signed-in AWS account and Azure subscription, plus an optional alias."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from maproom.inventory.accounts import AccountIdentityError, CloudAccount
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def resolve_aws_account(runner: Any) -> CloudAccount:
|
|
12
|
+
result = runner.run(
|
|
13
|
+
"aws",
|
|
14
|
+
["sts", "get-caller-identity", "--output", "json"],
|
|
15
|
+
record=False,
|
|
16
|
+
api_id="aws_sts_get_caller_identity",
|
|
17
|
+
)
|
|
18
|
+
payload = _object(result, "sts get-caller-identity")
|
|
19
|
+
account = str(payload.get("Account") or "").strip()
|
|
20
|
+
if not account:
|
|
21
|
+
raise AccountIdentityError("sts get-caller-identity did not return an account id")
|
|
22
|
+
return CloudAccount("aws", account, _aws_alias(runner))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def resolve_azure_account(runner: Any) -> CloudAccount:
|
|
26
|
+
result = runner.run(
|
|
27
|
+
"azure",
|
|
28
|
+
["account", "show", "--output", "json"],
|
|
29
|
+
record=False,
|
|
30
|
+
api_id="azure_account_show",
|
|
31
|
+
)
|
|
32
|
+
payload = _object(result, "az account show")
|
|
33
|
+
subscription = str(payload.get("id") or "").strip()
|
|
34
|
+
if not subscription:
|
|
35
|
+
raise AccountIdentityError("az account show did not return a subscription id")
|
|
36
|
+
name = payload.get("name")
|
|
37
|
+
alias = name.strip() if isinstance(name, str) and name.strip() else None
|
|
38
|
+
return CloudAccount("azure", subscription, alias)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _aws_alias(runner: Any) -> str | None:
|
|
42
|
+
"""IAM account alias when the caller can list it. Missing permission leaves the name off."""
|
|
43
|
+
try:
|
|
44
|
+
result = runner.run(
|
|
45
|
+
"aws",
|
|
46
|
+
["iam", "list-account-aliases", "--output", "json"],
|
|
47
|
+
record=False,
|
|
48
|
+
api_id="aws_iam_list_account_aliases",
|
|
49
|
+
)
|
|
50
|
+
except OSError:
|
|
51
|
+
return None
|
|
52
|
+
if int(result.exit_code) != 0 or not (result.stdout or "").strip():
|
|
53
|
+
return None
|
|
54
|
+
try:
|
|
55
|
+
payload = json.loads(result.stdout)
|
|
56
|
+
except json.JSONDecodeError:
|
|
57
|
+
return None
|
|
58
|
+
if not isinstance(payload, dict):
|
|
59
|
+
return None
|
|
60
|
+
aliases = payload.get("AccountAliases")
|
|
61
|
+
if not isinstance(aliases, list) or not aliases:
|
|
62
|
+
return None
|
|
63
|
+
alias = str(aliases[0]).strip()
|
|
64
|
+
return alias or None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _object(result: Any, operation: str) -> dict[str, Any]:
|
|
68
|
+
if int(result.exit_code) != 0:
|
|
69
|
+
detail = (result.stderr or "").strip() or f"exit code {result.exit_code}"
|
|
70
|
+
raise AccountIdentityError(f"{operation} failed: {detail}")
|
|
71
|
+
if not (result.stdout or "").strip():
|
|
72
|
+
raise AccountIdentityError(f"{operation} returned empty output")
|
|
73
|
+
try:
|
|
74
|
+
payload = json.loads(result.stdout)
|
|
75
|
+
except json.JSONDecodeError as exc:
|
|
76
|
+
raise AccountIdentityError(f"{operation} returned invalid JSON: {exc}") from exc
|
|
77
|
+
if not isinstance(payload, dict):
|
|
78
|
+
raise AccountIdentityError(f"{operation} returned unexpected JSON")
|
|
79
|
+
return payload
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Per-account folders for local reservation and quota snapshots."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
import shutil
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Callable
|
|
11
|
+
|
|
12
|
+
_AWS_ACCOUNT = re.compile(r"^\d{12}$")
|
|
13
|
+
_AZURE_SUBSCRIPTION = re.compile(
|
|
14
|
+
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
|
15
|
+
)
|
|
16
|
+
_AWS_DIR = re.compile(r"^aws-(\d{12})(?:-.+)?$")
|
|
17
|
+
_AZURE_DIR = re.compile(
|
|
18
|
+
r"^azure-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:-.+)?$"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AccountIdentityError(Exception):
|
|
23
|
+
"""The signed-in account id could not be read."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, message: str) -> None:
|
|
26
|
+
self.message = message
|
|
27
|
+
super().__init__(message)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class CloudAccount:
|
|
32
|
+
cloud: str
|
|
33
|
+
account_id: str
|
|
34
|
+
alias: str | None = None
|
|
35
|
+
|
|
36
|
+
def folder_name(self) -> str:
|
|
37
|
+
return account_folder_name(self.cloud, self.account_id, self.alias)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def account_folder_name(cloud: str, account_id: str, alias: str | None = None) -> str:
|
|
41
|
+
"""Return ``aws-<account>`` or ``azure-<subscription>``, plus ``-<alias>`` when set."""
|
|
42
|
+
cloud_name = cloud.strip().lower()
|
|
43
|
+
if cloud_name not in {"aws", "azure"}:
|
|
44
|
+
raise AccountIdentityError(f"unsupported cloud for a data folder: {cloud}")
|
|
45
|
+
ident = _account_segment(cloud_name, account_id)
|
|
46
|
+
name = f"{cloud_name}-{ident}"
|
|
47
|
+
slug = _alias_segment(alias)
|
|
48
|
+
if slug:
|
|
49
|
+
name = f"{name}-{slug}"
|
|
50
|
+
return name
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def parse_account_folder(name: str) -> tuple[str, str] | None:
|
|
54
|
+
"""Return ``(cloud, account id)`` when ``name`` is an account data folder."""
|
|
55
|
+
aws = _AWS_DIR.match(name)
|
|
56
|
+
if aws:
|
|
57
|
+
return "aws", aws.group(1)
|
|
58
|
+
azure = _AZURE_DIR.match(name)
|
|
59
|
+
if azure:
|
|
60
|
+
return "azure", azure.group(1)
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def snapshot_files(root: Path, filename: str) -> list[Path]:
|
|
65
|
+
"""Legacy file at ``root`` plus one file inside each account folder."""
|
|
66
|
+
found: list[Path] = []
|
|
67
|
+
legacy = root / filename
|
|
68
|
+
if legacy.is_file():
|
|
69
|
+
found.append(legacy)
|
|
70
|
+
if not root.is_dir():
|
|
71
|
+
return found
|
|
72
|
+
for child in sorted(root.iterdir(), key=lambda path: path.name):
|
|
73
|
+
if not child.is_dir():
|
|
74
|
+
continue
|
|
75
|
+
candidate = child / filename
|
|
76
|
+
if candidate.is_file():
|
|
77
|
+
found.append(candidate)
|
|
78
|
+
return found
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def primary_snapshot_path(root: Path, filename: str) -> Path:
|
|
82
|
+
"""The one account file when that is all that exists, otherwise the legacy path."""
|
|
83
|
+
legacy = root / filename
|
|
84
|
+
account_files = [path for path in snapshot_files(root, filename) if path.parent != root]
|
|
85
|
+
if len(account_files) == 1 and not legacy.is_file():
|
|
86
|
+
return account_files[0]
|
|
87
|
+
return legacy
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def retire_other_account_folders(root: Path, account: CloudAccount, *, keep: str) -> None:
|
|
91
|
+
"""Remove earlier folders for this account, including a previous alias in the name."""
|
|
92
|
+
if not root.is_dir():
|
|
93
|
+
return
|
|
94
|
+
identity = (account.cloud, account.account_id)
|
|
95
|
+
for child in list(root.iterdir()):
|
|
96
|
+
if not child.is_dir() or child.name == keep:
|
|
97
|
+
continue
|
|
98
|
+
if parse_account_folder(child.name) == identity:
|
|
99
|
+
shutil.rmtree(child)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def drop_replaced_legacy_snapshot(
|
|
103
|
+
root: Path,
|
|
104
|
+
filename: str,
|
|
105
|
+
*,
|
|
106
|
+
clouds: set[str],
|
|
107
|
+
sources: set[str],
|
|
108
|
+
cloud_of: Callable[[dict[str, Any]], str],
|
|
109
|
+
write_json: Callable[[Path, dict[str, Any]], Any],
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Drop legacy root snapshots after a cloud refresh writes an account folder."""
|
|
112
|
+
path = root / filename
|
|
113
|
+
if not path.is_file() or not clouds:
|
|
114
|
+
return
|
|
115
|
+
try:
|
|
116
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
117
|
+
except (OSError, json.JSONDecodeError):
|
|
118
|
+
return
|
|
119
|
+
if not isinstance(data, dict):
|
|
120
|
+
return
|
|
121
|
+
if str(data.get("source") or "") not in sources:
|
|
122
|
+
return
|
|
123
|
+
records = data.get("records")
|
|
124
|
+
if not isinstance(records, list):
|
|
125
|
+
path.unlink(missing_ok=True)
|
|
126
|
+
return
|
|
127
|
+
kept = [
|
|
128
|
+
row
|
|
129
|
+
for row in records
|
|
130
|
+
if not isinstance(row, dict) or cloud_of(row) not in clouds
|
|
131
|
+
]
|
|
132
|
+
if kept:
|
|
133
|
+
data["records"] = kept
|
|
134
|
+
data["record_count"] = len(kept)
|
|
135
|
+
write_json(path, data)
|
|
136
|
+
return
|
|
137
|
+
path.unlink(missing_ok=True)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _account_segment(cloud: str, account_id: str) -> str:
|
|
141
|
+
text = account_id.strip().lower()
|
|
142
|
+
if cloud == "aws":
|
|
143
|
+
if not _AWS_ACCOUNT.match(text):
|
|
144
|
+
raise AccountIdentityError("AWS account id must be 12 digits")
|
|
145
|
+
return text
|
|
146
|
+
if not _AZURE_SUBSCRIPTION.match(text):
|
|
147
|
+
raise AccountIdentityError("Azure subscription id must be a GUID")
|
|
148
|
+
return text
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _alias_segment(alias: str | None) -> str:
|
|
152
|
+
if not alias:
|
|
153
|
+
return ""
|
|
154
|
+
slug = re.sub(r"[^a-z0-9]+", "-", alias.strip().lower())
|
|
155
|
+
return slug.strip("-")
|