public static Tenant ReturnTenantByName(string url, string User_Token, string TenantName) { try { List<Tenant> CurrentTenant = Tenant.List(url, User_Token); Tenant ReturnTenant = new Tenant(); for (int x = 0; x < CurrentTenant.Count; x++) { if (CurrentTenant[x].name == TenantName) { ReturnTenant = CurrentTenant[x]; break; } else { continue; } } return ReturnTenant; } catch (Exception x) { throw x; } }
public static string PrettyPrint(Tenant printTen) { string ret = string.Empty; ret = "{" + '\n' + "Name: " + printTen.name + '\n' + "Id: " + printTen.id + '\n' + "Description: " + printTen.description + '\n' + "Enabled: " + printTen.enabled + '\n' + "}" + "\n\n"; return ret; }
//=======================================================================================// //-- Tenant - List --// //---------------------------------------------------------------------------------------// //-- --// //-- Purpose : Send a Get request to the Keystone Service and recieve back a --// //-- json string containing the list of tenants --// //-- --// //-- Written By : Tommy Arnold Operating System : Windows 7 --// //-- Date : 11/23/2012 Language : VS 2012 C# --// //=======================================================================================// public static List<Tenant> List(string url, string User_Token) { string ret = string.Empty; try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "/v2.0/tenants"); webRequest.Method = "GET"; webRequest.ServicePoint.Expect100Continue = false; webRequest.Headers.Add("X-Auth-Token", User_Token); webRequest.Timeout = 2000; HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse(); Stream resStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(resStream); ret = reader.ReadToEnd(); //return ret; Tenant newTenant = new Tenant(); List<Tenant> tenantList = new List<Tenant>(); JObject root = JObject.Parse(ret); JArray ServerReturn = (JArray)root["tenants"]; for (int i = 0; i < ServerReturn.Count; i++) { newTenant = new Tenant(); try { newTenant = Tenant.Parse(ServerReturn[i].ToString()); } catch (Exception x) { throw; //throw x; } tenantList.Add(newTenant); } return tenantList; } catch (Exception x) { throw x; } }
public static Tenant Parse(string string_to_parse) { Tenant _tenant = new Tenant(); JObject oServerReturn = JObject.Parse(string_to_parse); String tenantEnabled = oServerReturn["enabled"].ToString(); String tenantDescription = oServerReturn["description"].ToString(); String tenantName = oServerReturn["name"].ToString(); String tenantID = oServerReturn["id"].ToString(); _tenant.name = tenantName; _tenant.id = tenantID; _tenant.enabled = tenantEnabled; _tenant.description = tenantDescription; return _tenant; }
//=======================================================================================// //-- Tenant - Get --// //---------------------------------------------------------------------------------------// //-- --// //-- Purpose : Send a Get request to the Keystone Service and recieve back a --// //-- json string containing the list of tenants --// //-- --// //-- Written By : Tommy Arnold Operating System : Windows 7 --// //-- Date : 11/23/2012 Language : VS 2012 C# --// //=======================================================================================// public static Tenant Get(string url, string User_Token, string TenantID) { string ret = string.Empty; try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "/v2.0/tenants/" + TenantID); webRequest.Method = "GET"; webRequest.ServicePoint.Expect100Continue = false; webRequest.Headers.Add("X-Auth-Token", User_Token); webRequest.Timeout = 2000; HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse(); Stream resStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(resStream); ret = reader.ReadToEnd(); JObject root = JObject.Parse(ret); JToken ServerReturn = root["tenant"]; Tenant newTenant = new Tenant(); newTenant = Tenant.Parse(ServerReturn.ToString()); return newTenant; } catch (Exception x) { throw x; } }