Skip to content

Commit

Permalink
Merge pull request #2954 from terraform-providers/f/api-management-users
Browse files Browse the repository at this point in the history
New Resource/Data Source: `azurerm_api_management_user`
  • Loading branch information
tombuildsstuff authored Feb 27, 2019
2 parents 6b6d6da + 9994fd2 commit f6b1708
Show file tree
Hide file tree
Showing 11 changed files with 986 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type ArmClient struct {
apiManagementGroupClient apimanagement.GroupClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient

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

usersClient := apimanagement.NewUserClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&usersClient.Client, auth)
c.apiManagementUsersClient = usersClient
}

func (c *ArmClient) registerAppInsightsClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
78 changes: 78 additions & 0 deletions azurerm/data_source_api_management_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package azurerm

import (
"fmt"

"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 dataSourceArmApiManagementUser() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmApiManagementUserRead,

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

"api_management_name": azure.SchemaApiManagementDataSourceName(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"first_name": {
Type: schema.TypeString,
Computed: true,
},

"email": {
Type: schema.TypeString,
Computed: true,
},

"last_name": {
Type: schema.TypeString,
Computed: true,
},

"note": {
Type: schema.TypeString,
Computed: true,
},

"state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

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

resp, err := client.Get(ctx, resourceGroup, serviceName, userId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("User %q was not found in API Management Service %q / Resource Group %q", userId, serviceName, resourceGroup)
}

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

d.SetId(*resp.ID)

if props := resp.UserContractProperties; props != nil {
d.Set("first_name", props.FirstName)
d.Set("last_name", props.LastName)
d.Set("email", props.Email)
d.Set("note", props.Note)
d.Set("state", string(props.State))
}

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

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMApiManagementUser_basic(t *testing.T) {
dataSourceName := "data.azurerm_api_management_user.test"
rInt := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApiManagementUser_basic(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "user_id", "test-user"),
resource.TestCheckResourceAttr(dataSourceName, "first_name", "Acceptance"),
resource.TestCheckResourceAttr(dataSourceName, "last_name", "Test"),
resource.TestCheckResourceAttr(dataSourceName, "email", fmt.Sprintf("azure-acctest%[email protected]", rInt)),
resource.TestCheckResourceAttr(dataSourceName, "state", "active"),
resource.TestCheckResourceAttr(dataSourceName, "note", "Used for testing in dimension C-137."),
),
},
},
})
}

func testAccDataSourceApiManagementUser_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "amtestRG-%d"
location = "%s"
}
resource "azurerm_api_management" "test" {
name = "acctestAM-%d"
publisher_name = "pub1"
publisher_email = "[email protected]"
sku {
name = "Developer"
capacity = 1
}
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_api_management_user" "test" {
user_id = "test-user"
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%[email protected]"
state = "active"
note = "Used for testing in dimension C-137."
}
data "azurerm_api_management_user" "test" {
user_id = "${azurerm_api_management_user.test.user_id}"
api_management_name = "${azurerm_api_management_user.test.api_management_name}"
resource_group_name = "${azurerm_api_management_user.test.resource_group_name}"
}
`, rInt, location, rInt, rInt)
}
17 changes: 17 additions & 0 deletions azurerm/helpers/azure/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ func SchemaApiManagementChildDataSourceName() *schema.Schema {
ValidateFunc: validate.ApiManagementChildName,
}
}

func SchemaApiManagementUserName() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiManagementUserName,
}
}

func SchemaApiManagementUserDataSourceName() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementUserName,
}
}
13 changes: 13 additions & 0 deletions azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ func ApiManagementServiceName(v interface{}, k string) (warnings []string, error
return warnings, errors
}

func ApiManagementUserName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// TODO: confirm this

// from the portal: `The field may contain only numbers, letters, and dash (-) sign when preceded and followed by number or a letter.`
if matched := regexp.MustCompile(`(^[a-zA-Z0-9])([a-zA-Z0-9-]{1,78})([a-zA-Z0-9]$)`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 80 characters in length", k))
}

return warnings, errors
}

func ApiManagementServicePublisherName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management": dataSourceApiManagementService(),
"azurerm_api_management_group": dataSourceApiManagementGroup(),
"azurerm_api_management_product": dataSourceApiManagementProduct(),
"azurerm_api_management_user": dataSourceArmApiManagementUser(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_app_service": dataSourceArmAppService(),
"azurerm_application_insights": dataSourceArmApplicationInsights(),
Expand Down Expand Up @@ -168,6 +169,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
"azurerm_app_service_plan": resourceArmAppServicePlan(),
Expand Down
Loading

0 comments on commit f6b1708

Please sign in to comment.