-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2737 from hashicorp/data-api/support-common-types
data-api: support for common types (models and constants)
- Loading branch information
Showing
18 changed files
with
357 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Pandora.Api.V1.Helpers; | ||
using Pandora.Data.Models; | ||
using Pandora.Data.Repositories; | ||
|
||
namespace Pandora.Api.V1.Controllers; | ||
|
||
public class CommonTypesController : ControllerBase | ||
{ | ||
private readonly ICommonTypesRepository _repo; | ||
|
||
public CommonTypesController(ICommonTypesRepository repo) | ||
{ | ||
_repo = repo; | ||
} | ||
|
||
[Route("/v1/microsoft-graph/{apiVersion}/commonTypes")] | ||
public IActionResult MicrosoftGraph(string apiVersion) | ||
{ | ||
var definitionType = apiVersion.ParseServiceDefinitionTypeFromApiVersion(); | ||
if (definitionType == null) | ||
{ | ||
return BadRequest($"the API Version {apiVersion} is not supported"); | ||
} | ||
|
||
return CommonTypes(definitionType.Value); | ||
} | ||
|
||
[Route("/v1/resource-manager/commonTypes")] | ||
public IActionResult ResourceManager(string apiVersion) | ||
{ | ||
return CommonTypes(ServiceDefinitionType.ResourceManager); | ||
} | ||
|
||
private IActionResult CommonTypes(ServiceDefinitionType serviceDefinitionType) | ||
{ | ||
var commonTypes = _repo.Get(serviceDefinitionType); | ||
return new JsonResult(MapResponse(commonTypes)); | ||
} | ||
|
||
private class CommonTypesResponse | ||
{ | ||
[JsonPropertyName("constants")] | ||
public Dictionary<string, ConstantApiDefinition> Constants { get; set; } | ||
|
||
[JsonPropertyName("models")] | ||
public Dictionary<string, ModelApiDefinition> Models { get; set; } | ||
} | ||
|
||
private static CommonTypesResponse MapResponse(IEnumerable<CommonTypesDefinition> resource) | ||
{ | ||
var constants = resource.SelectMany(c => c.Constants); | ||
var models = resource.SelectMany(c => c.Models); | ||
|
||
return new CommonTypesResponse | ||
{ | ||
Constants = constants.ToDictionary(c => c.Name, ConstantApiDefinition.Map), | ||
Models = models.ToDictionary(m => m.Name, MapModel), | ||
}; | ||
} | ||
|
||
private class ModelApiDefinition | ||
{ | ||
[JsonPropertyName("fields")] | ||
public Dictionary<string, PropertyApiDefinition> Fields { get; set; } | ||
|
||
[JsonPropertyName("parentTypeName")] | ||
public string? ParentTypeName { get; set; } | ||
|
||
[JsonPropertyName("typeHintIn")] | ||
public string? TypeHintIn { get; set; } | ||
|
||
[JsonPropertyName("typeHintValue")] | ||
public string? TypeHintValue { get; set; } | ||
} | ||
|
||
private static ModelApiDefinition MapModel(ModelDefinition model) | ||
{ | ||
return new ModelApiDefinition | ||
{ | ||
Fields = model.Properties.ToDictionary(p => p.Name, MapProperty), | ||
ParentTypeName = model.ParentTypeName, | ||
TypeHintIn = model.TypeHintIn, | ||
TypeHintValue = model.TypeHintValue, | ||
}; | ||
} | ||
|
||
private class PropertyApiDefinition | ||
{ | ||
[JsonPropertyName("dateFormat")] | ||
public string? DateFormat { get; set; } | ||
|
||
[JsonPropertyName("default")] | ||
public object? Default { get; set; } | ||
|
||
[JsonPropertyName("forceNew")] | ||
public bool ForceNew { get; set; } | ||
|
||
[JsonPropertyName("isTypeHint")] | ||
public bool IsTypeHint { get; set; } | ||
|
||
[JsonPropertyName("jsonName")] | ||
public string JsonName { get; set; } | ||
|
||
[JsonPropertyName("objectDefinition")] | ||
public ApiObjectDefinition ObjectDefinition { get; set; } | ||
|
||
[JsonPropertyName("optional")] | ||
public bool Optional { get; set; } | ||
|
||
[JsonPropertyName("required")] | ||
public bool Required { get; set; } | ||
|
||
[JsonPropertyName("validation")] | ||
public ValidationApiDefinition? Validation { get; set; } | ||
} | ||
|
||
private static PropertyApiDefinition MapProperty(PropertyDefinition definition) | ||
{ | ||
return new PropertyApiDefinition | ||
{ | ||
DateFormat = definition.DateFormat, | ||
Default = definition.Default, | ||
ForceNew = definition.ForceNew, | ||
JsonName = definition.JsonName, | ||
IsTypeHint = definition.IsTypeHint, | ||
ObjectDefinition = ApiObjectDefinitionMapper.Map(definition.ObjectDefinition), | ||
Optional = definition.Optional, | ||
Required = definition.Required, | ||
Validation = ValidationApiDefinition.Map(definition.Validation), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Pandora.Data.Models; | ||
using Pandora.Definitions.Interfaces; | ||
using CommonTypesDefinition = Pandora.Definitions.Interfaces.CommonTypesDefinition; | ||
|
||
namespace Pandora.Data; | ||
|
||
public static class SupportedCommonTypes | ||
{ | ||
public static Dictionary<ServiceDefinitionType, IEnumerable<CommonTypesDefinition>> Get() | ||
{ | ||
var data = DefinitionTypes.Get(); | ||
var output = new Dictionary<ServiceDefinitionType, IEnumerable<CommonTypesDefinition>>(); | ||
foreach (var item in data) | ||
{ | ||
var commonTypes = item.Value.Select(Definitions.Discovery.CommonTypesDefinition.ForServiceDefinition); | ||
output.Add(item.Key, commonTypes); | ||
} | ||
|
||
return output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Collections.Generic; | ||
using Pandora.Data.Models; | ||
using Pandora.Definitions.DataPlane; | ||
using Pandora.Definitions.HandDefined; | ||
using Pandora.Definitions.Interfaces; | ||
using Pandora.Definitions.MicrosoftGraph.Beta; | ||
using Pandora.Definitions.MicrosoftGraph.StableV1; | ||
using Pandora.Definitions.ResourceManager; | ||
using ServiceDefinition = Pandora.Definitions.Interfaces.ServiceDefinition; | ||
|
||
namespace Pandora.Data; | ||
|
||
public static class DefinitionTypes | ||
{ | ||
public static Dictionary<ServiceDefinitionType, List<ServicesDefinition>> Get() | ||
{ | ||
return new Dictionary<ServiceDefinitionType, List<ServicesDefinition>> | ||
{ | ||
{ | ||
ServiceDefinitionType.DataPlane, | ||
new List<ServicesDefinition> | ||
{ | ||
new DataPlaneServices(), | ||
} | ||
}, | ||
{ | ||
ServiceDefinitionType.MicrosoftGraphBeta, | ||
new List<ServicesDefinition> | ||
{ | ||
new MicrosoftGraphBetaServices(), | ||
} | ||
}, | ||
{ | ||
ServiceDefinitionType.MicrosoftGraphStableV1, | ||
new List<ServicesDefinition> | ||
{ | ||
new MicrosoftGraphStableV1Services(), | ||
} | ||
}, | ||
{ | ||
ServiceDefinitionType.ResourceManager, | ||
new List<ServicesDefinition> | ||
{ | ||
new HandDefinedServices(), | ||
new ResourceManagerServices(), | ||
} | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Pandora.Data.Models; | ||
|
||
public class CommonTypesDefinition | ||
{ | ||
public ServiceDefinitionType ServiceDefinitionType { get; set; } | ||
public IEnumerable<ConstantDefinition> Constants { get; set; } | ||
public IEnumerable<ModelDefinition> Models { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Pandora.Data.Models; | ||
using CommonTypesDefinition = Pandora.Data.Models.CommonTypesDefinition; | ||
|
||
namespace Pandora.Data.Repositories; | ||
|
||
public class CommonTypesRepository : ICommonTypesRepository | ||
{ | ||
private readonly Dictionary<ServiceDefinitionType, List<CommonTypesDefinition>> _commonTypes; | ||
|
||
public CommonTypesRepository(Dictionary<ServiceDefinitionType, IEnumerable<Definitions.Interfaces.CommonTypesDefinition>> commonTypes) | ||
{ | ||
_commonTypes = new Dictionary<ServiceDefinitionType, List<CommonTypesDefinition>>(); | ||
foreach (var commonType in commonTypes) | ||
{ | ||
var mapped = commonType.Value.Select(s => Transformers.CommonTypes.Map(s, commonType.Key)).ToList(); | ||
_commonTypes.Add(commonType.Key, mapped); | ||
} | ||
} | ||
|
||
public IEnumerable<CommonTypesDefinition> Get(ServiceDefinitionType serviceDefinitionType) | ||
{ | ||
var commonTypes = _commonTypes.Where(c => c.Key == serviceDefinitionType); | ||
if (!commonTypes.Any()) | ||
{ | ||
return null; | ||
} | ||
|
||
return commonTypes.SelectMany(v => v.Value).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
using Pandora.Data.Models; | ||
|
||
namespace Pandora.Data.Repositories; | ||
|
||
public interface ICommonTypesRepository | ||
{ | ||
IEnumerable<CommonTypesDefinition> Get(ServiceDefinitionType serviceDefinitionType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.