none
DNS For Softlayer

    Question

  • It could be that it is rather late for me but I am unsure what I am doing wrong here in getting the module for SoftLayer DNS to work.

    Here is the exception:

    System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
     Parameter name: index 
    at System.Web.UI.ControlCollection.get_Item(Int32 index) 
    at WebsitePanel.Portal.ServersEditService.BindServiceProperties() in C:\projects\websitepanel_72e0a6158d19\WebsitePanel\Sources\WebsitePanel.WebPortal\DesktopModules\WebsitePanel\ServersEditService.ascx.cs:line 143
     at WebsitePanel.Portal.ServersEditService.Page_Load(Object sender, EventArgs e) in C:\projects\websitepanel_72e0a6158d19\WebsitePanel\Sources\WebsitePanel.WebPortal\DesktopModules\WebsitePanel\ServersEditService.ascx.cs:line 67 


     

    Here is my code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using WebsitePanel.Providers;
    using WebsitePanel.Providers.Common;
    using WebsitePanel.Providers.DNS;
    
    namespace WebsitePanel.Providers.DNS.SoftLayer
    {
        public class SoftLayer : HostingServiceProviderBase, IDnsServer
        {
            public int SLRefreshInterval { get { return ProviderSettings.GetInt("SLRefreshInterval"); } }
            public string SLAPIKey { get { return ProviderSettings["SLAPIKey"]; } }
            public string SLUsername { get { return ProviderSettings["SLUsername"]; } }
    
            public bool ApplyDefaultTemplate
            {
                get
                {
                    bool res;
                    bool.TryParse(ProviderSettings["ApplyDefaultTemplate"], out res);
                    return res;
                }
            }
            
            public SoftLayer() { }
    
            private static SoftLayer_Dns_Domain_ResourceRecord ConvertToDomainRecord(DnsRecord dnsRecord, string zoneName)
            {                        
                SoftLayer_Dns_Domain_ResourceRecord domainRecord = new SoftLayer_Dns_Domain_ResourceRecord();
    
                domainRecord.data = dnsRecord.RecordData;
                domainRecord.mxPriority = dnsRecord.MxPriority;
                domainRecord.type = dnsRecord.RecordType.ToString();
                domainRecord.host = dnsRecord.RecordName;
                return domainRecord;
            }
           
            private static DnsRecord ConvertToDnsRecord(SoftLayer_Dns_Domain_ResourceRecord record)
            {                        
                DnsRecord dnsRecord = new DnsRecord();
                
                dnsRecord.RecordName = record.host;            
                dnsRecord.MxPriority = (int)record.mxPriority;
                dnsRecord.RecordData = record.data;
                                        
                switch(record.type)
                {
                    case "A": dnsRecord.RecordType = DnsRecordType.A; break;
                    case "MX": dnsRecord.RecordType = DnsRecordType.MX; break;
                    case "CNAME": dnsRecord.RecordType = DnsRecordType.CNAME; break;
                    case "NS": dnsRecord.RecordType = DnsRecordType.NS; break;                   
                    case "SOA": dnsRecord.RecordType = DnsRecordType.SOA; break;
                    case "TXT": dnsRecord.RecordType = DnsRecordType.TXT; break;                                  
                }
                 
                return dnsRecord;
            }
    
            #region IDnsServer Members
    
            public bool ZoneExists(string zoneName)
            {
                if (string.IsNullOrEmpty(zoneName))
                    throw new ArgumentNullException("zoneName");
    
                SoftLayer_Dns_Domain DomainInfo = GetDomainInformation(zoneName);
                if (DomainInfo != null)
                    return true;
    
                return false;
            }
    
            public string[] GetZones()
            {
                List<SoftLayer_Dns_Domain> Domains = new List<SoftLayer_Dns_Domain>();
                List<string> DomainStrings = new List<string>();
                try
                {
                    Domains = GetAllDomains();
                    Domains.ForEach(delegate(SoftLayer_Dns_Domain Domain)
                    {
                        DomainStrings.Add(Domain.name);
                    });
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not get zones. {0}", ex.Message));  
                }
    
                return DomainStrings.ToArray();                        
            }
    
            public void AddPrimaryZone(string zoneName, string[] secondaryServers)
            {
                if (string.IsNullOrEmpty(zoneName))
                    throw new ArgumentNullException("zoneName");
    
                SoftLayer_Dns_Domain NewZone = new SoftLayer_Dns_Domain();
    
                try
                {
                    NewZone = AddZone(zoneName);
    
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not add primary zone with name {0}. {1}", zoneName, ex.Message));
                }
    
                try
                {
                    if (ApplyDefaultTemplate)
                    {
                        bool result = ApplydefaultTemplate(zoneName);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not apply default domain template for primary zone with name {0}. {1}", zoneName, ex.Message));
                }
            }
    
            public void AddSecondaryZone(string zoneName, string[] masterServers)
            {
                // OMG WTF NO 2nd Zone
            }
    
    
            private bool ApplydefaultTemplate(string zonename)
            {
                //DnsResult res = proxy.ApplyTemplate(login, password, zonename, group);
                
                return true; 
            }
            
            public void DeleteZone(string zoneName)
            {
                try
                {
                    if (string.IsNullOrEmpty(zoneName)) throw new ArgumentNullException("zoneName");
                    SoftLayer_Dns_DomainService service = new SoftLayer_Dns_DomainService();
                    service.deleteObject();
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not delete zone with name {0}. {1}", zoneName, ex.Message));
                }
            }
            
            public void UpdateSoaRecord(string zoneName, string host, string primaryNsServer, string primaryPerson) { }
            
            public DnsRecord[] GetZoneRecords(string zoneName)
            {
                List<DnsRecord> Records = new List<DnsRecord>();
    
                try
                {
                    if (string.IsNullOrEmpty(zoneName))
                        throw new ArgumentNullException("zoneName");
    
                    var Domain = GetDomainInformation(zoneName);
    
                    Domain.resourceRecords.ToList().ForEach(delegate(SoftLayer_Dns_Domain_ResourceRecord record)
                    {
                        Records.Add(ConvertToDnsRecord(record));
                    });
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not get zone records. {0}", ex.Message));
                }
                return Records.ToArray();
            }
    
            public void AddZoneRecord(string zoneName, DnsRecord record)
            {
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                domainInitParameters.id = (int)domainService.getByDomainName(zoneName).First().id;;
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
                // Create a new A record in a domain.
    
                switch (record.RecordType)
                {
                    case DnsRecordType.A:
                        domainService.createARecord(record.RecordName, record.RecordData, SLRefreshInterval);
                        break;
                    case DnsRecordType.MX:
                        domainService.createMxRecord(record.RecordName, record.RecordData, SLRefreshInterval, record.MxPriority);
                        break;
                    case DnsRecordType.CNAME:
                        domainService.createCnameRecord(record.RecordName, record.RecordData, SLRefreshInterval);
                        break;
                    case DnsRecordType.NS:
                        domainService.createNsRecord(record.RecordName, record.RecordData, SLRefreshInterval);
                        break;
                    //case DnsRecordType.SOA: domainService.create break;
                    case DnsRecordType.TXT:
                        domainService.createTxtRecord(record.RecordName, record.RecordData, SLRefreshInterval);
                        break;
                }
                domainService.SoftLayer_Dns_DomainInitParametersValue = null;
    
                SoftLayer_Dns_Domain domain = new SoftLayer_Dns_Domain();
                List<SoftLayer_Dns_Domain_ResourceRecord> recordList = domain.resourceRecords.ToList();
                SoftLayer_Dns_Domain_ResourceRecord ResourceRecord = ConvertToDomainRecord(record, zoneName);
                recordList.Add(ResourceRecord);
                domain.resourceRecords = recordList.ToArray();
    
                SoftLayer_Dns_Domain newDomain = domainService.createObject(domain);
                Console.WriteLine("New domain id: " + newDomain.id.ToString());
            }
    
            public void AddZoneRecords(string zoneName, DnsRecord[] records)
            {
                if (string.IsNullOrEmpty(zoneName))
                    throw new ArgumentNullException("zoneName");
    
                if (records == null)
                    throw new ArgumentNullException("records");
    
                foreach (DnsRecord record in records)
                {
                   AddZoneRecord(zoneName, record);
                }            
            }
    
            public void DeleteZoneRecord(string zoneName, DnsRecord record)
            {
                try
                {
                    if (string.IsNullOrEmpty(zoneName))
                        throw new ArgumentNullException("zoneName");
    
                    if (record == null)
                        throw new ArgumentNullException("record");
    
                    SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                    SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                    objectMask.mask = new SoftLayer_Dns_Domain();
    
                    domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                    SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                    domainInitParameters.id = (int)domainService.getByDomainName(zoneName).First().id; ;
    
                    domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
                    domainService.deleteObject();
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Could not delete zone record. {0}", ex.Message));
                }
                
            }
    
            public void DeleteZoneRecords(string zoneName, DnsRecord[] records)
            {
                if (string.IsNullOrEmpty(zoneName))
                    throw new ArgumentNullException("zoneName");
    
                if (records == null)
                    throw new ArgumentNullException("records");
    
                foreach (DnsRecord record in records)
                {
                    DeleteZoneRecord(zoneName, record);
                }
            }
            
            #endregion
    
            public override void DeleteServiceItems(ServiceProviderItem[] items)
            {
                if (items == null) throw new ArgumentNullException("items");
                foreach (ServiceProviderItem item in items) { DeleteZone(item.Name);     }
                base.DeleteServiceItems(items);
            }
    
            public override bool IsInstalled() { return true; }
    
    
            //protected virtual void UpdateZone(string zoneName, List<DnsRecord> records)
            //{
            //    UpdateZone(zoneName, records, null);
            //}
    
            //protected virtual void UpdateZone(string zoneName, List<DnsRecord> records, string[] masterServers)
            //{
             
            //}
    
            private SoftLayer_Dns_Domain GetDomainInformation(int DomainID)
            {
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                domainInitParameters.id = DomainID;// 772299;
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
    
                SoftLayer_Dns_Domain DomainInfo = domainService.getObject();
                DomainInfo.resourceRecords = domainService.getResourceRecords();
    
                return DomainInfo;
            }
    
            private SoftLayer_Dns_Domain GetDomainInformation(string DomainName)
            {
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                domainInitParameters.id = (int)domainService.getByDomainName(DomainName).First().id;// 772299;
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
    
                SoftLayer_Dns_Domain DomainInfo = domainService.getObject();
                DomainInfo.resourceRecords = domainService.getResourceRecords();
    
                return DomainInfo;
            }
    
            private SoftLayer_Dns_Domain AddZone(string zoneName)
            {
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                domainInitParameters.id = (int)domainService.getByDomainName(zoneName).First().id;;
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
                // Create a new A record in a domain.
                SoftLayer_Dns_Domain_ResourceRecord_AType newRecord = domainService.createARecord("myhost", "127.0.0.1", 86400);
    
                Console.WriteLine("New A record id: " + newRecord.id.ToString());
    
    
                // Create a new domain record.
                //
                // This requires a null init parameter and a single SoftLayer_Dns_Domain
                // object defined.
                domainService.SoftLayer_Dns_DomainInitParametersValue = null;
    
                SoftLayer_Dns_Domain domain = new SoftLayer_Dns_Domain();
                SoftLayer_Dns_Domain_ResourceRecord_AType[] domainResourceRecords = new SoftLayer_Dns_Domain_ResourceRecord_AType[1];
                domainResourceRecords[0].host = "@";
                domainResourceRecords[0].data = "127.0.0.1";
                domainResourceRecords[0].type = "a";
                domain.name = "example.org";
                domain.resourceRecords = domainResourceRecords;
    
                SoftLayer_Dns_Domain newDomain = domainService.createObject(domain);
                Console.WriteLine("New domain id: " + newDomain.id.ToString());
    
                return newDomain;
            }
    
            private SoftLayer_Dns_Domain ReplaceObject(int DomainID, SoftLayer_Dns_Domain New)
            {
                SoftLayer_Dns_Domain Updated = new SoftLayer_Dns_Domain();
    
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                domainInitParameters.id = DomainID;
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
    
                bool Status = domainService.deleteObject();
                if (!Status)
                {
                    throw new Exception(string.Format("Unable to delete and replace the domain id of {0}", DomainID));
                }
    
                #region Add updated entry
    
                domainService = ConnectToDomainService();
                objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
    
                // Create a new A record in a domain.
                SoftLayer_Dns_Domain_ResourceRecord_AType newRecord = domainService.createARecord("myhost", "127.0.0.1", 86400);
                
                foreach (var entry in New.resourceRecords)
                {
                    switch (entry.type)
                    {
                        case "A": domainService.createARecord(entry.host, entry.data, (int)entry.ttl); break;
                        case "MX": domainService.createMxRecord(entry.host, entry.data, (int)entry.ttl, (int)entry.mxPriority); break;
                        case "CNAME": domainService.createCnameRecord(entry.host, entry.data, (int)entry.ttl); break;
                        case "NS": domainService.createNsRecord(entry.host, entry.data, (int)entry.ttl); break;
                        //case "SOA": domainService.create break;
                        case "TXT": domainService.createTxtRecord(entry.host, entry.data, (int)entry.ttl); break;
                    }
                }
    
                domainService.SoftLayer_Dns_DomainInitParametersValue = null;
    
                SoftLayer_Dns_Domain domain = new SoftLayer_Dns_Domain();
                domain.resourceRecords = New.resourceRecords;
    
                SoftLayer_Dns_Domain newDomain = domainService.createObject(domain);
                Console.WriteLine("New domain id: " + newDomain.id.ToString());
                #endregion
    
    
                return Updated;
            }
    
            private List<SoftLayer_Dns_Domain> GetAllDomains()
            {
                List<SoftLayer_Dns_Domain> Domains = new List<SoftLayer_Dns_Domain>();
    
                string[] TLDs = new string[] { ".com", ".net", ".org", ".co.uk" };
    
    
                SoftLayer_Dns_DomainService domainService = ConnectToDomainService();
                SoftLayer_Dns_DomainObjectMask objectMask = new SoftLayer_Dns_DomainObjectMask();
                objectMask.mask = new SoftLayer_Dns_Domain();
    
                domainService.SoftLayer_Dns_DomainObjectMaskValue = objectMask;
                foreach (string TLD in TLDs)
                {
                    foreach (SoftLayer_Dns_Domain domain in domainService.getByDomainName(TLD))
                    {
                        SoftLayer_Dns_DomainInitParameters domainInitParameters = new SoftLayer_Dns_DomainInitParameters();
                        domainInitParameters.id = (int)domain.id;
    
                        domainService.SoftLayer_Dns_DomainInitParametersValue = domainInitParameters;
    
                        SoftLayer_Dns_Domain DomainInfo = domainService.getObject();
                        DomainInfo.resourceRecords = domainService.getResourceRecords();
    
                        Domains.Add(DomainInfo);
                    }
                }
                return Domains;
            }
    
            private SoftLayer_Dns_DomainService ConnectToDomainService()
            {
                SoftLayer_Dns_DomainService domainService = new SoftLayer_Dns_DomainService();
    
                authenticate authenticate = new authenticate();
                authenticate.username = SLUsername;
                authenticate.apiKey = SLAPIKey;
    
                domainService.authenticateValue = authenticate;
    
                return domainService;
            }
    
        }
    }
    


     

    Monday, January 30, 2012 8:28 AM

Answers

  • Hi,

    According to the error information, you encounter the error when add the softlayer dns service to server serivces settings? Since this is a generic out of range error, we cannot determine what could be the cause without debug information. Like which method is causing this error, which parameter is incorrect. So it will be direct help if there has dev debug into the code. And here's softlayer api information I could find which may help you:

    SoftLayer API Overview
    http://sldn.softlayer.com/article/SoftLayer-API-Overview

    Hope this helps, thanks.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com. Microsoft One Code Framework
    Thursday, February 02, 2012 9:20 AM
    Moderator

All replies

  • I am not sure if I am missing some sort of field that the ServersEditService control is looking for.  I can set up a local dev later to debug but I was hoping someone has see this error before.
    Monday, January 30, 2012 9:36 PM
  • Hi,

    According to the error information, you encounter the error when add the softlayer dns service to server serivces settings? Since this is a generic out of range error, we cannot determine what could be the cause without debug information. Like which method is causing this error, which parameter is incorrect. So it will be direct help if there has dev debug into the code. And here's softlayer api information I could find which may help you:

    SoftLayer API Overview
    http://sldn.softlayer.com/article/SoftLayer-API-Overview

    Hope this helps, thanks.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com. Microsoft One Code Framework
    Thursday, February 02, 2012 9:20 AM
    Moderator