|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This is a Terraform provider for Cortex.io (https://www.cortexapp.com), built using the terraform-plugin-framework. It allows users to manage Cortex resources (catalog entities, departments, scorecards, resource definitions, etc.) through Terraform. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Environment Setup |
| 12 | +- Copy `.env-example` to `.env` and set `CORTEX_API_TOKEN` before running tests |
| 13 | +- The Makefile loads environment variables from `.env` file automatically |
| 14 | + |
| 15 | +### Build and Installation |
| 16 | +- `make build` - Build the provider binary to `./bin/terraform-provider-cortex` |
| 17 | +- `make install` - Build and install to local Terraform plugins directory (`~/.terraform.d/plugins/`) |
| 18 | +- `make release` - Build release binaries for Linux (amd64) and Darwin (amd64, arm64) |
| 19 | + |
| 20 | +### Testing |
| 21 | +- `make test` - Run unit tests (clears test cache first) |
| 22 | +- `make testacc` - Run acceptance tests (requires valid `CORTEX_API_TOKEN`, creates real resources) |
| 23 | +- `go test -v ./internal/provider -run TestName` - Run a specific test |
| 24 | + |
| 25 | +### Code Quality |
| 26 | +- `make lint` - Run golangci-lint |
| 27 | +- `make format` - Format code with go fmt |
| 28 | +- `make docs` - Generate provider documentation via `go generate` |
| 29 | + |
| 30 | +### Local Development |
| 31 | +To use a locally built provider: |
| 32 | +1. Build: `go build -o build/terraform-provider-cortex .` |
| 33 | +2. Create `~/.terraformrc` with dev_overrides pointing to the build directory |
| 34 | +3. Use `source = "cortexlocal/cortex"` in your terraform configuration |
| 35 | + |
| 36 | +## Architecture |
| 37 | + |
| 38 | +### Package Structure |
| 39 | +- `main.go` - Provider entry point, runs the provider server |
| 40 | +- `internal/provider/` - Terraform provider implementation (resources, data sources, schemas) |
| 41 | +- `internal/cortex/` - Cortex API client library |
| 42 | + |
| 43 | +### API Client Architecture (`internal/cortex/`) |
| 44 | +The HTTP client uses a functional options pattern for initialization: |
| 45 | +- `HttpClient` - Core client with two sling clients (JSON and YAML decoders) |
| 46 | +- Client interfaces accessed via methods like `client.CatalogEntities()`, `client.Teams()`, etc. |
| 47 | +- `BaseUris` map defines API endpoints for different resource types |
| 48 | +- `Route(domain, path)` helper constructs full API paths |
| 49 | +- Error handling via `ApiError` type with special handling for 404 and 401 |
| 50 | + |
| 51 | +### Provider Architecture (`internal/provider/`) |
| 52 | +- `provider.go` - Main provider configuration, registers all resources and data sources |
| 53 | +- Resources implement `resource.Resource` and `resource.ResourceWithImportState` interfaces |
| 54 | +- Data sources implement `datasource.DataSource` interface |
| 55 | +- Each resource/data source has corresponding `*_models.go` files for Terraform state models |
| 56 | +- Models use `tfsdk` struct tags to map to Terraform schema attributes |
| 57 | + |
| 58 | +### Resource/Data Source Pattern |
| 59 | +Each resource follows this structure: |
| 60 | +1. Resource struct with `client *cortex.HttpClient` field |
| 61 | +2. Model struct(s) with `types.String`, `types.List`, etc. for state management |
| 62 | +3. CRUD methods: `Create`, `Read`, `Update`, `Delete` (resources only) |
| 63 | +4. Schema definition with attributes, validators, and plan modifiers |
| 64 | +5. Conversion methods between Terraform models and API models (e.g., `ToApiModel`, `FromApiResponse`) |
| 65 | + |
| 66 | +### Available Resources |
| 67 | +- `cortex_catalog_entity` - Catalog entities (services, resources, teams, domains) |
| 68 | +- `cortex_catalog_entity_custom_data` - Custom data for catalog entities |
| 69 | +- `cortex_catalog_entity_openapi` - OpenAPI specs for catalog entities (YAML format) |
| 70 | +- `cortex_department` - Departments |
| 71 | +- `cortex_resource_definition` - Resource type definitions |
| 72 | +- `cortex_scorecard` - Scorecards |
| 73 | + |
| 74 | +### Available Data Sources |
| 75 | +- `cortex_catalog_entity` - Read catalog entities |
| 76 | +- `cortex_catalog_entity_custom_data` - Read custom data |
| 77 | +- `cortex_department` - Read departments |
| 78 | +- `cortex_resource_definition` - Read resource definitions |
| 79 | +- `cortex_scorecard` - Read scorecards |
| 80 | +- `cortex_team` - Read teams |
| 81 | + |
| 82 | +### Special Parsers |
| 83 | +- `catalog_entity_parser.go` - Handles conversion between Cortex API entity format and Terraform state |
| 84 | +- `scorecard_parser.go` - Handles scorecard-specific conversions and validation |
| 85 | + |
| 86 | +## Provider Configuration |
| 87 | + |
| 88 | +Environment variables: |
| 89 | +- `CORTEX_API_TOKEN` - Required API token for authentication |
| 90 | +- `CORTEX_API_URL` - Optional API URL (defaults to `https://api.getcortexapp.com`) |
| 91 | +- `HTTP_DEBUG=1` - Enable HTTP request/response logging via go-loghttp |
| 92 | + |
| 93 | +## Important Notes |
| 94 | + |
| 95 | +- The provider uses version from Makefile (`VERSION=0.4.6-dev`), injected at build time via ldflags |
| 96 | +- Acceptance tests create real resources and require a valid API token |
| 97 | +- Documentation is auto-generated via terraform-plugin-docs (run `go generate`) |
| 98 | +- The `catalog_entity_openapi` resource uses YAML decoder for OpenAPI specs |
| 99 | +- Many resources support both JSON and YAML format responses from the API |
0 commit comments