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

New Resource: azurerm_api_management_product_group #2984

Merged
merged 4 commits into from
Mar 4, 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
15 changes: 10 additions & 5 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

// API Management
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementProductGroupsClient apimanagement.ProductGroupClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient

// Application Insights
appInsightsClient appinsights.ComponentsClient
Expand Down Expand Up @@ -507,6 +508,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&productsClient.Client, auth)
c.apiManagementProductsClient = productsClient

productGroupsClient := apimanagement.NewProductGroupClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&productGroupsClient.Client, auth)
c.apiManagementProductGroupsClient = productGroupsClient

usersClient := apimanagement.NewUserClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&usersClient.Client, auth)
c.apiManagementUsersClient = usersClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_product_group": resourceArmApiManagementProductGroup(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
Expand Down
121 changes: 121 additions & 0 deletions azurerm/resource_arm_api_management_product_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementProductGroup() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementProductGroupCreate,
Read: resourceArmApiManagementProductGroupRead,
Delete: resourceArmApiManagementProductGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"product_id": azure.SchemaApiManagementChildName(),

"group_name": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

"api_management_name": azure.SchemaApiManagementName(),
},
}
}

func resourceArmApiManagementProductGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductGroupsClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
groupName := d.Get("group_name").(string)
productId := d.Get("product_id").(string)

if requireResourcesToBeImported {
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, groupName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error checking for present of existing Product %q / Group %q (API Management Service %q / Resource Group %q): %+v", productId, groupName, serviceName, resourceGroup, err)
}
}

if !utils.ResponseWasNotFound(resp) {
subscriptionId := meta.(*ArmClient).subscriptionId
resourceId := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/products/%s/groups/%s", subscriptionId, resourceGroup, serviceName, groupName, productId)
return tf.ImportAsExistsError("azurerm_api_management_product_group", resourceId)
}
}

resp, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, productId, groupName)
if err != nil {
return fmt.Errorf("Error adding Product %q to Group %q (API Management Service %q / Resource Group %q): %+v", productId, groupName, serviceName, resourceGroup, err)
}

// there's no Read so this is best-effort
d.SetId(*resp.ID)

return resourceArmApiManagementProductGroupRead(d, meta)
}

func resourceArmApiManagementProductGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductGroupsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
groupName := id.Path["groups"]
productId := id.Path["products"]

resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, groupName)
if err != nil {
if utils.ResponseWasNotFound(resp) {
log.Printf("[DEBUG] Product %q was not found in Group %q (API Management Service %q / Resource Group %q) was not found - removing from state!", productId, groupName, serviceName, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving Product %q / Group %q (API Management Service %q / Resource Group %q): %+v", productId, groupName, serviceName, resourceGroup, err)
}

d.Set("group_name", groupName)
d.Set("product_id", productId)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

return nil
}

func resourceArmApiManagementProductGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductGroupsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
groupName := id.Path["groups"]
productId := id.Path["products"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, productId, groupName); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error removing Product %q from Group %q (API Management Service %q / Resource Group %q): %+v", productId, groupName, serviceName, resourceGroup, err)
}
}

return nil
}
176 changes: 176 additions & 0 deletions azurerm/resource_arm_api_management_product_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementProductGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementProductGroup_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementProductGroupExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMAPIManagementProductGroup_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_api_management_product_group.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementProductGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementProductGroup_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementProductGroupExists(resourceName),
),
},
{
Config: testAccAzureRMAPIManagementProductGroup_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management_product_group"),
},
},
})
}

func testCheckAzureRMAPIManagementProductGroupDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).apiManagementGroupUsersClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_api_management_product_group" {
continue
}

productId := rs.Primary.Attributes["product_id"]
groupName := rs.Primary.Attributes["group_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
serviceName := rs.Primary.Attributes["api_management_name"]

ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, groupName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return err
}
}

return nil
}
return nil
}

func testCheckAzureRMAPIManagementProductGroupExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

productId := rs.Primary.Attributes["product_id"]
groupName := rs.Primary.Attributes["group_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
serviceName := rs.Primary.Attributes["api_management_name"]

client := testAccProvider.Meta().(*ArmClient).apiManagementProductGroupsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, groupName)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Bad: Product %q / Group %q (API Management Service %q / Resource Group %q) does not exist", productId, groupName, serviceName, resourceGroup)
}
return fmt.Errorf("Bad: Get on apiManagementProductGroupsClient: %+v", err)
}

return nil
}
}

func testAccAzureRMAPIManagementProductGroup_basic(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"
subscription_required = true
approval_required = false
published = true
}

resource "azurerm_api_management_group" "test" {
name = "acctestAMGroup-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
api_management_name = "${azurerm_api_management.test.name}"
display_name = "Test Group"
}

resource "azurerm_api_management_product_group" "test" {
product_id = "${azurerm_api_management_product.test.product_id}"
group_name = "${azurerm_api_management_group.test.name}"
api_management_name = "${azurerm_api_management.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAPIManagementProductGroup_requiresImport(rInt int, location string) string {
template := testAccAzureRMAPIManagementProductGroup_basic(rInt, location)
return fmt.Sprintf(`
%s

resource "azurerm_api_management_product_group" "import" {
product_id = "${azurerm_api_management_product_group.test.product_id}"
group_name = "${azurerm_api_management_product_group.test.group_name}"
api_management_name = "${azurerm_api_management_product_group.test.api_management_name}"
resource_group_name = "${azurerm_api_management_product_group.test.resource_group_name}"
}
`, template)
}
6 changes: 5 additions & 1 deletion website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,13 @@
<a href="/docs/providers/azurerm/r/api_management_group_user.html">azurerm_api_management_group_user</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-product") %>>
<li<%= sidebar_current("docs-azurerm-resource-api-management-product-x") %>>
<a href="/docs/providers/azurerm/r/api_management_product.html">azurerm_api_management_product</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-product-group") %>>
<a href="/docs/providers/azurerm/r/api_management_product_group.html">azurerm_api_management_product_group</a>
</li>
</ul>
</li>

Expand Down
1 change: 0 additions & 1 deletion website/docs/r/api_management_group_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ In addition to all arguments above, the following attributes are exported:
API Management Group Users can be imported using the `resource id`, e.g.

```shell

terraform import azurerm_api_management_group_user.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/service1/groups/groupId/users/user123
```
4 changes: 2 additions & 2 deletions website/docs/r/api_management_product.html.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_api_management_product"
sidebar_current: "docs-azurerm-resource-api-management-product"
sidebar_current: "docs-azurerm-resource-api-management-product-x"
description: |-
Manages an API Management Product.
---
Expand Down Expand Up @@ -68,7 +68,7 @@ The following arguments are supported:

* `subscriptions_limit` - (Optional) The number of subscriptions a user can have to this Product at the same time.

-> **NOTE:** `subscriptions_limit` can only be set when `subscription_required` is set to `true`.
-> **NOTE:** `subscriptions_limit` can only be set when `subscription_required` is set to `true`.

* `terms` - (Optional) The Terms and Conditions for this Product, which must be accepted by Developers before they can begin the Subscription process.

Expand Down
Loading