Skip to content

Instantly share code, notes, and snippets.

@jjgriff93
Last active February 4, 2018 06:46
Show Gist options
  • Select an option

  • Save jjgriff93/4100c5a0e6837bf727daf3f3e28fd5e8 to your computer and use it in GitHub Desktop.

Select an option

Save jjgriff93/4100c5a0e6837bf727daf3f3e28fd5e8 to your computer and use it in GitHub Desktop.
Azure .NET Mgmt API - program.cs with vm and storage creation methods
using System;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
namespace FluentAPIApp
{
class Program
{
public static void Main(string[] args)
{
var azure = Azure.Authenticate("Azure-authentication.txt").WithDefaultSubscription();
Console.WriteLine("Welcome to the Azure Wizard. What would you like to do?");
Menu(azure);
}
public static void Menu(IAzure azure)
{
Console.WriteLine("Type 'New VM' to create a new Windows VM or 'New Storage' to create a new storage account, or Exit to close the app.");
string menuChoice = Console.ReadLine().ToUpper();
switch(menuChoice)
.To {
case "NEW VM":
Console.WriteLine("What would you like to call your new VM?");
string VMName = Console.ReadLine();
NewVM(azure, VMName);
break;
case "NEW STORAGE":
Console.WriteLine("What would you like to call your new storage?");
string StorageName = Console.ReadLine(); NewStorage(azure, StorageName);
break;
case "EXIT":
break;
default:
Console.WriteLine("Sorry, didn't recognise that command.");
Menu(azure);
break;
}
}
private static void NewVM(IAzure azure, string VMName)
{
Console.WriteLine("Creating a new VM...");
var windowsVM = azure.VirtualMachines.Define(VMName)
.WithRegion("West Europe")
.WithNewResourceGroup("FluentRG")
.WithNewPrimaryNetwork("10.0.0.0/28")
.WithPrimaryPrivateIPAddressDynamic()
.WithNewPrimaryPublicIPAddress("fluentdns-" + VMName)
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012Datacenter)
.WithAdminUsername("serverAdmin")
.WithAdminPassword("mySuperSecurePassword18")
.WithSize(VirtualMachineSizeTypes.StandardDS3V2)
.Create();
Console.WriteLine("Successfully created your new VM: {0}!", windowsVM.Id);
Menu(azure);
}
private static void NewStorage(IAzure azure, string StorageName)
{
Console.WriteLine("Creating a new storage account...");
var storageAccount = azure.StorageAccounts.Define(StorageName.ToLower())
.WithRegion("West Europe")
.WithNewResourceGroup("FluentRG")
.Create();
Console.WriteLine("Created your new storage account: {0}!", storageAccount.Id);
Menu(azure);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment