juju / terraform-provider-juju

A Terraform provider for Juju
Apache License 2.0
19 stars 32 forks source link

SAAS application not removed when removing offering model #473

Open gboutry opened 2 months ago

gboutry commented 2 months ago

Description

SAAS application are not removed when removing the offering model which in turns makes it impossible re-relate the application when re-creating the second model

Urgency

Casually reporting

Terraform Juju Provider version

0.11.0

Terraform version

v1.8.2

Terraform Configuration(s)

terraform {

  required_providers {
    juju = {
      source  = "juju/juju"
      version = "= 0.11.0"
    }
  }

}

provider "juju" {}

## MODEL A
resource "juju_model" "rodents" {
  name = "rodents"
}

resource "juju_application" "mysquirrel" {
  name  = "mysquirrel"
  model = juju_model.rodents.name
  trust = true

  charm {
    name    = "mysql-k8s"
    channel = "8.0/stable"
    base    = "ubuntu@22.04"
  }
}

resource "juju_application" "grafana-agent" {
  name  = "grafana-agent"
  model = juju_model.rodents.name

  charm {
    name    = "grafana-agent-k8s"
    channel = "1.0/stable"
    base    = "ubuntu@22.04"
  }
}

resource "juju_integration" "mysquirrel-to-grafana-agent" {
  model = juju_model.rodents.name

  application {
    name     = juju_application.mysquirrel.name
    endpoint = "grafana-dashboard"
  }

  application {
    name     = juju_application.grafana-agent.name
    endpoint = "grafana-dashboards-consumer"
  }

}

## MODEL B
resource "juju_model" "observabitaly" {
  name = "observabitaly"
}

resource "juju_application" "grafana" {
  name  = "grafana"
  model = juju_model.observabitaly.name
  trust = true

  charm {
    name    = "grafana-k8s"
    channel = "1.0/stable"
    base    = "ubuntu@20.04"
  }
}

resource "juju_application" "traefik" {
  name  = "traefik"
  model = juju_model.observabitaly.name

  charm {
    name    = "traefik-k8s"
    channel = "1.0/stable"
    base    = "ubuntu@20.04"
  }
}

resource "juju_integration" "traefik-to-grafana" {
  model = juju_model.observabitaly.name

  application {
    name     = juju_application.traefik.name
    endpoint = "traefik-route"
  }

  application {
    name     = juju_application.grafana.name
    endpoint = "ingress"
  }
}

## OFFER

resource "juju_offer" "grafana-dashboard-offer" {
  name             = "grafana-dashboards"
  model            = juju_model.observabitaly.name
  application_name = juju_application.grafana.name
  endpoint         = "grafana-dashboard"
}

## INTEGRATE TO OFFER

resource "juju_integration" "grafana-agent-to-grafana" {
  model = juju_model.rodents.name

  application {
    name     = juju_application.grafana-agent.name
    endpoint = "grafana-dashboards-provider"
  }

  application {
    offer_url = juju_offer.grafana-dashboard-offer.url
  }
}

Reproduce / Test

- terraform init
- terraform apply
- terraform destroy -target juju_model.observabitaly
- terraform apply

Debug/Panic Output

╷
│ Error: Client Error
│ 
│   with juju_integration.grafana-agent-to-grafana,
│   on main.tf line 111, in resource "juju_integration" "grafana-agent-to-grafana":
│  111: resource "juju_integration" "grafana-agent-to-grafana" {
│ 
│ Unable to consume remote offer, got error: saas application called "grafana-dashboards" from a different model already exists
|

Notes & References

This plan is a simplified version from Sunbeam, where an user can enable / disable the Observability plugin at will, but this is preventing the user from re-enabling it.

https://bugs.launchpad.net/snap-openstack/+bug/2063183

gboutry commented 2 months ago

One way to solve this problem I believe, would be to add a new resource like juju_remote_application that would essentially behave like consume on create and remove-saas on delete.

flowchart LR
    A[App A] -->|Integrate| C(Remote APP)
    B[App B] -->|Integrate| C
    C -->|Cross Model| D[Offer]
    D--> E[App C]

Remove-saas removes relation, but with the dependency graph, dependent relations would be removed by terraform before the remote app is destroyed, which should be fine.

This would also help with: https://github.com/juju/terraform-provider-juju/issues/323 I think