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 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
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
14 changes: 14 additions & 0 deletions azurerm/resource_arm_api_management_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"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/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -73,6 +74,19 @@ func resourceArmApiManagementGroupCreateUpdate(d *schema.ResourceData, meta inte
externalID := d.Get("external_id").(string)
groupType := d.Get("type").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Group %q (API Management Service %q / Resource Group %q): %s", name, serviceName, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_api_management_group", *existing.ID)
}
}

parameters := apimanagement.GroupCreateParameters{
GroupCreateParametersProperties: &apimanagement.GroupCreateParametersProperties{
DisplayName: utils.String(displayName),
Expand Down
45 changes: 45 additions & 0 deletions azurerm/resource_arm_api_management_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ func TestAccAzureRMAPIManagementGroup_basic(t *testing.T) {
})
}

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementGroup_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "display_name", "Test Group"),
resource.TestCheckResourceAttr(resourceName, "type", "custom"),
),
},
{
Config: testAccAzureRMAPIManagementGroup_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management_group"),
},
},
})
}

func TestAccAzureRMAPIManagementGroup_complete(t *testing.T) {
resourceName := "azurerm_api_management_group.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -186,6 +217,20 @@ resource "azurerm_api_management_group" "test" {
`, rInt, location, rInt, rInt)
}

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

resource "azurerm_api_management_group" "import" {
name = "${azurerm_api_management_group.test.name}"
resource_group_name = "${azurerm_api_management_group.test.resource_group_name}"
api_management_name = "${azurerm_api_management_group.test.api_management_name}"
display_name = "${azurerm_api_management_group.test.display_name}"
}
`, template)
}

func testAccAzureRMAPIManagementGroup_complete(rInt int, location string, displayName, description string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
121 changes: 121 additions & 0 deletions azurerm/resource_arm_api_management_group_user.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 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)

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

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

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
}
Loading