-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Issue We would like an easy way to deploy MongoDB using terraform on lxd and openstack ## Solution Add ## Manual Testing ``` terraform init terraform apply ``` done on both LXD and on OpenStack --------- Co-authored-by: Neha Oudin <[email protected]>
- Loading branch information
1 parent
c74db02
commit 62dd700
Showing
11 changed files
with
333 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,66 @@ | ||
# MongoDB Operator Terraform module | ||
# Terraform module for mongodb-operator | ||
|
||
This folder contains a base [Terraform][Terraform] module for the `mongodb` charm. | ||
This is a Terraform module facilitating the deployment of the MongoDB charm with [Terraform juju provider](https://github.com/juju/terraform-provider-juju/). For more information, refer to the provider [documentation](https://registry.terraform.io/providers/juju/juju/latest/docs). | ||
|
||
The module uses the [Terraform Juju provider][Terraform Juju provider] to model the charm deployment onto any Kubernetes environment managed by [Juju][Juju]. | ||
## Requirements | ||
This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details. | ||
|
||
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. | ||
## API | ||
|
||
## Module structure | ||
### Inputs | ||
The module offers the following configurable inputs: | ||
|
||
- **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. | ||
| Name | Type | Description | Required | | ||
| - | - | - | - | | ||
| `app_name`| string | Application name | False | | ||
| `channel`| string | Channel that the charm is deployed from | False | | ||
| `base`| string | The series to be used for this charm | False | | ||
| `config`| map(string) | Map of the charm configuration options | False | | ||
| `model_name`| string | Name of the model that the charm is deployed on | True | | ||
| `resources`| map(string) | Map of the charm resources | False | | ||
| `revision`| number | Revision number of the charm name | False | | ||
| `units`| number | Number of units to be deployed | False | | ||
| `constraints`| string | Machine constraints for the charm | False | | ||
| `storage`| map(string) | Storage description, must follow the juju provider schema | False | | ||
|
||
## 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. | ||
### Outputs | ||
Upon applied, the module exports the following outputs: | ||
|
||
```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) | ||
} | ||
``` | ||
| Name | Description | | ||
| - | - | | ||
| `app_name`| Application name | | ||
| `provides`| Map of `provides` endpoints | | ||
| `requires`| Map of `requires` endpoints | | ||
|
||
Please see the link to customize the Grafana configuration variables if needed. | ||
## Usage | ||
|
||
- [MongoDB configuration options][MongoDB configuration options] | ||
This module is intended to be used as part of a higher-level module. When defining one, users should ensure that Terraform is aware of the `juju_model` dependency of the charm module. There are two options to do so when creating a high-level module: | ||
|
||
Create the integrations, for instance: | ||
### Define a `juju_model` resource | ||
Define a `juju_model` resource and pass to the `model_name` input a reference to the `juju_model` resource's name. For example: | ||
|
||
```text | ||
resource "juju_integration" "amf-db" { | ||
model = var.model_name | ||
application { | ||
name = module.amf.app_name | ||
endpoint = module.amf.database_endpoint | ||
} | ||
``` | ||
resource "juju_model" "mongodb" { | ||
name = mongodb | ||
} | ||
application { | ||
name = module.mongodb.app_name | ||
endpoint = module.mongodb.database_endpoint | ||
} | ||
module "mongodb-operator" { | ||
source = "<path-to-this-directory>" | ||
model_name = juju_model.mongodb.name | ||
} | ||
``` | ||
|
||
Please check the available [integration pairs][integration pairs]. | ||
### Define a `data` source | ||
Define a `data` source and pass to the `model_name` input a reference to the `data.juju_model` resource's name. This will enable Terraform to look for a `juju_model` resource with a name attribute equal to the one provided, and apply only if this is present. Otherwise, it will fail before applying anything. | ||
|
||
[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 | ||
``` | ||
data "juju_model" "mongodb" { | ||
name = var.model_name | ||
} | ||
module "mongodb" { | ||
source = "<path-to-this-directory>" | ||
model_name = data.juju_model.mongodb.name | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,40 @@ | |
# 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]" | ||
name = "mongodb" | ||
channel = var.channel | ||
revision = var.revision | ||
base = "[email protected]" | ||
} | ||
config = var.config | ||
units = 1 | ||
trust = true | ||
} | ||
config = var.config | ||
model = var.model | ||
name = var.app_name | ||
units = var.units | ||
constraints = var.constraints | ||
|
||
|
||
# TODO: uncomment once final fixes have been added for: | ||
# Error: juju/terraform-provider-juju#443, juju/terraform-provider-juju#182 | ||
# placement = join(",", var.machines) | ||
|
||
endpoint_bindings = [ | ||
for k, v in var.endpoint_bindings : { | ||
endpoint = k, space = v | ||
} | ||
] | ||
|
||
storage_directives = var.storage | ||
|
||
lifecycle { | ||
precondition { | ||
condition = length(var.machines) == 0 || length(var.machines) == var.units | ||
error_message = "Machine count does not match unit count" | ||
} | ||
precondition { | ||
condition = length(var.storage) == 0 || lookup(var.storage, "count", 0) <= 1 | ||
error_message = "Only one storage is supported" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
resource "null_resource" "preamble" { | ||
provisioner "local-exec" { | ||
command = <<-EOT | ||
sudo snap install juju-wait --classic || true | ||
EOT | ||
} | ||
} | ||
|
||
resource "juju_application" "self-signed-certificates" { | ||
charm { | ||
name = "self-signed-certificates" | ||
channel = "latest/stable" | ||
} | ||
model = var.model_name | ||
depends_on = [null_resource.preamble] | ||
} | ||
|
||
resource "juju_application" "data-integrator" { | ||
charm { | ||
name = "data-integrator" | ||
channel = "latest/stable" | ||
} | ||
model = var.model_name | ||
depends_on = [null_resource.preamble] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
terraform { | ||
required_providers { | ||
juju = { | ||
source = "juju/juju" | ||
version = "~> 0.14.0" | ||
} | ||
http = { | ||
source = "hashicorp/http" | ||
version = "~> 3.4.5" | ||
} | ||
external = { | ||
source = "hashicorp/external" | ||
version = "~> 2.3.4" | ||
} | ||
} | ||
} |
Oops, something went wrong.