public void ContainsWhenNoUser()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID1, RunspaceID2, RunspaceID2);

            // Act
            var containsResult = userRunspaces.Contains(UserID2);

            // Assert
            Assert.IsFalse(containsResult);
        }
        public void RemoveUser()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID2, RunspaceID2, RunspaceID2);

            // Act
            userRunspaces.RemoveUser(UserID2);

            // Assert
            Assert.IsFalse(userRunspaces.Contains(UserID2));
        }
        public void ListWhenNoUser()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID1, RunspaceID2, RunspaceID2);

            // Act
            var listResult = userRunspaces.List(UserID2);

            // Assert
            Assert.IsNull(listResult);
        }
        public void RemoveRunspaceWhenNoUser()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID1, RunspaceID2, RunspaceID2);

            // Act && Assert
            Assert.DoesNotThrow(() => userRunspaces.RemoveData(UserID2, RunspaceID2));

            Assert.IsTrue(userRunspaces.Contains(UserID1));
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID1));
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID2));
        }
        public IWebConsoleData CreateWebConsole(
            string userId,
            ISessionToken sessionToken,
            ISolutionStsClient stsClient,
            string vcEndpoint)
        {
            IWebConsoleData result = null;

            _logger.LogInformation("StartCreateWebConsole");

            try {
                Sessions.Instance.EnsureValidUser(userId);
                _logger.LogDebug("RunspaceProvider -> CreateWebConsole call");

                string bearerSamlToken = "";
                try {
                    _logger.LogDebug($"HoK Saml Token availble: {sessionToken.HoKSamlToken != null}");
                    if (sessionToken.HoKSamlToken == null)
                    {
                        throw new Exception(APIGatewayResources.PowerCLIVCloginController_NoRefreshTokenAvailable_For_Session);
                    }

                    _logger.LogDebug($"STSClient -> IssueBearerTokenBySolutionToken call");
                    bearerSamlToken = stsClient
                                      .IssueBearerTokenBySolutionToken(sessionToken.HoKSamlToken.RawXmlElement)
                                      .OuterXml;
                } catch (Exception exc) {
                    _logger.LogError(exc, "Issue Bearer Token failed");
                    result.State        = DataTypes.WebConsoleState.Error;
                    result.ErrorDetails = new DataTypes.ErrorDetails(exc);
                }

                var token          = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(bearerSamlToken));
                var webConsoleInfo = _runspaceProvider.CreateWebConsole(vcEndpoint, token, true);
                _logger.LogDebug($"Runspace provider result: {webConsoleInfo.Id}, {webConsoleInfo.CreationState}, {webConsoleInfo.CreationError}");
                result = new WebConsoleData(webConsoleInfo);
                result.CreationTime = DateTime.Now;
                result.State        = DataTypes.WebConsoleState.Available;

                _runspacesStatsMonitor.RegisterWebConsole(result, sessionToken.SessionId);
                _userWebConsoles.Add(userId, result.Id, result);
            } catch (RunspaceProviderException runspaceProviderException) {
                _logger.LogError(runspaceProviderException, "Runspace provider exception was thrown");
                throw;
            } catch (Exception ex) {
                throw new RunspaceProviderException(
                          string.Format(
                              APIGatewayResources.MultiTenantRunspaceProvider_CreateFailed,
                              userId,
                              ex.Message),
                          ex);
            }

            return(result);
        }
        public void GetData()
        {
            // Arrange
            const string UserID        = "user-id";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceData1 = "runspace-data1";
            const string RunspaceID2   = "runspace-id2";
            const string RunspaceData2 = "runspace-data2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID, RunspaceID1, RunspaceData1);
            userRunspaces.Add(UserID, RunspaceID2, RunspaceData2);

            // Act
            var actualData = userRunspaces.GetData(UserID, RunspaceID2);

            // Assert
            Assert.NotNull(actualData);
            Assert.AreEqual(RunspaceData2, actualData);
        }
        public void AddContains()
        {
            // Arrange
            const string UserID1       = "user-id1";
            const string UserID2       = "user-id2";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceID2   = "runspace-id2";
            const string RunspaceID3   = "runspace-id3";
            var          userRunspaces = new UserToIdentifiableData <string>();

            // Act
            userRunspaces.Add(UserID1, RunspaceID1, RunspaceID1);
            userRunspaces.Add(UserID1, RunspaceID2, RunspaceID2);
            userRunspaces.Add(UserID2, RunspaceID3, RunspaceID3);

            // Assert
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID1));
            Assert.IsTrue(userRunspaces.Contains(UserID1, RunspaceID2));
            Assert.IsTrue(userRunspaces.Contains(UserID2, RunspaceID3));
        }
        public void List()
        {
            // Arrange
            const string UserID        = "user-id";
            const string RunspaceID1   = "runspace-id1";
            const string RunspaceData1 = "runspace-data1";
            const string RunspaceID2   = "runspace-id2";
            const string RunspaceData2 = "runspace-data2";
            var          userRunspaces = new UserToIdentifiableData <string>();

            userRunspaces.Add(UserID, RunspaceID1, RunspaceData1);
            userRunspaces.Add(UserID, RunspaceID2, RunspaceData2);

            // Act
            var listResult = userRunspaces.List(UserID);

            // Assert
            Assert.NotNull(listResult);
            Assert.AreEqual(2, listResult.Length);
            Assert.IsTrue(listResult.Contains(RunspaceData1));
            Assert.IsTrue(listResult.Contains(RunspaceData2));
        }
