Skip to content

Instantly share code, notes, and snippets.

@bayological
Created July 8, 2019 23:29
Show Gist options
  • Save bayological/14b5369fbd4fcca7f97bf6c1c81a95f1 to your computer and use it in GitHub Desktop.
Save bayological/14b5369fbd4fcca7f97bf6c1c81a95f1 to your computer and use it in GitHub Desktop.
Create resource group, app service & website
public static async Task Main(string[] args)
{
string _location = "West Europe";
TokenCredentials credentials = await GetTokenCredentialsAsync().ConfigureAwait(false);
var _webClient = new WebSiteManagementClient(credentials)
{
SubscriptionId = _subscriptionId
};
var _resourceClient = new ResourceManagementClient(credentials)
{
SubscriptionId = _subscriptionId
};
Console.WriteLine("Enter name of site without spaces: ");
var newSiteName = Console.ReadLine();
Console.WriteLine("Enter the name of the app service plan witnout spaces:");
var newAppServicePlanName = Console.ReadLine();
Console.WriteLine("Enter the name of the resource group without spaces:");
var newResourceGroupName = Console.ReadLine();
Console.Clear();
Console.WriteLine("Creating site." + Environment.NewLine + "Please wait.");
//Create resource group
var resourceGroup = await _resourceClient.ResourceGroups
.CreateOrUpdateAsync(newResourceGroupName, new ResourceGroup(_location)).ConfigureAwait(false);
if (resourceGroup == null) return;
//Create app service plan
var appServicePlan = await _webClient.AppServicePlans
.CreateOrUpdateAsync(resourceGroup.Name, newAppServicePlanName, new AppServicePlan(_location)).ConfigureAwait(false);
if (appServicePlan == null) return;
//Create site
Site website = await _webClient.WebApps.CreateOrUpdateAsync(resourceGroup.Name, newSiteName,
new Site(_location, null, newSiteName) { ServerFarmId = appServicePlan.Id }).ConfigureAwait(false);
if (website == null) return;
Console.Clear();
Console.WriteLine($"New {website.Name} was successfully created. And has state {website.State}.");
Console.WriteLine($"Hostname is - {website.DefaultHostName}");
Console.ReadKey();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment