Skip to content

Commit

Permalink
Bug Fix: azurerm_api_managment_product - Additional validation for …
Browse files Browse the repository at this point in the history
…`approval_required` (#3945)
  • Loading branch information
mbfrahry authored Jul 26, 2019
1 parent d65ecdd commit 5e0d95b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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 = "pub1@email.com"
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)
}

0 comments on commit 5e0d95b

Please sign in to comment.