Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Created April 22, 2016 04:13
Show Gist options
  • Save OlegJakushkin/74c72589a1bc041de25158ca8fbc6fe5 to your computer and use it in GitHub Desktop.
Save OlegJakushkin/74c72589a1bc041de25158ca8fbc6fe5 to your computer and use it in GitHub Desktop.
//Install-Package Microsoft.Azure.Management.Resources -Pre
//Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Pre
using System;
using System.IO;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Newtonsoft.Json;
namespace AzureCreateDepl {
internal static class Program {
private static string GetAuthorizationHeader() {
// Details on setup here https://msdn.microsoft.com/en-us/library/dn722415.aspx
var cc = new ClientCredential("{Client Id}", "{Key}");
var context = new AuthenticationContext("{OAUTH 2.0 AUTHORIZATION ENDPOINT}");
var result = context.AcquireTokenAsync("https://management.azure.com/", cc);
if(result == null) {
throw new InvalidOperationException("Failed to obtain the JWT token");
}
var token = result.Result.AccessToken;
return token;
}
private static void CreateTemplateDeployment(
TokenCredentials credential,
string groupName,
string deploymentName,
string subscriptionId) {
Console.WriteLine("Navigate to https://github.com/Azure/azure-quickstart-templates to get deployment templates");
Console.WriteLine("Please enter path to deployment template");
var templatePath = Console.ReadLine();
if(!File.Exists(templatePath)) {
Console.WriteLine("Using Default Path");
templatePath = "template.json";
}
Console.WriteLine("Please enter path to deployment parameters");
var paramsPath = Console.ReadLine();
if (!File.Exists(paramsPath)) {
Console.WriteLine("Using Default Path");
paramsPath = "parameters.json";
}
var templateText = File.ReadAllText(templatePath);
var paramsText = File.ReadAllText(paramsPath);
var deploymentTemplate = JsonConvert.DeserializeObject<dynamic>(templateText);
var deploymentParams = JsonConvert.DeserializeObject<dynamic>(paramsText);
Console.WriteLine("Creating the template deployment...");
var deployment = new Deployment {
Properties = new DeploymentProperties {
Mode = DeploymentMode.Incremental,
Parameters = deploymentParams,
Template = deploymentTemplate
}
};
var resourceManagementClient = new ResourceManagementClient(credential) {
SubscriptionId = subscriptionId
};
var dpResult = resourceManagementClient.Deployments.CreateOrUpdate(
groupName,
deploymentName,
deployment);
Console.WriteLine(dpResult.Properties.ProvisioningState);
}
private static async void DeleteResourceGroup(
ServiceClientCredentials credential,
string groupName) {
Console.WriteLine("Deleting resource group...");
var resourceGroupClient = new ResourceManagementClient(credential);
await resourceGroupClient.ResourceGroups.DeleteAsync(groupName);
}
private static void Main(string[] args) {
var token = GetAuthorizationHeader();
var credential = new TokenCredentials(token);
const string groupName = "test";
const string deploymentName = "AzureCreateDepl";
const string subscriptionId = "{Subscription ID}";
Console.WriteLine("Creating VM from template...");
CreateTemplateDeployment(credential, groupName, deploymentName, subscriptionId);
Console.ReadLine();
Console.WriteLine("Deleting VM and all resources...");
DeleteResourceGroup(credential,groupName);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment