/// <summary> /// Parses and checks the given JSON object to see if any armTemplateParameters were added or removed. /// If parameters were added then they are created in the db along with a join entry in the armTemplateArmTemplateParameters table. /// If parameters were removed then the join entry in the armTemplateArmTemplateParameters table is removed. /// </summary> /// <param name="offer">The name of the offer the parameters belong to</param> /// <param name="armTemplateJSON">The JSON object to parse.</param> /// <param name="armTemplateId">The ID of the armTemplate.</param> /// <returns></returns> private async Task UpdateArmTemplateParameters(Offer offer, object armTemplateJSON, long armTemplateId) { List <KeyValuePair <string, string> > incompleteParams = ARMTemplateHelper.GetArmTemplateParameters(armTemplateJSON.ToString()); List <ArmTemplateArmTemplateParameter> joinEntries = await _armTemplateArmTemplateParameterService.GetAllJoinEntries(armTemplateId); Dictionary <string, ArmTemplateParameter> paramsDb = new Dictionary <string, ArmTemplateParameter>(); HashSet <string> usedParamNames = new HashSet <string>(); // Populate paramsDb so that it maps the ArmTemplateParameter name to the ArmTemplateParameter object foreach (ArmTemplateArmTemplateParameter entry in joinEntries) { ArmTemplateParameter armTemplateParameter = await _context.ArmTemplateParameters.FindAsync(entry.ArmTemplateParameterId); if (!paramsDb.ContainsKey(armTemplateParameter.Name)) { paramsDb.Add(armTemplateParameter.Name, armTemplateParameter); } } foreach (KeyValuePair <string, string> incompleteParam in incompleteParams) { // Check if a param with the same name as the incompleteParam already exists if (!paramsDb.ContainsKey(incompleteParam.Key)) { ArmTemplateParameter armParameter = new ArmTemplateParameter { OfferId = offer.Id, Name = incompleteParam.Key, Type = incompleteParam.Value, // TODO: do we need to indicate an incomplete parameter? Value = string.Empty }; // A param with the same name as the incompleteParam does not exist, so create it await _armTemplateParameterService.CreateAsync(offer.OfferName, armTemplateId, armParameter); } // Keep track of all the new parameters we are using in usedParamNames if (!usedParamNames.Contains(incompleteParam.Key)) { usedParamNames.Add(incompleteParam.Key); } } foreach (KeyValuePair <string, ArmTemplateParameter> paramDb in paramsDb) { // Check if there is a param in the db that we are no longer using if (!usedParamNames.Contains(paramDb.Key)) { ArmTemplateArmTemplateParameter armTemplateArmTemplateParameter = await _context.ArmTemplateArmTemplateParameters.FindAsync(armTemplateId, paramDb.Value.Id); // Remove the join entry for any unused params _context.ArmTemplateArmTemplateParameters.Remove(armTemplateArmTemplateParameter); await _context._SaveChangesAsync(); } } }
/// <summary> /// Creates an entry in the armTemplateArmTemplateParameters table if it does not exist. /// </summary> /// <param name="armTemplateId">The ID of the armTemplate.</param> /// <param name="armTemplateParameterId">The ID of the armTemplateParameter.</param> /// <returns></returns> public async Task CreateJoinEntryAsync(long armTemplateId, long armTemplateParameterId) { if (await ExistsAsync(armTemplateId, armTemplateParameterId)) { throw new LunaServerException("This entry cannot be created since it already exists in the DB.", false); } ArmTemplateArmTemplateParameter armTemplateArmTemplateParameter = new ArmTemplateArmTemplateParameter { ArmTemplateId = armTemplateId, ArmTemplateParameterId = armTemplateParameterId }; _context.ArmTemplateArmTemplateParameters.Add(armTemplateArmTemplateParameter); await _context._SaveChangesAsync(); }