Example #1
0
 protected void Button4_Click(object sender, EventArgs e)
 {
     // invalid Admin Token
     Service serv = new Service();
     try
     {
         serv = Service.Get(AdminURL, ServiceID, "wrong_admin_token");
         Text4.Text = "ID: " + serv.id + ", Name: " + serv.name + ", Type: " + serv.type + ", Description: " + serv.description + Environment.NewLine;
     }
     catch (Exception x)
     {
         Text4.Text = x.Message;
     }
 }
Example #2
0
 protected void Button2_Click(object sender, EventArgs e)
 {
     // invalid URL
     Service serv = new Service();
     try
     {
         serv = Service.Get("http://wrong.cs.trinity.edu", ServiceID, AdminToken);
         Text2.Text = "ID: " + serv.id + ", Name: " + serv.name + ", Type: " + serv.type + ", Description: " + serv.description + Environment.NewLine;
     }
     catch (Exception x)
     {
         Text2.Text = x.Message;
     }
 }
Example #3
0
        string ServiceID = "2cdf772f245140be893bcf4f16f2ba9f"; // service ID for Cinder

        #endregion Fields

        #region Methods

        protected void Button1_Click(object sender, EventArgs e)
        {
            // valid parameters
            Service serv = new Service();
            try
            {
                serv = Service.Get(AdminURL, ServiceID, AdminToken);
                Text1.Text = "ID: " + serv.id + ", Name: " + serv.name + ", Type: " + serv.type + ", Description: " + serv.description + Environment.NewLine;
            }
            catch (Exception x)
            {
                Text1.Text = x.Message;
            }
        }
 protected string AddService()
 {
     Service serv = new Service();
     try
     {
         serv = Service.Create(AdminURL, "Test2_Name", "Test2_Type", "Test2_Description", AdminToken);
     }
     catch (Exception x)
     {
         txtTest1.Text = x.Message;
     }
     txtTest1.Text += "ID: " + serv.id + ", Name: " + serv.name + ", Type: " + serv.type + ", Description: " + serv.description + Environment.NewLine;
     return serv.id;
 }
 protected void TestCase1(object sender, EventArgs e)
 {
     // correct URL and AdminToken
     txtTest1.Text = "";
     Service serv = new Service();
     try
     {
         serv = Service.Create(AdminURL, "Test1_Name", "Test1_Type", "Test1_Description", AdminToken);
     }
     catch (Exception x)
     {
         txtTest1.Text = x.Message;
     }
     txtTest1.Text += "ID: " + serv.id + ", Name: " + serv.name + ", Type: " + serv.type + ", Description: " + serv.description + Environment.NewLine;
 }
        public static Service Parse1(string server_return)
        {
            // server_return does not have OS-KSADM:service

            Service return_service = new Service();

            try
            {
                JObject oServerReturn = JObject.Parse(server_return);
                // String serviceStr = oServerReturn["OS-KSADM:service"].ToString();

                // JObject oServiceStr = JObject.Parse(serviceStr);
                String service_id = oServerReturn["id"].ToString();
                String service_name = oServerReturn["name"].ToString();
                String service_type = oServerReturn["type"].ToString();
                String service_description = oServerReturn["description"].ToString();

                return_service.id = service_id;
                return_service.name = service_name;
                return_service.type = service_type;
                return_service.description = service_description;

                return return_service;
            }
            catch
            {
                throw new BadJson("Service Parse command contained incorrect fields.");
            }
        }
        //------------------------------------------------
        // Service - List
        // List all services in service catalog
        // GET
        /// <summary>
        /// Lists all the services associated with the admin token with a GET call.
        /// </summary>
        /// <param name=”url”> The url is the OpenStack server address. </param>
        /// <param name=”admin_token”> The admin’s token ID. </param>
        /// <returns> If successful, returns the response from Keystone, 
        /// which is a json string that lists all the services associated 
        /// with the admin’s token as well as the ID, type, name, and description 
        /// of each of the services. If unsuccessful, returns an error message as a string. </returns>
        /// Function approved by: David Nikaido & Kendall Bailey
        //------------------------------------------------
        public static List<Service> List(string url, string admin_token)
        {
            string ret = "";
            List<Service> ServiceList = new List<Service>();

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

                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();
                /*
                JObject oServerReturn = JObject.Parse(ret);
                String usersStr = oServerReturn["users"].ToString();

                return usersStr;
                 */
                //return ret;

            }
            catch (Exception x)
            {
                //return (x.ToString());
                throw OpenStackObject.Parse_Error(x);
            }

            try
            {
                JObject root = JObject.Parse(ret);
                JArray ServerReturn = (JArray)root["OS-KSADM:services"];

                if (ServerReturn != null)
                {
                    for (int i = 0; i < ServerReturn.Count; i++)
                    {
                        Service newService = new Service();
                        try
                        {
                            newService = Service.Parse1(ServerReturn[i].ToString());
                        }
                        catch (Exception x)
                        {
                            throw new BadJson("Service List failed to parse correctly, but was retrieved.");
                        }
                        ServiceList.Add(newService);
                    }
                }
            }
            catch (Exception x)
            {
                throw OpenStackObject.Parse_Error(x);
            }
            return ServiceList;
        }
        //------------------------------------------------
        // Service - Get
        // Display service from service catalog
        // GET
        /// <summary>
        /// Gets the user-specified service with a GET call.
        /// </summary>
        /// <param name=”url”> The url is the OpenStack server address. </param>
        /// <param name=”service_id”> The id of the service to be retrieved. </param>
        /// <param name=”admin_token”> The admin’s token ID. </param>
        /// <returns> If successful, returns the response from Keystone, 
        /// which is a json string that includes the ID, type, name, and 
        /// description of the user-specified service. If unsuccessful, 
        /// returns an error message as a string. </returns>
        /// Function approved by: David Nikaido & Kendall Bailey
        //------------------------------------------------
        public static Service Get(string url, string service_id, string admin_token)
        {
            Service return_service = new Service();
            string ret = "";

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

                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_user = User.Parse(ret);

                return return_user;*/
                //return ret;
            }
            catch (Exception x)
            {   /*
                return_user.enabled = x.ToString();
                return return_user;*/
                //return "failed";
                throw OpenStackObject.Parse_Error(x);
            }
            try
            {
                return Service.Parse(ret);
            }
            catch
            {
                throw new BadJson("Service failed to parse correctly, but was retrieved.");
            }
        }
        //------------------------------------------------
        // Service - Get
        // Display service from service catalog
        // GET
        //------------------------------------------------
        public static string Get(string url, string service_id, string admin_token)
        {
            Service return_service = new Service();

            string ret = "";
            try
            {
                string get_url = url + "/v2.0/OS-KSADM/services/" + service_id;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(get_url);

                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_user = User.Parse(ret);

                return return_user;*/
                return ret;
            }
            catch (Exception x)
            {   /*
                return_user.enabled = x.ToString();
                return return_user;*/
                return "failed";
            }
        }
