public async Task <HttpResponseData> CreateNode([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "testhooks/nodeOperations/node")] HttpRequestData req)
        {
            _log.Info("create node");

            var query = UriExtension.GetQueryComponents(req.Url);

            Guid poolId    = Guid.Parse(query["poolId"]);
            var  poolName  = PoolName.Parse(query["poolName"]);
            Guid machineId = Guid.Parse(query["machineId"]);

            Guid?scaleSetId = default;

            if (query.ContainsKey("scaleSetId"))
            {
                scaleSetId = Guid.Parse(query["scaleSetId"]);
            }

            string version = query["version"];

            bool isNew = UriExtension.GetBool("isNew", query, false);

            var node = await _nodeOps.Create(poolId, poolName, machineId, scaleSetId, version, isNew);

            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteAsJsonAsync(JsonSerializer.Serialize(node, EntityConverter.GetJsonSerializerOptions()));

            return(resp);
        }
        public async Task <HttpResponseData> SearchStates([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "testhooks/nodeOperations/searchStates")] HttpRequestData req)
        {
            _log.Info("search states");

            var  query      = UriExtension.GetQueryComponents(req.Url);
            Guid?poolId     = UriExtension.GetGuid("poolId", query);
            Guid?scaleSetId = UriExtension.GetGuid("scaleSetId", query);

            List <NodeState>?states = default;

            if (query.ContainsKey("states"))
            {
                states = query["states"].Split('-').Select(s => Enum.Parse <NodeState>(s)).ToList();
            }
            string?poolNameString = UriExtension.GetString("poolName", query);

            PoolName?poolName = poolNameString is null ? null : PoolName.Parse(poolNameString);

            var excludeUpdateScheduled = UriExtension.GetBool("excludeUpdateScheduled", query, false);
            int?numResults             = UriExtension.GetInt("numResults", query);
            var r    = _nodeOps.SearchStates(poolId, scaleSetId, states, poolName, excludeUpdateScheduled, numResults);
            var json = JsonSerializer.Serialize(await r.ToListAsync(), EntityConverter.GetJsonSerializerOptions());
            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteStringAsync(json);

            return(resp);
        }
        public async Task <HttpResponseData> ReimageLongLivedNodes([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "testhooks/nodeOperations/reimageLongLivedNodes")] HttpRequestData req)
        {
            _log.Info("reimage long lived nodes");
            var query = UriExtension.GetQueryComponents(req.Url);

            var r    = _nodeOps.ReimageLongLivedNodes(Guid.Parse(query["scaleSetId"]));
            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteAsJsonAsync(r);

            return(resp);
        }
        public async Task <HttpResponseData> GetByMachineId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/nodeOperations/getByMachineId")] HttpRequestData req)
        {
            _log.Info("Get node by machine id");

            var query     = UriExtension.GetQueryComponents(req.Url);
            var machineId = query["machineId"];

            var node = await _nodeOps.GetByMachineId(Guid.Parse(machineId));

            var msg  = JsonSerializer.Serialize(node, EntityConverter.GetJsonSerializerOptions());
            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteStringAsync(msg);

            return(resp);
        }
        public async Task <HttpResponseData> GetDeadNodes([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "testhooks/nodeOperations/getDeadNodes")] HttpRequestData req)
        {
            _log.Info("get dead nodes");

            var query = UriExtension.GetQueryComponents(req.Url);

            Guid     scaleSetId = Guid.Parse(query["scaleSetId"]);
            TimeSpan timeSpan   = TimeSpan.Parse(query["timeSpan"]);

            var nodes = await(_nodeOps.GetDeadNodes(scaleSetId, timeSpan).ToListAsync());
            var json  = JsonSerializer.Serialize(nodes, EntityConverter.GetJsonSerializerOptions());
            var resp  = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteStringAsync(json);

            return(resp);
        }
        public async Task <HttpResponseData> ToReimage([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "testhooks/nodeOperations/toReimage")] HttpRequestData req)
        {
            _log.Info("to reimage");

            var query = UriExtension.GetQueryComponents(req.Url);
            var done  = UriExtension.GetBool("done", query, false);

            var s = await req.ReadAsStringAsync();

            var node = JsonSerializer.Deserialize <Node>(s !, EntityConverter.GetJsonSerializerOptions());

            var r    = _nodeOps.ToReimage(node !, done);
            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteAsJsonAsync(r);

            return(resp);
        }
        public async Task <HttpResponseData> SetState([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "testhooks/nodeOperations/setState")] HttpRequestData req)
        {
            _log.Info("set state");

            var query = UriExtension.GetQueryComponents(req.Url);
            var state = Enum.Parse <NodeState>(query["state"]);

            var s = await req.ReadAsStringAsync();

            var node = JsonSerializer.Deserialize <Node>(s !, EntityConverter.GetJsonSerializerOptions());

            var r    = _nodeOps.SetState(node !, state);
            var resp = req.CreateResponse(HttpStatusCode.OK);
            await resp.WriteAsJsonAsync(r);

            return(resp);
        }