Пример #1
0
 public IBXCommonMethods(InfobloxSession session, TimeSpan?timeout = null)
 {
     if (session != null)
     {
         if (session.Cookie != null && !session.Cookie.Expired)
         {
             this._Client = CommandHelpers.BuildHttpClient(session.GridMaster, session.Version, session.Cookie, timeout).Result;
         }
         else
         {
             if (session.Credential != null)
             {
                 this._Client = CommandHelpers.BuildHttpClient(session.GridMaster, session.Version, session.Credential.UserName, session.Credential.Password, timeout).Result;
             }
             else
             {
                 throw new ArgumentException("The session parameter did not contain a valid cookie and a null credential.", "session");
             }
         }
     }
     else
     {
         throw new ArgumentNullException("session", "The session object cannot be null.");
     }
 }
Пример #2
0
 public DnsMethods(InfobloxSession session, TimeSpan?timeout = null) : base(session, timeout)
 {
 }
Пример #3
0
        protected override void ProcessRecord()
        {
            if (InfobloxSessionData.Version.Equals("LATEST"))
            {
                using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, "1.0", this.Credential.UserName, this.Credential.Password).Result)
                {
                    WriteVerbose("Getting supported versions.");

                    try
                    {
                        HttpResponseMessage Response = Client.GetAsync("?_schema").Result;

                        WriteVerbose(Response.RequestMessage.RequestUri.ToString());

                        if (Response.IsSuccessStatusCode)
                        {
                            string Content = Response.Content.ReadAsStringAsync().Result;

                            WriteVerbose($"Response {Content}");

                            dynamic Obj = JsonConvert.DeserializeObject(Content);
                            IEnumerable <string> Versions = Obj.supported_versions;

                            WriteVerbose("Got versions");

                            Versions = Versions.Select(x => { return(new Version(x)); }).OrderByDescending(x => x).Select(x => { return(x.ToString()); });

                            WriteVerbose("Sorted versions");
                            this.Version = Versions.First();
                            WriteVerbose($"Latest supported version is {this.Version}");
                        }
                        else
                        {
                            WriteVerbose("Failed to get schema, reverting to using version 2.0");
                            this.Version = "2.0";
                        }
                    }
                    catch (WebException e)
                    {
                        InfobloxCustomException Ex = new InfobloxCustomException(e);
                        PSCommon.WriteExceptions(Ex, this.Host);
                    }
                    catch (HttpRequestException e)
                    {
                        InfobloxCustomException Ex;

                        if (e.InnerException is WebException)
                        {
                            Ex = new InfobloxCustomException((WebException)e.InnerException);
                        }
                        else
                        {
                            Ex = new InfobloxCustomException(e);
                        }

                        PSCommon.WriteExceptions(Ex, this.Host);
                    }
                    catch (Exception e)
                    {
                        InfobloxCustomException Ex = new InfobloxCustomException(e);
                        PSCommon.WriteExceptions(Ex, this.Host);
                    }
                }
            }

            InfobloxSession Session = new InfobloxSession()
            {
                GridMaster = this.GridMaster,
                Credential = new InfobloxCredential(this.Credential.UserName, this.Credential.Password),
                Version    = this.Version
            };

            using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, this.Version, this.Credential.UserName, this.Credential.Password).Result)
            {
                try
                {
                    HttpResponseMessage Response = Client.GetAsync("grid").Result;

                    Cookie Cookie = CommandHelpers.GetResponseCookie(Response);

                    if (Cookie != null && !Cookie.Expired)
                    {
                        Session.Cookie = Cookie;
                    }

                    WriteObject(Session);
                }
                catch (WebException e)
                {
                    InfobloxCustomException Ex = new InfobloxCustomException(e);
                    PSCommon.WriteExceptions(Ex, this.Host);
                    this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this));
                }
                catch (HttpRequestException e)
                {
                    InfobloxCustomException Ex;

                    if (e.InnerException is WebException)
                    {
                        Ex = new InfobloxCustomException((WebException)e.InnerException);
                    }
                    else
                    {
                        Ex = new InfobloxCustomException(e);
                    }

                    PSCommon.WriteExceptions(Ex, this.Host);

                    this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this));
                }
                catch (Exception e)
                {
                    InfobloxCustomException Ex = new InfobloxCustomException(e);
                    PSCommon.WriteExceptions(Ex, this.Host);
                    this.ThrowTerminatingError(new ErrorRecord(Ex, Ex.HttpStatus, ErrorCategory.NotSpecified, this));
                }
            }
        }