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

How to create a RoleAssignmentResource #4637

Open
1 of 9 tasks
swjain23 opened this issue Jul 17, 2024 · 0 comments
Open
1 of 9 tasks

How to create a RoleAssignmentResource #4637

swjain23 opened this issue Jul 17, 2024 · 0 comments
Assignees

Comments

@swjain23
Copy link

Link to sample

No response

Library name and version

Azure.ResourceManager.Authorization 1.1.3

Language of the Sample

  • C#/.NET
  • Java
  • JavaScript/TypedScript
  • Python
  • Golang
  • Other - Please specify in Issue details field

Sample Issue Type

  • Sample not working
  • Sample missing
  • Do not understand sample

Issue details

Hi,

We are trying to create a role assignment for a storage account where we assign managed identity access to a blob storage by creating a role assignment with Storage Blob data Contributor role.

I found this below sample, but here is assumes that the RoleAssignmentResource exists, how do we go about when it doe snot exist? I have the storage account created, a managed identity which will get access and the built-in role "Storage Blob data Contributor". So how do we do a new role assignment using Azure SDK? I have tried to find a lot of samples, but not sur ehow to go about it. Do we have a sample for this?

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Authorization;
using Azure.ResourceManager.Authorization.Models;

// Generated from example definition: specification/authorization/resource-manager/Microsoft.Authorization/stable/2022-04-01/examples/RoleAssignments_CreateForResource.json
// this example is just showing the usage of "RoleAssignments_Create" operation, for the dependent resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this RoleAssignmentResource created on azure
// for more information of creating RoleAssignmentResource, please refer to the document of RoleAssignmentResource
string scope = "subscriptions/a925f2f7-5c63-4b7b-8799-25a5f97bc3b2/resourceGroups/testrg/providers/Microsoft.DocumentDb/databaseAccounts/test-db-account";
string roleAssignmentName = "05c5a614-a7d6-4502-b150-c2fb455033ff";
ResourceIdentifier roleAssignmentResourceId = RoleAssignmentResource.CreateResourceIdentifier(scope, roleAssignmentName);
RoleAssignmentResource roleAssignment = client.GetRoleAssignmentResource(roleAssignmentResourceId);

// invoke the operation
RoleAssignmentCreateOrUpdateContent content = new RoleAssignmentCreateOrUpdateContent(new ResourceIdentifier("/subscriptions/a925f2f7-5c63-4b7b-8799-25a5f97bc3b2/providers/Microsoft.Authorization/roleDefinitions/0b5fe924-9a61-425c-96af-cfe6e287ca2d"), Guid.Parse("ce2ce14e-85d7-4629-bdbc-454d0519d987"))
{
PrincipalType = RoleManagementPrincipalType.User,
};
ArmOperation lro = await roleAssignment.UpdateAsync(WaitUntil.Completed, content);
RoleAssignmentResource result = lro.Value;

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
RoleAssignmentData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");

Expected behavior

No response

Actual behavior

No response

Reproduction Steps

No response

Environment

Windows 10 .NET Framework 4.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants