Example #1
0
        private string TestAdd(string adminUrl, string rolename, string token_id)
        {
            List<Role> rolelist = new List<Role>();
            Role newrole = new Role();
            string ret = String.Empty;

            try
            {
                rolelist = Role.List(adminUrl, token_id);
                ret += "List of roles before Add:" + Environment.NewLine;
                foreach (Role r in rolelist)
                    ret += r.name + " " + r.id + Environment.NewLine;
                try
                {
                    newrole = Role.Add(adminUrl, rolename, token_id);
                    ret += Environment.NewLine + "Added role: TestRole1" + Environment.NewLine + Environment.NewLine;
                    try
                    {
                        rolelist = Role.List(adminUrl, token_id);
                        ret += "List of roles after Add:" + Environment.NewLine;
                        foreach (Role r in rolelist)
                            ret += r.name + " " + r.id + Environment.NewLine;
                        try
                        {
                            Role.Delete(adminUrl, newrole.id, token_id);
                        }
                        catch (Exception x)
                        {
                            return "Post delete failed: " + x.Message;
                        }
                    }
                    catch (Exception x)
                    {
                        return x.Message;
                    }

                }
                catch (Exception x)
                {
                    try
                    {
                        if (rolename == "")
                            return x.Message;
                        else
                            Role.Delete(adminUrl, newrole.id, token_id);
                    }
                    catch (Exception x2)
                    {
                        return "Catch delete failed: " + x2.Message;
                    }
                    return x.Message;
                }
            }
            catch (Exception x)
            {
               return x.Message;
            }
            return ret;
        }
