コード例 #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);
                });
            }
        }
コード例 #2
0
        /// <summary>	A SessionState extension method that adds a service connection. </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null. </exception>
        /// <param name="sessionState">     . </param>
        /// <param name="connectionName">	The connection Name. </param>
        /// <param name="connection">	    The connection. </param>
        /// <returns>	A St2ClientConnection. </returns>
        public static St2ClientConnection AddServiceConnection(this SessionState sessionState,
                                                               string connectionName, St2ClientConnection connection)
        {
            if (sessionState == null)
            {
                throw new ArgumentNullException("sessionState");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }


            if (string.IsNullOrEmpty(connectionName))
            {
                throw new ArgumentNullException("connectionName");
            }


            Dictionary <string, St2ClientConnection> connections;
            PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ServiceSessions);

            if (connectionsVariable == null)
            {
                connectionsVariable = new PSVariable(
                    VariableNames.ServiceSessions, connections = new Dictionary <string, St2ClientConnection>(),
                    ScopedItemOptions.AllScope
                    );
                sessionState.PSVariable.Set(connectionsVariable);
            }
            else
            {
                connections = (Dictionary <string, St2ClientConnection>)connectionsVariable.Value;
                if (connections == null)
                {
                    connectionsVariable.Value = connections = new Dictionary <string, St2ClientConnection>();
                    sessionState.PSVariable.Set(connectionsVariable);
                }
            }

            if (!connections.ContainsKey(connectionName))
            {
                connections.Add(connectionName, connection);
            }
            else
            {
                connections[connectionName] = connection;
            }

            if (string.IsNullOrEmpty(_defaultComputeServiceConnectionName) || connections.Count().Equals(1))
            {
                _defaultComputeServiceConnectionName = connectionName;
            }

            return(connection);
        }
コード例 #3
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;
                    });
            }
        }
コード例 #4
0
        /// <summary>
        ///     The begin processing.
        /// </summary>
        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            // If CaaS connection is NOT set via parameter, get it from the PS session
            if (Connection == null)
            {
                Connection = SessionState.GetDefaultServiceConnection();
                if (Connection == null)
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new AuthenticationException(
                                "Cannot find a valid connection. Use New-St2ClientConnection to create or Set-St2ActiveConnection to set a valid connection"),
                            "-1",
                            ErrorCategory.AuthenticationError,
                            this));
            }
        }
コード例 #5
0
        /// <summary>	A SessionState extension method that adds a service connection. </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null. </exception>
        /// <param name="sessionState">  	. </param>
        /// <param name="connectionName">	The connection Name. </param>
        /// <param name="connection">	 	The connection. </param>
        /// <returns>	A St2ClientConnection. </returns>
        public static St2ClientConnection AddServiceConnection(this SessionState sessionState,
			string connectionName, St2ClientConnection connection)
        {
            if (sessionState == null)
                throw new ArgumentNullException("sessionState");

            if (connection == null)
                throw new ArgumentNullException("connection");

            if (string.IsNullOrEmpty(connectionName))
                throw new ArgumentNullException("connectionName");

            Dictionary<string, St2ClientConnection> connections;
            PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ServiceSessions);
            if (connectionsVariable == null)
            {
                connectionsVariable = new PSVariable(
                    VariableNames.ServiceSessions, connections = new Dictionary<string, St2ClientConnection>(),
                    ScopedItemOptions.AllScope
                    );
                sessionState.PSVariable.Set(connectionsVariable);
            }
            else
            {
                connections = (Dictionary<string, St2ClientConnection>)connectionsVariable.Value;
                if (connections == null)
                {
                    connectionsVariable.Value = connections = new Dictionary<string, St2ClientConnection>();
                    sessionState.PSVariable.Set(connectionsVariable);
                }
            }

            if (!connections.ContainsKey(connectionName))
                connections.Add(connectionName, connection);
            else
                connections[connectionName] = connection;

            if (string.IsNullOrEmpty(_defaultComputeServiceConnectionName) || connections.Count().Equals(1))
                _defaultComputeServiceConnectionName = connectionName;

            return connection;
        }
コード例 #6
0
        /// <summary>
        ///     The begin processing.
        /// </summary>
        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            // If CaaS connection is NOT set via parameter, get it from the PS session
            if (Connection == null)
            {
                Connection = SessionState.GetDefaultServiceConnection();
                if (Connection == null)
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new AuthenticationException(
                                "Cannot find a valid connection. Use New-St2ClientConnection to create or Set-St2ActiveConnection to set a valid connection"),
                            "-1",
                            ErrorCategory.AuthenticationError,
                            this));
                }
            }
        }