Skip to content

Commit

Permalink
Create and Delete functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooja Adhikari committed Dec 20, 2018
1 parent abd7fb0 commit 81c7575
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public interface IControlPlaneDataStore
Task<Role> GetRoleAsync(string name, CancellationToken cancellationToken);

Task<Role> UpsertRoleAsync(Role role, CancellationToken cancellationToken);

Task<Role> AddRoleAsync(Role role, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public interface IRbacService
Task<Role> GetRoleAsync(string name, CancellationToken cancellationToken);

Task<Role> UpsertRoleAsync(Role role, CancellationToken cancellationToken);

Task<Role> AddRoleAsync(Role role, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public async Task<Role> UpsertRoleAsync(Role role, CancellationToken cancellatio
{
return await _controlPlaneDataStore.UpsertRoleAsync(role, cancellationToken);
}

public async Task<Role> AddRoleAsync(Role role, CancellationToken cancellationToken)
{
return await _controlPlaneDataStore.AddRoleAsync(role, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ public async Task<Role> UpsertRoleAsync(Role role, CancellationToken cancellatio
return resultRole.ToRole();
}

public async Task<Role> AddRoleAsync(Role role, CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(role, nameof(role));

var cosmosRole = new CosmosRole(role);
var resultRole = await UpsertSystemObjectAsync(cosmosRole, CosmosRole.RolePartition, cancellationToken);
return resultRole.ToRole();
}

public async Task<Role> DeleteRoleAsync(Role role, CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(role, nameof(role));

var cosmosRole = new CosmosRole(role);
var resultRole = await DeleteSystemObjectAsync(cosmosRole);
return resultRole.ToRole();
}

public async Task<IdentityProvider> UpsertIdentityProviderAsync(IdentityProvider identityProvider, CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(identityProvider, nameof(identityProvider));
Expand Down Expand Up @@ -156,5 +174,58 @@ private async Task<T> UpsertSystemObjectAsync<T>(T systemObject, string partitio
throw;
}
}

private async Task<T> AddSystemObjectAsync<T>(T systemObject, string partitionKey, CancellationToken cancellationToken)
where T : class
{
EnsureArg.IsNotNull(systemObject, nameof(systemObject));
var eTagAccessCondition = new AccessCondition();

var requestOptions = new RequestOptions
{
PartitionKey = new PartitionKey(partitionKey),
AccessCondition = eTagAccessCondition,
};
try
{
var response = await _documentClient.Value.CreateDocumentAsync(
_collectionUri,
systemObject,
requestOptions,
true,
cancellationToken);
_logger.LogInformation("Request charge: {RequestCharge}, latency: {RequestLatency}", response.RequestCharge, response.RequestLatency);
return (dynamic)response.Resource;
}
catch (DocumentClientException dce)
{
_logger.LogError(dce, "Unhandled Document Client Exception");
throw;
}
}

private async Task<T> DeleteSystemObjectAsync<T>(T systemObject, string partitionKey, CancellationToken cancellationToken)
where T : class
{
EnsureArg.IsNotNull(systemObject, nameof(systemObject));
var eTagAccessCondition = new AccessCondition();

var requestOptions = new RequestOptions
{
PartitionKey = new PartitionKey(partitionKey),
AccessCondition = eTagAccessCondition,
};
try
{
var response = await _documentClient.Value.DeleteDocumentAsync(_collectionUri);
_logger.LogInformation("Request charge: {RequestCharge}, latency: {RequestLatency}", response.RequestCharge, response.RequestLatency);
return (dynamic)response.Resource;
}
catch (DocumentClientException dce)
{
_logger.LogError(dce, "Unhandled Document Client Exception");
throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public async Task<IActionResult> UpdateRole([FromBody] Role role, CancellationTo
return Ok(response);
}


[HttpPost]
[Route("Admin/Roles")]
[AllowAnonymous]
public async Task<IActionResult> CreateRole([FromBody] Role role, CancellationToken cancellationToken)
{
var response = await _rbacService.UpsertRoleAsync(role, cancellationToken);
var response = await _rbacService.AddRoleAsync(role, cancellationToken);
return Ok(response);
}
}
Expand Down

0 comments on commit 81c7575

Please sign in to comment.