From de38dd0c9cb6863866957f740ebf3c6cf7e9634e Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 7 Apr 2022 08:27:42 +0300 Subject: [PATCH] start stop vm test --- notes/pve-api.txt | 2 ++ .../IPveConnector.cs | 2 ++ .../Services/PveConnector.cs | 31 ++++++++++++++++++- .../Queries/TestQ.cs | 1 - .../Controllers/ConnectorController.cs | 23 +++++++++++++- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/notes/pve-api.txt b/notes/pve-api.txt index 2d63702..af3050a 100644 --- a/notes/pve-api.txt +++ b/notes/pve-api.txt @@ -17,6 +17,8 @@ https://forum.proxmox.com/threads/shutdown-the-server-via-api.98125/ https://github.com/Corsinvest/cv4pve-api-dotnet +API Viewer: https://pve.proxmox.com/pve-docs/api-viewer/index.html + ***REMOVED*** https://***REMOVED******REMOVED***/ diff --git a/src/integration/ProxmoxConnector.Integration.Abstractions/IPveConnector.cs b/src/integration/ProxmoxConnector.Integration.Abstractions/IPveConnector.cs index ec2f48f..7b86e87 100644 --- a/src/integration/ProxmoxConnector.Integration.Abstractions/IPveConnector.cs +++ b/src/integration/ProxmoxConnector.Integration.Abstractions/IPveConnector.cs @@ -6,5 +6,7 @@ namespace ProxmoxConnector.Integration.Abstractions { Task TestWithLogin(); Task TestWithToken(); + Task Start(); + Task Stop(); } } diff --git a/src/integration/ProxmoxConnector.Integration.Corsinvest/Services/PveConnector.cs b/src/integration/ProxmoxConnector.Integration.Corsinvest/Services/PveConnector.cs index d777726..d813c09 100644 --- a/src/integration/ProxmoxConnector.Integration.Corsinvest/Services/PveConnector.cs +++ b/src/integration/ProxmoxConnector.Integration.Corsinvest/Services/PveConnector.cs @@ -10,10 +10,11 @@ namespace ProxmoxConnector.Integration.Corsinvest.Services { public async Task TestWithLogin() { + //var client = new PveClient("***REMOVED***"); ***REMOVED*** ***REMOVED*** var client = new PveClient("***REMOVED***"); ***REMOVED*** if (await client.Login(***REMOVED***)) { - var vm = client.Nodes["pve-test1"].Qemu[100]; + var vm = client.Nodes["pve-lora"].Qemu[100]; //config vm var config = await vm.Config.VmConfig(); @@ -50,5 +51,33 @@ namespace ProxmoxConnector.Integration.Corsinvest.Services var version = await client2.Version.Version(); Console.WriteLine(JsonConvert.SerializeObject(version.Response.data, Formatting.Indented)); } + + public async Task Start() + { + var client = new PveClient("***REMOVED***"); ***REMOVED*** + if (await client.Login(***REMOVED***)) + { + var vm = client.Nodes["pve-lora"].Qemu[100]; + + var startEvent = vm.Status.Start; + var start = await startEvent.VmStart(); + if (start.IsSuccessStatusCode) + Console.WriteLine(JsonConvert.SerializeObject(start.Response, Formatting.Indented)); + } + } + + public async Task Stop() + { + var client = new PveClient("***REMOVED***"); ***REMOVED*** + if (await client.Login(***REMOVED***)) + { + var vm = client.Nodes["pve-lora"].Qemu[100]; + + var stopEvent = vm.Status.Stop; + var stop = await stopEvent.VmStop(); + if (stop.IsSuccessStatusCode) + Console.WriteLine(JsonConvert.SerializeObject(stop.Response, Formatting.Indented)); + } + } } } diff --git a/src/server/ProxmoxConnector.Server.Application/Queries/TestQ.cs b/src/server/ProxmoxConnector.Server.Application/Queries/TestQ.cs index d725f35..81fd2cb 100644 --- a/src/server/ProxmoxConnector.Server.Application/Queries/TestQ.cs +++ b/src/server/ProxmoxConnector.Server.Application/Queries/TestQ.cs @@ -1,6 +1,5 @@ using MediatR; using ProxmoxConnector.Integration.Abstractions; -using ProxmoxConnector.Server.Application.Utils; using System.Threading; using System.Threading.Tasks; diff --git a/src/server/ProxmoxConnector.Server/Controllers/ConnectorController.cs b/src/server/ProxmoxConnector.Server/Controllers/ConnectorController.cs index 1225a49..821d4c9 100644 --- a/src/server/ProxmoxConnector.Server/Controllers/ConnectorController.cs +++ b/src/server/ProxmoxConnector.Server/Controllers/ConnectorController.cs @@ -1,7 +1,9 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using ProxmoxConnector.Integration.Abstractions; using ProxmoxConnector.Server.Application.Queries; +using System; using System.Threading.Tasks; namespace ProxmoxConnector.Server.Controllers @@ -12,10 +14,12 @@ namespace ProxmoxConnector.Server.Controllers public class ConnectorController : ControllerBase { private readonly IMediator _mediator; + private readonly IPveConnector _pveConnector; - public ConnectorController(IMediator mediator) + public ConnectorController(IMediator mediator, IPveConnector pveConnector) { _mediator=mediator; + _pveConnector=pveConnector; } [AllowAnonymous] @@ -25,5 +29,22 @@ namespace ProxmoxConnector.Server.Controllers var result = await _mediator.Send(query); return Ok(result); } + + [AllowAnonymous] + [HttpGet("start")] + public async Task Start() + { + await _pveConnector.Start(); + return Ok(DateTime.Now); + } + + + [AllowAnonymous] + [HttpGet("stop")] + public async Task Stop() + { + await _pveConnector.Stop(); + return Ok(DateTime.Now); + } } }