Last active
November 25, 2021 15:09
-
-
Save FabianoCampos/dd5357f5e312247c7a2f86aa61cb87bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LigarDesligarVM | |
{ | |
/* informações de token e o estado correto devem ser tratados antes*/ | |
public void Ligar(TokenCredentials credentialstring, string subscriptionId, string resourceGroup, string nomeDaVM) | |
{ | |
var computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId }; | |
computeManagementClient.VirtualMachines.Start(resourceGroup, nomeDaVM); | |
} | |
public void Desligar(TokenCredentials credentialstring, subscriptionId, string resourceGroup, string nomeDaVM) | |
{ | |
computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId }; | |
computeManagementClient.VirtualMachines.DeallocateAsync(resourceGroup, nomeDaVM); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Informações | |
* https://docs.microsoft.com/pt-br/azure/active-directory/develop/howto-create-service-principal-portal | |
*/ | |
public class VirtualMachine | |
{ | |
public bool Ativa { get; set; } | |
public bool Code { get; set; } | |
public bool DisplayStatus { get; set; } | |
} | |
public class VerificarEstadoVM | |
{ | |
public VirtualMachine Verificar(string resourceGroup, string nomeDaVM) | |
{ | |
var subscriptionId = "00000000-0000-0000-0000-000000000000"; | |
var token = GetToken(entrada.NomeDaVM); | |
var credential = new TokenCredentials(token.Result.AccessToken); | |
var computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId }; | |
return GetEstado(credential, subscriptionId, resourceGroup, nomeDaVM); | |
} | |
private Task<AuthenticationResult> GetToken() | |
{ | |
var cc = new ClientCredential("00000000-0000-0000-0000-000000000000", "senhaficaaqui"); | |
var context = new AuthenticationContext("https://login.windows.net/00000000-0000-0000-0000-000000000000"); | |
var token = context.AcquireTokenAsync("https://management.azure.com/", cc); | |
return token; | |
} | |
private VirtualMachine GetEstado(TokenCredentials credential, string subscriptionId, string resourceGroup, string nomeDaVM) | |
{ | |
var computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId }; | |
var vmResult = computeManagementClient.VirtualMachines.Get(resourceGroup, nomeDaVM, InstanceViewTypes.InstanceView); | |
var statusVM = vmResult.InstanceView.Statuses.Where(x => x.Code.Contains("PowerState")).FirstOrDefault(); | |
var vm = new VirtualMachine(); | |
if (statusVM == null) | |
{ | |
statusVM = vmResult.InstanceView.Statuses.Where(x => x.Code.Contains("ProvisioningState")).FirstOrDefault(); | |
} | |
var ativa = statusVM.Code == "PowerState/running" || "PowerState/starting"; | |
return new VirtualMachine() | |
{ | |
Ativa = ativa, | |
Code = statusVM.Code, | |
DisplayStatus = statusVM.DisplayStatus | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment