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

Bug Fix: azurerm_api_managment_product - Additional validation for approval_required #3945

Merged
merged 5 commits into from
Jul 26, 2019
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
2 changes: 2 additions & 0 deletions azurerm/resource_arm_api_management_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func resourceArmApiManagementProductCreateUpdate(d *schema.ResourceData, meta in
if subscriptionRequired && subscriptionsLimit > 0 {
properties.ProductContractProperties.ApprovalRequired = utils.Bool(approvalRequired)
properties.ProductContractProperties.SubscriptionsLimit = utils.Int32(int32(subscriptionsLimit))
} else if approvalRequired {
return fmt.Errorf("`subscription_required` must be true and `subscriptions_limit` must be greater than 0 to use `approval_required`")
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, productId, properties, ""); err != nil {
Expand Down
56 changes: 56 additions & 0 deletions azurerm/resource_arm_api_management_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -221,6 +222,26 @@ func TestAccAzureRMApiManagementProduct_complete(t *testing.T) {
})
}

func TestAccAzureRMApiManagementProduct_approvalRequiredError(t *testing.T) {
resourceName := "azurerm_api_management_product.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApiManagementProductDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApiManagementProduct_approvalRequiredError(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApiManagementProductExists(resourceName)),
ExpectError: regexp.MustCompile("`subscription_required` must be true and `subscriptions_limit` must be greater than 0 to use `approval_required`"),
},
},
})
}

func testCheckAzureRMApiManagementProductExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -323,6 +344,7 @@ resource "azurerm_api_management_product" "test" {
display_name = "Test Updated Product"
subscription_required = true
approval_required = true
subscriptions_limit = 1
published = true
}
`, rInt, location, rInt)
Expand Down Expand Up @@ -395,3 +417,37 @@ resource "azurerm_api_management_product" "test" {
}
`, rInt, location, rInt)
}

func testAccAzureRMApiManagementProduct_approvalRequiredError(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_api_management" "test" {
name = "acctestAM-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
publisher_name = "pub1"
publisher_email = "[email protected]"

sku {
name = "Developer"
capacity = 1
}
}

resource "azurerm_api_management_product" "test" {
product_id = "test-product"
api_management_name = "${azurerm_api_management.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
display_name = "Test Product"
approval_required = true
subscription_required = false
published = true
description = "This is an example description"
terms = "These are some example terms and conditions"
}
`, rInt, location, rInt)
}