< Summary

Class:Azure.Security.KeyVault.Administration.KeyVaultAccessControlClient
Assembly:Azure.Security.KeyVault.Administration
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\KeyVaultAccessControlClient.cs
Covered lines:123
Uncovered lines:61
Coverable lines:184
Total lines:382
Line coverage:66.8% (123 of 184)
Covered branches:4
Total branches:6
Branch coverage:66.6% (4 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_VaultUri()-100%100%
.ctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
GetRoleDefinitions(...)-62.07%100%
GetRoleDefinitionsAsync(...)-61.29%100%
GetRoleAssignments(...)-62.07%100%
GetRoleAssignmentsAsync(...)-61.29%100%
CreateRoleAssignment(...)-62.5%50%
CreateRoleAssignmentAsync()-66.67%50%
GetRoleAssignment(...)-100%100%
GetRoleAssignmentAsync()-62.5%100%
DeleteRoleAssignment(...)-57.14%100%
DeleteRoleAssignmentAsync()-62.5%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\KeyVaultAccessControlClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.Core;
 8using Azure.Core.Pipeline;
 9using Azure.Security.KeyVault.Administration.Models;
 10
 11namespace Azure.Security.KeyVault.Administration
 12{
 13    /// <summary>
 14    /// The KeyVaultAccessControlClient provides synchronous and asynchronous methods to view and manage Role Based Acce
 15    /// The client supports creating, listing, updating, and deleting <see cref="RoleAssignment"/>.
 16    /// The client also supports listing <see cref="RoleDefinition" />.
 17    /// </summary>
 18    public class KeyVaultAccessControlClient
 19    {
 20        private readonly ClientDiagnostics _diagnostics;
 21        private readonly RoleDefinitionsRestClient _definitionsRestClient;
 22        private readonly RoleAssignmentsRestClient _assignmentsRestClient;
 23
 24        /// <summary>
 25        /// The vault Uri.
 26        /// </summary>
 27        /// <value></value>
 8828        public virtual Uri VaultUri { get; }
 29
 30        /// <summary>
 31        /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for mocking.
 32        /// </summary>
 3833        protected KeyVaultAccessControlClient()
 3834        { }
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for the specified vault.
 38        /// </summary>
 39        /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" 
 40        /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as
 41        /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null
 42        public KeyVaultAccessControlClient(Uri vaultUri, TokenCredential credential)
 443            : this(vaultUri, credential, null)
 44        {
 45
 446        }
 47
 48        /// <summary>
 49        /// Initializes a new instance of the <see cref="KeyVaultAccessControlClient"/> class for the specified vault.
 50        /// </summary>
 51        /// <param name="vaultUri">A <see cref="Uri"/> to the vault on which the client operates. Appears as "DNS Name" 
 52        /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, such as
 53        /// <param name="options"><see cref="KeyVaultAccessControlClientOptions"/> that allow to configure the managemen
 54        /// <exception cref="ArgumentNullException"><paramref name="vaultUri"/> or <paramref name="credential"/> is null
 4255        public KeyVaultAccessControlClient(Uri vaultUri, TokenCredential credential, KeyVaultAccessControlClientOptions 
 56        {
 4257            Argument.AssertNotNull(vaultUri, nameof(vaultUri));
 4258            Argument.AssertNotNull(credential, nameof(credential));
 59
 4260            VaultUri = vaultUri;
 61
 4262            options ??= new KeyVaultAccessControlClientOptions();
 4263            string apiVersion = options.GetVersionString();
 64
 4265            HttpPipeline pipeline = HttpPipelineBuilder.Build(options,
 4266                    new ChallengeBasedAuthenticationPolicy(credential));
 67
 4268            _diagnostics = new ClientDiagnostics(options);
 4269            _definitionsRestClient = new RoleDefinitionsRestClient(_diagnostics, pipeline, apiVersion);
 4270            _assignmentsRestClient = new RoleAssignmentsRestClient(_diagnostics, pipeline, apiVersion);
 4271        }
 72
 73        /// <summary>
 74        /// Get all role definitions that are applicable at scope and above.
 75        /// </summary>
 76        /// <param name="roleScope"> The scope of the role assignments. </param>
 77        /// <param name="cancellationToken"> The cancellation token to use. </param>
 78        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 79        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception>
 80        public virtual Pageable<RoleDefinition> GetRoleDefinitions(RoleAssignmentScope roleScope, CancellationToken canc
 81        {
 1882            return PageableHelpers.CreateEnumerable(_ =>
 1883            {
 3684                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 3685                scope.Start();
 1886                try
 1887                {
 3688                    var response = _definitionsRestClient.List(vaultBaseUrl: VaultUri.AbsoluteUri, scope: roleScope.ToSt
 3689                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 1890                }
 091                catch (Exception ex)
 1892                {
 093                    scope.Failed(ex);
 094                    throw;
 1895                }
 3696            }, (nextLink, _) =>
 1897            {
 098                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 099                scope.Start();
 18100                try
 18101                {
 0102                    var response = _definitionsRestClient.ListNextPage(nextLink: nextLink, vaultBaseUrl: VaultUri.Absolu
 0103                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 18104                }
 0105                catch (Exception ex)
 18106                {
 0107                    scope.Failed(ex);
 0108                    throw;
 18109                }
 0110            });
 111        }
 112
 113        /// <summary>
 114        /// Get all role definitions that are applicable at scope and above.
 115        /// </summary>
 116        /// <param name="roleScope"> The scope of the role definition. </param>
 117        /// <param name="cancellationToken"> The cancellation token to use. </param>
 118        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 119        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception>
 120        public virtual AsyncPageable<RoleDefinition> GetRoleDefinitionsAsync(RoleAssignmentScope roleScope, Cancellation
 121        {
 16122            return PageableHelpers.CreateAsyncEnumerable(async _ =>
 16123            {
 32124                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 32125                scope.Start();
 16126                try
 16127                {
 32128                    var response = await _definitionsRestClient.ListAsync(vaultBaseUrl: VaultUri.AbsoluteUri, scope: rol
 32129                            .ConfigureAwait(false);
 32130                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 16131                }
 0132                catch (Exception ex)
 16133                {
 0134                    scope.Failed(ex);
 0135                    throw;
 16136                }
 32137            }, async (nextLink, _) =>
 16138            {
 0139                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 0140                scope.Start();
 16141                try
 16142                {
 0143                    var response = await _definitionsRestClient.ListNextPageAsync(nextLink: nextLink, vaultBaseUrl: Vaul
 0144                        .ConfigureAwait(false);
 0145                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 16146                }
 0147                catch (Exception ex)
 16148                {
 0149                    scope.Failed(ex);
 0150                    throw;
 16151                }
 0152            });
 153        }
 154
 155        /// <summary>
 156        /// Gets the <see cref="RoleAssignment"/>s for a scope.
 157        /// </summary>
 158        /// <param name="roleScope"> The scope of the role assignments. </param>
 159        /// <param name="cancellationToken"> The cancellation token to use. </param>
 160        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 161        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception>
 162        public virtual Pageable<RoleAssignment> GetRoleAssignments(RoleAssignmentScope roleScope, CancellationToken canc
 163        {
 4164            return PageableHelpers.CreateEnumerable(_ =>
 4165            {
 8166                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 8167                scope.Start();
 4168                try
 4169                {
 8170                    var response = _assignmentsRestClient.ListForScope(vaultBaseUrl: VaultUri.AbsoluteUri, scope: roleSc
 8171                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 4172                }
 0173                catch (Exception ex)
 4174                {
 0175                    scope.Failed(ex);
 0176                    throw;
 4177                }
 8178            }, (nextLink, _) =>
 4179            {
 0180                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 0181                scope.Start();
 4182                try
 4183                {
 0184                    var response = _assignmentsRestClient.ListForScopeNextPage(nextLink: nextLink, vaultBaseUrl: VaultUr
 0185                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 4186                }
 0187                catch (Exception ex)
 4188                {
 0189                    scope.Failed(ex);
 0190                    throw;
 4191                }
 0192            });
 193        }
 194
 195        /// <summary>0
 196        /// Gets the <see cref="RoleAssignment"/>s for a scope.
 197        /// </summary>
 198        /// <param name="roleScope"> The scope of the role assignments. </param>
 199        /// <param name="cancellationToken"> The cancellation token to use. </param>
 200        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 201        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> is null.</exception>
 202        public virtual AsyncPageable<RoleAssignment> GetRoleAssignmentsAsync(RoleAssignmentScope roleScope, Cancellation
 203        {
 2204            return PageableHelpers.CreateAsyncEnumerable(async _ =>
 2205            {
 4206                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 4207                scope.Start();
 2208                try
 2209                {
 4210                    var response = await _assignmentsRestClient.ListForScopeAsync(vaultBaseUrl: VaultUri.AbsoluteUri, sc
 4211                            .ConfigureAwait(false);
 4212                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 2213                }
 0214                catch (Exception ex)
 2215                {
 0216                    scope.Failed(ex);
 0217                    throw;
 2218                }
 4219            }, async (nextLink, _) =>
 2220            {
 0221                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(G
 0222                scope.Start();
 2223                try
 2224                {
 0225                    var response = await _assignmentsRestClient.ListForScopeNextPageAsync(nextLink: nextLink, vaultBaseU
 0226                        .ConfigureAwait(false);
 0227                    return Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
 2228                }
 0229                catch (Exception ex)
 2230                {
 0231                    scope.Failed(ex);
 0232                    throw;
 2233                }
 0234            });
 235        }
 236
 237        /// <summary>
 238        /// Creates a <see cref="RoleAssignment"/>.
 239        /// </summary>
 240        /// <param name="roleScope"> The scope of the role assignment to create. </param>
 241        /// <param name="properties"> Properties for the role assignment. </param>
 242        /// <param name="name">The Name used to create the role assignment.</param>
 243        /// <param name="cancellationToken"> The cancellation token to use. </param>
 244        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 245        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="properties"/> is nul
 246        public virtual Response<RoleAssignment> CreateRoleAssignment(RoleAssignmentScope roleScope, RoleAssignmentProper
 247        {
 14248            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Creat
 14249            scope.Start();
 250            try
 251            {
 14252                var _name = name == default ? Guid.NewGuid().ToString() : name.ToString();
 14253                return _assignmentsRestClient.Create(VaultUri.AbsoluteUri, roleScope.ToString(), _name, new RoleAssignme
 254            }
 0255            catch (Exception ex)
 256            {
 0257                scope.Failed(ex);
 0258                throw;
 259            }
 14260        }
 261
 262        /// <summary>
 263        /// Creates a <see cref="RoleAssignment"/>.
 264        /// </summary>
 265        /// <param name="roleScope"> The scope of the role assignment to create. </param>
 266        /// <param name="properties"> Properties for the role assignment. </param>
 267        /// <param name="name">The name used to create the role assignment.</param>
 268        /// <param name="cancellationToken"> The cancellation token to use. </param>
 269        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 270        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="properties"/> is nul
 271        public virtual async Task<Response<RoleAssignment>> CreateRoleAssignmentAsync(RoleAssignmentScope roleScope, Rol
 272        {
 12273            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Creat
 12274            scope.Start();
 275            try
 276            {
 12277                var _name = name == default ? Guid.NewGuid().ToString() : name.ToString();
 12278                return await _assignmentsRestClient.CreateAsync(VaultUri.AbsoluteUri, roleScope.ToString(), _name, new R
 12279                .ConfigureAwait(false);
 280            }
 0281            catch (Exception ex)
 282            {
 0283                scope.Failed(ex);
 0284                throw;
 285            }
 12286        }
 287
 288        /// <summary>
 289        /// Get the specified role assignment.
 290        /// </summary>
 291        /// <param name="roleScope"> The scope of the role assignment. </param>
 292        /// <param name="roleAssignmentName"> The name of the role assignment to get. </param>
 293        /// <param name="cancellationToken"> The cancellation token to use. </param>
 294        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 295        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/
 296        public virtual Response<RoleAssignment> GetRoleAssignment(RoleAssignmentScope roleScope, string roleAssignmentNa
 297        {
 8298            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(GetRo
 8299            scope.Start();
 300            try
 301            {
 8302                return _assignmentsRestClient.Get(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentName, cancel
 303            }
 2304            catch (Exception ex)
 305            {
 2306                scope.Failed(ex);
 2307                throw;
 308            }
 6309        }
 310
 311        /// <summary>
 312        /// Get the specified role assignment.
 313        /// </summary>
 314        /// <param name="roleScope"> The scope of the role assignment. </param>
 315        /// <param name="roleAssignmentName"> The name of the role assignment to get. </param>
 316        /// <param name="cancellationToken"> The cancellation token to use. </param>
 317        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 318        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/
 319        public virtual async Task<Response<RoleAssignment>> GetRoleAssignmentAsync(RoleAssignmentScope roleScope, string
 320        {
 4321            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(GetRo
 4322            scope.Start();
 323            try
 324            {
 4325                return await _assignmentsRestClient.GetAsync(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentN
 4326                .ConfigureAwait(false);
 327            }
 0328            catch (Exception ex)
 329            {
 0330                scope.Failed(ex);
 0331                throw;
 332            }
 4333        }
 334
 335        /// <summary>
 336        /// Delete the specified role assignment.
 337        /// </summary>
 338        /// <param name="roleScope"> The scope of the role assignment. </param>
 339        /// <param name="roleAssignmentName"> The name of the role assignment to get. </param>
 340        /// <param name="cancellationToken"> The cancellation token to use. </param>
 341        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 342        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/
 343        public virtual Response<RoleAssignment> DeleteRoleAssignment(RoleAssignmentScope roleScope, string roleAssignmen
 344        {
 6345            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Delet
 6346            scope.Start();
 347            try
 348            {
 6349                return _assignmentsRestClient.Delete(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignmentName, can
 350            }
 0351            catch (Exception ex)
 352            {
 0353                scope.Failed(ex);
 0354                throw;
 355            }
 6356        }
 357
 358        /// <summary>
 359        /// Delete the specified role assignment.
 360        /// </summary>
 361        /// <param name="roleScope"> The scope of the role assignment. </param>
 362        /// <param name="roleAssignmentName"> The name of the role assignment to get. </param>
 363        /// <param name="cancellationToken"> The cancellation token to use. </param>
 364        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f
 365        /// <exception cref="ArgumentNullException"><paramref name="roleScope"/> or <paramref name="roleAssignmentName"/
 366        public virtual async Task<Response<RoleAssignment>> DeleteRoleAssignmentAsync(RoleAssignmentScope roleScope, str
 367        {
 4368            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(KeyVaultAccessControlClient)}.{nameof(Delet
 4369            scope.Start();
 370            try
 371            {
 4372                return await _assignmentsRestClient.DeleteAsync(VaultUri.AbsoluteUri, roleScope.ToString(), roleAssignme
 4373                .ConfigureAwait(false);
 374            }
 0375            catch (Exception ex)
 376            {
 0377                scope.Failed(ex);
 0378                throw;
 379            }
 4380        }
 381    }
 382}