Azure / Azure/azure-rest-api-specs-examples

How to create a RoleAssignmentResource

Open
#4,637 1 comment 0 reactions 1 assignee Assigned to @HarveyLink View on GitHub
Dominant language
No language data
Stars
40
Forks
23
Avg merge
23h 36m
Merged PRs (30d)
172

Description

### Link to sample

_No response_

### Library name and version

Azure.ResourceManager.Authorization 1.1.3

### Language of the Sample

- [X] 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.