Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 67 additions & 41 deletions .github/workflows/test_terraform_module.yaml
Original file line number Diff line number Diff line change
@@ -1,58 +1,84 @@
# Copyright 2025 Canonical Ltd.
# See LICENSE file for licensing details.

# Attention: valid for K8s charms only

name: Terraform module tests
name: Terraform module tests (machine-based)

on:
pull_request:
paths:
- 'terraform/**'
- "terraform/**"

permissions:
contents: read

jobs:
test-terraform:
# remove this condition for enabling the workflow
if: false
name: Test Terraform with Juju
runs-on: ubuntu-latest
env:
WORKING_DIR: 'terraform/tests'
WORKING_DIR: "terraform/tests"
steps:
- uses: actions/checkout@@v4.2.2
- uses: charmed-kubernetes/actions-operator@main
with:
provider: "k8s"
use-canonical-k8s: true
channel: 1.33-classic/stable
juju-channel: 3.6/stable
- name: Prepare juju tf provider environment
run: |
CONTROLLER=$(juju whoami | yq .Controller)
JUJU_CONTROLLER_ADDRESSES="$(juju show-controller | yq '.[$CONTROLLER]'.details.\"api-endpoints\" | tr -d "[]' "|tr -d '"'|tr -d '\n')"
JUJU_USERNAME="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.user|tr -d '"')"
JUJU_PASSWORD="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.password|tr -d '"')"

echo "JUJU_CONTROLLER_ADDRESSES=$JUJU_CONTROLLER_ADDRESSES" >> "$GITHUB_ENV"
echo "JUJU_USERNAME=$JUJU_USERNAME" >> "$GITHUB_ENV"
echo "JUJU_PASSWORD=$JUJU_PASSWORD" >> "$GITHUB_ENV"
{
echo 'JUJU_CA_CERT<<EOF'
juju show-controller $(echo $CONTROLLER|tr -d '"') | yq '.[$CONTROLLER]'.details.\"ca-cert\"|tr -d '"'
echo EOF
} >> "$GITHUB_ENV"
- uses: hashicorp/setup-terraform@v3.1.2
- run: terraform init
working-directory: ${{env.WORKING_DIR}}
- run: terraform plan -out=tfplan
working-directory: ${{env.WORKING_DIR}}
- run: terraform show tfplan
working-directory: ${{env.WORKING_DIR}}
- run: |
juju add-model prod-chat-example
set -e # Exit on error
terraform test || { echo "Terraform test failed"; exit 1; }
working-directory: ${{env.WORKING_DIR}}
- name: Check out code
uses: actions/checkout@v4.2.2

- name: Setup Juju with LXD
uses: charmed-kubernetes/actions-operator@main
with:
provider: "lxd"
juju-channel: 3/stable
channel: "latest/stable"

- name: Prepare juju tf provider environment
run: |
set -e
CONTROLLER=$(juju whoami | yq .Controller)
JUJU_CONTROLLER_ADDRESSES="$(juju show-controller | yq '.[$CONTROLLER]'.details.\"api-endpoints\" | tr -d "[]' "|tr -d '"'|tr -d '\n')"
JUJU_USERNAME="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.user|tr -d '"')"
JUJU_PASSWORD="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.password|tr -d '"')"

echo "JUJU_CONTROLLER_ADDRESSES=$JUJU_CONTROLLER_ADDRESSES" >> "$GITHUB_ENV"
echo "JUJU_USERNAME=$JUJU_USERNAME" >> "$GITHUB_ENV"
echo "JUJU_PASSWORD=$JUJU_PASSWORD" >> "$GITHUB_ENV"
{
echo 'JUJU_CA_CERT<<EOF'
juju show-controller $(echo $CONTROLLER|tr -d '"') | yq '.[$CONTROLLER]'.details.\"ca-cert\"|tr -d '"'
echo EOF
} >> "$GITHUB_ENV"
- uses: hashicorp/setup-terraform@v3.1.2
- run: terraform init
working-directory: ${{env.WORKING_DIR}}
- run: terraform validate
working-directory: ${{env.WORKING_DIR}}
- run: terraform plan -out=tfplan
working-directory: ${{env.WORKING_DIR}}
- run: terraform show tfplan
working-directory: ${{env.WORKING_DIR}}
- run: |
juju add-model test-aproxy-example
set -e # Exit on error
terraform test || { echo "Terraform test failed"; exit 1; }
working-directory: ${{env.WORKING_DIR}}
Comment thread
yanksyoon marked this conversation as resolved.

- name: Apply Terraform
run: terraform apply -auto-approve
working-directory: ${{ env.WORKING_DIR }}

- name: Deploy principal and relate aproxy
run: |
juju deploy ubuntu --channel=latest/stable --model test-aproxy-example
juju integrate ubuntu aproxy --model test-aproxy-example

- name: Wait for aproxy to be deployed
run: |
echo "Waiting for aproxy to be deployed..."
juju wait-for application aproxy --query='status=="maintenance" || status=="blocked"' --timeout=10m

- name: Verify aproxy application is deployed
run: |
STATUS=$(juju status aproxy --model test-aproxy-example --format=json | jq -r '.applications.aproxy["application-status"].current')
echo "aproxy status: $STATUS"
if [ "$STATUS" == "error" ] || [ "$STATUS" == "unknown" ]; then
echo "aproxy failed to deploy or is unknown"
juju status --model test-aproxy-example
exit 1
fi
27 changes: 13 additions & 14 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- Remember to update this file for your charm -- replace <charm_name> with the appropriate name. -->
# <charm_name> Terraform module
# Aproxy terraform module

This folder contains a base [Terraform][Terraform] module for the <charm_name> charm.
This folder contains a base [Terraform][Terraform] module for the aproxy charm.

The module uses the [Terraform Juju provider][Terraform Juju provider] to model the charm
deployment onto any Kubernetes environment managed by [Juju][Juju].
Expand All @@ -16,18 +15,18 @@ deployment onto any Kubernetes environment managed by [Juju][Juju].
the Juju application name.
- **versions.tf** - Defines the Terraform provider version.

## Using <charm_name> base module in higher level modules
## Using aproxy base module in higher level modules

If you want to use `<charm_name>` base module as part of your Terraform module, import it
If you want to use `aproxy` base module as part of your Terraform module, import it
like shown below:

```text
data "juju_model" "my_model" {
name = var.model
}

module "<charm_name>" {
source = "git::https://github.com/canonical/<charm_name>-operator//terraform"
module "aproxy" {
source = "git::https://github.com/canonical/aproxy-operator//terraform"

model = juju_model.my_model.name
# (Customize configuration variables here if needed)
Expand All @@ -37,22 +36,22 @@ module "<charm_name>" {
Create integrations, for instance:

```text
resource "juju_integration" "<charm_name>-loki" {
resource "juju_integration" "aproxy_to_principal" {
model = juju_model.my_model.name
application {
name = module.<charm_name>.app_name
endpoint = module.<charm_name>.endpoints.logging
name = module.aproxy.app_name
endpoint = module.aproxy.endpoints.juju_info
}
application {
name = "loki-k8s"
endpoint = "logging"
name = "principal-app"
endpoint = "juju-info"
}
}
```

The complete list of available integrations can be found [in the Integrations tab][<charm_name>-integrations].
The complete list of available integrations can be found [in the Integrations tab][aproxy-integrations].

[Terraform]: https://www.terraform.io/
[Terraform Juju provider]: https://registry.terraform.io/providers/juju/juju/latest
[Juju]: https://juju.is
[<charm_name>-integrations]: https://charmhub.io/<charm_name>/integrations
[aproxy-integrations]: https://charmhub.io/aproxy/integrations
15 changes: 6 additions & 9 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# Copyright 2025 Canonical Ltd.
# See LICENSE file for licensing details.

resource "juju_application" "charm_name" {
name = var.app_name
model = var.model

resource "juju_application" "aproxy" {
name = var.app_name
charm {
name = "<charm_name>"
name = "aproxy"
channel = var.channel
revision = var.revision
base = var.base
}

config = var.config
constraints = var.constraints
units = var.units
storage_directives = var.storage
model = var.model
config = var.config
units = 1
}
4 changes: 2 additions & 2 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

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

output "endpoints" {
value = {
ingress = "ingress"
juju_info = "juju-info"
}
}
21 changes: 17 additions & 4 deletions terraform/tests/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,31 @@ variable "revision" {
terraform {
required_providers {
juju = {
version = "~> 0.20.0"
version = "~> 0.23.0"
source = "juju/juju"
}
}
}

provider "juju" {}

module "charm_name" {
module "aproxy" {
source = "./.."
app_name = "charm_name"
app_name = "aproxy"
channel = var.channel
model = "prod-charm_name-example"
revision = var.revision
model = "test-aproxy-example"
config = {
proxy-address = "127.0.0.1:80"
}
}

output "app_name" {
description = "The name of the deployed aproxy charm application."
value = module.aproxy.app_name
}

output "endpoints" {
description = "Integration endpoints exposed by aproxy charm."
value = module.aproxy.endpoints
}
6 changes: 3 additions & 3 deletions terraform/tests/main.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

variables {
channel = "latest/edge"
# renovate: depName="charm_name"
# renovate: depName="aproxy"
revision = 1
}

run "basic_deploy" {
assert {
condition = module.charm_name.app_name == "charm_name"
error_message = "charm_name app_name did not match expected"
condition = module.aproxy.app_name == "aproxy"
error_message = "aproxy app_name did not match expected"
}
}
24 changes: 3 additions & 21 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# See LICENSE file for licensing details.

variable "app_name" {
description = "Name of the application in the Juju model."
description = "Application name for aproxy."
type = string
default = "<charm-name>"
default = "aproxy"
}

variable "base" {
Expand All @@ -20,17 +20,11 @@ variable "channel" {
}

variable "config" {
description = "Application config. Details about available options can be found at https://charmhub.io/<charm-name>/configurations."
description = "Application config. Details about available options can be found at https://charmhub.io/aproxy/configurations."
type = map(string)
default = {}
}

variable "constraints" {
description = "Juju constraints to apply for this application."
type = string
default = ""
}

variable "model" {
description = "Reference to a `juju_model`."
type = string
Expand All @@ -41,15 +35,3 @@ variable "revision" {
type = number
default = null
}

variable "storage" {
description = "Map of storage used by the application."
type = map(string)
default = {}
}

variable "units" {
description = "Number of units to deploy"
type = number
default = 1
}
2 changes: 1 addition & 1 deletion terraform/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
juju = {
source = "juju/juju"
version = ">= 0.20.0"
version = ">= 0.23.0"
}
}
}
Loading