Esempio n. 1
0
        /// <summary>
        ///     Process the record
        /// </summary>
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            St2Client apiClient = new St2Client(AuthApiUrl, ApiUrl, Username, Password, IgnoreCertificateValidation.ToBool());

            St2ClientConnection st2ClientConnection = new St2ClientConnection(apiClient);


            WriteDebug("Trying to login to the REST API");

            try
            {
                st2ClientConnection.ApiClient.RefreshTokenAsync().Wait(TimeSpan.FromSeconds(15));

                if (st2ClientConnection != null)
                {
                    if (!st2ClientConnection.ApiClient.HasToken())
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception("Could not login, check credentials and access to the API"), "100", ErrorCategory.AuthenticationError, st2ClientConnection));
                    }

                    WriteDebug(string.Format("connection created successfully: {0}", st2ClientConnection));
                    if (string.IsNullOrEmpty(Name))
                    {
                        Name = Guid.NewGuid().ToString();
                    }

                    if (!SessionState.GetServiceConnections().Any())
                    {
                        WriteDebug("This is the first connection and will be the default connection.");
                    }
                    SessionState.AddServiceConnection(Name, st2ClientConnection);
                    if (SessionState.GetServiceConnections().Count > 1)
                    {
                        WriteWarning(
                            "You have created more than one connection on this session, please use the cmdlet Set-St2ActiveConnection -Name <name> to change the active/default connection");
                    }

                    SessionState.AddServiceConnection(Name, st2ClientConnection);
                    WriteObject(st2ClientConnection);
                }
            }
            catch (FailedRequestException fre)
            {
                ThrowTerminatingError(new ErrorRecord(fre, "-1", ErrorCategory.AuthenticationError, null));
            }
            catch (AggregateException ae)
            {
                ae.Handle(
                    e =>
                {
                    ThrowTerminatingError(new ErrorRecord(e, "-1", ErrorCategory.AuthenticationError, null));
                    return(true);
                });
            }
        }