# megum1n/terraform-provider-mongodb architecture

> Architecture diagram of the GitHub repository megum1n/terraform-provider-mongodb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/megum1n/terraform-provider-mongodb
- Repository: https://github.com/megum1n/terraform-provider-mongodb

## Overview

##### Project type and purpose
- This is a Go-based Terraform provider plugin whose sole purpose is to let Terraform users manage MongoDB resources (users, roles) via Terraform HCL configurations.
- Key indicators:
- README title: “Terraform provider for MongoDB resources management”
- Dir “internal/provider” with Terraform resource implementations
- Dir “internal/mongodb” with client code to talk to MongoDB
- Main entrypoint “main.go” (Terraform SDK plugin)

##### File structure
- Top-level Go module (go.mod, go.sum), build tooling (Makefile, goreleaser, golangci-lint), CI workflows under .github.
- internal/provider – provider definition and resource code (role_resource.go, user_resource.go, index_resource.go).
- internal/mongodb – MongoDB client wrappers and type definitions.
- example/ – sample Terraform configurations and Docker-Compose to spin up MongoDB.
- docs/ – markdown docs describing resources (user, role).

##### README for additional insights
- Very concise, points to Terraform registry.
- No in-depth architecture notes, so we infer structure from code layout and Terraform SDK conventions.

a. Main components
- Terraform CLI & core engine (user runs “terraform apply”)
- Provider plugin (the compiled binary from main.go)
- Resource modules (UserResource, RoleResource, IndexResource under internal/provider)
- MongoDB client library layer (internal/mongodb)
- MongoDB server/cluster
- Example/deployment environment (Docker-Compose, example.tf for demo)
b. Relationships and interactions
1. User writes HCL → Terraform CLI
2. Terraform CLI invokes Terraform core → calls provider plugin
3. Provider plugin’s Configure function reads provider config, initializes MongoDB client
4. For each resource block:
– Terraform calls Resource.Create/Read/Update/Delete
– Resource code in internal/provider calls into internal/mongodb client methods
– MongoDB client issues commands to MongoDB server
5. State is returned back up Terraform core → CLI outputs results
c. Architectural patterns and design principles
- Plugin architecture: clear separation between Terraform core and provider code
- Layered design:
– Provider/resource abstraction layer
– Database client abstraction layer
- Dependency injection: client is created in provider.Configure, injected into resources
d. Relevant technologies, frameworks, libraries
- Go language modules (go.mod)
- Terraform Plugin SDK (imported in provider code)
- MongoDB Go driver (used in internal/mongodb/client.go)
- Docker Compose (in example/)
- CI/CD via GitHub Actions + GoReleaser

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Terraform Layer
    HCL["Terraform HCL Config"]:::terraform
    CLI["Terraform CLI & Core"]:::terraform

    %% Plugin Boundary
    CLI -->|"initialize plugin over RPC"| Plugin
    CLI -->|"invoke Create/Read/Update/Delete"| Plugin

    %% Provider Plugin
    subgraph "Provider Plugin" 
        direction TB
        PMain["main.go"]:::provider
        ProvDef["provider.go"]:::provider

        subgraph "Resource Modules"
            direction TB
            UserRes["user_resource.go"]:::provider
            RoleRes["role_resource.go"]:::provider
            IndexRes["index_resource.go"]:::provider
        end

        subgraph "MongoDB Client Layer"
            direction TB
            Client["client.go"]:::db
            Errors["errors.go"]:::db

            subgraph "User Operations"
                direction TB
                UserOp["user.go"]:::db
                UserTypes["types_user.go"]:::db
            end

            subgraph "Role Operations"
                direction TB
                RoleOp["role.go"]:::db
                RoleTypes["types_role.go"]:::db
            end

            subgraph "Index Operations"
                direction TB
                IndexOp["index.go"]:::db
                IndexTypes["types_index.go"]:::db
            end
        end
    end

    %% Connections inside Provider
    Plugin --> PMain
    PMain --> ProvDef
    ProvDef --> UserRes
    ProvDef --> RoleRes
    ProvDef --> IndexRes

    UserRes -->|"calls client methods"| Client
    RoleRes -->|"calls client methods"| Client
    IndexRes -->|"calls client methods"| Client

    Client --> Errors
    Client --> UserOp
    Client --> RoleOp
    Client --> IndexOp

    UserOp --> UserTypes
    RoleOp --> RoleTypes
    IndexOp --> IndexTypes

    %% MongoDB Server
    DB["MongoDB Server/Cluster"]:::db
    Client -->|"sends commands"| DB

    %% Example Environment
    subgraph "Example Deployment"
        direction TB
        ExampleDocker["docker-compose.yaml"]:::example
        ExampleTF["example.tf"]:::example
        TerraformTF["terraform.tf"]:::example
        VarsTF["variables.tf"]:::example
    end
    ExampleDocker --> DB
    ExampleTF --> HCL
    TerraformTF --> HCL
    VarsTF --> HCL

    %% Documentation
    subgraph "Documentation"
        direction TB
        DocsIndex["docs/index.md"]:::docs
        DocsResIndex["docs/resources/index.md"]:::docs
        DocsUser["docs/resources/user.md"]:::docs
        DocsRole["docs/resources/role.md"]:::docs
    end

    %% Click Events
    click PMain "https://github.com/megum1n/terraform-provider-mongodb/blob/master/main.go"
    click ProvDef "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/provider/provider.go"
    click UserRes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/provider/user_resource.go"
    click RoleRes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/provider/role_resource.go"
    click IndexRes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/provider/index_resource.go"
    click Client "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/client.go"
    click Errors "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/errors.go"
    click UserOp "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/user.go"
    click UserTypes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/types_user.go"
    click RoleOp "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/role.go"
    click RoleTypes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/types_role.go"
    click IndexOp "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/index.go"
    click IndexTypes "https://github.com/megum1n/terraform-provider-mongodb/blob/master/internal/mongodb/types_index.go"
    click ExampleDocker "https://github.com/megum1n/terraform-provider-mongodb/blob/master/example/docker-compose.yaml"
    click ExampleTF "https://github.com/megum1n/terraform-provider-mongodb/blob/master/example/example.tf"
    click TerraformTF "https://github.com/megum1n/terraform-provider-mongodb/blob/master/example/terraform.tf"
    click VarsTF "https://github.com/megum1n/terraform-provider-mongodb/blob/master/example/variables.tf"
    click DocsIndex "https://github.com/megum1n/terraform-provider-mongodb/blob/master/docs/index.md"
    click DocsResIndex "https://github.com/megum1n/terraform-provider-mongodb/blob/master/docs/resources/index.md"
    click DocsUser "https://github.com/megum1n/terraform-provider-mongodb/blob/master/docs/resources/user.md"
    click DocsRole "https://github.com/megum1n/terraform-provider-mongodb/blob/master/docs/resources/role.md"

    %% Styles
    classDef terraform fill:#cce5ff,stroke:#004085,color:#004085;
    classDef provider fill:#d4edda,stroke:#155724,color:#155724;
    classDef db fill:#fff3cd,stroke:#856404,color:#856404;
    classDef example fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef docs fill:#f8d7da,stroke:#721c24,color:#721c24;
```
