-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
katbyte
merged 7 commits into
hashicorp:master
from
logachev:kiril/user_identity_data_source
May 7, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c9b34ad
Implement UAI data source
logachev d48fca9
Add tests
logachev 6a25cde
Improve tests
logachev cd1bf19
Update docs
logachev 0c3f0f0
gofmt
logachev 63500e0
Review feedback
logachev 165fdf3
Fix tests
logachev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.