Exemple #1
0
        public async Task <IActionResult> RequestVm(Vm newVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ToString()));
            }
            if (newVm?.Cluster?.Datacenter?.Adapter == null)
            {
                return(BadRequest("Неверно заполнены поля"));
            }
            #region CheckResponsibles
            // Проверка на корректность задания ответственных: администратора, менеджера и владельца
            var responsibles = new List <User>()
            {
                newVm.Admin, newVm.Manager, newVm.Owner
            };
            responsibles = responsibles.GroupBy(x => x.Sam).Select(x => x.FirstOrDefault()).ToList();
            List <User> users = _authService.GetUsersList(responsibles);

            if (responsibles.Count != users.Count)
            {
                return(BadRequest("Неверно заполнены поля ответственных за ВМ. Пожалуйста, выберите тех, кто есть в списке"));
            }

            #endregion

            Adapter adapter = await _context.Adapters.Where(x => x.IsOK).Include(c => c.Credentials)
                              .Include(p => p.Provider)
                              .Where(a => a.Id == newVm.Cluster.Datacenter.Adapter.Id)
                              .SingleOrDefaultAsync();

            var auditDays = (await _context.Audits.SingleOrDefaultAsync(x => x.AdapterId == adapter.Id && x.DatacenterId == newVm.Cluster.Datacenter.Id && x.ClusterId == newVm.Cluster.Id))?.AuditDays;
            if (auditDays != null)
            {
                newVm.AuditDate = DateTime.Now.ToShortDateString();
                newVm.Deadline  = DateTime.Now.AddDays((double)auditDays).ToShortDateString();
            }
            var user = User.Claims.SingleOrDefault(x => x.Type == ClaimTypes.Name).Value;
            switch (adapter.Provider.Id)
            {
            case (int)ProviderType.Ovirt:
                ServicesResponse newVmResponse = await VmService.RequestVm(newVm, adapter, newVm.Network.Id, newVm.HddSize, newVm.Cluster.Datacenter.Id);

                if (!newVmResponse.isSuccess)
                {
                    return(StatusCode(newVmResponse.errorCode, newVmResponse.errorMessage));
                }
                break;

            case (int)ProviderType.VMware:
                var task = await SendVmRequest(adapter, newVm, user);

                TrackBackendTask(adapter, task);
                break;
            }

            _mailer.NotifyVmRequested(users, newVm);

            return(CreatedAtAction("RequestVm", newVm));
        }