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/Data Source: azurerm_api_management_user #2954

Merged
merged 7 commits into from
Feb 27, 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
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%d@hashicorptest.com", 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 = "pub1@email.com"

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%d@example.com"
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