Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created Oct 12, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@JoshuaCarroll JoshuaCarroll commented Dec 12, 2016

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

@cmatskas

This comment has been minimized.

Copy link
Owner Author

@cmatskas cmatskas commented Dec 20, 2016

@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