public async Task <EdgeAgentDeploymentPayload> PrepareDeploymentTemplateForInternalAgentIdAsync(string plantId, string internalAgentId) { using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name)) { try { EdgeAgentDeploymentPayload agentPayload = new EdgeAgentDeploymentPayload(); JTokenWrapper deploymentJsonObject = new JTokenWrapper(); var deploymentTemplate = await _edgeAgentDeploymentRepository.GetDeploymentTemplateAsync(); deploymentJsonObject.Value = JObject.Parse(deploymentTemplate.DeploymentTemplate); var edgeAgent = await _edgeAgentRepository.GetEdgeAgentByIdAsync(plantId, internalAgentId); var edgeConfiguration = await _edgeConfigurationRepository.GetEdgeConfigurationByInternalAgentIdAsync(plantId, internalAgentId); var edgeGatewayConfiguration = await _edgeGatewayConfigurationRepository.GetEdgeGatewayConfigurationByInternalAgentIdAsync(plantId, internalAgentId); var modulesOfAgent = await _edgeMessagePipelineRepository.GetEdgeModuleConfigurationsByAgentIdAsync(plantId, internalAgentId); var defaultModulesOfAgent = await GetDefaultModulesOfAgentAsync(plantId, internalAgentId); var finalModulesOfAgent = GetFinalModulesOfAgent(defaultModulesOfAgent, modulesOfAgent); var routesForTemplate = await GetRoutesForTemplateAsync(plantId, internalAgentId, defaultModulesOfAgent, finalModulesOfAgent); var modulesForTemplate = GetModulesForTemplate(defaultModulesOfAgent, modulesOfAgent, edgeConfiguration); JObject modules = JObject.FromObject(modulesForTemplate); var edgeAgentDesiredPropertiesModules = GetEdgeAgentDesiredPropertiesModules(deploymentJsonObject).Value; edgeAgentDesiredPropertiesModules.Replace(modules); JObject routes = JObject.FromObject(routesForTemplate); var edgeHubDesiresPropertiesRoutes = GetEdgeHubDesiredPropertiesRoutes(deploymentJsonObject).Value; edgeHubDesiresPropertiesRoutes.Replace(routes); SetModuleDesiredProperties(finalModulesOfAgent, deploymentJsonObject, edgeAgent, edgeConfiguration); UpdateEdgeHub(deploymentJsonObject, edgeConfiguration, edgeGatewayConfiguration); UpdateEdgeAgent(deploymentJsonObject, edgeConfiguration, edgeGatewayConfiguration); SetRegistryCredentials(deploymentJsonObject); agentPayload.DeploymentTemplate = deploymentJsonObject.Value; agentPayload.AgentConfigurationName = string.Format(AgentonfigurationNameTemplate, edgeAgent.AgentName, DateTime.UtcNow.ToString()); agentPayload.DeploymentConfiguration = await PrepareDeploymentConfigurationForInternalAgentIdAsync(plantId, internalAgentId); return(agentPayload); } catch (Exception ex) { logger.LogException(ex); throw; } } }
public async Task <EdgeAgentDeploymentPayload> DeployTemplateOnAgentAsync(string plantId, string internalAgentId, EdgeAgentDeploymentPayload edgeAgentPayload) { using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name)) { try { var edgeAgent = await _edgeAgentRepository.GetEdgeAgentByIdAsync(plantId, internalAgentId); EdgeAgentDeploymentPayloadDto edgeAgentPayloadDto = new EdgeAgentDeploymentPayloadDto(edgeAgentPayload); edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent = await _ioTHubRepository.SetDeviceModuleAsync(edgeAgent.AgentId, edgeAgentPayload.DeploymentTemplate.ToString()); var edgeAgentDeployedTemplates = await _orchestrateRepository.GetDeployedTemplatesAsync(plantId, internalAgentId); edgeAgent.UpdatedDate = DateTime.UtcNow.ToString(); edgeAgent.ManagedBy = Constants.ManagedByEdgeV2; edgeAgent.VisibleOnScreen = Constants.VisibleOnScreenEdgeV2; if (edgeAgentDeployedTemplates == null || !edgeAgentDeployedTemplates.Any()) { edgeAgent.StartDate = DateTime.UtcNow.ToString(); } await _edgeAgentRepository.UpdateEdgeAgentAsync(edgeAgent.Id, edgeAgent); var deployTemplates = await _orchestrateRepository.GetDeployedTemplatesAsync(plantId, internalAgentId); if (deployTemplates?.Any() == true) { var fileterdeployTemplates = deployTemplates.Where(template => template.IsLatestDeployedTemplate); foreach (var template in fileterdeployTemplates) { template.IsLatestDeployedTemplate = false; await _orchestrateRepository.UpdateDeployedTemplateAsync(template.Id, template); } } if (string.IsNullOrEmpty(edgeAgentPayloadDto.Id)) { edgeAgentPayloadDto.IsDeleted = false; edgeAgentPayloadDto.PlantId = plantId; edgeAgentPayloadDto.InternalAgentId = internalAgentId; edgeAgentPayloadDto.IsLatestDeployedTemplate = true; edgeAgentPayloadDto.AgentConfigurationName = string.Format(AgentonfigurationNameTemplate, edgeAgent.AgentName, DateTime.UtcNow.ToString()); await _orchestrateRepository.AddDeployedTemplateAsync(edgeAgentPayloadDto); } else { var deployedTemplate = deployTemplates.FirstOrDefault(template => template.Id == edgeAgentPayloadDto.Id); deployedTemplate.UserComments = edgeAgentPayloadDto.UserComments; deployedTemplate.DeploymentConfiguration = edgeAgentPayloadDto.DeploymentConfiguration; deployedTemplate.DeploymentTemplate = edgeAgentPayloadDto.DeploymentTemplate; deployedTemplate.IsLatestDeployedTemplate = true; deployedTemplate.AgentConfigurationName = edgeAgentPayloadDto.AgentConfigurationName; deployedTemplate.IsApplyConfigurationContentOnAgent = edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent; await _orchestrateRepository.UpdateDeployedTemplateAsync(edgeAgentPayloadDto.Id, deployedTemplate); } return(new EdgeAgentDeploymentPayload { Id = edgeAgentPayloadDto.Id, IsApplyConfigurationContentOnAgent = edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent, DeploymentTemplate = edgeAgentPayloadDto.DeploymentTemplate, AgentConfigurationName = edgeAgentPayloadDto.AgentConfigurationName, DeploymentConfiguration = edgeAgentPayload.DeploymentConfiguration, UserComments = edgeAgentPayloadDto.UserComments, IsLatestDeployedTemplate = edgeAgentPayloadDto.IsLatestDeployedTemplate }); } catch (Exception ex) { logger.LogException(ex); throw; } } }