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_group_user #2972

Merged
merged 4 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

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

// Application Insights
appInsightsClient appinsights.ComponentsClient
Expand Down Expand Up @@ -494,6 +495,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&groupsClient.Client, auth)
c.apiManagementGroupClient = groupsClient

groupUsersClient := apimanagement.NewGroupUserClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&groupUsersClient.Client, auth)
c.apiManagementGroupUsersClient = groupUsersClient

serviceClient := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&serviceClient.Client, auth)
c.apiManagementServiceClient = serviceClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
Expand Down
2 changes: 2 additions & 0 deletions azurerm/resource_arm_api_management_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func resourceArmApiManagementGroupCreateUpdate(d *schema.ResourceData, meta inte
externalID := d.Get("external_id").(string)
groupType := d.Get("type").(string)

// TODO: support for Requiring Imports

parameters := apimanagement.GroupCreateParameters{
GroupCreateParametersProperties: &apimanagement.GroupCreateParametersProperties{
DisplayName: utils.String(displayName),
Expand Down
105 changes: 105 additions & 0 deletions azurerm/resource_arm_api_management_group_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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/utils"
)

func resourceArmApiManagementGroupUser() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementGroupUserCreate,
Read: resourceArmApiManagementGroupUserRead,
Delete: resourceArmApiManagementGroupUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"group_name": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

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

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

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
groupName := d.Get("group_name").(string)
userId := d.Get("user_id").(string)

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

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

return resourceArmApiManagementGroupUserRead(d, meta)
}

func resourceArmApiManagementGroupUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementGroupUsersClient
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"]
userId := id.Path["users"]

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

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

d.Set("group_name", groupName)
d.Set("user_id", userId)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

return nil
}

func resourceArmApiManagementGroupUserDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementGroupUsersClient
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"]
userId := id.Path["users"]

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

return nil
}
132 changes: 132 additions & 0 deletions azurerm/resource_arm_api_management_group_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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 TestAccAzureRMAPIManagementGroupUser_basic(t *testing.T) {
resourceName := "azurerm_api_management_group_user.test"
ri := tf.AccRandTimeInt()
location := testLocation()

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

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

userId := rs.Primary.Attributes["user_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, groupName, userId)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return err
}
}

return nil
}
return nil
}

func testCheckAzureRMAPIManagementGroupUserExists(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)
}

userId := rs.Primary.Attributes["user_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).apiManagementGroupUsersClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, groupName, userId)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Bad: User %q / Group %q (API Management Service %q / Resource Group %q) does not exist", userId, groupName, serviceName, resourceGroup)
}
return fmt.Errorf("Bad: Get on apiManagementGroupUsersClient: %+v", err)
}

return nil
}
}

func testAccAzureRMAPIManagementGroupUser_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_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_user" "test" {
user_id = "acctestuser%d"
api_management_name = "${azurerm_api_management.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
first_name = "Acceptance"
last_name = "Test"
email = "azure-acctest%d@example.com"
}

resource "azurerm_api_management_group_user" "test" {
user_id = "${azurerm_api_management_user.test.user_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, rInt, rInt)
}
6 changes: 5 additions & 1 deletion website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@
<a href="/docs/providers/azurerm/r/api_management.html">azurerm_api_management</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-group") %>>
<li<%= sidebar_current("docs-azurerm-resource-api-management-group-x") %>>
<a href="/docs/providers/azurerm/r/api_management_group.html">azurerm_api_management_group</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-group-user") %>>
<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") %>>
<a href="/docs/providers/azurerm/r/api_management_product.html">azurerm_api_management_product</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/api_management_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Use this data source to access information about an existing API Management Grou
```hcl
data "azurerm_api_management_group" "test" {
name = "my-group"
api_management_name = "example-apim
api_management_name = "example-apim"
resource_group_name = "search-service"
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/api_management_product.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Use this data source to access information about an existing API Management Prod
```hcl
data "azurerm_api_management_product" "test" {
product_id = "my-product"
api_management_name = "example-apim
api_management_name = "example-apim"
resource_group_name = "search-service"
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/api_management_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Use this data source to access information about an existing API Management User
```hcl
data "azurerm_api_management_user" "test" {
user_id = "my-user"
api_management_name = "example-apim
api_management_name = "example-apim"
resource_group_name = "search-service"
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/api_management_group.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_group"
sidebar_current: "docs-azurerm-resource-api-management-group"
sidebar_current: "docs-azurerm-resource-api-management-group-x"
description: |-
Manages an API Management Group.
---
Expand Down
57 changes: 57 additions & 0 deletions website/docs/r/api_management_group_user.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_api_management_group_user"
sidebar_current: "docs-azurerm-resource-api-management-group-user"
description: |-
Manages an API Management User Assignment to a Group.
---

# azurerm_api_management_group_user

Manages an API Management User Assignment to a Group.


## Example Usage

```hcl
data "azurerm_api_management_user" "example" {
user_id = "my-user"
api_management_name = "example-apim"
resource_group_name = "search-service"
}

resource "azurerm_api_management_group_user" "example" {
user_id = "${data.azurerm_api_management_user.example.id}"
group_name = "example-group"
resource_group_name = "${data.azurerm_api_management_user.example.resource_group_name}"
api_management_name = "${data.azurerm_api_management_user.example.api_management_name}"
}
```


## Argument Reference

The following arguments are supported:

* `user_id` - (Required) The ID of the API Management User which should be assigned to this API Management Group. Changing this forces a new resource to be created.

* `group_name` - (Required) The Name of the API Management Group within the API Management Service. Changing this forces a new resource to be created.

* `api_management_name` - (Required) The name of the API Management Service. Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the Resource Group in which the API Management Service exists. Changing this forces a new resource to be created.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the API Management Group User.

## Import

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
```