iControl REST API in C# to change virtual server status
Problem this snippet solves:
iControl REST API in C# to show, enable, or disable status of a virtual server
How to use this snippet:
Compile the attached program and execute. Use credential must have admin priviledge. If executing on another machine, copy all files under ..\bin\Debug directory.
**Tested in the following environment: Microsoft Visual Studio Community 2017 Version 15.2 (26430.16) Release VisualStudio.15.Release/15.2.0+26430.16 Microsoft .NET Framework Version 4.6.01055 Installed Version: Community
Visual C# 2017 00369-60000-00001-AA991 Microsoft Visual C# 2017
Code :
//******************************* //Lab F5 Console Application - ltmVipShowEnableDisable.cs //Aim: // 1. Show VIP settings "test-vip-443" in PARTITION 'test' // 2. Enable VIP "test-vip-443" in PARTITION 'test' // 3. Disable VIP "test-vip-443" in PARTITION 'test' //Version 1.0 //Only1MasterBlaster 4/8/2017 //Modifications: // 1. // 2. // 3. // 4. // 5. // ******************************* using System; using System.Text; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using Newtonsoft.Json; // Third Party JSON serializer. Download from Package Manager Cosnole > Install- Package Newtonsoft.Json namespace ltmVipShowEnableDisable { public class vipEnableObject { public string enabled { get; set; } } public class vipDisableObject { public string disabled { get; set; } } class Program { static void Main(string[] args) { //use the httpclient using (var client = new HttpClient()) { // IGNORE Certificate Error ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; // BASE HttpClient connectivity client.BaseAddress = new Uri("https://10.10.10.10/"); //url of our account client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); //ADD Basic Authentication var authByteArray = Encoding.ASCII.GetBytes("user1:password1"); var authCredentials = Convert.ToBase64String(authByteArray); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authCredentials); // string partitionName = "~test~"; string vipName = "test-vip-443"; string vip = partitionName + vipName; string vipPath = "/mgmt/tm/ltm/virtual/" + vip; Console.WriteLine("\nSelect\n1. Show VIP Settings \n2. Enable VIP\n3. Disable VIP\n4. Exit\nChoice="); char choice = Console.ReadKey().KeyChar; switch (choice) { case '1': //HTTP GET to the REST endpoint HttpResponseMessage response = client.GetAsync(vipPath).Result; var value = response.Content.ReadAsStringAsync().Result; //CHECK to see if we have a succesful response if (response.IsSuccessStatusCode) { //set the viewmodel from the content in the response Console.WriteLine("\nShow current settings of VIP {0}\n", vip); Console.WriteLine(value); } else { // HTTP Response status in not 200 OK Console.WriteLine("\nError ... Not Successful\n"); Console.WriteLine(value); } break; case '2': //HTTP PUT to Enable the VIP Console.WriteLine("\nEnabling VIP {0}...\n", vip); vipEnableObject vipEnableState = new vipEnableObject { enabled = "true" }; var jsonEnableContent = new StringContent(JsonConvert.SerializeObject (vipEnableState)); jsonEnableContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue ("application/json"); response = client.PutAsync(vipPath, jsonEnableContent).Result; response.EnsureSuccessStatusCode(); value = response.Content.ReadAsStringAsync().Result; Console.WriteLine("\nNew settings of VIP {0} \n\n {1}\n", vip, value); break; case '3': //HTTP PUT to Disable the VIP Console.WriteLine("\nDisabling VIP {0}...\n", vip); vipDisableObject vipDisableState = new vipDisableObject { disabled = "true" }; var jsonDisableContent = new StringContent(JsonConvert.SerializeObject (vipDisableState)); jsonDisableContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue ("application/json"); response = client.PutAsync(vipPath, jsonDisableContent).Result; response.EnsureSuccessStatusCode(); value = response.Content.ReadAsStringAsync().Result; Console.WriteLine("\nNew settings of VIP {0} \n\n {1}\n", vip, value); break; default: break; } Console.WriteLine("\nTask Completed..."); //choice = Console.ReadKey().KeyChar; } } } }
Published Aug 08, 2017
Version 1.0Only1masterbla1
Cirrus
Joined March 13, 2007
Only1masterbla1
Cirrus
Joined March 13, 2007
- wjw_313334Nimbostratus
Then how to disable/enable status of vitrual server? Just like this: curl -sk -u user:passwd -H 'Content-Type: application/json' -X POST -d ....