Skip to content

Commit

Permalink
feat: Add the Terraform module - apply Gulsum's changes (#355)
Browse files Browse the repository at this point in the history
##Feature
Adding the Terraform module for the mongodb-operator charm.

---------

Co-authored-by: Mehdi-Bendriss <[email protected]>
Co-authored-by: Gulsum Atici <[email protected]>
  • Loading branch information
3 people authored Feb 29, 2024
1 parent a3a3ff5 commit 14b6468
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 4 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
run: python3 -m pip install tox
- name: Run tests
run: tox run -e unit

lib-check:
name: Check libraries
runs-on: ubuntu-latest
Expand All @@ -54,6 +55,37 @@ jobs:
credentials: "${{ secrets.CHARMHUB_TOKEN }}" # FIXME: current token will expire in 2023-07-04
github-token: "${{ secrets.GITHUB_TOKEN }}"

jobs:
check-terraform:
name: Check Terraform
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./terraform

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3

- name: Initialize Terraform Module
run: terraform init

- name: Validate Terraform Module
run: terraform validate -no-color

- name: Validate terraform fmt
run: |
set +e
terraform fmt -recursive -check -diff
FMT_STATUS="$?"
if [[ "$FMT_STATUS" -ne 0 ]]; then
echo "❌ terraform fmt failed" >> "$GITHUB_STEP_SUMMARY"
fi
exit "$FMT_STATUS"
build:
strategy:
matrix:
Expand Down
54 changes: 50 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
venv/
build/
*.charm
.tox/
*.idea
.vscode/
.coverage
.tox/
venv/
build/

# Python
**/venv/**
*.pyc
.python-version
.mypy_cache/
__pycache__/
*.py[cod]

# Charmcraft
*.charm

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
.terraform.lock.hcl

58 changes: 58 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# MongoDB Operator Terraform module

This folder contains a base [Terraform][Terraform] module for the `mongodb` charm.

The module uses the [Terraform Juju provider][Terraform Juju provider] to model the charm deployment onto any Kubernetes environment managed by [Juju][Juju].

The base module is not intended to be deployed in separation (it is possible though), but should rather serve as a building block for higher level modules.

## Module structure

- **main.tf** - Defines the Juju application to be deployed.
- **variables.tf** - Allows customization of the deployment such as Juju model name, channel or application name and charm configuration.
- **output.tf** - Responsible for integrating the module with other Terraform modules, primarily by defining potential integration endpoints (charm integrations), but also by exposing the application name.
- **terraform.tf** - Defines the Terraform provider.

## Using mongodb base module in higher level modules

If you want to use `mongodb-operator` base module as part of your Terraform module, import it like shown below.

```text
module "mongodb-operator" {
source = "git::https://github.com/canonical/mongodb-operator.git/terraform"
model_name = "juju_model_name"
(Customize configuration variables here if needed)
}
```

Please see the link to customize the Grafana configuration variables if needed.

- [MongoDB configuration options][MongoDB configuration options]

Create the integrations, for instance:

```text
resource "juju_integration" "amf-db" {
model = var.model_name
application {
name = module.amf.app_name
endpoint = module.amf.database_endpoint
}
application {
name = module.mongodb.app_name
endpoint = module.mongodb.database_endpoint
}
}
```

Please check the available [integration pairs][integration pairs].

[Terraform]: https://www.terraform.io/
[Juju]: https://juju.is
[Terraform Juju provider]: https://registry.terraform.io/providers/juju/juju/latest
[MongoDB configuration options]: https://charmhub.io/mongodb/configure?channel=6/edge
[integration pairs]: https://charmhub.io/mongodb/integrations?channel=6/edge
16 changes: 16 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

resource "juju_application" "mongodb" {
name = var.app_name
model = var.model_name

charm {
name = "mongodb"
channel = var.channel
base = "[email protected]"
}
config = var.config
units = 1
trust = true
}
67 changes: 67 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

output "app_name" {
description = "Name of the deployed application."
value = juju_application.mongodb.name
}

# Provided integration endpoints

output "database_endpoint" {
description = "Name of the endpoint to provide the mongodb_client interface."
value = "database"
}

output "obsolete_endpoint" {
description = "Name of the endpoint to provide the mongodb interface."
value = "obsolete"
}

output "cos_agent_endpoint" {
description = "Name of the endpoint to provide the cos_agent interface."
value = "cos-agent"
}

output "config_server_endpoint" {
description = "Name of the endpoint to provide the shards interface."
value = "config-server"
}

output "cluster_endpoint" {
description = "Name of the endpoint to provide the config-server interface."
value = "cluster"
}

output "metrics_endpoint" {
description = "Name of the endpoint to provide the prometheus_scrape interface."
value = "metrics-endpoint"
}

output "grafana_dashboard_endpoint" {
description = "Name of the endpoint to provide the grafana_dashboard interface."
value = "grafana-dashboard"
}

output "logging_endpoint" {
description = "Name of the endpoint to provide the loki_push_api relation interface."
value = "logging"
}


# Required integration endpoints

output "certificates_endpoint" {
description = "Name of the endpoint to provide the tls-certificates interface."
value = "certificates"
}

output "s3_credentials_endpoint" {
description = "Name of the endpoint to provide the s3 interface."
value = "s3-credentials"
}

output "sharding_endpoint" {
description = "Name of the endpoint to provide the shards interface."
value = "sharding"
}
11 changes: 11 additions & 0 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

terraform {
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.10.1"
}
}
}
26 changes: 26 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

variable "model_name" {
description = "Name of Juju model to deploy application to"
type = string
default = ""
}

variable "app_name" {
description = "Name of the application in the Juju model"
type = string
default = "mongodb"
}

variable "channel" {
description = "The channel to use when deploying a charm"
type = string
default = "6/beta"
}

variable "config" {
description = "Additional configuration for the MongoDB. Details about available options can be found at https://charmhub.io/mongodb/configure?channel=6/beta."
type = map(string)
default = {}
}

0 comments on commit 14b6468

Please sign in to comment.