|
| 1 | +# API Reference |
| 2 | + |
| 3 | +This page provides detailed API documentation for hier-config-cli's Python modules. |
| 4 | + |
| 5 | +## Command-Line Interface |
| 6 | + |
| 7 | +### Main CLI Group |
| 8 | + |
| 9 | +::: hier_config_cli.cli |
| 10 | + options: |
| 11 | + show_root_heading: true |
| 12 | + show_source: true |
| 13 | + |
| 14 | +## Core Functions |
| 15 | + |
| 16 | +### Configuration Processing |
| 17 | + |
| 18 | +::: hier_config_cli.process_configs |
| 19 | + options: |
| 20 | + show_root_heading: true |
| 21 | + show_source: true |
| 22 | + |
| 23 | +### Output Formatting |
| 24 | + |
| 25 | +::: hier_config_cli.format_output |
| 26 | + options: |
| 27 | + show_root_heading: true |
| 28 | + show_source: true |
| 29 | + |
| 30 | +::: hier_config_cli.get_output_text |
| 31 | + options: |
| 32 | + show_root_heading: true |
| 33 | + show_source: true |
| 34 | + |
| 35 | +### Logging |
| 36 | + |
| 37 | +::: hier_config_cli.setup_logging |
| 38 | + options: |
| 39 | + show_root_heading: true |
| 40 | + show_source: true |
| 41 | + |
| 42 | +## Commands |
| 43 | + |
| 44 | +### Remediation Command |
| 45 | + |
| 46 | +::: hier_config_cli.remediation |
| 47 | + options: |
| 48 | + show_root_heading: true |
| 49 | + show_source: true |
| 50 | + |
| 51 | +### Rollback Command |
| 52 | + |
| 53 | +::: hier_config_cli.rollback |
| 54 | + options: |
| 55 | + show_root_heading: true |
| 56 | + show_source: true |
| 57 | + |
| 58 | +### Future Command |
| 59 | + |
| 60 | +::: hier_config_cli.future |
| 61 | + options: |
| 62 | + show_root_heading: true |
| 63 | + show_source: true |
| 64 | + |
| 65 | +### List Platforms Command |
| 66 | + |
| 67 | +::: hier_config_cli.list_platforms |
| 68 | + options: |
| 69 | + show_root_heading: true |
| 70 | + show_source: true |
| 71 | + |
| 72 | +### Version Command |
| 73 | + |
| 74 | +::: hier_config_cli.version |
| 75 | + options: |
| 76 | + show_root_heading: true |
| 77 | + show_source: true |
| 78 | + |
| 79 | +## Constants |
| 80 | + |
| 81 | +### Platform Mapping |
| 82 | + |
| 83 | +The `PLATFORM_MAP` dictionary maps platform names to `hier_config.Platform` enum values: |
| 84 | + |
| 85 | +```python |
| 86 | +PLATFORM_MAP = { |
| 87 | + "ios": Platform.CISCO_IOS, |
| 88 | + "nxos": Platform.CISCO_NXOS, |
| 89 | + "iosxr": Platform.CISCO_XR, |
| 90 | + "eos": Platform.ARISTA_EOS, |
| 91 | + "junos": Platform.JUNIPER_JUNOS, |
| 92 | + "vyos": Platform.VYOS, |
| 93 | + "fortios": Platform.FORTINET_FORTIOS, |
| 94 | + "generic": Platform.GENERIC, |
| 95 | + "hp_comware5": Platform.HP_COMWARE5, |
| 96 | + "hp_procurve": Platform.HP_PROCURVE, |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Type Definitions |
| 101 | + |
| 102 | +### Common Types |
| 103 | + |
| 104 | +```python |
| 105 | +from pathlib import Path |
| 106 | +from typing import Optional, Union |
| 107 | + |
| 108 | +# Configuration path types |
| 109 | +ConfigPath = Union[str, Path] |
| 110 | + |
| 111 | +# Output format types |
| 112 | +OutputFormat = Literal["text", "json", "yaml"] |
| 113 | + |
| 114 | +# Operation types |
| 115 | +Operation = Literal["remediation", "rollback", "future"] |
| 116 | +``` |
| 117 | + |
| 118 | +## Usage Examples |
| 119 | + |
| 120 | +### Programmatic Usage |
| 121 | + |
| 122 | +While hier-config-cli is primarily a CLI tool, you can use its functions programmatically: |
| 123 | + |
| 124 | +```python |
| 125 | +from hier_config_cli import process_configs, format_output |
| 126 | +from pathlib import Path |
| 127 | + |
| 128 | +# Process configurations |
| 129 | +result, platform = process_configs( |
| 130 | + platform_str="ios", |
| 131 | + running_config_path="configs/running.conf", |
| 132 | + generated_config_path="configs/intended.conf", |
| 133 | + operation="remediation", |
| 134 | +) |
| 135 | + |
| 136 | +# Format output |
| 137 | +output = format_output( |
| 138 | + hconfig=result, |
| 139 | + platform=platform, |
| 140 | + output_format="text", |
| 141 | +) |
| 142 | + |
| 143 | +print(output) |
| 144 | +``` |
| 145 | + |
| 146 | +### Custom Integration |
| 147 | + |
| 148 | +```python |
| 149 | +import subprocess |
| 150 | +import json |
| 151 | +from pathlib import Path |
| 152 | + |
| 153 | +def generate_remediation( |
| 154 | + platform: str, |
| 155 | + running_config: Path, |
| 156 | + intended_config: Path, |
| 157 | +) -> dict: |
| 158 | + """Generate remediation using hier-config-cli. |
| 159 | +
|
| 160 | + Args: |
| 161 | + platform: Platform name |
| 162 | + running_config: Path to running config |
| 163 | + intended_config: Path to intended config |
| 164 | +
|
| 165 | + Returns: |
| 166 | + Remediation data as dictionary |
| 167 | +
|
| 168 | + Raises: |
| 169 | + subprocess.CalledProcessError: If command fails |
| 170 | + """ |
| 171 | + result = subprocess.run( |
| 172 | + [ |
| 173 | + "hier-config-cli", |
| 174 | + "remediation", |
| 175 | + "--platform", platform, |
| 176 | + "--running-config", str(running_config), |
| 177 | + "--generated-config", str(intended_config), |
| 178 | + "--format", "json", |
| 179 | + ], |
| 180 | + capture_output=True, |
| 181 | + text=True, |
| 182 | + check=True, |
| 183 | + ) |
| 184 | + |
| 185 | + return json.loads(result.stdout) |
| 186 | + |
| 187 | +# Usage |
| 188 | +remediation = generate_remediation( |
| 189 | + platform="ios", |
| 190 | + running_config=Path("configs/running.conf"), |
| 191 | + intended_config=Path("configs/intended.conf"), |
| 192 | +) |
| 193 | + |
| 194 | +for command in remediation["config"]: |
| 195 | + print(command) |
| 196 | +``` |
| 197 | + |
| 198 | +## Error Handling |
| 199 | + |
| 200 | +### Exception Types |
| 201 | + |
| 202 | +hier-config-cli uses `click.ClickException` for all user-facing errors: |
| 203 | + |
| 204 | +```python |
| 205 | +from click import ClickException |
| 206 | + |
| 207 | +try: |
| 208 | + result = process_configs( |
| 209 | + platform_str="invalid", |
| 210 | + running_config_path="running.conf", |
| 211 | + generated_config_path="intended.conf", |
| 212 | + operation="remediation", |
| 213 | + ) |
| 214 | +except ClickException as e: |
| 215 | + print(f"Error: {e.message}") |
| 216 | +``` |
| 217 | + |
| 218 | +### Common Exceptions |
| 219 | + |
| 220 | +| Exception | Cause | Message Example | |
| 221 | +|-----------|-------|-----------------| |
| 222 | +| `ClickException` | Unknown platform | "Unknown platform: invalid_platform" | |
| 223 | +| `ClickException` | File not found | "Running config file not found: path/to/file" | |
| 224 | +| `ClickException` | Permission denied | "Permission denied reading running config: path/to/file" | |
| 225 | +| `ClickException` | Parsing error | "Error parsing configuration: [details]" | |
| 226 | + |
| 227 | +## Exit Codes |
| 228 | + |
| 229 | +| Code | Meaning | |
| 230 | +|------|---------| |
| 231 | +| 0 | Success | |
| 232 | +| 1 | Error occurred | |
| 233 | + |
| 234 | +## Environment Variables |
| 235 | + |
| 236 | +hier-config-cli doesn't use environment variables directly, but respects standard Python environment variables: |
| 237 | + |
| 238 | +| Variable | Purpose | |
| 239 | +|----------|---------| |
| 240 | +| `PYTHONPATH` | Python module search path | |
| 241 | +| `PYTHONDONTWRITEBYTECODE` | Disable .pyc files | |
| 242 | + |
| 243 | +## Logging |
| 244 | + |
| 245 | +### Log Levels |
| 246 | + |
| 247 | +Control logging verbosity with the `-v` flag: |
| 248 | + |
| 249 | +```bash |
| 250 | +# WARNING level (default) |
| 251 | +hier-config-cli remediation ... |
| 252 | + |
| 253 | +# INFO level |
| 254 | +hier-config-cli -v remediation ... |
| 255 | + |
| 256 | +# DEBUG level |
| 257 | +hier-config-cli -vv remediation ... |
| 258 | +``` |
| 259 | + |
| 260 | +### Log Format |
| 261 | + |
| 262 | +``` |
| 263 | +LEVEL: message |
| 264 | +``` |
| 265 | + |
| 266 | +Examples: |
| 267 | +``` |
| 268 | +INFO: Using platform: ios |
| 269 | +INFO: Reading running config from: running.conf |
| 270 | +INFO: Reading generated config from: intended.conf |
| 271 | +INFO: Parsing configurations |
| 272 | +INFO: Generating remediation configuration |
| 273 | +``` |
| 274 | + |
| 275 | +## Related Documentation |
| 276 | + |
| 277 | +- [Commands Reference](commands.md) - Detailed command documentation |
| 278 | +- [Integration Guide](integration/index.md) - Integration examples |
| 279 | +- [Development Guide](development/contributing.md) - Contributing guidelines |
| 280 | + |
| 281 | +## External Dependencies |
| 282 | + |
| 283 | +hier-config-cli depends on: |
| 284 | + |
| 285 | +- [hier-config](https://github.com/netdevops/hier_config) - Core configuration analysis |
| 286 | +- [click](https://click.palletsprojects.com/) - CLI framework |
| 287 | +- [PyYAML](https://pyyaml.org/) - YAML support |
| 288 | + |
| 289 | +Refer to their documentation for advanced usage and features. |
0 commit comments