Example #9
0
        public IRunspaceData StartCreate(
            string userId,
            ISessionToken sessionToken,
            string name,
            bool runVcConnectionScript,
            ISolutionStsClient stsClient,
            string vcEndpoint)
        {
            IRunspaceData result = null;

            _logger.LogInformation("StartCreate");

            try {
                Sessions.Instance.EnsureValidUser(userId);
                _logger.LogDebug("RunspaceProvider -> StartCreate call");
                var runspaceInfo = _runspaceProvider.StartCreate();
                _logger.LogDebug($"Runspace provider result: {runspaceInfo.Id}, {runspaceInfo.CreationState}, {runspaceInfo.CreationError}");
                result = new RunspaceData(runspaceInfo);
                result.CreationTime          = DateTime.Now;
                result.Name                  = name;
                result.RunVcConnectionScript = runVcConnectionScript;
                result.State                 = DataTypes.RunspaceState.Creating;

                _userRunspaces.Add(userId, result.Id, result);
                _runspacesStatsMonitor.Register(result, sessionToken.SessionId);

                Task.Run(() => {
                    _logger.LogDebug("RunspaceProvider -> WaitCreateCompletion call");
                    var waitResult = _runspaceProvider.WaitCreateCompletion(result);
                    _logger.LogDebug($"Runspace provider WaitCreateCompletion result: {waitResult.Id}, {waitResult.CreationState}, {waitResult.CreationError}");
                    if (waitResult.CreationState == RunspaceCreationState.Error)
                    {
                        ((RunspaceData)result).ErrorDetails = new DataTypes.ErrorDetails(waitResult.CreationError);
                        ((RunspaceData)result).State        = DataTypes.RunspaceState.Error;
                    }
                    else
                    {
                        // Update endpoint info
                        ((RunspaceData)result).Endpoint = waitResult.Endpoint;
                    }

                    if (waitResult.CreationState == RunspaceCreationState.Ready &&
                        !runVcConnectionScript)
                    {
                        ((RunspaceData)result).State = DataTypes.RunspaceState.Ready;
                    }

                    _logger.LogDebug($"Connect VC requested: {runVcConnectionScript}");
                    if (runVcConnectionScript && waitResult.CreationState == RunspaceCreationState.Ready)
                    {
                        string bearerSamlToken = null;

                        try {
                            _logger.LogDebug($"HoK Saml Token availble: {sessionToken.HoKSamlToken != null}");
                            if (sessionToken.HoKSamlToken == null)
                            {
                                throw new Exception(APIGatewayResources.PowerCLIVCloginController_NoRefreshTokenAvailable_For_Session);
                            }

                            _logger.LogDebug($"STSClient -> IssueBearerTokenBySolutionToken call");
                            bearerSamlToken = stsClient
                                              .IssueBearerTokenBySolutionToken(sessionToken.HoKSamlToken.RawXmlElement)
                                              .OuterXml;
                        } catch (Exception exc) {
                            _logger.LogError(exc, "Issue Bearer Token failed");
                            result.State        = DataTypes.RunspaceState.Error;
                            result.ErrorDetails = new DataTypes.ErrorDetails(exc);
                        }

                        if (bearerSamlToken != null)
                        {
                            // Connect PowerCLI
                            try {
                                var scriptExecutionRequest = new DataTypes.ScriptExecution {
                                    OutputObjectsFormat = OutputObjectsFormat.Json,
                                    Name             = "powerclivclogin",
                                    Script           = PCLIScriptsReader.ConnectByStringSamlToken,
                                    ScriptParameters = new DataTypes.ScriptParameter[] {
                                        new DataTypes.ScriptParameter {
                                            Name  = "server",
                                            Value = vcEndpoint
                                        },
                                        new DataTypes.ScriptParameter {
                                            Name  = "samlToken",
                                            Value = bearerSamlToken
                                        },
                                        new DataTypes.ScriptParameter {
                                            Name  = "allLinked",
                                            Value = true
                                        }
                                    }
                                };

                                _logger.LogDebug($"Start Connect VC script");
                                var scriptResult = ScriptExecutionMediatorSingleton.
                                                   Instance.
                                                   ScriptExecutionMediator.
                                                   StartScriptExecution(sessionToken.UserName, result, scriptExecutionRequest).Result;
                                result.VcConnectionScriptId = scriptResult.Id;

                                _logger.LogDebug($"Wait Connect VC script to complete");
                                while (scriptResult.State == ScriptState.Running)
                                {
                                    var intermediateResult = ScriptExecutionMediatorSingleton.Instance.ScriptExecutionMediator.GetScriptExecution(
                                        sessionToken.UserName,
                                        scriptResult.Id);
                                    if (intermediateResult != null)
                                    {
                                        scriptResult = intermediateResult;
                                    }
                                    Thread.Sleep(200);
                                }
                            } catch (RunspaceEndpointException runspaceEndointException) {
                                _logger.LogError(runspaceEndointException, "Runspace endpoint exception while waiting connect VC script");
                                result.ErrorDetails = new DataTypes.ErrorDetails(runspaceEndointException);
                                result.State        = DataTypes.RunspaceState.Error;
                            } catch (Exception exc) {
                                _logger.LogError(exc, "Wait Connect VC script failed");
                                result.ErrorDetails = new DataTypes.ErrorDetails(exc);
                                result.State        = DataTypes.RunspaceState.Error;
                            }
                        }

                        if (result.State != DataTypes.RunspaceState.Error)
                        {
                            result.State = DataTypes.RunspaceState.Ready;
                        }
                    }
                });
            } catch (RunspaceProviderException runspaceProviderException) {
                _logger.LogError(runspaceProviderException, "Runspace provider exception was thrown");
                throw;
            }
            catch (Exception ex) {
                throw new RunspaceProviderException(
                          string.Format(
                              APIGatewayResources.MultiTenantRunspaceProvider_CreateFailed,
                              userId,
                              ex.Message),
                          ex);
            }

            return(result);
        }