Beispiel #1
0
        private void ProcessByNextAvailableIp()
        {
            StringWriter SWriter     = new StringWriter();
            TextWriter   OriginalOut = Console.Out;

            Console.SetOut(SWriter);

            try
            {
                base.Response = base.IBX.NewDnsHostRecordWithNextAvailableIP(this._HostName, this._Network, this._AddToDns, this._DHCP, this._SetHostName, this._MAC).Result;
                base.FinishResponse(this.PassThru);
            }
            catch (AggregateException ae)
            {
                PSCommon.WriteExceptions(ae, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            catch (Exception e)
            {
                PSCommon.WriteExceptions(e, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            finally
            {
                WriteVerbose(SWriter.ToString());
                Console.SetOut(OriginalOut);
            }
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            //Use base._Ref since the user can provide an input object that sets it
            if (base.Force == true || ShouldProcess($"{base._Ref}", $"Delete"))
            {
                if (base.Force == true || ShouldContinue($"Do you want to delete the IBX Object {base._Ref}?", "Deleting IBX Object"))
                {
                    StringWriter SWriter     = new StringWriter();
                    TextWriter   OriginalOut = Console.Out;
                    Console.SetOut(SWriter);

                    try
                    {
                        base.Response = base.IBX.DeleteIbxObject(base._Ref).Result;
                        base.FinishResponse(this.PassThru);
                    }
                    catch (AggregateException ae)
                    {
                        PSCommon.WriteExceptions(ae, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    catch (Exception e)
                    {
                        PSCommon.WriteExceptions(e, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    finally
                    {
                        WriteVerbose(SWriter.ToString());
                        Console.SetOut(OriginalOut);
                    }
                }
            }
        }
        protected virtual void ProcessByAttributeForUpdatedObject(string attribute, List <KeyValuePair <string, string> > additionalProperties)
        {
            if (this.ParameterDictionary.ContainsKey(attribute))
            {
                string ParamValue = this.ParameterDictionary[attribute].Value as string;

                if (!String.IsNullOrEmpty(ParamValue) && Enum.TryParse <InfoBloxObjectsEnum>(ParamValue.ToUpper(), out this.ObjectType))
                {
                    List <KeyValuePair <string, string> > PropertyList = new List <KeyValuePair <string, string> >();

                    foreach (KeyValuePair <string, RuntimeDefinedParameter> obj in this.ParameterDictionary)
                    {
                        if (obj.Value.Value != null && !String.IsNullOrEmpty(obj.Value.Value as string))
                        {
                            PropertyList.Add(new KeyValuePair <string, string>(obj.Key, obj.Value.Value as string));
                        }
                    }

                    if (additionalProperties != null && additionalProperties.Count > 0)
                    {
                        PropertyList.AddRange(additionalProperties);
                    }

                    StringWriter SWriter     = new StringWriter();
                    TextWriter   OriginalOut = Console.Out;
                    Console.SetOut(SWriter);

                    try
                    {
                        this.Response = typeof(IBXCommonMethods).GetMethod("UpdateIbxObject").MakeGenericMethod(this.ObjectType.GetObjectType()).InvokeGenericAsync(this.IBX, new object[] { this.ObjectType, PropertyList }).Result as string;
                        this.FinishResponse(this._PassThru);
                    }
                    catch (AggregateException ae)
                    {
                        PSCommon.WriteExceptions(ae, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    catch (Exception e)
                    {
                        PSCommon.WriteExceptions(e, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    finally
                    {
                        WriteVerbose(SWriter.ToString());
                        Console.SetOut(OriginalOut);
                    }
                }
                else
                {
                    throw new PSArgumentException($"Object type parameter was not an allowed value, {ParamValue} was provided.");
                }
            }
            else
            {
                throw new PSArgumentException("The object type parameter does not exist in the dynamic parameter dictionary.");
            }
        }
        protected virtual void ProcessByUpdatedObject(object updatedObject, bool removeEmpty)
        {
            if (!this.MyInvocation.BoundParameters.ContainsKey("RemoveEmptyProperties"))
            {
                int UserChoice = 0;

                ChoiceDescription Yes = new ChoiceDescription("&Remove", "Will remove empty and null values from the object during serialization.");
                ChoiceDescription No  = new ChoiceDescription("&Keep", "Will keep empty and null values and overwrite any existing values.");
                Collection <ChoiceDescription> Choices = new Collection <ChoiceDescription>()
                {
                    Yes, No
                };
                UserChoice = Host.UI.PromptForChoice("Update Infoblox Object", ("Do you want to remove the empty properties when sending the object?"), Choices, 0);

                switch (UserChoice)
                {
                default:
                case 0:
                {
                    removeEmpty = true;
                    break;
                }

                case 1:
                {
                    removeEmpty = false;
                    break;
                }
                }
            }

            StringWriter SWriter     = new StringWriter();
            TextWriter   OriginalOut = Console.Out;

            Console.SetOut(SWriter);

            try
            {
                updatedObject.GetType().GetProperty("_ref").SetValue(updatedObject, this._Ref);
                this.Response = this.IBX.UpdateIbxObject(updatedObject, removeEmpty).Result;
            }
            catch (AggregateException ae)
            {
                PSCommon.WriteExceptions(ae, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            catch (Exception e)
            {
                PSCommon.WriteExceptions(e, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            finally
            {
                WriteVerbose(SWriter.ToString());
                Console.SetOut(OriginalOut);
            }
        }
Beispiel #5
0
        protected override void ProcessRecord()
        {
            List <string> IPsToDelete = new List <string>();

            if (!this.Force)
            {
                if (ShouldProcess("DNS Host Record", "Delete IPs", "Updating IBX DNS Object"))
                {
                    foreach (string ip in this._IPs)
                    {
                        if (ShouldContinue($"Do you want to delete {ip} from the DNS Host Record?", "Updating IBX DNS Object"))
                        {
                            IPsToDelete.Add(ip);
                        }
                    }
                }
            }
            else
            {
                IPsToDelete = this._IPs;
            }

            if (IPsToDelete.Count > 0)
            {
                StringWriter SWriter     = new StringWriter();
                TextWriter   OriginalOut = Console.Out;
                Console.SetOut(SWriter);

                try
                {
                    base.Response = base.IBX.RemoveDnsHostRecordIPAddresses(base._Ref, IPsToDelete.ToArray()).Result;
                    base.FinishResponse(this.PassThru);
                }
                catch (AggregateException ae)
                {
                    PSCommon.WriteExceptions(ae, this.Host);
                    this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                }
                catch (Exception e)
                {
                    PSCommon.WriteExceptions(e, this.Host);
                    this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                }
                finally
                {
                    WriteVerbose(SWriter.ToString());
                    Console.SetOut(OriginalOut);
                }
            }
            else
            {
                WriteWarning("No IP addresses to delete.");
            }
        }
        private void ProcessByReference()
        {
            string[] Temp = base._Ref.Split('/');
            if (Temp.Length > 0)
            {
                try
                {
                    base.ObjectType = IBXCommonMethods.GetInfobloxObjectEnumFromName(Temp[0]);

                    StringWriter SWriter     = new StringWriter();
                    TextWriter   OriginalOut = Console.Out;
                    Console.SetOut(SWriter);

                    try
                    {
                        base.ObjectResponse = typeof(IBXCommonMethods).GetMethod("GetIbxObject").MakeGenericMethod(base.ObjectType.GetObjectType()).InvokeGenericAsync(base.IBX, new object[] { base._Ref, base._FieldsToReturn }).Result;
                    }
                    catch (AggregateException ae)
                    {
                        PSCommon.WriteExceptions(ae, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    catch (Exception e)
                    {
                        PSCommon.WriteExceptions(e, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    finally
                    {
                        WriteVerbose(SWriter.ToString());
                        Console.SetOut(OriginalOut);
                    }
                }
                catch (Exception e)
                {
                    PSCommon.WriteExceptions(e, this.Host);
                    this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                }
            }
            else
            {
                throw new PSArgumentException("The reference value was not properly formatted.");
            }
        }
        protected virtual void ProcessByNewObject(object newObject)
        {
            if (newObject != null)
            {
                if (newObject.GetType().IsInfobloxType())
                {
                    StringWriter SWriter     = new StringWriter();
                    TextWriter   OriginalOut = Console.Out;
                    Console.SetOut(SWriter);

                    try
                    {
                        this.Response = this.IBX.NewIbxObject(newObject).Result;
                        this.FinishResponse(this._PassThru);
                    }
                    catch (AggregateException ae)
                    {
                        PSCommon.WriteExceptions(ae, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    catch (Exception e)
                    {
                        PSCommon.WriteExceptions(e, this.Host);
                        this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                    }
                    finally
                    {
                        WriteVerbose(SWriter.ToString());
                        Console.SetOut(OriginalOut);
                    }
                }
                else
                {
                    throw new PSArgumentException("newObject", $"The new object must be an Infoblox type, {newObject.GetType().FullName} was provided.");
                }
            }
            else
            {
                throw new PSArgumentNullException("newObject", "The new object cannot be null.");
            }
        }
        protected override void BeginProcessing()
        {
            try
            {
                if (this.ParameterSetName.StartsWith(_GRID))
                {
                    if (this.Version.Equals("LATEST"))
                    {
                        using (HttpClient Client = CommandHelpers.BuildHttpClient(this.GridMaster, "1.0", this.Credential.UserName, this.Credential.Password, TimeSpan.FromSeconds(Timeout)).Result)
                        {
                            WriteVerbose("Getting supported versions.");

                            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";
                            }
                        }
                    }

                    this._IBX = new IBXCommonMethods(this.GridMaster, this.Version, this.Credential.UserName, this.Credential.Password, TimeSpan.FromSeconds(Timeout));
                }
                else if (this.ParameterSetName.StartsWith(_SESSION))
                {
                    this._IBX = new IBXCommonMethods(this.Session, TimeSpan.FromSeconds(Timeout));
                }
                else if (this.ParameterSetName.StartsWith(_ENTERED_SESSION))
                {
                    this._IBX = new IBXCommonMethods(TimeSpan.FromSeconds(Timeout));
                }
                else
                {
                    this.ThrowTerminatingError(new ErrorRecord(new PSArgumentException($"Could not identify parameter set from {this.ParameterSetName}"), "PSArgumentException", ErrorCategory.InvalidArgument, this));
                }
            }
            catch (AggregateException ae)
            {
                PSCommon.WriteExceptions(ae, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            catch (Exception e)
            {
                PSCommon.WriteExceptions(e, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
        }
        protected override void ProcessRecord()
        {
            if (this._DHCP)
            {
                if (base.ParameterDictionary.ContainsKey("SetHostNameInDhcp") && base.ParameterDictionary["SetHostNameInDhcp"].IsSet)
                {
                    SwitchParameter temp = (SwitchParameter)base.ParameterDictionary["SetHostNameInDhcp"].Value;
                    this._SetHostName = temp.ToBool();
                }
            }

            if (base.ParameterDictionary.ContainsKey("MAC") && base.ParameterDictionary["MAC"].IsSet)
            {
                this._MAC = base.ParameterDictionary["MAC"].Value as string;
            }

            StringWriter SWriter     = new StringWriter();
            TextWriter   OriginalOut = Console.Out;

            Console.SetOut(SWriter);

            if (base.Force == true || ShouldProcess("DNS Host Record", "Add IP Address", "Updating IBX DNS Object"))
            {
                if (base.Force == true || ShouldContinue("Do you want to add the IP Address(es) to the DNS Host Record?", "Updating IBX DNS Object"))
                {
                    if (this.ParameterSetName.EndsWith(_SINGLE_IP))
                    {
                        try
                        {
                            base.Response = base.IBX.AddDnsHostRecordIPAddress(base._Ref, this._IP, this._DHCP, this._SetHostName, this._MAC).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else if (this.ParameterSetName.EndsWith(_GRID_MULTIPLE_IP))
                    {
                        try
                        {
                            base.Response = base.IBX.AddDnsHostRecordIPAddresses(base._Ref, this._IPs).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else if (this.ParameterSetName.EndsWith(_NEXT_AVAILABLE_IP))
                    {
                        try
                        {
                            base.Response = base.IBX.AddDnsHostRecordNextAvailableIPAddress(base._Ref, this._Network, this._DHCP, this._SetHostName, this._MAC).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else
                    {
                        throw new PSArgumentException($"Bad ParameterSet Name: {this.ParameterSetName}");
                    }
                }
            }
        }
        private void Search()
        {
            if (base.ParameterDictionary.ContainsKey("ObjectType"))
            {
                string objectType = base.ParameterDictionary["ObjectType"].Value as string;

                if (!String.IsNullOrEmpty(objectType))
                {
                    if (!Enum.TryParse <InfoBloxObjectsEnum>(objectType.ToUpper(), out base.ObjectType))
                    {
                        throw new PSArgumentException("Object type parameter was not an allowed value.");
                    }
                    else
                    {
                        this._SearchField = base.ParameterDictionary["SearchField"].Value.ToString();

                        if (base.ParameterDictionary.ContainsKey("SearchType"))
                        {
                            string searchType = base.ParameterDictionary["SearchType"].Value as string;

                            if (!String.IsNullOrEmpty(searchType))
                            {
                                if (!Enum.TryParse <SearchType>(searchType.ToUpper(), out this._searchType))
                                {
                                    throw new PSArgumentException("Search type parameter was not an allowed value.");
                                }
                            }
                            else
                            {
                                throw new PSArgumentNullException("SearchType", "The search type cannot be null or empty.");
                            }
                        }
                        else
                        {
                            throw new PSArgumentNullException("SearchType", "The search type parameter is required for a search.");
                        }
                    }
                }
                else
                {
                    throw new PSArgumentNullException("ObjectType", "The object type parameter cannot be null or empty.");
                }
            }
            else
            {
                throw new PSArgumentNullException("ObjectType", "The object type parameter is required for a search.");
            }

            StringWriter SWriter     = new StringWriter();
            TextWriter   OriginalOut = Console.Out;

            Console.SetOut(SWriter);

            try
            {
                base.ObjectResponse = (IEnumerable <object>) typeof(IBXCommonMethods).GetMethod("SearchIbxObject").MakeGenericMethod(base.ObjectType.GetObjectType()).InvokeGenericAsync(base.IBX, new object[] { this._searchType, this._SearchField, this.SearchValue, base._FieldsToReturn }).Result;
            }
            catch (AggregateException ae)
            {
                PSCommon.WriteExceptions(ae, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            catch (Exception e)
            {
                PSCommon.WriteExceptions(e, this.Host);
                this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
            }
            finally
            {
                WriteVerbose(SWriter.ToString());
                Console.SetOut(OriginalOut);
            }
        }
        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));
                }
            }
        }
Beispiel #12
0
        protected override void ProcessRecord()
        {
            if (base.ParameterDictionary.ContainsKey("SetHostName"))
            {
                this._SetHostName = ((SwitchParameter)base.ParameterDictionary["SetHostName"].Value).ToBool();
            }

            if (base.ParameterDictionary.ContainsKey("Network"))
            {
                this._Network = base.ParameterDictionary["Network"].Value as string;
            }

            if (base.ParameterDictionary.ContainsKey("MAC"))
            {
                this._MAC = base.ParameterDictionary["MAC"].Value as string;
            }

            if (base._Force == true || ShouldProcess("DNS Host Record", "Update", "Updating IBX DNS Object"))
            {
                if (base._Force == true || ShouldContinue("Do you want to update the DNS Host Record?", "Updating IBX DNS Object"))
                {
                    if (this.ParameterSetName.EndsWith(_SPECIFY_IP))
                    {
                        StringWriter SWriter     = new StringWriter();
                        TextWriter   OriginalOut = Console.Out;
                        Console.SetOut(SWriter);

                        try
                        {
                            base.Response = base.IBX.SetDnsHostRecordIP(base._Ref, this._IPAddress, this._DHCP, this._SetHostName, this._MAC).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else if (this.ParameterSetName.EndsWith(_HOST_NAME))
                    {
                        StringWriter SWriter     = new StringWriter();
                        TextWriter   OriginalOut = Console.Out;
                        Console.SetOut(SWriter);

                        try
                        {
                            base.Response = base.IBX.SetDnsHostRecordName(base._Ref, this._HostRecord).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else if (this.ParameterSetName.EndsWith(_NEXT_AVAILABLE_IP))
                    {
                        StringWriter SWriter     = new StringWriter();
                        TextWriter   OriginalOut = Console.Out;
                        Console.SetOut(SWriter);

                        try
                        {
                            base.Response = base.IBX.SetDnsHostRecordNextAvailableIP(base._Ref, this._Network, this._DHCP, this._SetHostName, this._MAC).Result;
                            base.FinishResponse(this.PassThru);
                        }
                        catch (AggregateException ae)
                        {
                            PSCommon.WriteExceptions(ae, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(ae.InnerException, ae.InnerException.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        catch (Exception e)
                        {
                            PSCommon.WriteExceptions(e, this.Host);
                            this.ThrowTerminatingError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.NotSpecified, this));
                        }
                        finally
                        {
                            WriteVerbose(SWriter.ToString());
                            Console.SetOut(OriginalOut);
                        }
                    }
                    else if (this.ParameterSetName.EndsWith(_BY_OBJECT))
                    {
                        base.ProcessByUpdatedObject(this._InputObject, this.RemoveEmptyProperties);
                    }
                    else
                    {
                        throw new PSArgumentException("Bad ParameterSet Name");
                    }
                }
            }
        }
        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();
        }
Beispiel #14
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));
                }
            }
        }