Skip to content

Commit

Permalink
Feature 1 / add tfdoc and readme (#2)
Browse files Browse the repository at this point in the history
* feature-1 || aligning all tf-postgresql provider version

* feature-1 || add README for modules and examples

---------

Co-authored-by: Ridwan Fadjar <ridwan.fadjar@dkatalis.com>
  • Loading branch information
ridwanbejo and Ridwan Fadjar authored Dec 26, 2023
1 parent 4486ebd commit 2938162
Show file tree
Hide file tree
Showing 18 changed files with 471 additions and 192 deletions.
189 changes: 26 additions & 163 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,184 +1,47 @@
# Terraform MongoDB User
# Terraform PostgreSQL Admin

This is a Terraform module for managing user access at MongoDB. You can use this module both for commercial or non-commercial purposes.
This is a Terraform module for managing objects at PostgreSQL. You can use this module both for commercial or non-commercial purposes.

Currently, you can manage these resources in MongoDB by using this module:
Currently, you can manage these resources in PostgreSQL by using this module:

- users
- roles
- role
- database
- schema
- grant
- extension
- physical_replication_slot

Tested in:

- MongoDB
- PostgreSQL

## A. Prerequisites

Requirements:

- Terraform with version >= 1.4
- Kaginari/mongodb
- Cyrilgdn/postgresql
- Hashicorp/random

Before we continue to use the module, please pay attention to these terms for `database `in this module:
## B. How to use this module for your Terraform project ?

- `role_db`, selected database for storing role configuration. Default value from tf-provider is `admin`
- `target_db`, selected database which the privilege will be assigned to.
- `auth_database`, database that will be used by the user for authentication
You can check any examples projects that use this module under `examples` directory.

## B. How to use this module for your Terraform project ?
- config-1-basic
- role-1-basic
- database-1-basic
- grant-1-basic

- Copy `example/basic-1` project from this module. You can extend it as per your requirements
- Configure MongoDB host and port by modifying the `providers.tf`. For example `localhost` and `27017`
- If you want to use another authentication method, [please read more at the provider documentation](https://registry.terraform.io/providers/Kaginari/mongodb/latest/docs)
- Configure `TF_VAR_mongodb_username` and `TF_VAR_mongodb_password` as environment variables. For example:

```
$ export TF_VAR_mongodb_username=root
$ export TF_VAR_mongodb_password=example
```

- Check `terraform.tfvars` inside the Project. Please try to see how the variables configured.
- Adjust the tfvars based on your requirements. The tfvars is just example. Then, Save it
- Run these commands:

```
$ terraform init
$ terraform plan
```
This is the output when you run terraform plan successfully:

```
...
# module.tf_mongodb_user.random_password.password["septian"] will be created
+ resource "random_password" "password" {
+ bcrypt_hash = (sensitive value)
+ id = (known after apply)
+ length = 16
+ lower = true
+ min_lower = 0
+ min_numeric = 0
+ min_special = 0
+ min_upper = 0
+ number = true
+ numeric = true
+ override_special = "!#$%&*()-_=+[]{}<>:?"
+ result = (sensitive value)
+ special = true
+ upper = true
}
Plan: 10 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ mongodb_roles = {
+ developer = "admin"
+ guest = "admin"
}
+ mongodb_users = {
+ bejo = "reporting"
+ fadjar = "reporting"
+ ridwan = "reporting"
+ septian = "reporting"
}
```

After you feel confidence with the terraform plan output, let's apply it.

```
$ terraform apply -auto-approve
```

- If it succeed, you must see this kind of output on your terminal

```
...
module.tf_mongodb_user.mongodb_db_role.roles["developer"]: Creation complete after 0s [id=YWRtaW4uZGV2ZWxvcGVy]
module.tf_mongodb_user.mongodb_db_role.roles["guest"]: Creation complete after 0s [id=YWRtaW4uZ3Vlc3Q=]
module.tf_mongodb_user.random_password.password["septian"]: Creation complete after 0s [id=none]
module.tf_mongodb_user.random_password.password["bejo"]: Creation complete after 0s [id=none]
module.tf_mongodb_user.random_password.password["fadjar"]: Creation complete after 0s [id=none]
module.tf_mongodb_user.random_password.password["ridwan"]: Creation complete after 0s [id=none]
module.tf_mongodb_user.mongodb_db_user.users["bejo"]: Creating...
module.tf_mongodb_user.mongodb_db_user.users["fadjar"]: Creating...
module.tf_mongodb_user.mongodb_db_user.users["septian"]: Creating...
module.tf_mongodb_user.mongodb_db_user.users["ridwan"]: Creating...
module.tf_mongodb_user.mongodb_db_user.users["bejo"]: Creation complete after 0s [id=cmVwb3J0aW5nLmJlam8=]
module.tf_mongodb_user.mongodb_db_user.users["ridwan"]: Creation complete after 0s [id=cmVwb3J0aW5nLnJpZHdhbg==]
module.tf_mongodb_user.mongodb_db_user.users["septian"]: Creation complete after 0s [id=cmVwb3J0aW5nLnNlcHRpYW4=]
module.tf_mongodb_user.mongodb_db_user.users["fadjar"]: Creation complete after 0s [id=cmVwb3J0aW5nLmZhZGphcg==]
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
Outputs:
mongodb_roles = {
"developer" = "admin"
"guest" = "admin"
}
mongodb_users = {
"bejo" = "reporting"
"fadjar" = "reporting"
"ridwan" = "reporting"
"septian" = "reporting"
}
```

You will see at your MongoDB that users and roles are created once the terraform applied.
> p.s.: If you see foreign data wrapper and pubsub modules, basically it's executable. But when I run the example it's still showing `No changes infrastructure`. I keep the modules until I've found the workaround.
## C. Understanding tfvars scenarios

There are some scenarios that you could choose when using this module. For example:

1. user could be assined without any roles

```
mongodb_users = [
{
name = "bejo"
auth_database = "reporting"
roles = []
},
]
```

2. user could be assigned with default roles (e.g. readAnyDatabase)

```
mongodb_users = [
{
name = "septian"
auth_database = "reporting"
roles = [
{
name = "readAnyDatabase",
role_db = "admin"
}
]
},
]
```

3. user could be assigned with custom roles (e.g. developer)

```
mongodb_users = [
{
name = "ridwan"
auth_database = "reporting"
roles = [
{
name = "developer",
role_db = "admin"
}
]
},
]
```
You can check any examples tfvars that use this module under `examples` directory.

- config-1-basic
- role-1-basic
- database-1-basic
- grant-1-basic

## D. Ensuring quality

Expand All @@ -202,8 +65,8 @@ The tools:

## E. How to contribute ?

If you find any issue, you can raise it here at our [Issue Tracker](https://github.com/ridwanbejo/terraform-mongodb-user/issues)
If you find any issue, you can raise it here at our [Issue Tracker](https://github.com/ridwanbejo/terraform-postgresql-admin/issues)

If you have something that you want to merge to this repo, just raise [Pull Requests](https://github.com/ridwanbejo/terraform-mongodb-user/pulls)
If you have something that you want to merge to this repo, just raise [Pull Requests](https://github.com/ridwanbejo/terraform-postgresql-admin/pulls)

Ensure that you install all the tools from section D. for development purpose.
48 changes: 48 additions & 0 deletions examples/config-1-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Basic PostgreSQL config example

## Usage

To run this example you need to execute:

```
$ terraform init
$ terraform plan
$ terraform apply
```

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4 |
| <a name="requirement_postgresql"></a> [postgresql](#requirement\_postgresql) | 1.21.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | 3.5.1 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_tf_postgres_config"></a> [tf\_postgres\_config](#module\_tf\_postgres\_config) | ../../modules/config | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_postgres_extension"></a> [postgres\_extension](#input\_postgres\_extension) | n/a | `list(any)` | `[]` | no |
| <a name="input_postgres_physical_replication_slots"></a> [postgres\_physical\_replication\_slots](#input\_postgres\_physical\_replication\_slots) | n/a | `list(any)` | `[]` | no |
| <a name="input_postgres_replication_slots"></a> [postgres\_replication\_slots](#input\_postgres\_replication\_slots) | n/a | `list(any)` | `[]` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_postgres_extension"></a> [postgres\_extension](#output\_postgres\_extension) | Current PostgreSQL Extensions |
<!-- END_TF_DOCS -->
2 changes: 1 addition & 1 deletion examples/config-1-basic/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
postgresql = {
source = "cyrilgdn/postgresql"
version = "1.21.1-beta.1"
version = "1.21.0"
}

random = {
Expand Down
47 changes: 47 additions & 0 deletions examples/database-1-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Basic PostgreSQL database and schema example

## Usage

To run this example you need to execute:

```
$ terraform init
$ terraform plan
$ terraform apply
```

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4 |
| <a name="requirement_postgresql"></a> [postgresql](#requirement\_postgresql) | 1.21.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | 3.5.1 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_tf_postgres_database"></a> [tf\_postgres\_database](#module\_tf\_postgres\_database) | ../../modules/database | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_postgres_databases"></a> [postgres\_databases](#input\_postgres\_databases) | n/a | `list(any)` | `[]` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_postgres_databases"></a> [postgres\_databases](#output\_postgres\_databases) | Current PostgreSQL databases |
| <a name="output_postgres_schemas"></a> [postgres\_schemas](#output\_postgres\_schemas) | Current PostgreSQL schemas |
<!-- END_TF_DOCS -->
2 changes: 1 addition & 1 deletion examples/database-1-basic/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
postgresql = {
source = "cyrilgdn/postgresql"
version = "1.21.1-beta.1"
version = "1.21.0"
}

random = {
Expand Down
48 changes: 48 additions & 0 deletions examples/grant-1-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Basic PostgreSQL grant example

## Usage

To run this example you need to execute:

```
$ terraform init
$ terraform plan
$ terraform apply
```

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4 |
| <a name="requirement_postgresql"></a> [postgresql](#requirement\_postgresql) | 1.21.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | 3.5.1 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_tf_postgres_grant"></a> [tf\_postgres\_grant](#module\_tf\_postgres\_grant) | ../../modules/grant | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_pg_grant_roles"></a> [pg\_grant\_roles](#input\_pg\_grant\_roles) | n/a | `list(any)` | `[]` | no |
| <a name="input_pg_grants"></a> [pg\_grants](#input\_pg\_grants) | n/a | `list(any)` | `[]` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_pg_grant_roles"></a> [pg\_grant\_roles](#output\_pg\_grant\_roles) | Current PostgreSQL grant roles |
| <a name="output_pg_grants"></a> [pg\_grants](#output\_pg\_grants) | Current PostgreSQL grants |
<!-- END_TF_DOCS -->
Loading

0 comments on commit 2938162

Please sign in to comment.