protected override void ProcessRecord() { InfobloxSessionData.Reset(); if (this.Version.Equals("LATEST")) { using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, "1.0", this.Credential.UserName, this.Credential.Password).Result) { WriteVerbose("Getting supported versions."); try { //Because I'm using .Result instead of await, the exception thrown is an AggregrateException //instead of a normal exception if we'd used await like IBXCommonMethods.GetAsnyc() does //So the resulting InfobloxCustomException isn't as informative, may need to update logic //to use the InvokeGenericAsync() logic being used in PSExtensionMethods to keep ProcessRecord //as a sync function 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); JArray JVersions = Obj.supported_versions; IEnumerable <string> Versions = JVersions.ToObject <string[]>(); 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); 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 (AggregateException e) { InfobloxCustomException Ex = new InfobloxCustomException(e.InnerException.Message); 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)); } } } WriteVerbose("Infoblox session data will be used now."); InfobloxSessionData.GridMaster = this.GridMaster; InfobloxSessionData.Credential = new InfobloxCredential(this.Credential.UserName, this.Credential.Password); InfobloxSessionData.Version = this.Version; InfobloxSessionData.UseSessionData = true; 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) { WriteVerbose("Infoblox cookie will be used for authentication."); InfobloxSessionData.Cookie = Cookie; } else { WriteWarning("Could not retrieve a valid cookie from the grid master."); } } 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)); } } }
protected override void ProcessRecord() { if (InfobloxSessionData.UseSessionData && InfobloxSessionData.Cookie != null) { if (!InfobloxSessionData.Cookie.Expired) { using (HttpClient Client = CommandHelpers.BuildHttpClient(InfobloxSessionData.GridMaster, InfobloxSessionData.Version).Result) { try { HttpResponseMessage Response = Client.PostAsync("logout", null).Result; if (Response.IsSuccessStatusCode) { WriteVerbose("Successfully ended session."); } else { ThrowTerminatingError(new ErrorRecord(new WebException(Response.StatusCode.ToString()), Response.StatusCode.ToString(), ErrorCategory.InvalidOperation, Client.BaseAddress)); } } 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)); } } } else if (InfobloxSessionData.Credential != null) { using (HttpClient Client = CommandHelpers.BuildHttpClient(InfobloxSessionData.GridMaster, InfobloxSessionData.Version, InfobloxSessionData.Credential.UserName, InfobloxSessionData.Credential.Password).Result) { try { HttpResponseMessage Response = Client.PostAsync("logout", null).Result; if (Response.IsSuccessStatusCode) { WriteVerbose("Successfully ended session."); } else { ThrowTerminatingError(new ErrorRecord(new WebException(Response.StatusCode.ToString()), Response.StatusCode.ToString(), ErrorCategory.InvalidOperation, Client.BaseAddress)); } } 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)); } } } else { WriteWarning($"No valid authentication methods present to use to logout of Infoblox session to {InfobloxSessionData.GridMaster}"); } } InfobloxSessionData.Reset(); }