Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_marketplace_agreement: recreate agreement if !accepted #5582

Merged
merged 1 commit into from
Feb 1, 2020
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
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