From cfab4569bf4a5ade7b3494b20f81f616c178de19 Mon Sep 17 00:00:00 2001 From: Ales Stimec Date: Wed, 18 Dec 2024 17:16:11 +0100 Subject: [PATCH] fix[offers]: remove force destroy of application offers Removes force removal of application offers, but rather errors out if there are existing integrations with the offer that must be removed first. This is a much cleaner way to destroy offers as the use of the `force` flag may leave the Juju state with leftover artefacts. --- internal/juju/offers.go | 15 ++++++++------- internal/provider/resource_integration_test.go | 6 ++++++ internal/provider/resource_offer_test.go | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/internal/juju/offers.go b/internal/juju/offers.go index b8428417..2574d88c 100644 --- a/internal/juju/offers.go +++ b/internal/juju/offers.go @@ -195,16 +195,17 @@ func (c offersClient) DestroyOffer(input *DestroyOfferInput) error { return err } - forceDestroy := false - //This code loops until it detects 0 connections in the offer or 3 minutes elapses + //This code loops until it detects 0 connections in the offer or 5 minutes elapses if len(offer.Connections) > 0 { end := time.Now().Add(5 * time.Minute) for ok := true; ok; ok = len(offer.Connections) > 0 { - //if we have been failing to destroy offer for 5 minutes then force destroy - //TODO: investigate cleaner solution (acceptance tests fail even if timeout set to 20m) + //if we have been failing to destroy offer for 5 minutes then fail on destroy if time.Now().After(end) { - forceDestroy = true - break + connections := make([]string, len(offer.Connections)) + for i, connection := range offer.Connections { + connections[i] = fmt.Sprintf("%s:%s", connection.SourceModelUUID, connection.Endpoint) + } + return fmt.Errorf("offer %q has remaining integrations: %s", input.OfferURL, strings.Join(connections, ", ")) } time.Sleep(10 * time.Second) offer, err = client.ApplicationOffer(input.OfferURL) @@ -214,7 +215,7 @@ func (c offersClient) DestroyOffer(input *DestroyOfferInput) error { } } - err = client.DestroyOffers(forceDestroy, input.OfferURL) + err = client.DestroyOffers(false, input.OfferURL) if err != nil { return err } diff --git a/internal/provider/resource_integration_test.go b/internal/provider/resource_integration_test.go index 4c334a2a..66517efe 100644 --- a/internal/provider/resource_integration_test.go +++ b/internal/provider/resource_integration_test.go @@ -208,6 +208,8 @@ resource "juju_integration" "a" { application { offer_url = juju_offer.b.url } + + depends_ok = [juju_offer.b] } `, srcModelName, aOS, dstModelName, bOS, viaCIDRs) } @@ -315,6 +317,8 @@ resource "juju_integration" "b1" { application { offer_url = juju_offer.a.url } + + depends_on = [juju_offer.a] } resource "juju_application" "b2" { @@ -339,6 +343,8 @@ resource "juju_integration" "b2" { application { offer_url = juju_offer.a.url } + + depends_on = [juju_offer.a] } variable "enable-b1-consumer" { diff --git a/internal/provider/resource_offer_test.go b/internal/provider/resource_offer_test.go index 53dd75af..396f06f0 100644 --- a/internal/provider/resource_offer_test.go +++ b/internal/provider/resource_offer_test.go @@ -101,6 +101,8 @@ resource "juju_integration" "int" { application { offer_url = juju_offer.offerone.url } + + depends_on = [juju_offer.offerone] } `, srcModelName, destModelName) }