Refactor code structure for improved readability and maintainability
This commit is contained in:
+35
-23
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
||||
using Microsoft.SelfService.Portal.Core.API.JsonDocuments;
|
||||
using Microsoft.SelfService.Portal.Core.API.Models;
|
||||
using System.Text.Json;
|
||||
|
||||
@@ -39,7 +40,7 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
PayloadJson = JsonSerializer.Serialize(payload),
|
||||
Targets = deployments.Select(deployment => new QueueJobTargetModel
|
||||
{
|
||||
VirtualMachineId = deployment.VirtualMachineId,
|
||||
TargetId = deployment.TargetId,
|
||||
DeploymentGroupId = deployment.DeploymentGroupId,
|
||||
TemplateId = templateId,
|
||||
Status = QueueJobStatus.Pending
|
||||
@@ -53,13 +54,20 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
return queueJob.Id;
|
||||
}
|
||||
|
||||
public Guid EnqueueDeploymentRequest(Guid deploymentGroupId, ICollection<Guid> virtualMachineIds, string jsonData)
|
||||
public Guid EnqueueDeploymentRequest(Guid deploymentGroupId, ICollection<Guid> targetIds, string jsonData)
|
||||
{
|
||||
var deploymentDocument = ConfigurationDocumentValidator.NormalizeAndValidate(
|
||||
jsonData,
|
||||
ConfigurationDocumentKind.DeploymentOverride);
|
||||
jsonData = deploymentDocument.JsonData;
|
||||
|
||||
var deploymentGroup = _context.DeploymentGroups
|
||||
.AsNoTracking()
|
||||
.Include(group => group.Template)
|
||||
.ThenInclude(template => template.DeploymentRule)
|
||||
.ThenInclude(rule => rule.Steps)
|
||||
.Include(group => group.DeploymentRule)
|
||||
.ThenInclude(rule => rule!.Steps)
|
||||
.FirstOrDefault(group => group.Id == deploymentGroupId);
|
||||
|
||||
if (deploymentGroup == null)
|
||||
@@ -68,43 +76,43 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
}
|
||||
|
||||
var templateId = deploymentGroup.TemplateId;
|
||||
var resolvedVirtualMachineIds = virtualMachineIds
|
||||
var resolvedTargetIds = targetIds
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
if (resolvedVirtualMachineIds.Count == 0)
|
||||
if (resolvedTargetIds.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("No target VirtualMachines provided.");
|
||||
throw new InvalidOperationException("No target Targets provided.");
|
||||
}
|
||||
|
||||
var existingVirtualMachines = _context.VirtualMachines
|
||||
var existingTargets = _context.Targets
|
||||
.AsNoTracking()
|
||||
.Where(virtualMachine => resolvedVirtualMachineIds.Contains(virtualMachine.Id))
|
||||
.Select(virtualMachine => virtualMachine.Id)
|
||||
.Where(target => resolvedTargetIds.Contains(target.Id))
|
||||
.Select(target => target.Id)
|
||||
.ToHashSet();
|
||||
|
||||
var missingVirtualMachines = resolvedVirtualMachineIds
|
||||
.Where(virtualMachineId => !existingVirtualMachines.Contains(virtualMachineId))
|
||||
var missingTargets = resolvedTargetIds
|
||||
.Where(targetId => !existingTargets.Contains(targetId))
|
||||
.ToList();
|
||||
|
||||
if (missingVirtualMachines.Count > 0)
|
||||
if (missingTargets.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Unknown VirtualMachine IDs: {string.Join(", ", missingVirtualMachines)}");
|
||||
throw new InvalidOperationException($"Unknown Target IDs: {string.Join(", ", missingTargets)}");
|
||||
}
|
||||
|
||||
foreach (var virtualMachineId in resolvedVirtualMachineIds)
|
||||
foreach (var targetId in resolvedTargetIds)
|
||||
{
|
||||
var deployment = _context.Deployments
|
||||
.FirstOrDefault(existing =>
|
||||
existing.DeploymentGroupId == deploymentGroupId
|
||||
&& existing.VirtualMachineId == virtualMachineId);
|
||||
&& existing.TargetId == targetId);
|
||||
|
||||
if (deployment == null)
|
||||
{
|
||||
deployment = new DeploymentModel
|
||||
{
|
||||
DeploymentGroupId = deploymentGroupId,
|
||||
VirtualMachineId = virtualMachineId,
|
||||
TargetId = targetId,
|
||||
Status = QueueJobStatus.Pending,
|
||||
JSONData = jsonData
|
||||
};
|
||||
@@ -118,13 +126,16 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
}
|
||||
}
|
||||
|
||||
var resolvedRule = deploymentGroup.DeploymentRule ?? deploymentGroup.Template?.DeploymentRule;
|
||||
|
||||
var payload = new
|
||||
{
|
||||
DeploymentGroupId = deploymentGroupId,
|
||||
TemplateId = templateId,
|
||||
VirtualMachineIds = resolvedVirtualMachineIds,
|
||||
DeploymentRuleId = resolvedRule?.Id,
|
||||
TargetIds = resolvedTargetIds,
|
||||
JsonData = jsonData,
|
||||
TargetCount = resolvedVirtualMachineIds.Count,
|
||||
TargetCount = resolvedTargetIds.Count,
|
||||
Created = DateTime.UtcNow
|
||||
};
|
||||
|
||||
@@ -133,18 +144,18 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
Type = QueueJobType.DeploymentRequested,
|
||||
Status = QueueJobStatus.Pending,
|
||||
PayloadJson = JsonSerializer.Serialize(payload),
|
||||
RuleSnapshotJson = deploymentGroup.Template?.DeploymentRuleId.HasValue == true
|
||||
? SerializeRuleSnapshot(deploymentGroup.Template!.DeploymentRule)
|
||||
RuleSnapshotJson = resolvedRule != null
|
||||
? SerializeRuleSnapshot(resolvedRule)
|
||||
: null,
|
||||
Targets = resolvedVirtualMachineIds.Select(virtualMachineId => new QueueJobTargetModel
|
||||
Targets = resolvedTargetIds.Select(targetId => new QueueJobTargetModel
|
||||
{
|
||||
VirtualMachineId = virtualMachineId,
|
||||
TargetId = targetId,
|
||||
DeploymentGroupId = deploymentGroupId,
|
||||
TemplateId = templateId,
|
||||
Status = QueueJobStatus.Pending
|
||||
}).ToList()
|
||||
};
|
||||
queueJob.Steps = BuildDeploymentSteps(deploymentGroup.Template?.DeploymentRule);
|
||||
queueJob.Steps = BuildDeploymentSteps(resolvedRule);
|
||||
|
||||
_context.QueueJobs.Add(queueJob);
|
||||
_context.SaveChanges();
|
||||
@@ -313,3 +324,4 @@ namespace Microsoft.SelfService.Portal.Core.API.Services
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user