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

User Assigned Identity: add corresponding data source #3343

Merged
merged 7 commits into from
May 7, 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
77 changes: 77 additions & 0 deletions azurerm/data_source_user_assigned_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package azurerm

import (
"fmt"

"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/utils"
)

func dataSourceArmUserAssignedIdentity() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmUserAssignedIdentityRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 24),
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": locationForDataSourceSchema(),

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

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

"tags": tagsForDataSourceSchema(),
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on User Assigned Identity %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.IdentityProperties; props != nil {
if principalId := props.PrincipalID; principalId != nil {
d.Set("principal_id", principalId.String())
}

if clientId := props.ClientID; clientId != nil {
d.Set("client_id", clientId.String())
}
}

flattenAndSetTags(d, resp.Tags)

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

import (
"fmt"
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"

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

func TestAccDataSourceAzureRMUserAssignedIdentity_basic(t *testing.T) {
dataSourceName := "data.azurerm_user_assigned_identity.test"
resourceName := "azurerm_user_assigned_identity.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)

location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s-uai", rs)),
resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", fmt.Sprintf("acctest%d-rg", ri)),
resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)),
resource.TestMatchResourceAttr(dataSourceName, "principal_id", validate.UUIDRegExp),
resource.TestMatchResourceAttr(dataSourceName, "client_id", validate.UUIDRegExp),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
testEqualResourceAttr(dataSourceName, resourceName, "principal_id"),
testEqualResourceAttr(dataSourceName, resourceName, "client_id"),
),
},
},
})
}

func testEqualResourceAttr(dataSourceName string, resourceName string, attrName string) resource.TestCheckFunc {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that in other data source tests there is no any checks that data source resource contains same information as a resource.
Do you think this is valuable to move this function to some shared place and reuse in other data source tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is definitely value and a place for that: helpers/tf/acctest.go package would be perfect.

return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("Not found: %s", dataSourceName)
}

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

dsAttr := ds.Primary.Attributes[attrName]
rsAttr := rs.Primary.Attributes[attrName]

if dsAttr != rsAttr {
return fmt.Errorf("Attributes not equal: %s, %s", dsAttr, rsAttr)
}

return nil
}
}

func testAccDataSourceAzureRMUserAssignedIdentity_basic(rInt int, location string, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctest%d-rg"
location = "%s"
}

resource "azurerm_user_assigned_identity" "test" {
name = "acctest%s-uai"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"

tags = {
"foo" = "bar"
}
}

data "azurerm_user_assigned_identity" "test" {
name = "${azurerm_user_assigned_identity.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rString)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_subscriptions": dataSourceArmSubscriptions(),
"azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(),
"azurerm_user_assigned_identity": dataSourceArmUserAssignedIdentity(),
"azurerm_virtual_machine": dataSourceArmVirtualMachine(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
Expand Down
13 changes: 6 additions & 7 deletions azurerm/resource_arm_user_assigned_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package azurerm

import (
"fmt"
"regexp"
"testing"

"net/http"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"

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

func TestAccAzureRMUserAssignedIdentity_basic(t *testing.T) {
generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$"
resourceName := "azurerm_user_assigned_identity.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(14)
Expand All @@ -28,8 +28,8 @@ func TestAccAzureRMUserAssignedIdentity_basic(t *testing.T) {
Config: testAccAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMUserAssignedIdentityExists(resourceName),
resource.TestMatchResourceAttr(resourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)),
resource.TestMatchResourceAttr(resourceName, "client_id", regexp.MustCompile(generatedUuidRegex)),
resource.TestMatchResourceAttr(resourceName, "principal_id", validate.UUIDRegExp),
resource.TestMatchResourceAttr(resourceName, "client_id", validate.UUIDRegExp),
),
},
{
Expand All @@ -46,7 +46,6 @@ func TestAccAzureRMUserAssignedIdentity_requiresImport(t *testing.T) {
return
}

generatedUuidRegex := "^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$"
resourceName := "azurerm_user_assigned_identity.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(14)
Expand All @@ -60,8 +59,8 @@ func TestAccAzureRMUserAssignedIdentity_requiresImport(t *testing.T) {
Config: testAccAzureRMUserAssignedIdentity_basic(ri, testLocation(), rs),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMUserAssignedIdentityExists(resourceName),
resource.TestMatchResourceAttr(resourceName, "principal_id", regexp.MustCompile(generatedUuidRegex)),
resource.TestMatchResourceAttr(resourceName, "client_id", regexp.MustCompile(generatedUuidRegex)),
resource.TestMatchResourceAttr(resourceName, "principal_id", validate.UUIDRegExp),
resource.TestMatchResourceAttr(resourceName, "client_id", validate.UUIDRegExp),
),
},
{
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@
<a href="/docs/providers/azurerm/d/traffic_manager_geographical_location.html">azurerm_traffic_manager_geographical_location</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-user-assigned-identity") %>>
<a href="/docs/providers/azurerm/d/user_assigned_identity.html">azurerm_user_assigned_identity</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-virtual-machine") %>>
<a href="/docs/providers/azurerm/d/virtual_machine.html">azurerm_virtual_machine</a>
</li>
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/user_assigned_identity.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azure_user_assigned_identity"
sidebar_current: "docs-azurerm-datasource-user-assigned-identity"
description: |-
Gets information about an existing User Assigned Identity.

---

# Data Source: azurerm_user_assigned_identity

Use this data source to access information about an existing User Assigned Identity.

## Example Usage (reference an existing)

```hcl
data "azurerm_user_assigned_identity" "example" {
name = "name_of_user_assigned_identity"
resource_group_name = "name_of_resource_group"
}

output "uai_client_id" {
value = "${data.azurerm_user_assigned_identity.example.client_id}"
}

output "uai_principal_id" {
value = "${data.azurerm_user_assigned_identity.example.principal_id}"
}
```

## Argument Reference

* `name` - (Required) The name of the User Assigned Identity.
* `resource_group_name` - (Required) The name of the Resource Group in which the User Assigned Identity exists.

## Attributes Reference
logachev marked this conversation as resolved.
Show resolved Hide resolved

The following attributes are exported:

* `id` - The Resource ID of the User Assigned Identity.
* `location` - The Azure location where the User Assigned Identity exists.
* `principal_id` - The Service Principal ID of the User Assigned Identity.
* `client_id` - The Client ID of the User Assigned Identity.
* `tags` - A mapping of tags assigned to the User Assigned Identity.