示例#1
0
        public string DecomServer(string URL, string Username, string Password, string Name, ServiceNowDecom Decom)
        {
            XmlDocument doc   = new XmlDocument();
            string      error = GetServer(URL, Username, Password, Name);

            try
            {
                doc.LoadXml(error);

                error = "";
                try
                {
                    XmlNodeList nodes = doc.SelectNodes("/root/result/item");
                    if (nodes.Count > 0)
                    {
                        Log   oLog   = new Log(0, dsn);
                        Asset oAsset = new Asset(0, dsnAsset);

                        ServiceNowUpdate update = new ServiceNowUpdate();
                        update.u_action = "update";
                        update.u_name   = nodes[0]["name"].InnerText;
                        oLog.AddEvent(update.u_name, "ServiceNow", "Updating Record...", LoggingType.Debug);
                        if (Decom == ServiceNowDecom.Decommission)
                        {
                            oLog.AddEvent(update.u_name, "ServiceNow", "Decommission", LoggingType.Debug);
                            int OperationalState = 0;
                            if (Int32.TryParse(nodes[0]["u_desired_operational_state"].InnerText, out OperationalState) == true)
                            {
                                oLog.AddEvent(update.u_name, "ServiceNow", "Desired Operational State (from Service Now) = " + OperationalState.ToString(), LoggingType.Debug);
                                oAsset.UpdateDecommissionServiceNowOperationalStatus(update.u_name, OperationalState);
                            }
                            update.u_desired_operational_state = ServiceNowDesiredOperationalStatus.PendingDecommission;
                        }
                        else if (Decom == ServiceNowDecom.Destroy)
                        {
                            oLog.AddEvent(update.u_name, "ServiceNow", "Destroy", LoggingType.Debug);
                            update.u_install_status            = ServiceNowInstallStatus.Retired;
                            update.u_desired_operational_state = ServiceNowDesiredOperationalStatus.Decommissioned;
                        }
                        else if (Decom == ServiceNowDecom.Recommission)
                        {
                            oLog.AddEvent(update.u_name, "ServiceNow", "Recommission", LoggingType.Debug);
                            DataSet dsDecom = oAsset.GetDecommission(update.u_name);
                            if (dsDecom.Tables[0].Rows.Count > 0)
                            {
                                int OperationalState = 0;
                                Int32.TryParse(dsDecom.Tables[0].Rows[0]["service_now_operational_status"].ToString(), out OperationalState);
                                oLog.AddEvent(update.u_name, "ServiceNow", "Desired Operational State (from ClearView DB) = " + OperationalState.ToString(), LoggingType.Debug);
                                if (OperationalState > 0)
                                {
                                    update.u_desired_operational_state = OperationalState.ToString();
                                }
                            }
                        }

                        string json = JSON.SerializeJSON <ServiceNowUpdate>(update);
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri(URL);
                            var byteArray = Encoding.ASCII.GetBytes(Username + ":" + Password);    // _clearview_user:_clearview_user
                            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                            var message = new HttpRequestMessage
                            {
                                Content = new StringContent(json, Encoding.UTF8, "application/json")
                            };
                            message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                            HttpResponseMessage post = client.PostAsync("/api/now/v1/import/imp_server", message.Content).Result;
                            var content = post.Content.ReadAsStringAsync();
                            oLog.AddEvent(update.u_name, "ServiceNow", content.Result, LoggingType.Debug);
                            if (post.IsSuccessStatusCode)
                            {
                                ServiceNowResponse response = JSON.DeserializeJSON <ServiceNowResponse>(content.Result);
                                foreach (ServiceNowResponseResult result in response.result)
                                {
                                    string status = result.status.Trim().ToLower();
                                    if (status != "updated" && status != "ignored")
                                    {
                                        error = "There was a problem updating the record in Service Now - status = " + status;
                                        break;
                                    }
                                }
                            }
                            else if (String.IsNullOrWhiteSpace(content.Result) == false)
                            {
                                error = content.Result;
                            }
                            else
                            {
                                error = post.ReasonPhrase + " (" + (int)post.StatusCode + ")";
                            }
                        }
                    }
                    else if (nodes.Count == 0)
                    {
                        // No error message since it doesn't exist.
                    }
                    else
                    {
                        error = "More than one record was returned (" + nodes.Count.ToString() + ")";
                    }
                }
                catch (Exception ex)
                {
                    error += ex.Message;
                    while (ex.InnerException != null)
                    {
                        ex     = ex.InnerException;
                        error += " ~ " + ex.Message;
                    }
                }
                return(error);
            }
            catch
            {
                return(error);
            }
        }
        public string CreateServiceNowServer(string URL, string Domain, string IP, string Manufacturer, string Mnemonic, string Model, string Name, string ZeusBuildTypeForOperatingSystem, string Serial, string ServiceNowClass, string ServiceNowEnvironment, bool IsVirtual, DateTime Installed, string ServiceNowLocation, string BuildingCodeForDataCenter, string Username, string Password)
        {
            string error = "";

            try
            {
                string dsn  = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DSN"]].ConnectionString;
                Log    oLog = new Log(0, dsn);
                oLog.AddEvent(Name, "ServiceNow", "Adding Record...", LoggingType.Debug);

                ServiceNowInsert insert = new ServiceNowInsert();
                insert.u_discovery_source    = "ClearView";
                insert.u_fqdn                = Name + "." + Domain;
                insert.u_install_status      = ((ServiceNowEnvironment == ServiceNowEnvironments.Production || ServiceNowEnvironment == ServiceNowEnvironments.DR) ? ServiceNowInstallStatus.PendingInstall : ServiceNowInstallStatus.Installed);
                insert.u_ip_address          = IP;
                insert.u_manufacturer        = Manufacturer;
                insert.u_associated_mnemonic = Mnemonic;
                insert.u_model_id            = Model;
                insert.u_name                = Name;
                insert.u_os             = ZeusBuildTypeForOperatingSystem;
                insert.u_serial_number  = Serial;
                insert.u_sys_class_name = ServiceNowClass;
                insert.u_environment    = ServiceNowEnvironment;
                insert.u_virtual        = (IsVirtual ? "true" : "false");
                insert.u_dns_domain     = Domain;
                insert.u_install_date   = Installed.ToString("yyyy-MM-dd hh:mm:ss");
                insert.u_company        = "PNC";
                insert.u_location       = ServiceNowLocation;
                insert.u_data_center    = BuildingCodeForDataCenter;
                insert.u_rack           = "Virtual";

                string json = JSON.SerializeJSON <ServiceNowInsert>(insert);
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(URL);
                    var byteArray = Encoding.ASCII.GetBytes(Username + ":" + Password);    // _clearview_user:_clearview_user
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                    var message = new HttpRequestMessage
                    {
                        Content = new StringContent(json, Encoding.UTF8, "application/json")
                    };
                    message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage post = client.PostAsync("/api/now/v1/import/imp_server", message.Content).Result;
                    var content = post.Content.ReadAsStringAsync();
                    oLog.AddEvent(Name, "ServiceNow", content.Result, LoggingType.Debug);
                    if (post.IsSuccessStatusCode)
                    {
                        ServiceNowResponse response = JSON.DeserializeJSON <ServiceNowResponse>(content.Result);
                        foreach (ServiceNowResponseResult result in response.result)
                        {
                            string status = result.status.Trim().ToLower();
                            if (status != "updated" && status != "inserted" && status != "ignored")
                            {
                                error = "There was a problem inserting the record into Service Now - status = " + status;
                                break;
                            }
                        }
                    }
                    else if (String.IsNullOrWhiteSpace(content.Result) == false)
                    {
                        error = content.Result;
                    }
                    else
                    {
                        error = post.ReasonPhrase + " (" + (int)post.StatusCode + ")";
                    }
                }
            }
            catch (Exception ex)
            {
                error += ex.Message;
                while (ex.InnerException != null)
                {
                    ex     = ex.InnerException;
                    error += " ~ " + ex.Message;
                }
            }
            return(error);
        }