Example #2
0
        private string TestDelete(string adminUrl, string token_id)
        {
            List<Role> rolelist = new List<Role>();
            Role testrole = new Role();
            string ret = String.Empty;

            try
            {
                testrole = Role.Add(adminUrl, "TestRole1", token_id);
                try
                {
                    rolelist = Role.List(adminUrl, token_id);
                    ret += "List of roles before Delete:" + Environment.NewLine;
                    foreach (Role r in rolelist)
                        ret += r.name + " " + r.id + Environment.NewLine;
                    ret += Environment.NewLine + "Attempting to delete TestRole1..." + Environment.NewLine + Environment.NewLine;
                    try
                    {
                        Role.Delete(adminUrl, testrole.id, token_id);
                        try
                        {
                            rolelist = Role.List(adminUrl, token_id);
                            ret += "List of roles after Delete: " + Environment.NewLine;
                            foreach (Role r in rolelist)
                                ret += r.name + " " + r.id + Environment.NewLine;
                        }
                        catch (Exception x)
                        {
                            return "After List failed: " + x.Message;
                        }
                    }
                    catch (Exception x)
                    {
                        return x.Message;
                    }
                }
                catch (Exception x)
                {
                    return "Before List failed: " + x.Message;
                }
            }
            catch (Exception x)
            {
                return x.Message;
            }
            return ret;
        }
        public static Role Parse(string server_return)
        {
            Role return_role = new Role();

            try
            {
                JObject oServerReturn = JObject.Parse(server_return);

                String role_id = oServerReturn["id"].ToString();
                String role_name = oServerReturn["name"].ToString();

                return_role.id = role_id;
                return_role.name = role_name;

                return return_role;

            }
            catch
            {
                throw new BadJson("Role Parse Json command contained incorrect fields.");
            }
        }
        public static List<Role> List(string url, string admin_token)
        {
            string ret = String.Empty;
            List<Role> RoleList = new List<Role>();

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "/v2.0/OS-KSADM/roles");

                webRequest.Method = "GET";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Headers.Add("X-Auth-Token", admin_token);
                webRequest.Timeout = 2000;

                HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                ret = reader.ReadToEnd();
            }
            catch (Exception x)
            {
                throw OpenStackObject.Parse_Error(x);
            }

            try
            {
                JObject root = JObject.Parse(ret);
                JArray ServerReturn = (JArray)root["roles"];

                if (ServerReturn != null)
                {
                    for (int i = 0; i < ServerReturn.Count; i++)
                    {
                        Role newRole = new Role();
                        try
                        {
                            newRole = Role.Parse(ServerReturn[i].ToString());
                        }
                        catch (Exception x)
                        {
                            throw x;
                        }
                        RoleList.Add(newRole);
                    }
                }
            }
            catch (Exception x)
            {
                throw OpenStackObject.Parse_Error(x);
            }
            return RoleList;
        }
        //==============================================================================//
        //
        //                       List Roles for a User on a Tenant
        //
        //
        //==============================================================================//
        public static List<Role> List_Roles(string url, string UserId, string TenantId, string AdminId)
        {
            string List_User_Roles_url = url + "/v2.0/tenants/" + TenantId + "/users/" + UserId + "/roles";
            string ret = string.Empty;
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(List_User_Roles_url);

                webRequest.Method = "GET";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Headers.Add("X-Auth-Token", AdminId);
                webRequest.Timeout = 2000;

                HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                ret = reader.ReadToEnd();

                try
                {
                    JObject oServerReturn = JObject.Parse(ret);
                    JArray rolesArr = (JArray)oServerReturn["roles"];

                    int counter;
                    List<Role> user_role_list = new List<Role>();
                    for (counter = 0; counter < rolesArr.Count(); counter++)
                    {
                        Role user_role = new Role();
                        user_role.name = rolesArr[counter]["name"].ToString();
                        user_role.id = rolesArr[counter]["id"].ToString();
                        user_role_list.Add(user_role);
                    }
                    return (user_role_list);
                }
                catch
                {
                    throw new BadJson("Syntax of argument was incorrect");
                }
                // return ret;

            }
            catch (Exception x)
            {
                throw OpenStackObject.Parse_Error(x);
            }
        }
        //==============================================================================//
        //
        //                       Add role to user
        //
        //
        //==============================================================================//
        public static Role AddRoleToUser(string url, string UserId, string TenantId, string RoleId, string Admin_Token)
        {
            string Add_Role_url = url + "/v2.0/tenants/" + TenantId + "/users/" + UserId +
                                "/roles/OS-KSADM/" + RoleId;
            string ret = string.Empty;
            Role user_role = new Role();

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Add_Role_url);
                webRequest.Headers.Add("X-Auth-Token", Admin_Token);
                webRequest.Method = "Put";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Timeout = 2000;
                webRequest.ContentType = "application/json";

                HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                ret = reader.ReadToEnd();

                try
                {
                    JObject oServerReturn = JObject.Parse(ret);
                    String roleStr = oServerReturn["role"].ToString();
                    JObject oUserString = JObject.Parse(roleStr);
                    String role_name = oUserString["name"].ToString();
                    String role_id = oUserString["id"].ToString();

                    user_role.name = role_name;
                    user_role.id = role_id;

                    return user_role;
                }
                catch
                {
                    throw new BadJson("Syntax of argument was incorrect");
                }
            }
            catch (Exception x)
            {
                throw OpenStackObject.Parse_Error(x);
            }
        }
        public static Role Parse(string server_return)
        {
            Role return_role = new Role();

            JObject oServerReturn = JObject.Parse(server_return);
            String roleStr = oServerReturn["role"].ToString();

            JObject oRoleStr = JObject.Parse(roleStr);
            String role_id = oRoleStr["id"].ToString();
            String role_name = oRoleStr["name"].ToString();

            return_role.id = role_id;
            return_role.name = role_name;

            return return_role;
        }
        public static Role Get(string url, string role_id, string admin_token)
        {
            string ret = string.Empty;
            Role return_role = new Role();

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "/v2.0/OS-KSADM/roles/" + role_id);

                webRequest.Method = "GET";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Headers.Add("X-Auth-Token", admin_token);
                webRequest.Timeout = 2000;

                HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                ret = reader.ReadToEnd();

                return_role = Parse(ret);

                return return_role;
            }
            catch (Exception x)
            {
                return_role.error = x.ToString();
                return return_role;
            }
        }
        public static Role Add(string url, string name, string admin_token)
        {
            Role return_role = new Role();
            string ret = string.Empty;

            StreamWriter requestWriter;

            string postData = "{\"role\": {\"name\":\"" + name + "\"}}";

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url + "/v2.0/OS-KSADM/roles");
                webRequest.Headers.Add("X-Auth-Token", admin_token);
                webRequest.Method = "POST";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Timeout = 2000;
                webRequest.ContentType = "application/json";

                requestWriter = new StreamWriter(webRequest.GetRequestStream());
                requestWriter.Write(postData);
                requestWriter.Close();

                HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
                Stream resStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(resStream);
                ret = reader.ReadToEnd();

                return_role = Parse(ret);

                return return_role;
            }
            catch (Exception x)
            {
                return_role.error = x.ToString();
                return return_role;
            }
        }