예제 #1
0
        /// <summary>
        /// Trys to assign an IP address to the given subscriptionId. If the IP address is successfully assigned
        /// then it will be returned. If there are no available IP addresses to assign then an exception
        /// will be thrown.
        /// </summary>
        /// <param name="subscriptionId">The subscriptionId to try to assign an IpAddress to.</param>
        /// <param name="offerName">The name of the offer the subscription belongs to.</param>
        /// <param name="ipConfigName">The name of the IpConfig to get an IpAdress from.</param>
        /// <returns>The assigned IpAddress.</returns>
        public async Task <IpAddress> TryAssignIpAddress(Guid subscriptionId, string offerName, string ipConfigName)
        {
            _logger.LogInformation($"Trying to assign IP addresses to subscription ID {subscriptionId} in offer {offerName} from IP Config {ipConfigName}.");

            IpConfig ipConfig = await _ipConfigService.GetAsync(offerName, ipConfigName);

            // We only want to assign IP addresses from IP blocks that belong to the given IP config
            List <IpBlock> ipBlocks = await _context.IpBlocks.Where(x => x.IpConfigId == ipConfig.Id).ToListAsync();

            // Keep track of IpBlock IDs that belong to the IpConfig
            HashSet <long> blockIds = new HashSet <long>();

            foreach (IpBlock ipBlock in ipBlocks)
            {
                blockIds.Add(ipBlock.Id);
            }

            try
            {
                // Find an IpAddress that is available and belongs to one of the IpBlocks we are tracking
                IpAddress ipAddress = await _context.IpAddresses.Where(x => x.IsAvailable == true && blockIds.Contains(x.IpBlockId)).FirstAsync();

                ipAddress.SubscriptionId = subscriptionId;
                ipAddress.IsAvailable    = false;

                _context.IpAddresses.Update(ipAddress);
                await _context._SaveChangesAsync();

                _logger.LogInformation($"The IP addresses in {ipAddress.Value} have been assigned to subscription ID {subscriptionId}.");

                return(ipAddress);
            }
            catch (ArgumentNullException)
            {
                throw new LunaServerException($"There are no IP addresses available in {ipConfigName}.");
            }
        }
예제 #2
0
 public async Task <ActionResult> GetAsync(string offerName, string name)
 {
     AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);
     _logger.LogInformation($"Get IPConfig {name} in offer {offerName}.");
     return(Ok(await _ipConfigService.GetAsync(offerName, name)));
 }