Ejemplo n.º 1
0
        public async Task <ActionResult <Vm[]> > Find([FromQuery] string filter)
        {
            var vms = await _pod.Find(filter);

            var keys = vms.Select(v => v.Name.Tag()).Distinct().ToArray();

            var map = await _templateService.ResolveKeys(keys);

            foreach (Vm vm in vms)
            {
                vm.GroupName = map[vm.Name.Tag()];
            }

            return(Ok(
                       vms
                       .OrderBy(v => v.GroupName)
                       .ThenBy(v => v.Name)
                       .ToArray()
                       ));
        }
Ejemplo n.º 2
0
        private async Task <GameState> LoadState(Data.Gamespace gamespace, int topoId)
        {
            GameState state = null;

            // gamespace should never be null in the engine service
            if (gamespace == null)
            {
                Data.Workspace topo = await _workspaceStore.Load(topoId);

                if (topo == null || !topo.IsPublished)
                {
                    throw new InvalidOperationException();
                }

                state = new GameState();

                state.Name = gamespace?.Name ?? topo.Name;

                state.WorkspaceDocument = topo.Document;

                state.Vms = topo.Templates
                            .Where(t => !t.IsHidden)
                            .Select(t => new VmState {
                    Name = t.Name, TemplateId = t.Id
                })
                            .ToArray();
            }
            else
            {
                // get vm's, look up template, add if template not mark as hidden.

                state = Mapper.Map <GameState>(gamespace);

                var vmState = new List <VmState>();

                var vms = await _pod.Find(gamespace.GlobalId);

                foreach (Vm vm in vms)
                {
                    string name = vm.Name.Untagged();

                    // a vm could be a replica, denoted by `_1` or some number,
                    // so strip that to find template.
                    int x = name.LastIndexOf('_');

                    var tmpl = gamespace.Workspace.Templates
                               .Where(t => !t.IsHidden && t.Name == name)
                               .FirstOrDefault();

                    if (tmpl == null && x == name.Length - 2)
                    {
                        name = name.Substring(0, x);

                        tmpl = gamespace.Workspace.Templates
                               .Where(t => !t.IsHidden && t.Name == name)
                               .FirstOrDefault();
                    }

                    if (tmpl != null)
                    {
                        vmState.Add(new VmState
                        {
                            Id         = vm.Id,
                            Name       = vm.Name,
                            IsRunning  = (vm.State == VmPowerState.Running),
                            TemplateId = tmpl.Id
                        });
                    }
                }

                state.Vms = vmState.ToArray();
            }

            return(state);
        }