Skip to content

Commit

Permalink
Merge pull request #124 from buildersoftdev/v3/feature/123
Browse files Browse the repository at this point in the history
v3/feature/123 Implementation of Producer Rest Endpoints
  • Loading branch information
eneshoxha authored Sep 12, 2022
2 parents 3bbe4fb + 99f25c3 commit ca298d1
Show file tree
Hide file tree
Showing 25 changed files with 681 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/Andy.X.App/Andy.X.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
<PackAsTool>False</PackAsTool>
<EnforceCodeStyleInBuild>False</EnforceCodeStyleInBuild>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Andy.X.App/Controllers/ClustersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Buildersoft.Andy.X.Core.Abstractions.Services.Clusters;
using Buildersoft.Andy.X.Extensions;
using Buildersoft.Andy.X.Model.Clusters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -32,6 +33,9 @@ public ClustersController(ILogger<ClustersController> logger, IClusterService te
[Authorize(Roles = "admin,readonly")]
public ActionResult<Cluster> GetClusters()
{
_logger.LogApiCallFrom(HttpContext);


var isFromCli = HttpContext.Request.Headers["x-called-by"].ToString();
if (isFromCli != "")
_logger.LogInformation($"{isFromCli} GET '{HttpContext.Request.Path}' is called");
Expand Down
41 changes: 38 additions & 3 deletions src/Andy.X.App/Controllers/ComponentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using Microsoft.AspNetCore.Authorization;
using System.Data;
using Buildersoft.Andy.X.Extensions;

namespace Buildersoft.Andy.X.Controllers
{
Expand Down Expand Up @@ -47,6 +48,8 @@ public ComponentsController(ILogger<ComponentsController> logger,
[Authorize(Roles = "admin,readonly")]
public ActionResult<List<string>> GetComponents(string tenant, string product)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -68,6 +71,8 @@ public ActionResult<List<string>> GetComponents(string tenant, string product)
[Authorize(Roles = "admin,readonly")]
public ActionResult<Component> GetComponent(string tenant, string product, string component)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -89,6 +94,8 @@ public ActionResult<Component> GetComponent(string tenant, string product, strin
[Authorize(Roles = "admin")]
public ActionResult<string> CreateComponent(string tenant, string product, string component, [FromQuery] string description, [FromBody] ComponentSettings componentSettings)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -101,7 +108,11 @@ public ActionResult<string> CreateComponent(string tenant, string product, strin
if (componentDetails is not null)
return BadRequest($"Component already exists");

var isCreated = _coreService.CreateComponent(tenant, product, component, description, componentSettings.IsTopicAutomaticCreationAllowed, componentSettings.IsSchemaValidationEnabled, componentSettings.IsAuthorizationEnabled, componentSettings.IsSubscriptionAutomaticCreationAllowed);
var isCreated = _coreService.CreateComponent(tenant, product, component, description,
componentSettings.IsTopicAutomaticCreationAllowed, componentSettings.IsSchemaValidationEnabled,
componentSettings.IsAuthorizationEnabled, componentSettings.IsSubscriptionAutomaticCreationAllowed,
componentSettings.IsProducerAutomaticCreationAllowed);

if (isCreated == true)
{
_tenantStateService.AddComponent(tenant, product, component, _tenantFactory.CreateComponent(component, description, componentSettings));
Expand All @@ -117,6 +128,8 @@ public ActionResult<string> CreateComponent(string tenant, string product, strin
[Authorize(Roles = "admin")]
public ActionResult<string> UpdateComponent(string tenant, string product, string component, [FromQuery] string description)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand Down Expand Up @@ -144,6 +157,8 @@ public ActionResult<string> UpdateComponent(string tenant, string product, strin
[Authorize(Roles = "admin")]
public ActionResult<string> DeleteComponent(string tenant, string product, string component)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -169,6 +184,8 @@ public ActionResult<string> DeleteComponent(string tenant, string product, strin
[Authorize(Roles = "admin,readonly")]
public ActionResult<ComponentSettings> GetComponentSettings(string tenant, string product, string component)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -193,6 +210,8 @@ public ActionResult<ComponentSettings> GetComponentSettings(string tenant, strin
[Authorize(Roles = "admin")]
public ActionResult<string> UpdateComponentSettings(string tenant, string product, string component, [FromBody] ComponentSettings componentSettings)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -219,6 +238,8 @@ public ActionResult<string> UpdateComponentSettings(string tenant, string produc
[Authorize(Roles = "admin")]
public ActionResult<string> CreateComponentToken(string tenant, string product, string component, [FromBody] ComponentToken componentToken)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -244,6 +265,8 @@ public ActionResult<string> CreateComponentToken(string tenant, string product,
[Authorize(Roles = "admin")]
public ActionResult<string> CreateComponentToken(string tenant, string product, string component, Guid key)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -269,6 +292,8 @@ public ActionResult<string> CreateComponentToken(string tenant, string product,
[Authorize(Roles = "admin,readonly")]
public ActionResult<List<ComponentToken>> GetComponentTokens(string tenant, string product, string component)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand Down Expand Up @@ -298,6 +323,8 @@ public ActionResult<List<ComponentToken>> GetComponentTokens(string tenant, stri
[Authorize(Roles = "admin,readonly")]
public ActionResult<ComponentToken> GetComponentToken(string tenant, string product, string component, Guid key)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -323,6 +350,8 @@ public ActionResult<ComponentToken> GetComponentToken(string tenant, string prod
[Authorize(Roles = "admin,readonly")]
public ActionResult<List<ComponentRetention>> GetComponentRetentions(string tenant, string product, string component)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -346,6 +375,8 @@ public ActionResult<List<ComponentRetention>> GetComponentRetentions(string tena
[Authorize(Roles = "admin")]
public ActionResult<string> AddComponentRetention(string tenant, string product, string component, [FromBody] ComponentRetention componentRetention)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -358,7 +389,7 @@ public ActionResult<string> AddComponentRetention(string tenant, string product,
if (componentDetails is null)
return NotFound($"Component {component} does not exists in {tenant}/{product}");

var isCreated = _coreService.CreateComponentRetention(tenant, product, component,componentRetention.Name, componentRetention.Type, componentRetention.TimeToLiveInMinutes);
var isCreated = _coreService.CreateComponentRetention(tenant, product, component, componentRetention.Name, componentRetention.Type, componentRetention.TimeToLiveInMinutes);
if (isCreated)
return Ok("Component retention has been created, this is async process, it will take some time to start reflecting");

Expand All @@ -370,8 +401,10 @@ public ActionResult<string> AddComponentRetention(string tenant, string product,
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpPut("{component}/retentions/{id}")]
[Authorize(Roles = "admin")]
public ActionResult<string> UpdateComponentRetention(string tenant, string product, string component, long id,[FromBody] ComponentRetention componentRetention)
public ActionResult<string> UpdateComponentRetention(string tenant, string product, string component, long id, [FromBody] ComponentRetention componentRetention)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand All @@ -397,6 +430,8 @@ public ActionResult<string> UpdateComponentRetention(string tenant, string produ
[Authorize(Roles = "admin")]
public ActionResult<string> DeleteComponentRetention(string tenant, string product, string component, long id)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");
Expand Down
175 changes: 175 additions & 0 deletions src/Andy.X.App/Controllers/ProducersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using Buildersoft.Andy.X.Core.Abstractions.Repositories.CoreState;
using Buildersoft.Andy.X.Core.Abstractions.Services.CoreState;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Net.Mime;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Buildersoft.Andy.X.Model.Entities.Core.Producers;
using Buildersoft.Andy.X.Model.Subscriptions;
using Buildersoft.Andy.X.Extensions;

namespace Buildersoft.Andy.X.Controllers
{
[Route("api/v3/tenants/{tenant}/products/{product}/components/{component}/topics/{topic}/producers")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ApiController]
public class ProducersController : ControllerBase
{
private readonly ILogger<ProducersController> _logger;
private readonly ICoreRepository _coreRepository;
private readonly ICoreService _coreService;

public ProducersController(ILogger<ProducersController> logger,
ICoreRepository coreRepository,
ICoreService coreService)
{
_logger = logger;
_coreRepository = coreRepository;
_coreService = coreService;
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("")]
[Authorize(Roles = "admin,readonly")]
public ActionResult<List<string>> GetProducers(string tenant, string product, string component, string topic)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");

var productDetails = _coreRepository.GetProduct(tenantDetails.Id, product);
if (productDetails is null)
return NotFound($"Product {product} does not exists in {tenant}");

var componentDetails = _coreRepository.GetComponent(tenantDetails.Id, productDetails.Id, component);
if (componentDetails is null)
return NotFound($"Component {component} does not exists in {tenant}/{product}");

var topicDetails = _coreRepository.GetTopic(componentDetails.Id, topic);
if (topicDetails is null)
return NotFound($"Topic {topic} does not exists in {tenant}/{product}/{component}");

var producers = _coreRepository
.GetProducers(topicDetails.Id)
.Select(x => new { Name = x.Name });

return Ok(producers);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{producer}")]
[Authorize(Roles = "admin,readonly")]
public ActionResult<Producer> GetProducer(string tenant, string product, string component, string topic, string producer)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");

var productDetails = _coreRepository.GetProduct(tenantDetails.Id, product);
if (productDetails is null)
return NotFound($"Product {product} does not exists in {tenant}");

var componentDetails = _coreRepository.GetComponent(tenantDetails.Id, productDetails.Id, component);
if (componentDetails is null)
return NotFound($"Component {component} does not exists in {tenant}/{product}");

var topicDetails = _coreRepository.GetTopic(componentDetails.Id, topic);
if (topicDetails is null)
return NotFound($"Topic {topic} does not exists in {tenant}/{product}/{component}");

var producerDetails = _coreRepository.GetProducer(topicDetails.Id, producer);
if (producerDetails is null)
return NotFound($"Producer {producer} does not exists in {tenant}/{product}/{component}/{topic}");

return Ok(producerDetails);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpPost("{producer}")]
[Authorize(Roles = "admin")]
public ActionResult<Subscription> CreateProducer(string tenant, string product, string component,
string topic, string producer, [FromQuery] string description, [FromQuery] ProducerInstanceType instanceType)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");

var productDetails = _coreRepository.GetProduct(tenantDetails.Id, product);
if (productDetails is null)
return NotFound($"Product {product} does not exists in {tenant}");

var componentDetails = _coreRepository.GetComponent(tenantDetails.Id, productDetails.Id, component);
if (componentDetails is null)
return NotFound($"Component {component} does not exists in {tenant}/{product}");

var topicDetails = _coreRepository.GetTopic(componentDetails.Id, topic);
if (topicDetails is null)
return NotFound($"Topic {topic} does not exists in {tenant}/{product}/{component}");

var producerDetails = _coreRepository.GetProducer(topicDetails.Id, producer);
if (producerDetails is not null)
return NotFound($"Producer {producer} already exists in {tenant}/{product}/{component}/{topic}");

var isCreated = _coreService.CreateProducer(tenant, product, component, topic, producer, description, instanceType);
if (isCreated == true)
{
return Ok($"Producer {producer} has been created");
}

return BadRequest("Something went wrong, producer couldnot be created");
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpDelete("{producer}")]
[Authorize(Roles = "admin")]
public ActionResult<Subscription> DeleteSubscription(string tenant, string product, string component, string topic, string producer)
{
_logger.LogApiCallFrom(HttpContext);

var tenantDetails = _coreRepository.GetTenant(tenant);
if (tenantDetails is null)
return NotFound($"Tenant {tenant} does not exists in this cluster");

var productDetails = _coreRepository.GetProduct(tenantDetails.Id, product);
if (productDetails is null)
return NotFound($"Product {product} does not exists in {tenant}");

var componentDetails = _coreRepository.GetComponent(tenantDetails.Id, productDetails.Id, component);
if (componentDetails is null)
return NotFound($"Component {component} does not exists in {tenant}/{product}");

var topicDetails = _coreRepository.GetTopic(componentDetails.Id, topic);
if (topicDetails is null)
return NotFound($"Topic {topic} does not exists in {tenant}/{product}/{component}");

var producerDetails = _coreRepository.GetProducer(topicDetails.Id, producer);
if (producerDetails is null)
return NotFound($"Producer {producer} does not exists in {tenant}/{product}/{component}/{topic}");

var isDeleted = _coreService.DeleteProducer(tenant, product, component, topic, producer);
if (isDeleted == true)
{
return Ok($"Producer is marked for deletion, this is async process, it will take some time to start reflecting");
}

return BadRequest("Something went wrong, producer couldnot be deleted");
}

}
}
Loading

0 comments on commit ca298d1

Please sign in to comment.