Exemple #1
0
            protected override async Task Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
                {
                    throw new ForbiddenException();
                }

                var workspace = await _db.Workspaces.FindAsync(request.Id);

                if (workspace == null)
                {
                    throw new EntityNotFoundException <Workspace>();
                }

                using (var lockResult = await _lockService.GetWorkspaceLock(request.Id).LockAsync(0))
                {
                    if (!lockResult.AcquiredLock)
                    {
                        throw new WorkspaceConflictException();
                    }

                    if (workspace.GetState().GetResources().Any())
                    {
                        throw new ConflictException("Cannot delete a Workspace with deployed Resources.");
                    }

                    if (await _db.AnyIncompleteRuns(request.Id))
                    {
                        throw new ConflictException("Cannot delete a Workspace with pending Runs.");
                    }

                    _db.Workspaces.Remove(workspace);
                    await _db.SaveChangesAsync(cancellationToken);
                }
            }
Exemple #2
0
            public async Task <Run> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
                {
                    throw new ForbiddenException();
                }

                await ValidateWorkspace(request.WorkspaceId);

                Domain.Models.Run run = null;

                using (var lockResult = await _lockService.GetWorkspaceLock(request.WorkspaceId).LockAsync(0))
                {
                    if (!lockResult.AcquiredLock)
                    {
                        throw new WorkspaceConflictException();
                    }

                    if ((await _db.AnyIncompleteRuns(request.WorkspaceId)))
                    {
                        throw new ConflictException("This Workspace's current Run must be rejected or applied before a new one can be created.");
                    }

                    run = await this.DoWork(request);
                }

                await _mediator.Publish(new RunCreated { RunId = run.Id });

                return(_mapper.Map <Run>(run));
            }
Exemple #3
0
            public async Task <Run> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
                {
                    throw new ForbiddenException();
                }

                var run = await _db.Runs
                          .Include(r => r.Apply)
                          .Include(r => r.Workspace)
                          .Where(r => r.Id == request.RunId)
                          .FirstOrDefaultAsync(cancellationToken);

                if (run == null)
                {
                    throw new EntityNotFoundException <Run>();
                }

                using (var lockResult = await _lockService.GetWorkspaceLock(run.WorkspaceId).LockAsync(0))
                {
                    if (!lockResult.AcquiredLock)
                    {
                        throw new WorkspaceConflictException();
                    }

                    await ValidateRun(run, cancellationToken);

                    var workingDir     = run.Workspace.GetPath(_options.RootWorkingDirectory);
                    var stateRetrieved = await run.Workspace.RetrieveState(workingDir);

                    if (stateRetrieved)
                    {
                        run.Apply.Status = run.Apply.Status == ApplyStatus.Applied_StateError ? ApplyStatus.Applied : ApplyStatus.Failed;
                        run.Status       = run.Status == RunStatus.Applied_StateError ? RunStatus.Applied : RunStatus.Failed;

                        await _db.SaveChangesAsync(cancellationToken);

                        await _mediator.Publish(new RunUpdated(run.Id));

                        await _mediator.Publish(new ApplyCompleted(run.Workspace));

                        run.Workspace.CleanupFileSystem(_options.RootWorkingDirectory);
                    }
                }

                return(await _db.Runs
                       .ProjectTo <Run>(_mapper.ConfigurationProvider)
                       .SingleOrDefaultAsync(x => x.Id == run.Id, cancellationToken));
            }
Exemple #4
0
            public async Task <Run> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
                {
                    throw new ForbiddenException();
                }

                var workspaceId = await _db.Runs.Where(r => r.Id == request.Id).Select(r => r.WorkspaceId).FirstOrDefaultAsync();

                using (var lockResult = await _lockService.GetWorkspaceLock(workspaceId).LockAsync(0))
                {
                    if (!lockResult.AcquiredLock)
                    {
                        throw new WorkspaceConflictException();
                    }

                    var run = await _db.Runs
                              .Include(r => r.Plan)
                              .Include(r => r.Apply)
                              .Include(r => r.Workspace)
                              .FirstOrDefaultAsync(r => r.Id == request.Id);

                    ValidateRun(run);

                    run.Workspace.CleanupFileSystem(_terraformOptions.RootWorkingDirectory);

                    run.Status = RunStatus.Rejected;

                    if (run.Plan != null)
                    {
                        run.Plan.Status = PlanStatus.Rejected;
                    }

                    run.Modify(_user.GetId());
                    await _db.SaveChangesAsync();

                    await _mediator.Publish(new RunUpdated(run.Id));

                    return(await _db.Runs
                           .ProjectTo <Run>(_mapper.ConfigurationProvider)
                           .SingleOrDefaultAsync(x => x.Id == run.Id, cancellationToken));
                }
            }
Exemple #5
0
        protected async Task <Workspace> PerformOperation(Workspace workspace, ResourceOperation operation, string[] addresses)
        {
            using (var lockResult = await _lockService.GetWorkspaceLock(workspace.Id).LockAsync(0))
            {
                if (!lockResult.AcquiredLock)
                {
                    throw new WorkspaceConflictException();
                }

                if (!(await _db.AnyIncompleteRuns(workspace.Id)))
                {
                    return(await this.OperationDoWork(workspace, operation, addresses));
                }
                else
                {
                    throw new WorkspaceConflictException();
                }
            }
        }
Exemple #6
0
            public async Task <Apply> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
                {
                    throw new ForbiddenException();
                }

                var workspaceId = await _db.Runs.Where(r => r.Id == request.RunId).Select(r => r.WorkspaceId).FirstOrDefaultAsync();

                Domain.Models.Apply apply = null;

                using (var lockResult = await _lockService.GetWorkspaceLock(workspaceId).LockAsync(0))
                {
                    if (!lockResult.AcquiredLock)
                    {
                        throw new WorkspaceConflictException();
                    }

                    var run = await _db.Runs
                              .Include(r => r.Plan)
                              .Include(r => r.Apply)
                              .SingleOrDefaultAsync(r => r.Id == request.RunId);

                    ValidateRun(run);

                    apply = new Domain.Models.Apply
                    {
                        RunId  = run.Id,
                        Status = ApplyStatus.Queued
                    };

                    await _db.Applies.AddAsync(apply);

                    run.Modify(_user.GetId());
                    await _db.SaveChangesAsync();
                }

                await _mediator.Publish(new ApplyCreated { ApplyId = apply.Id });

                await _mediator.Publish(new RunUpdated(apply.RunId));

                return(_mapper.Map <Apply>(apply));
            }