Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created October 12, 2016 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmatskas/6d176135f1fed9975ce30ddd778dc0a2 to your computer and use it in GitHub Desktop.
Save cmatskas/6d176135f1fed9975ce30ddd778dc0a2 to your computer and use it in GitHub Desktop.
Fluent Azure Management API example
using System;
using System.Collections.Generic;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.KeyVault.Fluent;
using Microsoft.Azure.Management.KeyVault.Fluent.Models;
using Microsoft.Azure.Management.Resource.Fluent.Authentication;
using Microsoft.Azure.Management.Resource.Fluent.Core;
using Microsoft.Azure.Management.Resource.Fluent.Core.ResourceActions;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var credentialFileLocation = "az-fluent-creds.txt";
var azure = Azure
.Authenticate(credentialFileLocation)
.WithDefaultSubscription();
//CreateAzureVMs(azure);
//CreateAzureKeyVault(azure, credentialFileLocation);
azure.ResourceGroups.Delete("testing");
Console.ReadKey();
}
private static void CreateAzureKeyVault(IAzure azure, string credentialFileLocation)
{
var keyVault = azure.Vaults
.Define("az-fluent-vault")
.WithRegion(Region.UK_WEST)
.WithExistingResourceGroup("testing")
.DefineAccessPolicy()
.ForServicePrincipal(AzureCredentials.FromFile(credentialFileLocation).ClientId)
.AllowKeyPermissions(KeyPermissions.List)
.AllowKeyPermissions(KeyPermissions.Get)
.AllowKeyPermissions(KeyPermissions.Decrypt)
.AllowSecretPermissions(SecretPermissions.Get)
.Attach()
.Create();
Console.Write("Vault created successfully");
}
private static void CreateAzureVMs(IAzure azure, int count=5)
{
var creatableNetwork = azure.Networks
.Define("az-fluent-vnet")
.WithRegion(Region.UK_WEST)
.WithExistingResourceGroup("testing")
.WithAddressSpace("172.16.0.0/16");
var creatableStorageAccount = azure.StorageAccounts
.Define("azfluentstorage")
.WithRegion(Region.UK_WEST)
.WithExistingResourceGroup("testing");
// Prepare a batch of Creatable Virtual Machines definitions
var creatableVirtualMachines = new List<ICreatable<IVirtualMachine>>();
for (int i = 0; i < count; i++)
{
var creatableVirtualMachine = azure.VirtualMachines
.Define("az-flunt-vm-" + i)
.WithRegion(Region.UK_WEST)
.WithExistingResourceGroup("testing")
.WithNewPrimaryNetwork(creatableNetwork)
.WithPrimaryPrivateIpAddressDynamic()
.WithoutPrimaryPublicIpAddress()
.WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.WithRootUserName("captain")
.WithPassword("12NewPA$$w0rd!")
.WithSize(VirtualMachineSizeTypes.StandardA1)
.WithNewStorageAccount(creatableStorageAccount);
creatableVirtualMachines.Add(creatableVirtualMachine);
}
//now go and create the VMs
var virtualMachines = azure.VirtualMachines.Create(creatableVirtualMachines.ToArray());
}
}
}
@JoshuaCarroll
Copy link

But what are the contents of the credentials file? I can't find any documentation on that at all.

@cmatskas
Copy link
Author

@JoshuaCarroll you can find the details on my blog post here: https://cmatskas.com/the-new-azure-management-fluent-api-is-has-landed/
I hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment