1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using clempaul.Dreamhost.ResponseData;
  6.  
  7. namespace clempaul.Dreamhost
  8. {
  9.     class PSRequests
  10.     {
  11.         private DreamhostAPI api;
  12.  
  13.         internal PSRequests(DreamhostAPI api)
  14.         {
  15.             this.api = api;
  16.         }
  17.  
  18.         #region dreamhost_ps-add_ps
  19.  
  20.         public void AddPS(string account_id, string type, bool? movedata)
  21.         {
  22.             // Check parameters
  23.  
  24.             if (type == null || type == string.Empty)
  25.             {
  26.                 throw new Exception("Missing type parameter");
  27.             }
  28.             else if (type == "web" && movedata == null)
  29.             {
  30.                 throw new Exception("Missing movedata parameter");
  31.             }
  32.  
  33.             // Build request
  34.  
  35.             List<QueryData> parameters = new List<QueryData>();
  36.  
  37.             if (account_id != null && account_id != string.Empty)
  38.             {
  39.                 parameters.Add(new QueryData("account_id", account_id));
  40.             }
  41.  
  42.             parameters.Add(new QueryData("type", type));
  43.  
  44.             if (movedata != null)
  45.             {
  46.                 parameters.Add(new QueryData("movedata", movedata.Asyesno()));
  47.             }
  48.  
  49.             api.SendCommand("dreamhost_ps-add_ps", parameters);
  50.         }
  51.  
  52.         /*
  53.          * Overloads
  54.          */
  55.  
  56.         public void AddPS(string type)
  57.         {
  58.             this.AddPS(null, type, null);
  59.         }
  60.  
  61.         public void AddPS(string type, bool movedata)
  62.         {
  63.             this.AddPS(null, type, movedata);
  64.         }
  65.  
  66.         public void AddPS(string account_id, string type)
  67.         {
  68.             this.AddPS(account_id, type, null);
  69.         }
  70.  
  71.         #endregion
  72.  
  73.         #region dreamhost_ps-list_pending_ps
  74.  
  75.         public IEnumerable<PendingPS> ListPendingPS()
  76.         {
  77.             XDocument response = api.SendCommand("dreamhost_ps-list_pending_ps");
  78.  
  79.             return from data in response.Element("dreamhost").Elements("data")
  80.                    select new PendingPS
  81.                    {
  82.                        account_id = data.Element("account_id").AsString(),
  83.                        ip = data.Element("ip").AsString(),
  84.                        stamp = data.Element("stamp").AsDateTime(),
  85.                        type = data.Element("type").AsString()
  86.                    };
  87.  
  88.         }
  89.  
  90.         #endregion
  91.  
  92.         #region dreamhost_ps-remove_pending_ps
  93.  
  94.         public IEnumerable<PendingPS> RemovePendingPS()
  95.         {
  96.             XDocument response = api.SendCommand("dreamhost_ps-remove_pending_ps");
  97.  
  98.             return from data in response.Element("dreamhost").Elements("data")
  99.                    select new PendingPS
  100.                    {
  101.                        account_id = data.Element("account_id").AsString(),
  102.                        ip = data.Element("ip").AsString(),
  103.                        stamp = data.Element("stamp").AsDateTime(),
  104.                        type = data.Element("type").AsString()
  105.                    };
  106.         }
  107.  
  108.         #endregion
  109.  
  110.         #region dreamhost_ps-list_ps
  111.  
  112.         public IEnumerable<ActivePS> ListPS()
  113.         {
  114.             XDocument response = api.SendCommand("dreamhost_ps-list_ps");
  115.  
  116.             return from data in response.Element("dreamhost").Elements("data")
  117.                    select new ActivePS
  118.                    {
  119.                        account_id = data.Element("account_id").AsString(),
  120.                        memory_mb = data.Element("memory_mb").AsInt(),
  121.                        ps = data.Element("ps").AsString(),
  122.                        start_date = data.Element("start_date").AsDateTime(),
  123.                        type = data.Element("type").AsString()
  124.                    };
  125.         }
  126.  
  127.         #endregion
  128.  
  129.         #region dreamhost_ps-list_settings
  130.  
  131.         public PSSettings ListSettings(string ps)
  132.         {
  133.             // Check parameters
  134.  
  135.             if (ps == null || ps == string.Empty)
  136.             {
  137.                 throw new Exception("Missing ps parameter");
  138.             }
  139.  
  140.             // Build request
  141.  
  142.             QueryData[] parameters = {
  143.                                          new QueryData("ps", ps)
  144.             };
  145.  
  146.             XDocument response = api.SendCommand("dreamhost_ps-list_settings", parameters);
  147.  
  148.             PSSettings result = new PSSettings();
  149.  
  150.             foreach (XElement data in response.Element("dreamhost").Elements("data"))
  151.             {
  152.                 result.set(data.Element("setting").AsString(), data.Element("value").AsString());
  153.             }
  154.  
  155.             return result;
  156.  
  157.         }
  158.  
  159.         #endregion
  160.  
  161.         #region dreamhost_ps-set_settings
  162.  
  163.         public void SetSettings(string ps, PSSettings Settings)
  164.         {
  165.             // Check parameters
  166.  
  167.             if (ps == null || ps == string.Empty)
  168.             {
  169.                 throw new Exception("Missing ps parameter");
  170.             }
  171.  
  172.             // Build request
  173.  
  174.             List<QueryData> parameters = new List<QueryData>();
  175.  
  176.             Dictionary<string, string> settings = Settings.getValues();
  177.  
  178.             string[] Keys = settings.Keys.ToArray<string>();
  179.             string[] Values = settings.Values.ToArray<string>();
  180.  
  181.             for (int i = 0; i < settings.Count; i++)
  182.             {
  183.                 parameters.Add(new QueryData(Keys[i], Values[i]));
  184.             }
  185.  
  186.             api.SendCommand("dreamhost_ps-set_settings", parameters.ToArray());
  187.         }
  188.  
  189.         #endregion
  190.  
  191.         #region dreamhost_ps-list_size_history
  192.  
  193.         public IEnumerable<PSSize> ListSizeHistory(string ps)
  194.         {
  195.             // Check parameters
  196.  
  197.             if (ps == null || ps == string.Empty)
  198.             {
  199.                 throw new Exception("Missing ps parameter");
  200.             }
  201.  
  202.             // Build request
  203.  
  204.             QueryData[] parameters = {
  205.                                          new QueryData("ps", ps)
  206.             };
  207.  
  208.             XDocument response = api.SendCommand("dreamhost_ps-list_size_history", parameters);
  209.  
  210.             return from data in response.Element("dreamhost").Elements("data")
  211.                    select new PSSize
  212.                    {
  213.                        stamp = data.Element("stamp").AsDateTime(),
  214.                        memory_mb = data.Element("memory_mb").AsInt(),
  215.                        period_seconds = data.Element("period_seconds").AsInt(),
  216.                        period_cost = data.Element("period_cost").AsDouble(),
  217.                        monthly_cost = data.Element("monthly_cost").AsDouble()
  218.                    };
  219.         }
  220.  
  221.         #endregion
  222.  
  223.         #region dreamhost_ps-set_size
  224.  
  225.         public void SetSize(string ps, int size)
  226.         {
  227.             // Check parameters
  228.  
  229.             if (ps == null || ps == string.Empty)
  230.             {
  231.                 throw new Exception("Missing ps parameter");
  232.             }
  233.             else if (size < 150 || size > 4000)
  234.             {
  235.                 throw new Exception("Size out of range");
  236.             }
  237.  
  238.             // Build request
  239.  
  240.             QueryData[] parameters = {
  241.                                          new QueryData("ps", ps),
  242.                                          new QueryData("size", size.ToString())
  243.                                      };
  244.  
  245.             api.SendCommand("dreamhost_ps-set_size", parameters);
  246.         }
  247.  
  248.         #endregion
  249.  
  250.         #region dreamhost_ps-list_reboot_history
  251.  
  252.         public IEnumerable<DateTime> ListRebootHistory(string ps)
  253.         {
  254.             QueryData[] parameters = {
  255.                                          new QueryData("ps", ps)
  256.             };
  257.  
  258.             XDocument response = api.SendCommand("dreamhost_ps-list_reboot_history", parameters);
  259.  
  260.             List<DateTime> result = new List<DateTime>();
  261.  
  262.             foreach (XElement data in response.Element("dreamhost").Elements("data"))
  263.             {
  264.                 result.Add(data.Element("stamp").AsDateTime());
  265.             }
  266.  
  267.             return result;
  268.         }
  269.  
  270.         #endregion
  271.  
  272.         #region dreamhost_ps-reboot
  273.  
  274.         public void Reboot(string ps)
  275.         {
  276.             // Check parameters
  277.  
  278.             if (ps == null || ps == string.Empty)
  279.             {
  280.                 throw new Exception("Missing ps parameter");
  281.             }
  282.  
  283.             // Construct request
  284.  
  285.             QueryData[] parameters = {
  286.                                          new QueryData("ps", ps)
  287.                                      };
  288.  
  289.             api.SendCommand("dreamhost_ps-reboot", parameters);
  290.         }
  291.  
  292.         #endregion
  293.  
  294.         #region dreamhost_ps-list_usage
  295.  
  296.         public IEnumerable<PSUsage> ListUsage(string ps)
  297.         {
  298.             // Check parameters
  299.  
  300.             if (ps == null || ps == string.Empty)
  301.             {
  302.                 throw new Exception("Missing ps parameter");
  303.             }
  304.  
  305.             // Build request
  306.  
  307.             QueryData[] parameters = {
  308.                                          new QueryData("ps", ps)
  309.             };
  310.  
  311.             XDocument response = api.SendCommand("dreamhost_ps-list_ps", parameters);
  312.  
  313.             return from data in response.Element("dreamhost").Elements("data")
  314.                    select new PSUsage
  315.                    {
  316.                        stamp = data.Element("stamp").AsDateTime(),
  317.                        memory_mb = data.Element("memory_mb").AsInt(),
  318.                        load = data.Element("load").AsDouble()
  319.                    };
  320.         }
  321.  
  322.         #endregion
  323.     }
  324. }
  325.