Skip to content

Commit

Permalink
azurerm_marketplace_agreement: recreate agreement if !accepted (#5582)
Browse files Browse the repository at this point in the history
fixes #5577
  • Loading branch information
ryanewk authored Feb 1, 2020
1 parent 7842872 commit 6df5711
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func resourceArmMarketplaceAgreementRead(d *schema.ResourceData, meta interface{
d.Set("plan", plan)

if props := resp.AgreementProperties; props != nil {
if accepted := props.Accepted != nil && *props.Accepted; !accepted {
// if props.Accepted is not true, the agreement does not exist
d.SetId("")
}
d.Set("license_text_link", props.LicenseTextLink)
d.Set("privacy_policy_link", props.PrivacyPolicyLink)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
func TestAccAzureRMMarketplaceAgreement(t *testing.T) {
testCases := map[string]map[string]func(t *testing.T){
"basic": {
"basic": testAccAzureRMMarketplaceAgreement_basic,
"requiresImport": testAccAzureRMMarketplaceAgreement_requiresImport,
"basic": testAccAzureRMMarketplaceAgreement_basic,
"requiresImport": testAccAzureRMMarketplaceAgreement_requiresImport,
"agreementCanceled": testAccAzureRMMarketplaceAgreement_agreementCanceled,
},
}

Expand Down Expand Up @@ -81,6 +82,26 @@ func testAccAzureRMMarketplaceAgreement_requiresImport(t *testing.T) {
})
}

func testAccAzureRMMarketplaceAgreement_agreementCanceled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_marketplace_agreement", "test")

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMarketplaceAgreementDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMMarketplaceAgreement_basicConfig(),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMarketplaceAgreementExists(data.ResourceName),
testCheckAzureRMMarketplaceAgreementCanceled(data.ResourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testCheckAzureRMMarketplaceAgreementExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Compute.MarketplaceAgreementsClient
Expand All @@ -103,6 +124,36 @@ func testCheckAzureRMMarketplaceAgreementExists(resourceName string) resource.Te
return fmt.Errorf("Bad: Get on MarketplaceAgreementsClient: %+v", err)
}

if resp.AgreementProperties == nil || resp.AgreementProperties.Accepted == nil || !*resp.AgreementProperties.Accepted {
return fmt.Errorf("Bad: Marketplace Agreement for Publisher %q / Offer %q / Plan %q is not accepted", publisher, offer, plan)
}

return nil
}
}

func testCheckAzureRMMarketplaceAgreementCanceled(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Compute.MarketplaceAgreementsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

offer := rs.Primary.Attributes["offer"]
plan := rs.Primary.Attributes["plan"]
publisher := rs.Primary.Attributes["publisher"]

resp, err := client.Cancel(ctx, publisher, offer, plan)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Marketplace Agreement for Publisher %q / Offer %q / Plan %q does not exist", publisher, offer, plan)
}
return fmt.Errorf("Bad: Get on MarketplaceAgreementsClient: %+v", err)
}

return nil
}
}
Expand Down

0 comments on commit 6df5711

Please sign in to comment.