Example #10
0
        // from https://github.com/openstack/python-keystoneclient/blob/master/keystoneclient/v2_0/services.py
        // firefox plugin RESTClient
        // rackspace admin url http://198.61.199.47:35357/v2.0/

        //------------------------------------------------
        // Service - Create
        // Add service to service catalog
        // Method: POST
        //------------------------------------------------
        public static string Create(string url, string name, string service_type, string description,
                                            string admin_token)
        {
            // url: "/OS-KSADM/services/%s"
            // post: "OS-KSADM:services"
            /* 
             * body = {"OS-KSADM:service": {'name': name,
                                     'type': service_type,
                                     'description': description}}
               return self._create("/OS-KSADM/services", body, "OS-KSADM:service")
            */

            Service return_user = new Service();
            string ret = "";
            StreamWriter requestWriter;

            /*String post_data = "{" + "\"OS-KSADM:service\": { " +
                                     "\'name\': \"" + name + "\", " +
                                     "\'type\': \"" + service_type + "\", " +
                                     "\'description\': \"" + description +
                                     "}}";*/

            String post_data = "{" + "\"OS-KSADM:service\": { " +
                                     "\'type\': \"" + service_type + "\", " +
                                     "\'description\': \"" + description + "\", " +
                                     "\'name\': \"" + name +
                                     "}}";

            try
            {
                string create_url = url + "/v2.0/OS-KSADM/services/";
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(create_url);
                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(post_data);
                requestWriter.Close();

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

                /*
                //parse server return ------------------------------------
                JObject oServerReturn = JObject.Parse(ret);
                String userStr = oServerReturn["user"].ToString();

                JObject oUserString = JObject.Parse(userStr);
                String user_name = oUserString["name"].ToString();
                String user_email = oUserString["email"].ToString();
                String user_tenantid = oUserString["tenantId"].ToString();
                String user_id = oUserString["id"].ToString();
                String user_enabled = oUserString["enabled"].ToString();
                String user_password = oUserString["password"].ToString();

                return_user.name = user_name;
                return_user.email = user_email;
                return_user.tenantid = user_tenantid;
                return_user.id = user_id;
                return_user.enabled = user_enabled;
                return_user.password = user_password;
                //--------------------------------------------------------
             
                return (return_user);
                */
                return ret;

            }
            catch (Exception x)
            {   /*
                return_user.name = x.ToString();
                return (return_user); */
                return x.ToString();
            }
        }