From d638d667645d6fe7fda8528efced6400d7065f39 Mon Sep 17 00:00:00 2001 From: cmleite Date: Thu, 30 Jan 2025 14:55:17 +0000 Subject: [PATCH] Create environment as relation --- src/Common/Handlers/NewEnvironmentHandler.cs | 57 ++++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/Common/Handlers/NewEnvironmentHandler.cs b/src/Common/Handlers/NewEnvironmentHandler.cs index f61f55b..280ed34 100644 --- a/src/Common/Handlers/NewEnvironmentHandler.cs +++ b/src/Common/Handlers/NewEnvironmentHandler.cs @@ -92,11 +92,25 @@ bool terminateOtherVersionsRemoveVolumes environment.Description = description; } - environment.DeploymentPackage = isInfrastructureAgent || string.IsNullOrWhiteSpace(deploymentPackageName) ? environment.DeploymentPackage : await _customerPortalClient.GetObjectByName(deploymentPackageName); - environment.SoftwareLicense = isInfrastructureAgent || string.IsNullOrWhiteSpace(licenseName) ? environment.SoftwareLicense : await Utils.GetLicenseByUniqueName(licenseName); environment.DeploymentTarget = _newEnvironmentUtilities.GetDeploymentTargetValue(target); environment.ChangeSet = null; + var cedpCollection = new CustomerEnvironmentDeploymentPackageCollection(); + if (isInfrastructureAgent || string.IsNullOrWhiteSpace(deploymentPackageName)) + { + cedpCollection.AddRange(environment.RelationCollection.FirstOrDefault(x => x.Key == nameof(CustomerEnvironmentDeploymentPackage)).Value + .Cast()); + } + else + { + cedpCollection.Add(new CustomerEnvironmentDeploymentPackage() + { + SourceEntity = environment, + SoftwareLicense = await Utils.GetLicenseByUniqueName(licenseName), + TargetEntity = await _customerPortalClient.GetObjectByName(deploymentPackageName) + }); + } + // check environment connection await _newEnvironmentUtilities.CheckEnvironmentConnection(environment); @@ -105,7 +119,7 @@ bool terminateOtherVersionsRemoveVolumes // Update environment with the parameters to be merged instead of overwriting environment.Parameters = rawParameters; - environment = await UpdateEnvironment(environment); + environment = await UpdateEnvironment(environment, cedpCollection); // terminate other versions if (terminateOtherVersions) @@ -190,12 +204,22 @@ bool terminateOtherVersionsRemoveVolumes Description = description, Parameters = rawParameters, EnvironmentType = environmentType.ToString(), - DeploymentPackage = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName(deploymentPackageName), DeploymentTarget = _newEnvironmentUtilities.GetDeploymentTargetValue(target), Site = environmentSite, - SoftwareLicense = isInfrastructureAgent ? null : await Utils.GetLicenseByUniqueName(licenseName) }; + var deploymentPackage = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName(deploymentPackageName); + var softwareLicense = isInfrastructureAgent ? null : await Utils.GetLicenseByUniqueName(licenseName); + var cedpCollection = new CustomerEnvironmentDeploymentPackageCollection + { + new CustomerEnvironmentDeploymentPackage() + { + SourceEntity = environment, + TargetEntity = deploymentPackage, + SoftwareLicense = softwareLicense + } + }; + // check environment connection await CheckConnectionNewEnvironmentCreation(environment, customerInfrastructureName); @@ -205,7 +229,8 @@ bool terminateOtherVersionsRemoveVolumes { CustomerInfrastructureName = customerInfrastructureName, CustomerEnvironment = environment, - IsInfrastructureAgent = isInfrastructureAgent + IsInfrastructureAgent = isInfrastructureAgent, + CustomerEnvironmentDeploymentPackageRelations = cedpCollection }.CreateCustomerEnvironmentForCustomerInfrastructureAsync(true)).CustomerEnvironment; } // if not, just create a new environment @@ -218,13 +243,24 @@ bool terminateOtherVersionsRemoveVolumes EnvironmentType = environmentType.ToString(), Site = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName(siteName), Name = name, - DeploymentPackage = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName(deploymentPackageName), - SoftwareLicense = isInfrastructureAgent ? null : await Utils.GetLicenseByUniqueName(licenseName), DeploymentTarget = _newEnvironmentUtilities.GetDeploymentTargetValue(target), Parameters = rawParameters }; + var deploymentPackage = isInfrastructureAgent ? null : await _customerPortalClient.GetObjectByName(deploymentPackageName); + var softwareLicense = isInfrastructureAgent ? null : await Utils.GetLicenseByUniqueName(licenseName); + var cedpCollection = new CustomerEnvironmentDeploymentPackageCollection + { + new CustomerEnvironmentDeploymentPackage() + { + SourceEntity = environment, + TargetEntity = deploymentPackage, + SoftwareLicense = softwareLicense + } + }; + environment = await CreateEnvironment(_customerPortalClient, environment); + environment = await UpdateEnvironment(environment, cedpCollection); } Session.LogInformation($"Customer environment {name} created..."); @@ -293,13 +329,14 @@ public static async Task CreateNewEnvironmentEntityOrVersio /// /// customer environment /// - public static async Task UpdateEnvironment(CustomerEnvironment customerEnvironment) + public static async Task UpdateEnvironment(CustomerEnvironment customerEnvironment, CustomerEnvironmentDeploymentPackageCollection cedpCollection) { customerEnvironment.ChangeSet = null; return (await new UpdateCustomerEnvironmentInput { CustomerEnvironment = customerEnvironment, - DeploymentParametersMergeMode = DeploymentParametersMergeMode.Merge + DeploymentParametersMergeMode = DeploymentParametersMergeMode.Merge, + CustomerEnvironmentDeploymentPackageRelations = cedpCollection }.UpdateCustomerEnvironmentAsync(true)).CustomerEnvironment; } }