public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "node/create")] HttpRequestMessage req, TraceWriter log, ExecutionContext context) { var content = await req.Content.ReadAsStringAsync(); var nodeParams = JsonConvert.DeserializeObject <NodeParameters>(content); var err = ValidateRequest(req, nodeParams, log); if (err != null) { return(err); } //find out if there is any VM in MarkedForDeallocation state var vm = (await TableStorageHelper.Instance.GetMatchingVMsInStateAsync( nodeParams.ResourceGroup, nodeParams.Region, nodeParams.Size, VMState.MarkedForDeallocation)) .FirstOrDefault(); if (vm != null) { //set it as running so as not to be deallocated when game rooms are 0 vm.State = VMState.Running; await TableStorageHelper.Instance.ModifyVMDetailsAsync(vm); //we're done, this VM can now be used return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(vm), "application/json")); } //search if there are any deallocated VMs var deallocatedVM = (await TableStorageHelper.Instance .GetMatchingVMsInStateAsync(nodeParams.ResourceGroup, nodeParams.Region, nodeParams.Size, VMState.Deallocated)) .FirstOrDefault(); //no deallocated VMs, so let's create a new one if (deallocatedVM == null) { string vmName = "node" + System.Guid.NewGuid().ToString("N").Substring(0, 7); string sshKey = ConfigurationManager.AppSettings["VM_ADMIN_KEY"]?.ToString() ?? System.IO.File.ReadAllText(context.FunctionAppDirectory + "/default_key_rsa.pub"); string deploymentTemplate = System.IO.File.ReadAllText(context.FunctionAppDirectory + "/vmDeploy.json"); await DeployNodeAsync(vmName, nodeParams, sshKey, deploymentTemplate, log); var details = new VMDetails(vmName, nodeParams.ResourceGroup, VMState.Creating, nodeParams.Size, nodeParams.Region); await TableStorageHelper.Instance.AddVMEntityAsync(details); return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(details), "application/json")); } else//there's at least one deallocated VM, so start the .First() one { await AzureAPIHelper.StartDeallocatedVMAsync(deallocatedVM); deallocatedVM.State = VMState.Creating; await TableStorageHelper.Instance.ModifyVMDetailsAsync(deallocatedVM); return(req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(deallocatedVM), "application/json")); } }