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.
Files changed (199) hide show
  1. maproom/__init__.py +14 -0
  2. maproom/__main__.py +10 -0
  3. maproom/analyze/__init__.py +1 -0
  4. maproom/analyze/accel_families.py +45 -0
  5. maproom/analyze/account_azure_activity.py +260 -0
  6. maproom/analyze/account_azure_quota_requests.py +177 -0
  7. maproom/analyze/account_cloudtrail.py +316 -0
  8. maproom/analyze/account_quota_requests.py +180 -0
  9. maproom/analyze/analyze_v1_orchestration.py +1194 -0
  10. maproom/analyze/catalog.py +353 -0
  11. maproom/analyze/change_detection.py +652 -0
  12. maproom/analyze/context_client.py +99 -0
  13. maproom/analyze/context_lookup.py +142 -0
  14. maproom/analyze/cover_analyze.py +344 -0
  15. maproom/analyze/cover_client.py +65 -0
  16. maproom/analyze/cover_lookup.py +242 -0
  17. maproom/analyze/engine.py +644 -0
  18. maproom/analyze/fleetfit_endpoints.py +217 -0
  19. maproom/analyze/fleetfit_policy.py +66 -0
  20. maproom/analyze/instance_price_analyze.py +221 -0
  21. maproom/analyze/instance_price_client.py +108 -0
  22. maproom/analyze/instance_price_lookup.py +60 -0
  23. maproom/analyze/local_reads.py +484 -0
  24. maproom/analyze/lookup_http.py +280 -0
  25. maproom/analyze/metric_format.py +20 -0
  26. maproom/analyze/migration_method.py +130 -0
  27. maproom/analyze/move_list.py +93 -0
  28. maproom/analyze/operator_context.py +156 -0
  29. maproom/analyze/performance_rank.py +97 -0
  30. maproom/analyze/primary_recommendation.py +422 -0
  31. maproom/analyze/provider_comparison.py +401 -0
  32. maproom/analyze/quota_analyze.py +481 -0
  33. maproom/analyze/quota_client.py +75 -0
  34. maproom/analyze/quota_lookup.py +110 -0
  35. maproom/analyze/quota_outcome_steer.py +105 -0
  36. maproom/analyze/recommend_batch.py +199 -0
  37. maproom/analyze/recommend_client.py +362 -0
  38. maproom/analyze/recommend_share.py +118 -0
  39. maproom/analyze/recommendation.py +591 -0
  40. maproom/analyze/reservation_pack.py +883 -0
  41. maproom/analyze/reservation_read.py +461 -0
  42. maproom/analyze/safe_analyze.py +1134 -0
  43. maproom/analyze/safe_move.py +195 -0
  44. maproom/analyze/safe_moves.py +571 -0
  45. maproom/analyze/safe_moves_catalog.py +127 -0
  46. maproom/analyze/safe_moves_client.py +70 -0
  47. maproom/analyze/series_analyze.py +491 -0
  48. maproom/analyze/series_client.py +106 -0
  49. maproom/analyze/series_lookup.py +663 -0
  50. maproom/analyze/shape.py +254 -0
  51. maproom/analyze/size_analyze.py +883 -0
  52. maproom/analyze/size_client.py +81 -0
  53. maproom/analyze/size_keep.py +409 -0
  54. maproom/analyze/size_lookup.py +368 -0
  55. maproom/analyze/specs.py +325 -0
  56. maproom/analyze/spot_analyze.py +392 -0
  57. maproom/analyze/spot_client.py +74 -0
  58. maproom/analyze/spot_lookup.py +213 -0
  59. maproom/analyze/token_client.py +97 -0
  60. maproom/analyze/utilization.py +190 -0
  61. maproom/analyze/workload.py +104 -0
  62. maproom/analyze/workset.py +762 -0
  63. maproom/aws_region.py +78 -0
  64. maproom/cache/__init__.py +1 -0
  65. maproom/cache/catalog.py +372 -0
  66. maproom/cache/commands.py +215 -0
  67. maproom/cache/instances.py +460 -0
  68. maproom/cli.py +2084 -0
  69. maproom/cli_argv.py +329 -0
  70. maproom/cli_floors.py +89 -0
  71. maproom/cliio/__init__.py +1 -0
  72. maproom/cliio/registry.py +377 -0
  73. maproom/cliio/runner.py +489 -0
  74. maproom/consent/__init__.py +33 -0
  75. maproom/consent/gate.py +238 -0
  76. maproom/consent/policy.py +20 -0
  77. maproom/consent/prompt.py +61 -0
  78. maproom/consent/service_upload_gate.py +318 -0
  79. maproom/consent/service_upload_prompt.py +62 -0
  80. maproom/consent/stdin.py +45 -0
  81. maproom/consent/storage.py +24 -0
  82. maproom/consent/write_token.py +29 -0
  83. maproom/contracts/__init__.py +6 -0
  84. maproom/contracts/schema_diff.py +119 -0
  85. maproom/contracts/validate.py +113 -0
  86. maproom/data/__init__.py +1 -0
  87. maproom/data/clear.py +76 -0
  88. maproom/data/commands.py +14 -0
  89. maproom/doctor/__init__.py +1 -0
  90. maproom/doctor/aws_session.py +226 -0
  91. maproom/doctor/checks.py +701 -0
  92. maproom/doctor/context.py +32 -0
  93. maproom/doctor/display.py +96 -0
  94. maproom/doctor/inventory_display.py +377 -0
  95. maproom/doctor/runner.py +1467 -0
  96. maproom/errors.py +43 -0
  97. maproom/exitcodes.py +70 -0
  98. maproom/inventory/__init__.py +1 -0
  99. maproom/inventory/account_identity.py +79 -0
  100. maproom/inventory/accounts.py +155 -0
  101. maproom/inventory/command.py +961 -0
  102. maproom/inventory/messages.py +264 -0
  103. maproom/inventory/reservations_refresh.py +37 -0
  104. maproom/mock_environment/__init__.py +28 -0
  105. maproom/mock_environment/load.py +241 -0
  106. maproom/mock_environment/runner.py +387 -0
  107. maproom/output/__init__.py +1 -0
  108. maproom/output/json_envelope.py +241 -0
  109. maproom/output/redact.py +11 -0
  110. maproom/output/terminal.py +129 -0
  111. maproom/parsers.py +284 -0
  112. maproom/paths.py +105 -0
  113. maproom/plan/__init__.py +1 -0
  114. maproom/plan/accept.py +59 -0
  115. maproom/plan/analysis_context.py +78 -0
  116. maproom/plan/analyze_selection.py +279 -0
  117. maproom/plan/commands.py +586 -0
  118. maproom/plan/current.py +59 -0
  119. maproom/plan/errors.py +10 -0
  120. maproom/plan/export.py +36 -0
  121. maproom/plan/formatting.py +91 -0
  122. maproom/plan/ids.py +13 -0
  123. maproom/plan/migration_options.py +75 -0
  124. maproom/plan/model.py +265 -0
  125. maproom/plan/names.py +24 -0
  126. maproom/plan/store.py +193 -0
  127. maproom/plan/tty.py +34 -0
  128. maproom/pricing/__init__.py +5 -0
  129. maproom/pricing/prices.py +119 -0
  130. maproom/privacy/__init__.py +6 -0
  131. maproom/privacy/commands.py +159 -0
  132. maproom/privacy/delete_uploads.py +204 -0
  133. maproom/privacy/fleetfit_policy.py +125 -0
  134. maproom/privacy/fleetfit_statement.py +104 -0
  135. maproom/privacy/format.py +76 -0
  136. maproom/privacy/manifests.py +342 -0
  137. maproom/privacy/statement.py +58 -0
  138. maproom/quotas/__init__.py +21 -0
  139. maproom/quotas/aws.py +204 -0
  140. maproom/quotas/azure.py +75 -0
  141. maproom/quotas/commands.py +240 -0
  142. maproom/quotas/family_coverage.py +122 -0
  143. maproom/quotas/fetch.py +124 -0
  144. maproom/quotas/load.py +41 -0
  145. maproom/quotas/model.py +178 -0
  146. maproom/quotas/outcome_ledger.py +245 -0
  147. maproom/quotas/quota_request_sync.py +221 -0
  148. maproom/quotas/show.py +73 -0
  149. maproom/quotas/status.py +206 -0
  150. maproom/quotas/store.py +161 -0
  151. maproom/redaction.py +157 -0
  152. maproom/reference_data/__init__.py +1 -0
  153. maproom/reference_data/aws/aws_prices.csv +25562 -0
  154. maproom/reference_data/aws/capabilities.csv +1346 -0
  155. maproom/reference_data/aws/instance_types.csv +1346 -0
  156. maproom/reference_data/azure/azure_prices.csv +1 -0
  157. maproom/reference_data/azure/vm_types.csv +1 -0
  158. maproom/reference_data/benchmarks/cpu-lscpu-v2-instances.json +27 -0
  159. maproom/reference_data/benchmarks/safe-moves-v1.json +328 -0
  160. maproom/reference_data/manifest.json +174 -0
  161. maproom/reference_data/manifest.py +69 -0
  162. maproom/reservations/__init__.py +17 -0
  163. maproom/reservations/aws.py +76 -0
  164. maproom/reservations/azure.py +36 -0
  165. maproom/reservations/commands.py +149 -0
  166. maproom/reservations/load.py +41 -0
  167. maproom/reservations/model.py +316 -0
  168. maproom/reservations/show.py +64 -0
  169. maproom/reservations/status.py +181 -0
  170. maproom/reservations/store.py +391 -0
  171. maproom/resources.py +54 -0
  172. maproom/root_help.py +251 -0
  173. maproom/saving_plans/__init__.py +15 -0
  174. maproom/saving_plans/aws.py +73 -0
  175. maproom/saving_plans/commands.py +122 -0
  176. maproom/saving_plans/load.py +41 -0
  177. maproom/saving_plans/model.py +101 -0
  178. maproom/saving_plans/show.py +64 -0
  179. maproom/saving_plans/status.py +118 -0
  180. maproom/saving_plans/store.py +173 -0
  181. maproom/secret_egress_gate.py +108 -0
  182. maproom/service/install_id.py +60 -0
  183. maproom/settings/__init__.py +1 -0
  184. maproom/settings/defaults.py +73 -0
  185. maproom/settings/init.py +143 -0
  186. maproom/settings/model.py +105 -0
  187. maproom/settings/product_version_change.py +343 -0
  188. maproom/settings/product_versions.py +68 -0
  189. maproom/settings/set.py +347 -0
  190. maproom/settings/show.py +227 -0
  191. maproom/settings/write.py +273 -0
  192. maproom/snapshot_time.py +27 -0
  193. maproom-0.2.0.dist-info/METADATA +263 -0
  194. maproom-0.2.0.dist-info/RECORD +199 -0
  195. maproom-0.2.0.dist-info/WHEEL +5 -0
  196. maproom-0.2.0.dist-info/entry_points.txt +2 -0
  197. maproom-0.2.0.dist-info/licenses/LICENSE +201 -0
  198. maproom-0.2.0.dist-info/licenses/NOTICE +72 -0
  199. 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("-")