Skip to content

Commit

Permalink
managedapplications - fix `TestAccManagedApplicationDefinition_upda…
Browse files Browse the repository at this point in the history
…te` and `TestAccManagedApplication_plan` (hashicorp#27515)

* fix managed application test

* fix update; fmt error

* fix TestAccManagedApplication_plan
  • Loading branch information
teowa authored Oct 25, 2024
1 parent 56194d2 commit 66941c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,21 @@ func resourceManagedApplicationDefinitionUpdate(d *pluginsdk.ResourceData, meta
}

if d.HasChange("create_ui_definition") {
payload.Properties.CreateUiDefinition = pointer.To(d.Get("create_ui_definition"))
// handle API error: The 'MainTemplate, CreateUiDefinition' properties should be empty if package zip file uri is provided.
if v, ok := d.GetOk("create_ui_definition"); ok {
payload.Properties.CreateUiDefinition = pointer.To(v)
} else {
payload.Properties.CreateUiDefinition = nil
}
}

if d.HasChange("main_template") {
payload.Properties.MainTemplate = pointer.To(d.Get("main_template"))
// handle API error: The 'MainTemplate, CreateUiDefinition' properties should be empty if package zip file uri is provided.
if v, ok := d.GetOk("main_template"); ok {
payload.Properties.MainTemplate = pointer.To(v)
} else {
payload.Properties.MainTemplate = nil
}
}

if d.HasChange("package_file_uri") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ resource "azurerm_managed_application_definition" "test" {
name = "acctestAppDef%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
lock_level = "ReadOnly"
lock_level = "None"
display_name = "UpdatedTestManagedApplicationDefinition"
description = "Updated Test Managed Application Definition"
package_enabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2015-06-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/testclient"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -195,12 +196,11 @@ func TestAccManagedApplication_plan(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.empty(),
Check: acceptance.ComposeTestCheckFunc(
data.CheckWithClientWithoutResource(r.cancelExistingAgreement(publisher, offer, plan)),
),
},
{
PreConfig: func() {
if err := r.cancelExistingAgreement(t, publisher, offer, plan); err != nil {
t.Fatalf("Failed to cancel existing agreement with error: %+v", err)
}
},
Config: r.plan(data, publisher, offer, plan),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
Expand Down Expand Up @@ -701,43 +701,39 @@ resource "azurerm_managed_application_definition" "test" {
`, data.Locations.Primary, data.RandomInteger, parameters)
}

func (ManagedApplicationResource) empty() string {
return `
provider "azurerm" {
features {}
}
`
}
func (ManagedApplicationResource) cancelExistingAgreement(t *testing.T, publisher string, offer string, plan string) error {
clientManager, err := testclient.Build()
if err != nil {
t.Fatalf("building client: %+v", err)
}

func (ManagedApplicationResource) cancelExistingAgreement(publisher string, offer string, plan string) acceptance.ClientCheckFunc {
return func(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) error {
client := clients.Compute.MarketplaceAgreementsClient
subscriptionId := clients.Account.SubscriptionId
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(15*time.Minute))
defer cancel()
ctx, cancel := context.WithDeadline(clientManager.StopContext, time.Now().Add(15*time.Minute))
defer cancel()

idGet := agreements.NewOfferPlanID(subscriptionId, publisher, offer, plan)
idCancel := agreements.NewPlanID(subscriptionId, publisher, offer, plan)
client := clientManager.Compute.MarketplaceAgreementsClient
subscriptionId := clientManager.Account.SubscriptionId

existing, err := client.MarketplaceAgreementsGet(ctx, idGet)
if err != nil {
return err
}
idGet := agreements.NewOfferPlanID(subscriptionId, publisher, offer, plan)
idCancel := agreements.NewPlanID(subscriptionId, publisher, offer, plan)

if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.MarketplaceAgreementsCancel(ctx, idCancel)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("marketplace agreement %q does not exist", idGet)
}
return fmt.Errorf("canceling %s: %+v", idGet, err)
existing, err := client.MarketplaceAgreementsGet(ctx, idGet)
if err != nil {
return fmt.Errorf("retrieving %s: %s", idGet, err)
}

if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
if accepted := props.Accepted; accepted != nil && *accepted {
resp, err := client.MarketplaceAgreementsCancel(ctx, idCancel)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("marketplace agreement %q does not exist", idGet)
}
return fmt.Errorf("canceling %s: %+v", idGet, err)
}
}
}

return nil
}

return nil
}

0 comments on commit 66941c7

Please sign in to comment.