-
Notifications
You must be signed in to change notification settings - Fork 5k
List and identity - azurerm_user_assigned_identity #32667
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
Open
aayushsingh2502
wants to merge
8
commits into
main
Choose a base branch
from
list_identity-azurerm_user_assigned_identity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e929616
list and identity implementation - azurerm_user_assigned_identity
aayushsingh2502 e60bd62
wait timer added
aayushsingh2502 376e10f
review fixes
aayushsingh2502 04cf102
identity test added
aayushsingh2502 fd76b23
removed expect identity
aayushsingh2502 7496478
resource name update
aayushsingh2502 9ff5a1f
Merge branch 'main' into list_identity-azurerm_user_assigned_identity
aayushsingh2502 5499642
indentation fix
aayushsingh2502 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 hidden or 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 hidden or 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
123 changes: 123 additions & 0 deletions
123
internal/services/managedidentity/user_assigned_identity_resource_list.go
This file contains hidden or 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,123 @@ | ||
| // Copyright IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package managedidentity | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
| ) | ||
|
|
||
| type UserAssignedIdentityListResource struct{} | ||
|
|
||
| var _ sdk.FrameworkListWrappedResource = new(UserAssignedIdentityListResource) | ||
|
|
||
| func (UserAssignedIdentityListResource) Metadata(_ context.Context, _ resource.MetadataRequest, response *resource.MetadataResponse) { | ||
| response.TypeName = UserAssignedIdentityResource{}.ResourceType() | ||
| } | ||
|
|
||
| func (UserAssignedIdentityListResource) ResourceFunc() *pluginsdk.Resource { | ||
| return sdk.WrappedResource(UserAssignedIdentityResource{}) | ||
| } | ||
|
|
||
| func (UserAssignedIdentityListResource) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream, metadata sdk.ResourceMetadata) { | ||
| client := metadata.Client.ManagedIdentity.V20241130.Identities | ||
|
|
||
| var data sdk.DefaultListModel | ||
| diags := request.Config.Get(ctx, &data) | ||
| if diags.HasError() { | ||
| stream.Results = list.ListResultsStreamDiagnostics(diags) | ||
| return | ||
| } | ||
|
|
||
| subscriptionID := metadata.SubscriptionId | ||
| if !data.SubscriptionId.IsNull() { | ||
| subscriptionID = data.SubscriptionId.ValueString() | ||
| } | ||
|
|
||
| r := UserAssignedIdentityResource{} | ||
|
|
||
| switch { | ||
| case !data.ResourceGroupName.IsNull(): | ||
| resp, err := client.UserAssignedIdentitiesListByResourceGroupComplete(ctx, commonids.NewResourceGroupID(subscriptionID, data.ResourceGroupName.ValueString())) | ||
| if err != nil { | ||
| sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", r.ResourceType()), err) | ||
| return | ||
| } | ||
|
|
||
| stream.Results = func(push func(list.ListResult) bool) { | ||
| for _, item := range resp.Items { | ||
| result := request.NewListResult(ctx) | ||
| result.DisplayName = pointer.From(item.Name) | ||
|
|
||
| id, err := commonids.ParseUserAssignedIdentityIDInsensitively(pointer.From(item.Id)) | ||
| if err != nil { | ||
| sdk.SetErrorDiagnosticAndPushListResult(result, push, "parsing User Assigned Identity ID", err) | ||
| return | ||
| } | ||
|
|
||
| meta := sdk.NewResourceMetaData(metadata.Client, r) | ||
| meta.SetID(id) | ||
|
|
||
| if err := r.flatten(meta, id, &item); err != nil { | ||
| sdk.SetErrorDiagnosticAndPushListResult(result, push, fmt.Sprintf("encoding `%s` resource data", r.ResourceType()), err) | ||
| return | ||
| } | ||
|
|
||
| sdk.EncodeListResult(ctx, meta.ResourceData, &result) | ||
| if result.Diagnostics.HasError() { | ||
| push(result) | ||
| return | ||
| } | ||
|
|
||
| if !push(result) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| default: | ||
| resp, err := client.UserAssignedIdentitiesListBySubscriptionComplete(ctx, commonids.NewSubscriptionID(subscriptionID)) | ||
| if err != nil { | ||
| sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", r.ResourceType()), err) | ||
| return | ||
| } | ||
|
|
||
| stream.Results = func(push func(list.ListResult) bool) { | ||
| for _, item := range resp.Items { | ||
| result := request.NewListResult(ctx) | ||
| result.DisplayName = pointer.From(item.Name) | ||
|
|
||
| id, err := commonids.ParseUserAssignedIdentityIDInsensitively(pointer.From(item.Id)) | ||
| if err != nil { | ||
| sdk.SetErrorDiagnosticAndPushListResult(result, push, "parsing User Assigned Identity ID", err) | ||
| return | ||
| } | ||
|
|
||
| meta := sdk.NewResourceMetaData(metadata.Client, r) | ||
| meta.SetID(id) | ||
|
|
||
| if err := r.flatten(meta, id, &item); err != nil { | ||
| sdk.SetErrorDiagnosticAndPushListResult(result, push, fmt.Sprintf("encoding `%s` resource data", r.ResourceType()), err) | ||
| return | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| sdk.EncodeListResult(ctx, meta.ResourceData, &result) | ||
| if result.Diagnostics.HasError() { | ||
| push(result) | ||
| return | ||
| } | ||
|
|
||
| if !push(result) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
internal/services/managedidentity/user_assigned_identity_resource_list_test.go
This file contains hidden or 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,102 @@ | ||
| // Copyright IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package managedidentity_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/querycheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/provider/framework" | ||
| ) | ||
|
|
||
| func TestAccUserAssignedIdentity_list(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azurerm_user_assigned_identity", "test") | ||
| r := UserAssignedIdentityTestResource{} | ||
| resourceName := fmt.Sprintf("acctestuai-0-%d", data.RandomInteger) | ||
| resourceGroupName := fmt.Sprintf("acctestrg-%d", data.RandomInteger) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_14_0), | ||
| }, | ||
| ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"), | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: r.basicList(data), | ||
| }, | ||
| { | ||
| PreConfig: func() { time.Sleep(2 * time.Minute) }, | ||
|
aayushsingh2502 marked this conversation as resolved.
Outdated
|
||
| Query: true, | ||
| Config: r.subscriptionListQuery(), | ||
| QueryResultChecks: []querycheck.QueryResultCheck{ | ||
| querycheck.ExpectLengthAtLeast("azurerm_user_assigned_identity.list", 3), | ||
| querycheck.ExpectIdentity("azurerm_user_assigned_identity.list", map[string]knownvalue.Check{ | ||
| "name": knownvalue.StringExact(resourceName), | ||
| "resource_group_name": knownvalue.StringExact(resourceGroupName), | ||
| "subscription_id": knownvalue.StringExact(data.Subscriptions.Primary), | ||
| }), | ||
| }, | ||
| }, | ||
| { | ||
| Query: true, | ||
| Config: r.resourceGroupListQuery(data), | ||
| QueryResultChecks: []querycheck.QueryResultCheck{ | ||
| querycheck.ExpectLength("azurerm_user_assigned_identity.list", 3), | ||
| querycheck.ExpectIdentity("azurerm_user_assigned_identity.list", map[string]knownvalue.Check{ | ||
| "name": knownvalue.StringExact(resourceName), | ||
| "resource_group_name": knownvalue.StringExact(resourceGroupName), | ||
| "subscription_id": knownvalue.StringExact(data.Subscriptions.Primary), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func (UserAssignedIdentityTestResource) basicList(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| provider "azurerm" { | ||
| features {} | ||
| } | ||
|
|
||
| resource "azurerm_resource_group" "test" { | ||
| name = "acctestrg-%[1]d" | ||
| location = "%[2]s" | ||
| } | ||
|
|
||
| resource "azurerm_user_assigned_identity" "test" { | ||
| count = 3 | ||
|
|
||
| name = "acctestuai-${count.index}-%[1]d" | ||
| location = azurerm_resource_group.test.location | ||
| resource_group_name = azurerm_resource_group.test.name | ||
| } | ||
| `, data.RandomInteger, data.Locations.Primary) | ||
| } | ||
|
|
||
| func (UserAssignedIdentityTestResource) subscriptionListQuery() string { | ||
| return ` | ||
| list "azurerm_user_assigned_identity" "list" { | ||
| provider = azurerm | ||
| } | ||
| ` | ||
| } | ||
|
|
||
| func (UserAssignedIdentityTestResource) resourceGroupListQuery(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| list "azurerm_user_assigned_identity" "list" { | ||
| provider = azurerm | ||
| config { | ||
| resource_group_name = "acctestrg-%[1]d" | ||
| } | ||
| } | ||
| `, data.RandomInteger) | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
website/docs/list-resources/user_assigned_identity.html.markdown
This file contains hidden or 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,40 @@ | ||
| --- | ||
| subcategory: "Authorization" | ||
| layout: "azurerm" | ||
| page_title: "Azure Resource Manager: azurerm_user_assigned_identity" | ||
| description: |- | ||
| Lists User Assigned Identity resources. | ||
| --- | ||
|
|
||
| # List resource: azurerm_user_assigned_identity | ||
|
|
||
| Lists User Assigned Identity resources. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ### List all User Assigned Identities in the subscription | ||
|
|
||
| ```hcl | ||
| list "azurerm_user_assigned_identity" "example" { | ||
| provider = azurerm | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ### List all User Assigned Identities in a Resource Group | ||
|
|
||
| ```hcl | ||
| list "azurerm_user_assigned_identity" "example" { | ||
| provider = azurerm | ||
| config { | ||
| resource_group_name = "example-rg" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Argument Reference | ||
|
|
||
| This list resource supports the following arguments: | ||
|
|
||
| * `subscription_id` - (Optional) The ID of the Subscription to query. Defaults to the value specified in the Provider Configuration. | ||
|
|
||
| * `resource_group_name` - (Optional) The name of the Resource Group to query. | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.