/// <summary> /// Flow to create a new SwapGroup /// </summary> /// <returns></returns> private static async Task CreateSwapGroup() { Console.WriteLine("Creating new Swap Group"); //Get GroupName Console.WriteLine("Enter new group name:"); var groupName = Console.ReadLine(); SwapGroup newGroup = new SwapGroup(); newGroup.Name = groupName; var client = new WebSiteManagementClient(credentials); client.SubscriptionId = subscriptionId; var resourceClient = new ResourceManagementClient(credentials); resourceClient.SubscriptionId = subscriptionId; var resourceGroups = resourceClient.ResourceGroups.List(); var resourceGroepMenu = new TypedMenu <ResourceGroup>(resourceGroups.ToList(), "Choose a resource group", x => x.Name); var resourceGroup = resourceGroepMenu.Display(); var allWebsites = client.WebApps.ListByResourceGroup(resourceGroup.Name).ToList(); AskForSlot(newGroup, resourceGroup, client, allWebsites); //Save group //Write JSON to file _groupFileService.SaveGroupToFile(newGroup); Console.WriteLine("Saved SwapSlots Group {0} with {1} SwapSlots", newGroup.Name, newGroup.SwapSlots.Count); }
/// <summary> /// Read a swap.json file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public SwapGroup ReadGroupFile(string fileName) { using (StreamReader r = new StreamReader(fileName)) { string json = r.ReadToEnd(); SwapGroup result = JsonConvert.DeserializeObject <SwapGroup>(json); return(result); } }
/// <summary> /// Ask for an slot to add to a group /// </summary> /// <param name="newGroup"></param> /// <param name="allEnv"></param> private static void AskForSlot(SwapGroup newGroup, ResourceGroup resourceGroup, WebSiteManagementClient client, List <Site> allWebsites) { Console.WriteLine("Pick a website:"); var filteredWebsites = allWebsites.ToList().Where(x => !newGroup.SwapSlots.Select(s => s.Website).Contains(x.Name)).ToList(); var websiteMenu = new TypedMenu <Site>(filteredWebsites, "Choose a website", x => x.Name); var website = websiteMenu.Display(); var websiteSlots = client.WebApps.ListSlots(resourceGroup.Name, website.Name); if (!websiteSlots.Any()) { Console.WriteLine("No slots found"); } else if (websiteSlots.Count() == 1) { var slot = websiteSlots.First(); Console.WriteLine("Found 1 slot, using: " + slot.Name); var name = slot.Name.Replace(slot.RepositorySiteName + "/", string.Empty); newGroup.SwapSlots.Add(new SlotConfig() { Name = name, ResourceGroup = resourceGroup.Name, Website = website.Name }); } else { var slotMenu = new TypedMenu <Site>(websiteSlots.ToList(), "Choose a slot", x => x.Name); var slot = slotMenu.Display(); var name = slot.Name.Replace(slot.RepositorySiteName + "/", string.Empty); newGroup.SwapSlots.Add(new SlotConfig() { Name = name, ResourceGroup = resourceGroup.Name, Website = website.Name }); } Console.WriteLine("Add another slot? (Y/N) (default Y)"); var answer = Console.ReadLine(); if (answer.Equals("N", StringComparison.InvariantCultureIgnoreCase)) { return; } else { AskForSlot(newGroup, resourceGroup, client, allWebsites); } }
/// <summary> /// Save a swap.json file /// </summary> /// <param name="newGroup"></param> public void SaveGroupToFile(SwapGroup newGroup) { string fileName = string.Format("{0}.swap.json", newGroup.Name); using (FileStream fs = File.Open(fileName, FileMode.CreateNew)) using (StreamWriter sw = new StreamWriter(fs)) using (JsonWriter jw = new JsonTextWriter(sw)) { jw.Formatting = Formatting.Indented; JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(jw, newGroup); } }