Example #1
0
 private void cloneSessionToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if ((client == null) ||
         string.IsNullOrEmpty(client.SessionKey) ||
         (client.Cookies == null)) {
         MessageBox.Show(
             "You must first login or SSO to CloudStack to create a session to clone",
             "No Session",
             MessageBoxButtons.OK);
         return;
     }
     string sessionKey = client.SessionKey;
     CookieCollection cookies = client.Cookies;
     Cookie jsessionId = getSessionCookie(client.Cookies);
     if (jsessionId == null) {
         MessageBox.Show(
             "No session cookie was found - unable to clone session",
             "No Session",
             MessageBoxButtons.OK);
         return;
     }
     Cookie newCookie = new Cookie(jsessionId.Name, jsessionId.Value);
     newCookie.Domain = jsessionId.Domain;
     newCookie.Path = jsessionId.Path;
     client = new Client(client.ApiAddress, sessionKey, newCookie);
     MessageBox.Show(
         "You session has been replaced with a clone using JSESSIONID cookie and sessionKey",
         "Cloned Session",
         MessageBoxButtons.OK);
 }
        /// <summary>
        /// This method waits for the specified aysnc jon to complete and returns the response.
        /// </summary>
        /// <param name="session">Client session</param>
        /// <param name="jobid">Asynchronous task id</param>
        /// <returns>Returns the raw Xml response</returns>
        /// <exception cref="CloudStackException">Errors reported as exceptions.</exception>
        public static XDocument WaitForAsyncJobResult(Client session, string jobid)
        {
            bool done = false;
            string status = string.Empty;
            XDocument respQuery;
            do
            {
                respQuery = QueryAsyncJobResult(session, jobid);

                XElement statusTag = respQuery.Descendants("jobstatus").FirstOrDefault();
                if (statusTag == null)
                {
                    throw new CloudStackException("Response missing jobstatus for jobid " + jobid, "queryAsyncJobResult", respQuery);
                }
                status = statusTag.Value;

                switch (status)
                {
                    case "2":
                        throw new CloudStackException("Async jobid " + jobid + " ended with error", "WaitForIdFromAsyncJob", respQuery);
                    case "1":
                        return respQuery;
                    case "0":
                        System.Threading.Thread.Sleep(session.CloudPollRate);
                        break;
                    default:
                        done = true;
                        break;
                }
            }
            while (!done);

            throw new CloudStackException("Invalid async job status " + status + " get resulting for async job " + jobid, "WaitForIdFromAsyncJob", respQuery);
        }
        /// <summary>
        /// Kick off asynchronous request.
        /// </summary>
        /// <param name="session">Client session</param>
        /// <param name="req">API command</param>
        /// <returns>Asynchronous task job id.</returns>
        public static string StartAsyncJob(Client session, APIRequest req)
        {
            XDocument resp = session.SendRequest(req);

            XElement jobid = resp.Descendants("jobid").FirstOrDefault();

            if (jobid == null || string.IsNullOrEmpty(jobid.Value))
            {
                throw new CloudStackException("Async command failed to return jobid", req.ToString(), resp);
            }
            return jobid.Value;
        }
Example #4
0
 private void ListVirtualMachines(Client client)
 {
     ListVirtualMachinesRequest request = new ListVirtualMachinesRequest();
     //request.Parameters["listall"] = "true";
     ListVirtualMachinesResponse response = client.ListVirtualMachines(request);
     Console.WriteLine("Got {0} virtual machines", response.VirtualMachine.Length);
 }
Example #5
0
        private SessionDefinition GetSession(Account user)
        {
            IDesktopServiceConfiguration config = DesktopServiceConfiguration.Read();
            Client client = new Client(config.CloudStackUri);
            client.Login(user.Name, user.Password, user.Domain, config.HashCloudStackPassword);
            Console.WriteLine("Logged into Cloudstack as {0}", user.Name);
            ListVirtualMachines(client);

            Cookie cookie = FindSessionCookie(client.Cookies);
            if (cookie == null) {
                throw new ApplicationException("Failed to acquire session: Could not find session cookie");
            }
            ShowCookie(cookie);
            Console.WriteLine("Session Key is {0}", client.SessionKey);
            return new SessionDefinition() { SessionCookie = cookie, SessionKey = client.SessionKey };
        }
 /// <summary>
 /// Run an async job and wait for the result
 /// </summary>
 public static XContainer RunAsyncJob(Client session, APIRequest request)
 {
     string jobid = StartAsyncJob(session, request);
     XDocument response = WaitForAsyncJobResult(session, jobid);
     return response.Descendants("jobresult").FirstOrDefault().FirstNode as XContainer;
 }
 /// <summary>
 /// Polls AsyncJob, returns XDocument when job is done.
 /// </summary>
 /// <param name="session">Client connection to CloudStack API</param>
 /// <param name="jobid">Asynchronous job</param>
 /// <returns>Response from async operation.</returns>
 /// <remarks>
 /// Sample result in SampleResponses.QueryAsyncJobResponse
 /// </remarks>
 public static XDocument QueryAsyncJobResult(Client session, string jobid)
 {
     APIRequest req = new SDK.APIRequest("queryAsyncJobResult");
     req.Parameters["jobid"] = jobid;
     return session.SendRequest(req);
 }
        /// <summary>
        ///  Start a job, wait for it to complete and return the resulting resource id
        /// </summary>
        /// <param name="session">the session</param>
        /// <param name="req">the request object</param>
        /// <returns>resource id</returns>
        public static string WaitForResourceIdFromAsyncJob(Client session, APIRequest req)
        {
            string jobid = StartAsyncJob(session, req);
            XDocument asyncResult = WaitForAsyncJobResult(session, jobid);
            XElement id = asyncResult.Descendants("id").FirstOrDefault();

            if (id == null || string.IsNullOrEmpty(id.Value))
            {
                throw new CloudStackException("Failed to obtain new resource id after async job", req.ToString(), asyncResult);
            }
            return id.Value;
        }
Example #9
0
 private void useAPIKeysToolStripMenuItem_Click(object sender, EventArgs e)
 {
     CredentialsForm credentialsForm = new CredentialsForm("API Key", "Secret Key", false, false);
     DialogResult r = credentialsForm.ShowDialog();
     if (r == DialogResult.OK) {
         try {
             client = new Client(new Uri(textBoxUrl.Text), credentialsForm.UserName, credentialsForm.Password);
             WriteToLogBox("Warning: credentials have not be validated!!");
             UpdateCurentUser(credentialsForm.UserName.Substring(0, 10));
         } catch (Exception ex) {
             WriteToLogBox(ex.ToString());
         }
     }
 }
Example #10
0
 private void ssoToolStripMenuItem_Click(object sender, EventArgs e)
 {
     CredentialsForm credentialsForm = new CredentialsForm("User Name", "SSO Key", true, false);
     DialogResult r = credentialsForm.ShowDialog();
     if (r == DialogResult.OK) {
         try {
             client = new Client(new Uri(textBoxUrl.Text));
             client.LoginWithSso(credentialsForm.UserName, credentialsForm.Password, credentialsForm.DomainName);
             UpdateCurentUser(credentialsForm.UserName);
         } catch (Exception ex) {
             WriteToLogBox(ex.ToString());
         }
     }
 }
Example #11
0
 private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (SessionEstablished()) {
         try {
             client.Logout();
             client = null;
             UpdateCurentUser("Not logged in");
         } catch (Exception ex) {
             WriteToLogBox(ex.ToString());
         }
     }
 }