|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Essential Commands |
| 8 | + |
| 9 | +```bash |
| 10 | +# Install dependencies |
| 11 | +poetry install --with dev --all-extras |
| 12 | + |
| 13 | +# Format code |
| 14 | +poetry run invoke format |
| 15 | + |
| 16 | +# Run linting (ruff + mypy + yamllint + markdownlint) |
| 17 | +poetry run invoke lint |
| 18 | + |
| 19 | +# Run unit tests with coverage |
| 20 | +poetry run pytest --cov infrahub_sdk tests/unit/ |
| 21 | + |
| 22 | +# Run integration tests |
| 23 | +poetry run pytest tests/integration/ |
| 24 | + |
| 25 | +# Generate documentation |
| 26 | +poetry run invoke docs |
| 27 | + |
| 28 | +# Validate documentation |
| 29 | +poetry run invoke docs-validate |
| 30 | +``` |
| 31 | + |
| 32 | +### Testing Specific Components |
| 33 | + |
| 34 | +```bash |
| 35 | +# Run tests for specific modules |
| 36 | +poetry run pytest tests/unit/test_client.py |
| 37 | +poetry run pytest tests/unit/test_node.py |
| 38 | + |
| 39 | +# Run with verbose output |
| 40 | +poetry run pytest -v tests/unit/ |
| 41 | + |
| 42 | +# Run with parallel execution |
| 43 | +poetry run pytest -n 4 tests/unit/ |
| 44 | +``` |
| 45 | + |
| 46 | +## Architecture Overview |
| 47 | + |
| 48 | +### Core Client Architecture |
| 49 | + |
| 50 | +- **Dual Client Pattern**: `InfrahubClient` (async) and `InfrahubClientSync` (sync) provide identical interfaces |
| 51 | +- **Configuration**: Pydantic-based `Config` class with environment variable support |
| 52 | +- **Transport**: HTTPX-based with proxy support (single proxy or HTTP/HTTPS mounts) |
| 53 | +- **Authentication**: API tokens or JWT with automatic refresh |
| 54 | + |
| 55 | +### Key Modules Structure |
| 56 | + |
| 57 | +```text |
| 58 | +infrahub_sdk/ |
| 59 | +├── client.py # Main client implementations |
| 60 | +├── config.py # Configuration management with Pydantic |
| 61 | +├── node/ # Node system (core data model) |
| 62 | +│ ├── node.py # InfrahubNode and InfrahubNodeSync |
| 63 | +│ ├── attribute.py # Node attributes |
| 64 | +│ └── relationship.py # Relationship management |
| 65 | +├── ctl/ # CLI commands (infrahubctl) |
| 66 | +├── pytest_plugin/ # Custom pytest plugin for Infrahub testing |
| 67 | +└── protocols.py # Generated protocol classes |
| 68 | +``` |
| 69 | + |
| 70 | +### Node System Design |
| 71 | + |
| 72 | +- **Lazy Loading**: Nodes load attributes and relationships on demand |
| 73 | +- **Batch Operations**: Support for bulk create/update/delete operations |
| 74 | +- **Relationship Management**: Automatic handling of node relationships with add/remove/replace operations |
| 75 | +- **Validation**: Built-in data validation with GraphQL query generation |
| 76 | + |
| 77 | +## Infrahub-Specific Patterns |
| 78 | + |
| 79 | +### Checks Implementation |
| 80 | + |
| 81 | +```python |
| 82 | +# CRITICAL: Use validate() method, NOT check() |
| 83 | +class MyCheck(InfrahubCheck): |
| 84 | + def validate(self, data): # Must be validate(), not check() |
| 85 | + # validation logic |
| 86 | + pass |
| 87 | +``` |
| 88 | + |
| 89 | +### Async/Sync Pattern |
| 90 | + |
| 91 | +All operations follow dual implementation pattern: |
| 92 | + |
| 93 | +```python |
| 94 | +# Async version (default) |
| 95 | +client = InfrahubClient() |
| 96 | +node = await client.get(kind="NetworkDevice") |
| 97 | +await node.save() |
| 98 | + |
| 99 | +# Sync version |
| 100 | +client = InfrahubClientSync() |
| 101 | +node = client.get(kind="NetworkDevice") |
| 102 | +node.save() |
| 103 | +``` |
| 104 | + |
| 105 | +### Configuration Management |
| 106 | + |
| 107 | +- Environment variables prefixed with `INFRAHUB_` |
| 108 | +- Proxy configuration: `INFRAHUB_PROXY` (single) or `INFRAHUB_PROXY_MOUNTS_HTTP`/`INFRAHUB_PROXY_MOUNTS_HTTPS` (separate) |
| 109 | +- Mutual exclusivity validation between proxy configuration methods |
| 110 | + |
| 111 | +## Testing Framework |
| 112 | + |
| 113 | +### Custom Pytest Plugin |
| 114 | + |
| 115 | +The repository includes a custom pytest plugin (`infrahub_sdk.pytest_plugin`) that provides: |
| 116 | + |
| 117 | +- Fixtures for Infrahub clients and configuration |
| 118 | +- Support for testing checks, transforms, and queries |
| 119 | +- Integration with infrahub-testcontainers for Docker-based testing |
| 120 | + |
| 121 | +### Test Structure |
| 122 | + |
| 123 | +- **Unit Tests**: `tests/unit/` - Test individual components in isolation |
| 124 | +- **Integration Tests**: `tests/integration/` - Test against real Infrahub instances |
| 125 | +- **Coverage Target**: Maintained through codecov integration |
| 126 | + |
| 127 | +## CLI Architecture (`infrahubctl`) |
| 128 | + |
| 129 | +The CLI is built with Typer and provides extensive functionality: |
| 130 | + |
| 131 | +- **Schema Management**: Load, validate, and manage Infrahub schemas |
| 132 | +- **Transformations**: Run Jinja2-based data transformations |
| 133 | +- **Checks**: Execute validation checks against Infrahub data |
| 134 | +- **Branch Operations**: Create, merge, and manage branches |
| 135 | +- **Object Management**: CRUD operations on Infrahub objects |
| 136 | + |
| 137 | +CLI commands are auto-documented and organized in `infrahub_sdk/ctl/`. |
| 138 | + |
| 139 | +## Documentation System |
| 140 | + |
| 141 | +### Structure |
| 142 | + |
| 143 | +- **Docusaurus-based**: React/Node.js documentation system |
| 144 | +- **Auto-generation**: CLI docs and config reference generated via invoke tasks |
| 145 | +- **Multi-format**: Guides (task-oriented) and reference (API documentation) |
| 146 | + |
| 147 | +### Documentation Development |
| 148 | + |
| 149 | +```bash |
| 150 | +# Generate all docs |
| 151 | +poetry run invoke docs |
| 152 | + |
| 153 | +# Start development server (requires Node.js) |
| 154 | +cd docs && npm start |
| 155 | +``` |
| 156 | + |
| 157 | +## Development Tooling |
| 158 | + |
| 159 | +### Code Quality |
| 160 | + |
| 161 | +- **Ruff**: Comprehensive linting and formatting (0.11.0) |
| 162 | +- **mypy**: Type checking with strict configuration |
| 163 | +- **yamllint**: YAML file validation |
| 164 | +- **markdownlint**: Documentation consistency |
| 165 | +- **Vale**: Documentation style checking |
| 166 | + |
| 167 | +### CI/CD Integration |
| 168 | + |
| 169 | +GitHub Actions workflow runs: |
| 170 | + |
| 171 | +1. Multi-version Python testing (3.9-3.13) |
| 172 | +2. Comprehensive linting pipeline |
| 173 | +3. Documentation generation and validation |
| 174 | +4. Integration testing with Infrahub containers |
| 175 | +5. Coverage reporting |
| 176 | + |
| 177 | +## Key Configuration Files |
| 178 | + |
| 179 | +- **pyproject.toml**: Poetry dependencies, tool configurations (ruff, mypy, pytest) |
| 180 | +- **tasks.py**: Invoke task definitions for development workflows |
| 181 | +- **.github/workflows/ci.yml**: Comprehensive CI/CD pipeline |
| 182 | +- **docs/package.json**: Documentation build dependencies |
| 183 | + |
| 184 | +## Dependencies Management |
| 185 | + |
| 186 | +### Core Dependencies |
| 187 | + |
| 188 | +- **pydantic** (>=2.0.0): Configuration and data validation |
| 189 | +- **httpx**: Async/sync HTTP client with proxy support |
| 190 | +- **graphql-core**: GraphQL query building and parsing |
| 191 | +- **ujson**: Fast JSON serialization |
| 192 | + |
| 193 | +### Optional Dependencies |
| 194 | + |
| 195 | +- **ctl extra**: CLI functionality (typer, rich, jinja2) |
| 196 | +- **tests extra**: Testing framework extensions |
| 197 | +- **all extra**: Complete development environment |
| 198 | + |
| 199 | +## Documentation Writing Guidelines |
| 200 | + |
| 201 | +When writing or modifying MDX documentation files in this repository, follow these established patterns: |
| 202 | + |
| 203 | +### Framework: Diataxis |
| 204 | + |
| 205 | +All documentation follows the [Diataxis framework](https://diataxis.fr/): |
| 206 | + |
| 207 | +- **Tutorials** (learning-oriented): Step-by-step learning experiences |
| 208 | +- **How-to guides** (task-oriented): Problem-solving instructions |
| 209 | +- **Explanation** (understanding-oriented): Clarification and discussion of topics |
| 210 | +- **Reference** (information-oriented): Technical descriptions and specifications |
| 211 | + |
| 212 | +### Tone and Style |
| 213 | + |
| 214 | +- **Professional but approachable**: Use plain language with technical precision |
| 215 | +- **Concise and direct**: Prefer short, active sentences with minimal fluff |
| 216 | +- **Informative over promotional**: Focus on explaining how and why, not marketing |
| 217 | +- **Consistent structure**: Follow predictable patterns across documents |
| 218 | + |
| 219 | +### Document Structure Patterns |
| 220 | + |
| 221 | +#### How-to Guides (Task-oriented) |
| 222 | + |
| 223 | +```markdown |
| 224 | +--- |
| 225 | +title: How to [accomplish specific task] |
| 226 | +--- |
| 227 | + |
| 228 | +# Brief introduction stating the problem/goal |
| 229 | + |
| 230 | +## Prerequisites |
| 231 | +- What the user needs before starting |
| 232 | +- Required environment setup |
| 233 | + |
| 234 | +## Step-by-step Instructions |
| 235 | + |
| 236 | +### Step 1: [Action/Goal] |
| 237 | +- Clear, actionable instructions |
| 238 | +- Code snippets with proper language tags |
| 239 | +- Screenshots/images for visual guidance |
| 240 | +- Use Tabs for alternative methods (Web UI, GraphQL, Shell) |
| 241 | + |
| 242 | +### Step 2: [Next Action] |
| 243 | +- Continue structured approach |
| 244 | + |
| 245 | +## Validation |
| 246 | +- How to verify the solution worked |
| 247 | +- Expected outputs and potential issues |
| 248 | + |
| 249 | +## Related Resources |
| 250 | +- Links to related guides and topics |
| 251 | +``` |
| 252 | + |
| 253 | +#### Topics (Understanding-oriented) |
| 254 | + |
| 255 | +```markdown |
| 256 | +--- |
| 257 | +title: Understanding [concept or feature] |
| 258 | +--- |
| 259 | + |
| 260 | +# Introduction to what this explanation covers |
| 261 | + |
| 262 | +## Concepts & Definitions |
| 263 | +- Key terms and how they fit into Infrahub |
| 264 | + |
| 265 | +## Background & Context |
| 266 | +- Design decisions and rationale |
| 267 | +- Technical constraints and considerations |
| 268 | + |
| 269 | +## Architecture & Design |
| 270 | +- Diagrams and component interactions |
| 271 | +- Mental models and analogies |
| 272 | + |
| 273 | +## Connections |
| 274 | +- How this relates to other Infrahub concepts |
| 275 | +- Integration points and relationships |
| 276 | + |
| 277 | +## Further Reading |
| 278 | +- Links to guides, references, and external resources |
| 279 | +``` |
| 280 | + |
| 281 | +### Content Guidelines |
| 282 | + |
| 283 | +#### For Guides |
| 284 | + |
| 285 | +- Use conditional imperatives: "If you want X, do Y" |
| 286 | +- Address users directly: "Configure...", "Create...", "Deploy..." |
| 287 | +- Focus on practical tasks without digressing into explanations |
| 288 | +- Maintain focus on the specific goal |
| 289 | + |
| 290 | +#### For Topics |
| 291 | + |
| 292 | +- Use discursive, reflective tone that invites understanding |
| 293 | +- Include context, background, and rationale behind design decisions |
| 294 | +- Make connections between concepts and existing knowledge |
| 295 | +- Present alternative perspectives where appropriate |
| 296 | + |
| 297 | +### Terminology and Quality |
| 298 | + |
| 299 | +- **Define new terms** when first introduced |
| 300 | +- **Use domain-relevant language** from user perspective (playbooks, branches, schemas, commits) |
| 301 | +- **Be consistent** with Infrahub's data model and UI naming conventions |
| 302 | +- **Validate accuracy** against latest Infrahub version |
| 303 | +- **Follow markdown style** as defined in `.markdownlint.yaml` and Vale styles in `.vale/styles/` |
| 304 | + |
| 305 | +### Code Examples and Formatting |
| 306 | + |
| 307 | +- Use proper language tags for all code blocks |
| 308 | +- Include both async and sync examples where applicable using Tabs component |
| 309 | +- Provide realistic examples that reflect real-world complexity |
| 310 | +- Add validation steps to confirm success |
| 311 | +- Use callouts for warnings, tips, and important notes |
0 commit comments