Exemplo n.º 1
0
        /// <summary>
        /// Posts the specified backup tenant config to the APIC
        /// </summary>
        /// <param name="snapShotDirectory">Directory on the server where the xml backup config files are stored</param>
        /// <param name="snapShotGuid">GUID for the xml backup config</param>
        /// <returns></returns>
        public bool RollBackTenant(string tenantName, string snapShotGuid, string snapShotDirectory)
        {
            bool success      = false;
            var  apicSettings = new APICSettings();
            HttpResponseMessage result;

            //Build the components for POST call to the APIC
            var snapShotFile = string.Format(@"{0}\{1}----{2}.xml", snapShotDirectory, tenantName, snapShotGuid);
            var readLines    = System.IO.File.ReadAllText(snapShotFile);
            var requestUri   = string.Format(@"https://{0}/api/node/mo/.xml", apicSettings.url);

            //login
            if (apicSettings.credentialsSet)
            {
                ServicePointManager
                .ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;

                var APICSessionCookieContainer = new CookieContainer();
                using (var handler = new HttpClientHandler()
                {
                    CookieContainer = APICSessionCookieContainer, UseCookies = true,
                })
                    using (var httpClient = new HttpClient(handler))
                    {
                        httpClient.BaseAddress = new Uri("https://" + apicSettings.url);

                        result = httpClient.PostAsJsonAsync("api/aaaLogin.json", new
                        {
                            aaaUser = new
                            {
                                attributes = new
                                {
                                    name = apicSettings.username,
                                    pwd  = apicSettings.password
                                }
                            }
                        }).Result;

                        //delete current version of the tenant
                        if (result.IsSuccessStatusCode)
                        {
                            var apiContent = String.Format("api/mo/uni/tn-{0}.xml", tenantName);
                            result = httpClient.DeleteAsync(apiContent).Result;
                        }

                        //post the SnapShot for the tenant
                        if (result.IsSuccessStatusCode)
                        {
                            var httpContent = new StringContent(readLines, Encoding.UTF8, "application/xml");
                            success = httpClient.PostAsync(requestUri, httpContent).Result.IsSuccessStatusCode;
                        }
                    }
            }
            return(success);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Takes a GET API request and returns the results in a .NET JSON model for ACI responses
        /// </summary>
        /// <param name="apicCall"></param>
        /// <returns></returns>
        public ApicJsonModel ApicGetRequest(string apicCall)
        {
            var apicSettings = new APICSettings();

            //login to the APIC
            if (apicSettings.credentialsSet)
            {
                ServicePointManager
                .ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;

                var APICSessionCookieContainer = new CookieContainer();
                using (var handler = new HttpClientHandler()
                {
                    CookieContainer = APICSessionCookieContainer, UseCookies = true,
                })
                    using (var httpClient = new HttpClient(handler))
                    {
                        httpClient.BaseAddress = new Uri("https://" + apicSettings.url);

                        var result = httpClient.PostAsJsonAsync("api/aaaLogin.json", new
                        {
                            aaaUser = new
                            {
                                attributes = new
                                {
                                    name = apicSettings.username,
                                    pwd  = apicSettings.password
                                }
                            }
                        }).Result;

                        //get the results of the call
                        result = httpClient.GetAsync(apicCall).Result;


                        var collection = result.Content.ReadAsAsync <ApicJsonModel>().Result;
                        return(collection);
                    }
            }
            else
            {
                return(new ApicJsonModel());
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a new snap shot of a tenant and save it in the specified server directory along with the comments
        /// </summary>
        /// <param name="tenantName"></param>
        /// <param name="comments"></param>
        /// <param name="snapShotDirectory"></param>
        public void CreateTenantSnapShot(string tenantName, string comments, string snapShotDirectory)
        {
            Guid   guid         = Guid.NewGuid();
            string guidId       = guid.ToString();
            string response     = null;
            var    apicSettings = new APICSettings();

            //build the GET for the APIC
            string request = string.Format("api/node/mo/uni/tn-{0}.xml?query-target=self&rsp-subtree=full&rsp-prop-include=config-only", tenantName);

            //login
            if (apicSettings.credentialsSet)

            {
                ServicePointManager
                .ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;

                var APICSessionCookieContainer = new CookieContainer();
                using (var handler = new HttpClientHandler()
                {
                    CookieContainer = APICSessionCookieContainer, UseCookies = true,
                })
                    using (var httpClient = new HttpClient(handler))
                    {
                        httpClient.BaseAddress = new Uri("https://" + apicSettings.url);

                        var result = httpClient.PostAsJsonAsync("api/aaaLogin.json", new
                        {
                            aaaUser = new
                            {
                                attributes = new
                                {
                                    name = apicSettings.username,
                                    pwd  = apicSettings.password
                                }
                            }
                        }).Result;


                        //GET the config for the specified tenant
                        using (StreamReader streamIn = new StreamReader(httpClient.GetStreamAsync(request).Result))
                        {
                            response = streamIn.ReadToEnd();
                            streamIn.Close();
                        }

                        //modify the config - replace the imdata xml tag with polUni - making the XML usable to for APIC Config REST POSTs
                        string formattedResponse = Regex.Replace(response, "<imdata totalCount(.*?)(?:>|$)", "<polUni>");
                        formattedResponse = formattedResponse.Replace("</imdata>", "</polUni>");

                        //build the snapshot file with the date and a GUID for indexing later
                        DateTime    today   = DateTime.Today;
                        string      date    = today.ToString();
                        string      xmlFile = string.Format(@"{0}\{1}.xml", snapShotDirectory, tenantName + "----" + guidId);
                        XmlDocument xDoc    = new XmlDocument();
                        xDoc.LoadXml(formattedResponse);
                        xDoc.Save(xmlFile);

                        //write the SnapShot file
                        string commentFile = string.Format(@"{0}\{1}.txt", snapShotDirectory, tenantName + "----" + guidId);
                        System.IO.File.WriteAllText(commentFile, date + " - " + comments);
                    }
            }
        }