diff --git a/azurerm/resource_arm_api_management_product.go b/azurerm/resource_arm_api_management_product.go index 7f77286bb8ca..fff45954fd7b 100644 --- a/azurerm/resource_arm_api_management_product.go +++ b/azurerm/resource_arm_api_management_product.go @@ -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 { diff --git a/azurerm/resource_arm_api_management_product_test.go b/azurerm/resource_arm_api_management_product_test.go index 01d707509662..2c21b0866bca 100644 --- a/azurerm/resource_arm_api_management_product_test.go +++ b/azurerm/resource_arm_api_management_product_test.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -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 @@ -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) @@ -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) +}